async def get_user_role(ns={}): sor = ns['sor'] # get role ns['del_flg'] = '0' res_role = await sor.R('userrole', ns) if res_role: user_role = res_role[0] else: return { "status": False, "msg": "userrole table, user id can not find..." } roleid = user_role.get('roleid') # get role name role_name = await sor.R('role', {'id': roleid}) if role_name: role = role_name[0].get('role') else: return { "status": False, "msg": "role table, can not get role name" } return role async def publish_product_update(ns={}): """ 运营审批 运营编辑 客户编辑 运营发布 """ if ns.get('userid'): userid = ns.get('userid') else: userid = await get_user() db = DBPools() async with db.sqlorContext('kboss') as sor: # 区分运营和普通客户 user_list = await sor.R('users', {'id': userid}) if not user_list: return { 'status': False, 'msg': '没有找到匹配的用户' } # 处理图片 如果包含http就跳过 if ns.get('img') and ns['img'].startswith('http'): ns.pop('img', None) orgid = user_list[0]['orgid'] ns.pop('userid', None) ns_dic = ns if ns.get('discount') == '0': ns['discount'] = None ns_dic['discount'] = None if ns.get('price') and ns.get('discount'): ns_dic['discount_price'] = float(ns.get('price')) * float(ns['discount']) / 10 elif ns.get('price'): ns_dic['discount_price'] = ns['price'] else: pass user_role = await get_user_role({'userid': userid, 'sor': sor}) # 编辑状态 # 非客户角色编辑客户的产品 if user_role != '客户' and ns.get('orgid') != orgid: # 非客户角色审核客户的产品 # 如果是待审状态 更新审核状态为通过 上架状态为上架 清空驳回原因 if ns.get('audit_status') == 'approved': ns_dic['audit_status'] = 'approved' ns_dic['listing_status'] = 'listing' ns_dic['reject_reason'] = None # 如果是上架商品 更新publish_time为当前时间 if ns.get('listing_status') == 'listing': ns_dic['publish_time'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') # 如果是下架商品 更新publish_time为None elif ns.get('listing_status') == 'delisting': ns_dic['publish_time'] = None # 如果是删除商品 更新publish_time为None elif ns.get('del_flg') == '1': ns_dic['publish_time'] = None else: ns_dic['publish_time'] = None # 非客户角色编辑自己的产品 默认审批通过和发布成功 elif user_role != '客户': # 运营删除自己的产品 if ns.get('del_flg') == '1': pass else: ns['status'] = '1' ns_dic['audit_status'] = 'approved' # 运营下架自己的产品 if not ns.get('listing_status') == 'delisting': ns_dic['listing_status'] = 'listing' ns_dic['first_page'] = '1' # 客户编辑以后 审核状态是: 待审 上架状态是:空 else: # 客户要下架自己的产品 if ns.get('listing_status') == 'delisting': ns_dic['audit_status'] = None ns_dic['listing_status'] = 'delisting' # 客户仅编辑不是下架产品 else: ns_dic['audit_status'] = 'pending' ns_dic['listing_status'] = None ns_dic['publish_time'] = None try: await sor.U('user_publish_product', ns_dic) return { 'status': True, 'msg': 'publish product created successfully' } except Exception as e: return { 'status': False, 'msg': 'Failed to publish product, %s' % str(e) } ret = await publish_product_update(params_kw) return ret