# -*- coding: utf-8 -*- # @Time: 2023/6/26 18:32 async def sale_protocol_add(ns={}): """ :param ns: :return: """ ns = { 'offer_salemanid': '', 'offer_orgid': '6woiJ-_5tDmZUHFnLLty_', 'bid_orgid': '*', 'salemode': '0', 'settlemode': '0', 'settle_dp': '123settle_dp', 'protocolfile': '123protocolfile', 'start_date': '2023-07-01', 'end_date': '2023-11-01' } ns['id'] = uuid() db = DBPools() async with db.sqlorContext('kboss') as sor: try: # ns_exists_discount = { # 'offer_salemanid': ns.get('offer_salemanid'), # 'offer_orgid': ns.get('offer_orgid'), # 'bid_orgid': ns.get('bid_orgid'), # 'salemode': ns.get('salemode'), # 'sort': ['start_date'] # } # ns['del_flg'] = 0 # same_productid = await sor.R('saleprotocol', ns_exists_discount) # insert_date = datetime.datetime.strptime(ns['start_date'], '%Y-%m-%d').date() # for index, prd in enumerate(same_productid): # start_date = datetime.datetime.strptime(prd['start_date'], '%Y-%m-%d').date() # end_date = datetime.datetime.strptime(prd['end_date'], '%Y-%m-%d').date() # if index == 0 and insert_date < start_date: # ns['end_date'] = start_date # await sor.C('saleprotocol', ns) # break # if index == len(same_productid) - 1 and insert_date > start_date: # prd['end_date'] = ns.get('start_date') # await sor.U('saleprotocol', prd) # ns['end_date'] = '9999-12-31' # await sor.C('saleprotocol', ns) # break # if start_date < insert_date < end_date: # ns['end_date'] = prd.get('end_date') # prd['end_date'] = ns.get('start_date') # await sor.U('saleprotocol', prd) # await sor.C('saleprotocol', ns) # break # if start_date == insert_date: # return { # "status": False, # "msg": "Warning: The current date has already been configured" # } # if not same_productid: # ns['end_date'] = '9999-12-31' # await sor.C('saleprotocol', ns) # 优先判断是否含有回佣 设置过回佣就无法添加其它模式 nss_rebate_exist = { 'offer_orgid': ns.get('offer_orgid'), 'bid_orgid': ns.get('bid_orgid'), 'salemode': '1', 'del_flg': '0' } res = await sor.R('saleprotocol', nss_rebate_exist) if res: return { 'status': False, 'msg': '售方买方已经签订了回佣协议,无法再添加当前协议' } # 如果offer是供应商 await sor.C('saleprotocol', ns) return { "status": True, "msg": "saleprotocol add success" } except Exception as e: raise e return { "status": False, "msg": "saleprotocol add failed" } async def sale_protocol_search(ns={}): """ :param ns: :return: """ ns = { "bid_orgid": "6woiJ-_5tDmZUHFnLLty_", "offer_orgid": "kKhcwD0WQPTjt0CGmnNWj" } db = DBPools() async with db.sqlorContext('kboss') as sor: try: res_sql = """select * from saleprotocol where offer_orgid = '%s' and bid_orgid = '%s' and del_flg = '0';""" % (ns.get('offer_orgid'), ns.get('bid_orgid')) res_li = await sor.sqlExe(res_sql, {}) res_li.sort(key = lambda x : (x['salemode'], x['start_date'])) return { "status": True, "msg": "saleprotocol search success", "data": res_li } except Exception as e: raise e return { "status": False, "msg": "saleprotocol search failed" } async def sale_protocol_delete(ns={}): """ 协议删 :param ns: :return: """ ns = { 'id': 'RDyrGT' } db = DBPools() async with db.sqlorContext('kboss') as sor: try: await sor.U('saleprotocol', {'id': ns.get('id'), 'del_flg': '1'}) u_sql = """update product_salemode set del_flg = '1' where protocolid = '%s';""" % ns.get('id') await sor.sqlExe(u_sql, {}) return { 'status': True, 'msg': '协议删除成功' } except Exception as e: raise e return { 'status': False, 'msg': '协议删除失败' } async def shangji_provider_product_search(ns={}): """ 本机构针对某个provider持有的产品 递归 :param ns: :return: """ providerid = ns.get('providerid') offer_orgid = ns.get('offer_orgid') product_list = [] db = DBPools() async with db.sqlorContext('kboss') as sor: sql_find_bid = """select * from saleprotocol where bid_orgid = '%s' and del_flg = '0' and CURRENT_DATE between start_date and end_date;""" % offer_orgid sql_find_bid_li = await sor.sqlExe(sql_find_bid, {}) sql_res = [item.get('offer_orgid') for item in sql_find_bid_li] # 首先判断是不是当前分销商的直属供应商 if providerid in sql_res: print('该供应商就是上级机构的供应商, 返回该供应商所有产品') provider_product_sql = """select * from product WHERE providerid = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % providerid provider_product_li = await sor.sqlExe(provider_product_sql, {}) product_list.extend(provider_product_li) return product_list else: # 如果不是直属供应商 就是上级某级机构的供应商 for sett in sql_find_bid_li: salemode = sett['salemode'] offer_orgid = sett['offer_orgid'] protocolid = sett['id'] # 首先在折扣表里找对应的provider if salemode == '0': # 查找子表 zi_table = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) exits_star = [(item.get('providerid'), item.get('productid')) for item in zi_table] exist_st = (providerid, '*') if exist_st in exits_star: await shangji_provider_product_search({'providerid': providerid, 'offer_orgid': offer_orgid}) else: for zic in zi_table: zic_provider = zic['providerid'] zic_productid = zic['productid'] if zic_provider == providerid: provider_product_sql = """select * from product WHERE id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % zic_productid provider_product_li = await sor.sqlExe(provider_product_sql, {}) product_list.extend(provider_product_li) elif salemode == '2': # 查找子表 zi_table = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) for zic in zi_table: zic_provider = zic['providerid'] zic_productid = zic['productid'] if zic_provider == providerid: provider_product_sql = """select * from product WHERE id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % zic_productid provider_product_li = await sor.sqlExe(provider_product_sql, {}) product_list.extend(provider_product_li) return product_list async def self_product_search(ns={}): """ 产品不落地 本机构所有产品查找 回佣>折扣>底价 本机构是业主机构 协议表所有bid是本机构 *就查找product表 provider所有产品 本质是往上找一层 本机构是某级分销商 协议表所有bid是本机构 *就查找 上级offer为bid *对应的provider 循环 直到不含有*/offer为provider类型为供应商为止 找N层 :param ns: :return: """ # ns = { # 'bid_orgid': '6woiJ-_5tDmZUHFnLLty_', # } bid_orgid = ns.get('bid_orgid') db = DBPools() async with db.sqlorContext('kboss') as sor: try: if ns.get('sor'): sor = ns.get('sor') all_product = [] ns['del_flg'] = '0' # 查找所有bid是本机构的协议表 sql_find_bid = """select * from saleprotocol where bid_orgid = '%s' and del_flg = '0' and CURRENT_DATE between start_date and end_date;""" % bid_orgid sql_find_bid_li = await sor.sqlExe(sql_find_bid, {}) # 判断是业主机构还是分销商 yezhu_li = await sor.R('organization', {'id': bid_orgid}) yezhu_judge = yezhu_li[0]['org_type'] # 如果是供应商 就返回所有产品 if yezhu_judge == '4': provider_product_sql = """select * from product WHERE providerid = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % bid_orgid provider_product_li = await sor.sqlExe(provider_product_sql, {}) return provider_product_li # 如果是业主机构 if yezhu_judge == '0': for sett in sql_find_bid_li: salemode = sett['salemode'] offer_orgid = sett['offer_orgid'] protocolid = sett['id'] # 如果salemode是1 就是供应商所有产品 if salemode == '1': provider_product_sql = """select * from product WHERE providerid = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % offer_orgid provider_product_li = await sor.sqlExe(provider_product_sql, {}) all_product.extend(provider_product_li) # 如果salemode是折扣 如果有* 就是供应商所有产品 如果salemode是底价就获取所有productid if salemode == '0' or salemode == '2': # 查找子表 zi_table = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) # 如果有* 就获取providerid所有产品 star_flg = [item.get('productid') for item in zi_table] if '*' in star_flg: provider_product_sql = """select * from product WHERE providerid = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % offer_orgid provider_product_li = await sor.sqlExe(provider_product_sql, {}) all_product.extend(provider_product_li) else: prd_single_ids = [item.get('productid') for item in zi_table] for prd_single in prd_single_ids: single_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % prd_single single_res_li = await sor.sqlExe(single_sql, {}) single_res = single_res_li[0] if single_res_li else {} all_product.append(single_res) return all_product # 如果是分销商 if yezhu_judge == '1': for sett in sql_find_bid_li: salemode = sett['salemode'] offer_orgid = sett['offer_orgid'] protocolid = sett['id'] # 判断是业主机构还是分销商 yezhu_li = await sor.R('organization', {'id': offer_orgid}) yezhu_judge = yezhu_li[0]['org_type'] # 如果salemode是1 就是offer所有产品 if salemode == '1': prodcut_one = await self_product_search({'bid_orgid': offer_orgid}) all_product.extend(prodcut_one) # 如果salemode是折扣 如果有* 就是供应商所有产品 不是* 就获取所有productid if salemode == '0': # 如果上级机构是供应商就获取该供应商所有产品 if yezhu_judge == '4': provider_product_sql_ = """select * from product WHERE providerid = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % offer_orgid provider_product_li = await sor.sqlExe(provider_product_sql_, {}) all_product.extend(provider_product_li) else: # 如果不是供应商就查找子表 zi_table = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) # 如果有* 就获取providerid所有产品 for zic in zi_table: providerid_ = zic['providerid'] productid_ = zic['productid'] if productid_ == '*': # 查找上级机构该providerid持有的产品 product_res = await shangji_provider_product_search({'providerid': providerid_, 'offer_orgid': offer_orgid}) all_product.extend(product_res) else: single_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid_ single_res_li = await sor.sqlExe(single_sql, {}) single_res = single_res_li[0] if single_res_li else {} all_product.append(single_res) # 如果salemode是底价 就获取所有productid if salemode == '2': zi_table = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) # 如果有* 就获取providerid所有产品 for zic in zi_table: productid_ = zic['productid'] single_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid_ single_res_li = await sor.sqlExe(single_sql, {}) single_res = single_res_li[0] if single_res_li else {} all_product.append(single_res) return all_product # return { # "status": True, # "msg": "product_salemode search success", # "data": all_product # } except Exception as e: raise e return { "status": False, "msg": "product_salemode search failed" } async def product_salemode_search(ns={}): """ 产品价格不落地 查找sale_protocol以后 具体产品配置查找 :param ns: :return: """ ns = { 'salemode': '0', 'protocolid': 'YBw10e2kto1YfMeufgpIX', 'del_flg': '0' } db = DBPools() async with db.sqlorContext('kboss') as sor: try: if ns.get('salemode') == '0': res_list = [] ns_search = { 'protocolid': ns.get('protocolid'), 'del_flg': '0' } res = await sor.R('product_salemode', ns_search) # 首先查找不是*的产品 加到产品列表 # 已经存在的id列表 prd_singles = [item.get('productid') for item in res if item.get('productid') != '*'] # 已经存在的id列表 包含对应折扣 prd_single_ids = [(item.get('productid'), item.get('discount')) for item in res if item.get('productid') != '*'] for prd_single in prd_single_ids: single_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % prd_single[0] single_res_li = await sor.sqlExe(single_sql, {}) single_res = single_res_li[0] if single_res_li else {} single_res['discount'] = prd_single[1] res_list.append(single_res) # 查找*的所有产品id 通过prd_single筛选 nss = { 'productid': '*', 'protocolid': ns.get('protocolid'), 'del_flg': '0' } res_star = await sor.R('product_salemode', nss) if res_star: # 针对产品设置的统一折扣 start_discount = res_star[0].get('discount') provider_star = res_star[0].get('providerid') res_product_sql = """select * from product where providerid = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % provider_star res_product_li = await sor.sqlExe(res_product_sql, {}) for res_product in res_product_li: res_product_id = res_product.get('id') if res_product_id not in prd_singles: res_product['discount'] = start_discount res_list.append(res_product) return { "status": True, "msg": "product_salemode search success", "data": res_list } if ns.get('salemode') == '2': res_list = [] ns_search = { 'protocolid': ns.get('protocolid'), 'del_flg': '0' } res = await sor.R('product_salemode', ns_search) prd_single_ids = [(item.get('productid'), item.get('price')) for item in res if item.get('productid') != '*'] for prd_single in prd_single_ids: single_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % prd_single[0] single_res_li = await sor.sqlExe(single_sql, {}) single_res = single_res_li[0] if single_res_li else {} single_res['price'] = prd_single[1] res_list.append(single_res) return { "status": True, "msg": "product_salemode search success", "data": res_list } except Exception as e: return { "status": False, "msg": "product_salemode search failed", 'data': e } return { "status": False, "msg": "product_salemode search failed" } async def product_salemode_add(ns={}): """ 1. offer是供应商 运营设置折扣/回佣/底价-售价 折扣 providerid唯一 productid可能为*/部分产品id 回佣 providerid唯一 productid为* 底价 providerid唯一 productid不能为* 只能是产品id 2. offer是本机构 bid是下级分销商 销售设置针对分销商 折扣/回佣/底价 本机构产品来源 本机构是业主机构 协议表所有bid是本机构 *就查找product表 provider所有产品 本机构是某级分销商 协议表所有bid是本机构 *就查找 折扣 providerid不唯一 :param ns: :return: """ ns = { 'protocolid': 'dovwIp8OZ6_SFAByjW-c7', 'providerid': 'lV0EwNLdusKvbaTv2vhJt', 'productid': 'ycChkDuCPY6sGmOIUBeFN', # 'discount': 0.70, 'price': 200, } ns['id'] = uuid() db = DBPools() async with db.sqlorContext('kboss') as sor: try: # TODO 筛选discount和rebate是否同时含有* # TODO 只有offer_orgid org_type是供应商时才能添加* await sor.C('product_salemode', ns) return { "status": True, "msg": "product_salemode add success" } except Exception as e: raise e return { "status": False, "msg": "product_salemode add failed" } async def product_salemode_delete(ns={}): """ 删除协议子表 :param ns: :return: """ ns = { 'id': 'TnvKDSjV' } db = DBPools() async with db.sqlorContext('kboss') as sor: try: await sor.U('product_salemode', {'id': ns.get('id'), 'del_flg': '1'}) return { 'status': True, 'msg': '协议子表删除成功' } except Exception as e: return { 'status': False, 'msg': '协议子表删除失败', 'err_msg': e } # 客户查看本机构所有产品 # 运营查看上级机构所有产品 # 运营查看供应商所有产品 # 销售查看本机构所有产品 # 查看供应商产品 async def provider_product_search(ns={}): """ 查看供应商所有产品 :return: """ # ns = { # 'providerid': 'UiimaDChjdtdfV2gogwyM' # } db = DBPools() async with db.sqlorContext('kboss') as sor: try: res_product_sql = """select * from product where providerid = '%s' and del_flg = '0'""" % ns.get('providerid') res_product_li = await sor.sqlExe(res_product_sql, {}) return { 'status': True, 'msg': '供应商产品查找成功', 'data': res_product_li } except Exception as e: return { 'status': False, 'msg': '供应商产品查找失败', 'err_msg': e } async def huiyong_zibiao_jia(ns={}): ns = { 'protocolid': '123protocolid', 'rebate_cycle': '2', 'sette_dp': 'sette_dp', 'sale_amount': 50, 'rebate_rate': 0.12, # 'rebate_zhubiao_id': '9XQCfuXN' } db = DBPools() async with db.sqlorContext('kboss') as sor: try: if not ns.get('rebate_zhubiao_id'): ns_zhubiao = { 'id': uuid(), 'protocolid': ns.get('protocolid'), 'rebate_cycle': ns.get('rebate_cycle'), 'sette_dp': ns.get('sette_dp') } ns_zibiao = { 'id': uuid(), 'rebatecycleid': ns_zhubiao['id'], 'sale_amount':ns.get('sale_amount'), 'rebate_rate': float(ns.get('rebate_rate')) } await sor.C('rebate_cycle', ns_zhubiao) await sor.C('rp_rebate', ns_zibiao) return { 'status': True, 'msg': '回佣添加成功' } else: ns_zibiao = { 'id': uuid(), 'rebatecycleid': ns.get('rebate_zhubiao_id'), 'sale_amount': ns.get('sale_amount'), 'rebate_rate': float(ns.get('rebate_rate')) } await sor.C('rp_rebate', ns_zibiao) return { 'status': True, 'msg': '回佣内容添加成功' } except Exception as e: raise e return { 'status': False, 'msg': '回佣添加失败' } async def huiyong_zibiao_cha(): ns = { 'protocolid': '123protocolid' } db = DBPools() async with db.sqlorContext('kboss') as sor: try: res_dict = {} res_sql = """select cy.*, te.id as rp_rebate_id, te.sale_amount, te.rebate_rate, te.del_flg as rp_rebate_del_flg from rebate_cycle as cy INNER JOIN rp_rebate as te on cy.id = te.rebatecycleid where cy.protocolid = '%s' and cy.del_flg = '0' and te.del_flg = '0';""" % ns.get('protocolid') res_sql_li = await sor.sqlExe(res_sql, {}) for res in res_sql_li: cycle = res['rebate_cycle'] if res_dict.get(cycle): res_dict[cycle].append(res) else: res_dict[cycle] = [res] return { 'status': True, 'msg': '回佣查找成功', 'data': res_dict } except Exception as e: raise e return { 'status': False, 'msg': '回佣查询失败' } async def huiyong_zhubiao_shan(ns={}): """ 协议删 :param ns: :return: """ ns = { 'id': 'LV0zttug' } db = DBPools() async with db.sqlorContext('kboss') as sor: try: await sor.U('rebate_cycle', {'id': ns.get('id'), 'del_flg': '1'}) u_sql = """update rp_rebate set del_flg = '1' where rebatecycleid = '%s';""" % ns.get('id') await sor.sqlExe(u_sql, {}) return { 'status': True, 'msg': '回佣主表删除成功' } except Exception as e: raise e return { 'status': False, 'msg': '回佣主表删除失败' } async def huiyong_zibiao_shan(ns={}): """ 删除回佣子表 :param ns: :return: """ ns = { 'id': 'TnvKDSjV' } db = DBPools() async with db.sqlorContext('kboss') as sor: try: await sor.U('rp_rebate', {'id': ns.get('id'), 'del_flg': '1'}) return { 'status': True, 'msg': '回佣子表删除成功' } except Exception as e: return { 'status': False, 'msg': '回佣子表删除失败', 'err_msg': e } async def kehu_kan_xiaoshoujigou(ns={}): """ 客户查看所在的机构 :return: """ userid = await get_user() sor = ns.get('sor') user_li = await sor.R('users', {'id': userid, 'del_flg': '0'}) user_org_id = user_li[0]['orgid'] org_li = await sor.R('organization', {'id': user_org_id, 'del_flg': '0'}) sale_orgid = org_li[0]['parentid'] return user_org_id, sale_orgid async def kehu_kan_chanpin(ns={}): """ 价格不落地 :return: """ ns = { # 'userid': 'Du6MIptPaV5omnzrAadw-' # 'userid': 'mtgpa8O0Mqu0S0GwWu6F5' # 'userid': '4KxTBEne0FRkjuOAbhx4d' 'userid': 'Q-JfaqxfwxroN_2-Ayw18', # 'product_area': '', # 'ptype': 'ad' } db = DBPools() async with db.sqlorContext('kboss') as sor: try: # 配置产品id列表[] product_all_ids = [] product_all_infos = [] # 通过salemanid查看客户所在机构 user_org_id, orgid = await kehu_kan_xiaoshoujigou({'userid': await get_user(), 'sor': sor}) # 优先查看bid为该用户的配置 xieyi_yonghu_cha_sql = """select * from saleprotocol where offer_orgid = '%s' and bid_orgid = '%s' and del_flg = '0' and CURRENT_DATE between start_date and end_date;""" % (orgid, user_org_id) xieyi_yonghu_cha_li = await sor.sqlExe(xieyi_yonghu_cha_sql, {}) for xieyi_cha in xieyi_yonghu_cha_li: salemode = xieyi_cha['salemode'] protocolid = xieyi_cha['id'] # 查看子表 xieyi_zibiao_cha_li = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) xieyi_zibiao_cha_li_ = sorted(xieyi_zibiao_cha_li, key=lambda x: (x['productid'] == '*', x['productid'])) if salemode == '0' or salemode == '2': for xieyi_zibiao in xieyi_zibiao_cha_li_: providerid = xieyi_zibiao['providerid'] productid = xieyi_zibiao['productid'] discount = xieyi_zibiao['discount'] price = xieyi_zibiao['price'] if productid in product_all_ids: # 产品id已经存在 continue # 折扣表中 产品id不为* 就展示对应的产品信息和折扣信息和折扣后的价格 底价表中展示对应产品信息和售价信息 if salemode == '0': if productid != '*': # 筛选id product_all_ids.append(productid) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['discount'] = discount # TODO 从供应商那里获取对应的产品价格 product_info['price'] = round(random.random() * 100, 2) product_info['discount_price'] = round(product_info['price'] * product_info['discount'], 2) # 追加产品信息 product_all_infos.append(product_info) elif productid == '*': # 折扣表中 产品id为* 就向上级机构查找有对应providerid的多少产品 依次迭代 shangji_chanpin_li = await shangji_provider_product_search({'providerid': providerid, 'offer_orgid': orgid}) for shangji_chanpin in shangji_chanpin_li: shangji_chanpin_id = shangji_chanpin['id'] # 筛选id if shangji_chanpin_id in product_all_ids: continue product_all_ids.append(shangji_chanpin_id) shangji_chanpin['discount'] = discount product_all_infos.append(shangji_chanpin) elif salemode == '2': # 筛选id product_all_ids.append(productid) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['price'] = price # 追加产品信息 product_all_infos.append(product_info) # 查找offer对应的机构id bid为* 的所有配置 时间限制 xieyi_cha_sql = """select * from saleprotocol where offer_orgid = '%s' and bid_orgid = '*' and del_flg = '0' and CURRENT_DATE between start_date and end_date;""" % orgid xieyi_cha_res_li = await sor.sqlExe(xieyi_cha_sql, {}) for xieyi_cha in xieyi_cha_res_li: salemode = xieyi_cha['salemode'] protocolid = xieyi_cha['id'] # 查看子表 xieyi_zibiao_cha_li = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) xieyi_zibiao_cha_li_ = sorted(xieyi_zibiao_cha_li, key=lambda x: (x['productid'] == '*', x['productid'])) if salemode == '0' or salemode == '2': for xieyi_zibiao in xieyi_zibiao_cha_li_: providerid = xieyi_zibiao['providerid'] productid = xieyi_zibiao['productid'] discount = xieyi_zibiao['discount'] price = xieyi_zibiao['price'] if productid in product_all_ids: # 产品id已经存在 continue # 折扣表中 产品id不为* 就展示对应的产品信息和折扣信息和折扣后的价格 底价表中展示对应产品信息和售价信息 if salemode == '0': if productid != '*': # 筛选id product_all_ids.append(productid) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['discount'] = discount # TODO 从供应商那里获取对应的产品价格 product_info['price'] = round(random.random() * 100, 2) product_info['discount_price'] = round(product_info['price'] * product_info['discount'], 2) # 追加产品信息 product_all_infos.append(product_info) elif productid == '*': # 折扣表中 产品id为* 就向上级机构查找有对应providerid的多少产品 依次迭代 shangji_chanpin_li = await shangji_provider_product_search({'providerid': providerid, 'offer_orgid': orgid}) for shangji_chanpin in shangji_chanpin_li: shangji_chanpin_id = shangji_chanpin['id'] # 筛选id if shangji_chanpin_id in product_all_ids: continue product_all_ids.append(shangji_chanpin_id) shangji_chanpin['discount'] = discount product_all_infos.append(shangji_chanpin) elif salemode == '2': # 筛选id product_all_ids.append(productid) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['price'] = price # 追加产品信息 product_all_infos.append(product_info) # 没有任何配置的产品 折扣为1 provider_products = await self_product_search({'bid_orgid': orgid, 'sor': sor}) for prd in provider_products: prdid = prd.get('id') if prdid not in product_all_ids: # TODO 从供应商那里获取对应的产品价格 prd['price'] = round(random.random() * 100, 2) prd['discount_price'] = prd.get('price') prd['offer_orgid'] = orgid prd['bid_orgid'] = user_org_id prd['productid'] = prdid product_all_infos.append(prd) # TODO 没有配置折扣和底价客户看不到 回佣产品客户可以看到 # 筛选产品 product_area ptype if ns.get('product_area'): product_all_infos = [item_info for item_info in product_all_infos if item_info['product_area'] == ns.get('product_area')] if ns.get('ptype'): product_all_infos = [item_info for item_info in product_all_infos if item_info['ptype'] == ns.get('ptype')] return { 'status': True, 'msg': '客户获取商品信息成功', 'data': product_all_infos } except Exception as e: return { 'status': False, 'msg': '客户获取商品信息出错', 'err_msg': e } async def no_cofig_get(ns={}): """ 1. 供应商添加过产品 没有配置 2. 给分销商的配置 :return: """ ns = { 'offer_orgid': 'kKhcwD0WQPTjt0CGmnNWj', 'bid_orgid': '6woiJ-_5tDmZUHFnLLty_' } offer_orgid = ns.get('offer_orgid') bid_orgid = ns.get('bid_orgid') db = DBPools() async with db.sqlorContext('kboss') as sor: try: no_config_products = [] # 获取供应商所有产品 provider_products = await self_product_search({'bid_orgid': offer_orgid, 'sor': sor}) # 获取是否配置过回佣 ns_rebate_exist = { 'offer_orgid': offer_orgid, 'bid_orgid': bid_orgid, 'salemode': '1', 'del_flg': '0' } rebate_li = await sor.R('saleprotocol', ns_rebate_exist) if rebate_li: return { 'status': True, 'msg': '供应商已经配置过回佣, 即: 所有产品已经配置', 'data': [] } # 获取是否配置过折扣 判断折扣是否有* ns_discount_star_exist_sql = """select ode.productid from saleprotocol as col INNER JOIN product_salemode as ode on col.id = ode.protocolid where col.offer_orgid = '%s' and col.bid_orgid = '%s' and col.del_flg = '0' and ode.del_flg = '0';""" % (offer_orgid, bid_orgid) ns_discount_floorpirce_exists = await sor.sqlExe(ns_discount_star_exist_sql, {}) ns_discount_floorpirce_exist = [item_.get('productid') for item_ in ns_discount_floorpirce_exists if item_] if '*' in ns_discount_floorpirce_exist: return { 'status': True, 'msg': '供应商产品折扣已配置*, 即: 所有产品已经配置', 'data': [] } # 筛选产品 for item in provider_products: item_id = item.get('id') if item_id not in ns_discount_floorpirce_exist: no_config_products.append(item) return { 'status': True, 'msg': '获取没有配置过的产品成功', 'data': no_config_products } except Exception as e: return { 'status': False, 'msg': '没有配置过的产品查找失败', 'err_msg': e } async def xiaoshou_kan_tongyi11(ns={}): """ 销售看统一折扣和回佣 :param ns: :return: """ ns = { 'orgid': '6woiJ-_5tDmZUHFnLLty_' } orgid = ns.get('orgid') # 看销售所在机构配置过*的折扣和底价产品 db = DBPools() async with db.sqlorContext('kboss') as sor: try: # 配置产品id列表[] product_all_ids = [] product_all_infos = [] xiaoshou_sql = """select * from saleprotocol where offer_orgid = '%s' and bid_orgid = '*' and del_flg = '0' and CURRENT_DATE between start_date and end_date;""" % orgid xiaoshou_sql_li = await sor.sqlExe(xiaoshou_sql, {}) for xiaoshou in xiaoshou_sql_li: salemode = xiaoshou['salemode'] protocolid = xiaoshou['id'] start_date = xiaoshou['start_date'] end_date = xiaoshou['end_date'] # 查看子表 xieyi_zibiao_cha_li = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) xieyi_zibiao_cha_li_ = sorted(xieyi_zibiao_cha_li, key=lambda x: (x['productid'] == '*', x['productid'])) if salemode == '0' or salemode == '2': for xieyi_zibiao in xieyi_zibiao_cha_li_: providerid = xieyi_zibiao['providerid'] productid = xieyi_zibiao['productid'] discount = xieyi_zibiao['discount'] price = xieyi_zibiao['price'] if productid in product_all_ids: # 产品id已经存在 continue # 折扣表中 产品id不为* 就展示对应的产品信息和折扣信息和折扣后的价格 底价表中展示对应产品信息和售价信息 if salemode == '0': if productid != '*': # 筛选id product_all_ids.append(productid) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['start_date'] = start_date product_info['end_date'] = end_date product_info['discount'] = discount # TODO 从供应商那里获取对应的产品价格 product_info['price'] = round(random.random() * 100, 2) product_info['discount_price'] = round(product_info['price'] * product_info['discount'], 2) # 追加产品信息 product_all_infos.append(product_info) elif productid == '*': # 折扣表中 产品id为* 就向上级机构查找有对应providerid的多少产品 依次迭代 shangji_chanpin_li = await shangji_provider_product_search({'providerid': providerid, 'offer_orgid': orgid}) for shangji_chanpin in shangji_chanpin_li: shangji_chanpin_id = shangji_chanpin['id'] # 筛选id if shangji_chanpin_id in product_all_ids: continue product_all_ids.append(shangji_chanpin_id) product_info['start_date'] = start_date product_info['end_date'] = end_date shangji_chanpin['discount'] = discount product_info['price'] = round(random.random() * 100, 2) product_info['discount_price'] = round(product_info['price'] * product_info['discount'], 2) product_all_infos.append(shangji_chanpin) elif salemode == '2': # 筛选id product_all_ids.append(productid) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['price'] = price product_info['start_date'] = start_date product_info['end_date'] = end_date # 追加产品信息 product_all_infos.append(product_info) return{ 'status': True, 'msg': '销售查看统一折扣底价成功', 'data': product_all_infos } except Exception as e: return { 'status': False, 'msg': '' } async def xiaoshou_kan_tongyi(ns={}): """ 价格不落地 :return: """ ns = { # 'userid': 'Du6MIptPaV5omnzrAadw-' # 'userid': 'mtgpa8O0Mqu0S0GwWu6F5' # 'customerid': 'lnZUKlslisu0-ZUKCFXkQ' 'customerid': 'h4c6i1n4HMyKanQABL6UK' } db = DBPools() async with db.sqlorContext('kboss') as sor: try: # 配置产品id列表[] product_all_ids = [] product_all_infos = [] # 通过salemanid查看客户所在机构 # user_org_id, orgid = await kehu_kan_xiaoshoujigou({'userid': await get_user(), 'sor': sor}) user_org_id = ns.get('customerid') org_li = await sor.R('organization', {'id': user_org_id, 'del_flg': '0'}) orgid = org_li[0]['parentid'] # 优先查看bid为该用户的配置 xieyi_yonghu_cha_sql = """select * from saleprotocol where offer_orgid = '%s' and bid_orgid = '%s' and del_flg = '0' and CURRENT_DATE between start_date and end_date;""" % (orgid, user_org_id) xieyi_yonghu_cha_li = await sor.sqlExe(xieyi_yonghu_cha_sql, {}) for xieyi_yonghu_cha in xieyi_yonghu_cha_li: salemode_yonghu = xieyi_yonghu_cha['salemode'] protocolid_yonghu = xieyi_yonghu_cha['id'] # 查看子表 xieyi_zibiao_cha_li = await sor.R('product_salemode', {'protocolid': protocolid_yonghu, 'del_flg': '0'}) xieyi_zibiao_cha_li_ = sorted(xieyi_zibiao_cha_li, key=lambda x: (x['productid'] == '*', x['productid'])) if salemode_yonghu == '0' or salemode_yonghu == '2': for xieyi_zibiao in xieyi_zibiao_cha_li_: providerid_yonghu = xieyi_zibiao['providerid'] productid_yonghu = xieyi_zibiao['productid'] discount_yonghu = xieyi_zibiao['discount'] price_yonghu = xieyi_zibiao['price'] zibiao_id_yonghu = xieyi_zibiao['id'] if productid_yonghu in product_all_ids: # 产品id已经存在 continue # 折扣表中 产品id不为* 就展示对应的产品信息和折扣信息和折扣后的价格 底价表中展示对应产品信息和售价信息 if salemode_yonghu == '0': if productid_yonghu != '*': # 筛选id product_all_ids.append(productid_yonghu) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid_yonghu product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['offer_orgid'] = orgid product_info['bid_orgid'] = user_org_id product_info['zibiao_id'] = zibiao_id_yonghu product_info['protocolid'] = protocolid_yonghu product_info['discount'] = discount_yonghu # TODO 从供应商那里获取对应的产品价格 product_info['price'] = round(random.random() * 100, 2) product_info['discount_price'] = round(product_info['price'] * product_info['discount'], 2) # 追加产品信息 product_all_infos.append(product_info) elif productid_yonghu == '*': # 折扣表中 产品id为* 就向上级机构查找有对应providerid的多少产品 依次迭代 shangji_chanpin_li = await shangji_provider_product_search({'providerid': providerid_yonghu, 'offer_orgid': orgid}) for shangji_chanpin in shangji_chanpin_li: shangji_chanpin_id = shangji_chanpin['id'] # 筛选id if shangji_chanpin_id in product_all_ids: continue product_all_ids.append(shangji_chanpin_id) shangji_chanpin['discount'] = discount_yonghu shangji_chanpin['zibiao_id'] = '*' shangji_chanpin['protocolid'] = protocolid_yonghu shangji_chanpin['offer_orgid'] = orgid shangji_chanpin['bid_orgid'] = user_org_id product_all_infos.append(shangji_chanpin) elif salemode_yonghu == '2': # 筛选id product_all_ids.append(productid_yonghu) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid_yonghu product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['price'] = price_yonghu product_info['zibiao_id'] = zibiao_id_yonghu product_info['protocolid'] = protocolid_yonghu product_info['offer_orgid'] = orgid product_info['bid_orgid'] = user_org_id # 追加产品信息 product_all_infos.append(product_info) # 查找offer对应的机构id bid为* 的所有配置 时间限制 xieyi_cha_sql = """select * from saleprotocol where offer_orgid = '%s' and bid_orgid = '*' and del_flg = '0' and CURRENT_DATE between start_date and end_date;""" % orgid xieyi_cha_res_li = await sor.sqlExe(xieyi_cha_sql, {}) for xieyi_cha in xieyi_cha_res_li: salemode = xieyi_cha['salemode'] protocolid = xieyi_cha['id'] # 查看子表 xieyi_zibiao_cha_li = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) xieyi_zibiao_cha_li_ = sorted(xieyi_zibiao_cha_li, key=lambda x: (x['productid'] == '*', x['productid'])) if salemode == '0' or salemode == '2': for xieyi_zibiao in xieyi_zibiao_cha_li_: providerid = xieyi_zibiao['providerid'] productid = xieyi_zibiao['productid'] discount = xieyi_zibiao['discount'] price = xieyi_zibiao['price'] zibiao_id_ = xieyi_zibiao['id'] if productid in product_all_ids: # 产品id已经存在 continue # 折扣表中 产品id不为* 就展示对应的产品信息和折扣信息和折扣后的价格 底价表中展示对应产品信息和售价信息 if salemode == '0': if productid != '*': # 筛选id product_all_ids.append(productid) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['offer_orgid'] = orgid product_info['bid_orgid'] = user_org_id product_info['protocolid'] = protocolid product_info['zibiao_id'] = zibiao_id_ product_info['discount'] = discount # TODO 从供应商那里获取对应的产品价格 product_info['price'] = round(random.random() * 100, 2) product_info['discount_price'] = round(product_info['price'] * product_info['discount'], 2) # 追加产品信息 product_all_infos.append(product_info) elif productid == '*': # 折扣表中 产品id为* 就向上级机构查找有对应providerid的多少产品 依次迭代 shangji_chanpin_li = await shangji_provider_product_search({'providerid': providerid, 'offer_orgid': orgid}) for shangji_chanpin in shangji_chanpin_li: shangji_chanpin_id = shangji_chanpin['id'] # 筛选id if shangji_chanpin_id in product_all_ids: continue product_all_ids.append(shangji_chanpin_id) shangji_chanpin['discount'] = discount shangji_chanpin['zibiao_id'] = zibiao_id_ shangji_chanpin['protocolid'] = protocolid shangji_chanpin['offer_orgid'] = orgid shangji_chanpin['bid_orgid'] = user_org_id product_all_infos.append(shangji_chanpin) elif salemode == '2': # 筛选id product_all_ids.append(productid) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['offer_orgid'] = orgid product_info['bid_orgid'] = user_org_id product_info['price'] = price product_info['zibiao_id'] = zibiao_id_ product_info['protocolid'] = protocolid # 追加产品信息 product_all_infos.append(product_info) return { 'status': True, 'msg': '销售获取商品统一信息成功', 'data': product_all_infos } except Exception as e: return { 'status': False, 'msg': '销售获取商品统一信息出错', 'err_msg': e } async def xiaoshou_gai_zhekou_dijia(ns={}): """ 销售修改折扣底价 :return: """ # ns = { # 'offer_orgid': '', # 'bid_orgid': '', # 'providerid': 'lV0EwNLdusKvbaTv2vhJt', # 'productid': '92D', # 'discount': '0.9', # } ns = { "discount": "0.66", "productid": "6Y5HMrRRfn_IH4OlAAbS5", "bid_orgid": "h4c6i1n4HMyKanQABL6UK", "offer_orgid": "mIWUHBeeDM8mwAFPIQ8pS", "providerid": "E0RFifplwLEphX5ckLLtj" } productid = ns.get('productid') db = DBPools() async with db.sqlorContext('kboss') as sor: try: # 首先查找offer和bid是否已经存在 if ns.get('discount'): ns_offer_bid = { 'offer_orgid': ns.get('offer_orgid'), 'bid_orgid': ns.get('bid_orgid'), 'salemode': '0', 'del_flg': '0' } res_offer_bid_li = await sor.R('saleprotocol', ns_offer_bid) if res_offer_bid_li: protocolid = res_offer_bid_li[0]['id'] ns_zibiao = { 'protocolid': protocolid, 'del_flg': '0' } zibiao_cha_li = await sor.R('product_salemode', ns_zibiao) zibiao_ids = [item.get('productid') for item in zibiao_cha_li if item] if productid in zibiao_ids: for prd in zibiao_cha_li: prd_id = prd['productid'] if prd_id == productid: # 获取子表id更新 ns_zibiao_up = { 'id': prd['id'], 'discount': ns.get('discount') } await sor.U('product_salemode', ns_zibiao_up) else: # 产品是以*的形式展示 ns_zibiao_c = { 'id': uuid(), 'protocolid': protocolid, 'providerid': ns.get('providerid'), 'productid': ns.get('productid'), 'discount': ns.get('discount') } await sor.C('product_salemode', ns_zibiao_c) else: # 获取*的start_date和end_date ns_date_get = { 'offer_orgid': ns.get('offer_orgid'), 'bid_orgid': '*', 'salemode': '0', 'del_flg': '0' } res_date_li = await sor.R('saleprotocol', ns_date_get) if res_date_li: start_date, end_date = res_date_li[0]['start_date'], res_date_li[0]['end_date'] else: start_date, end_date = time.strftime('%Y-%m-%d %H:%M:%S'), '9999-12-31' ns_new_saleprotocol = { 'id': uuid(), 'offer_orgid': ns.get('offer_orgid'), 'bid_orgid': ns.get('bid_orgid'), 'salemode': '0', 'start_date': start_date, 'end_date': end_date } await sor.C('saleprotocol', ns_new_saleprotocol) ns_new_product_salemode = { 'id': uuid(), 'protocolid': ns_new_saleprotocol['id'], 'providerid': ns.get('providerid'), 'productid': ns.get('productid'), 'discount': ns.get('discount') } await sor.C('product_salemode', ns_new_product_salemode) return { 'status': True, 'msg': '销售修改折扣信息成功' } except Exception as e: raise e return { 'status': False, 'msg': '销售修改折扣底价信息失败' } async def shoujia_guanli_cha(ns={}): """ 销售看售价 :return: """ ns = { 'bid_orgid': 'mIWUHBeeDM8mwAFPIQ8pS' } db = DBPools() async with db.sqlorContext('kboss') as sor: try: ns_dijia = { 'bid_orgid': ns.get('bid_orgid'), 'salemode': '2', 'del_flg': '0' } ns_shoujia = { 'offer_orgid': ns.get('bid_orgid'), 'bid_orgid': '*', 'salemode': '2', 'del_flg': '0' } dijia_res_li = await sor.R('saleprotocol', ns_dijia) shoujia_res_li = await sor.R('saleprotocol', ns_shoujia) product_all = [] sj_zibiao_cha_li = [] for shoujia in shoujia_res_li: sj_protocolid = shoujia['id'] sj_zibiao_cha_li = await sor.R('product_salemode', {'protocolid': sj_protocolid, 'del_flg': '0'}) for floorprice in dijia_res_li: protocolid = floorprice['id'] zi_biao_cha_li = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) for zibiao in zi_biao_cha_li: product_detail = {} product_detail['gai_dijia_id'] = zibiao['id'] product_detail['productid'] = zibiao['productid'] product_detail['floorprice'] = zibiao['price'] product_table_li = await sor.R('product', {'id': product_detail['productid'], 'del_flg': '0'}) product_detail.update(product_table_li[0]) for sj_zibiao_cha in sj_zibiao_cha_li: sj_productid = sj_zibiao_cha['productid'] if product_detail['productid'] == sj_productid: product_detail['gai_shoujia_id'] = sj_zibiao_cha['id'] product_detail['shoujia'] = sj_zibiao_cha['price'] product_all.append(product_detail) return { 'status': True, 'msg': '销售看售价成功', 'data': product_all } except Exception as e: return { 'status': False, 'msg': '销售看售价失败', 'err_msg': e } async def price_guanli_gai(ns={}): """ 修改售价管理 :return: """ ns = { 'gai_jia_id': '0MDi3KG0DNiEulXKM8MOX', 'price': 200 } db = DBPools() async with db.sqlorContext('kboss') as sor: try: await sor.U('product_salemode', {'id': ns.get('gai_jia_id'), 'price': ns.get('price')}) return { 'status': True, 'msg': '修改售价成功' } except Exception as e: raise e return { 'status': False, 'msg': '修改售价失败' } async def price_guanli_jia(ns={}): """ 修改售价管理 :return: """ ns = { 'bid_orgid': 'mIWUHBeeDM8mwAFPIQ8pS', 'providerid': 'E0RFifplwLEphX5ckLLtj', 'productid': 'mbSMhLef1RdVWVxf_H9Xs', 'price': 80 } db = DBPools() async with db.sqlorContext('kboss') as sor: try: # 首先查找协议是否已经存在 ns_shoujia = { 'offer_orgid': ns.get('bid_orgid'), 'bid_orgid': '*', 'salemode': '2', 'del_flg': '0' } shoujia_res_li = await sor.R('saleprotocol', ns_shoujia) if shoujia_res_li: protocolid = shoujia_res_li[0]['id'] else: # 协议表不存在 就创建 ns_zhubiao = { 'id': uuid(), 'offer_orgid': ns.get('bid_orgid'), 'bid_orgid': '*', 'salemode': '2', 'start_date': '2010-10-01', 'end_date': '2099-10-01' } await sor.C('saleprotocol', ns_zhubiao) protocolid = ns_zhubiao['id'] # 根据protocolid创建售价 ns_zibiao_shoujia = { 'id': uuid(), 'protocolid': protocolid, 'providerid': ns.get('providerid'), 'productid': ns.get('productid'), 'price': ns.get('price') } await sor.C('product_salemode', ns_zibiao_shoujia) return { 'status': True, 'msg': '创建售价成功' } except Exception as e: raise e return { 'status': False, 'msg': '创建售价失败' } async def xiaoshou_kan_zhekou(ns={}): """ 销售看折扣 :param ns: :return: """ ns = { 'customerid': 'h4c6i1n4HMyKanQABL6UK' } db = DBPools() async with db.sqlorContext('kboss') as sor: try: # 配置产品id列表[] product_all_ids = [] product_all_infos = [] # 通过salemanid查看客户所在机构 # user_org_id, orgid = await kehu_kan_xiaoshoujigou({'userid': await get_user(), 'sor': sor}) user_org_id = ns.get('customerid') org_li = await sor.R('organization', {'id': user_org_id, 'del_flg': '0'}) orgid = org_li[0]['parentid'] # 优先查看bid为该用户的配置 xieyi_yonghu_cha_sql = """select * from saleprotocol where offer_orgid = '%s' and bid_orgid = '%s' and del_flg = '0' and CURRENT_DATE between start_date and end_date;""" % (orgid, user_org_id) xieyi_yonghu_cha_li = await sor.sqlExe(xieyi_yonghu_cha_sql, {}) for xieyi_yonghu_cha in xieyi_yonghu_cha_li: salemode_yonghu = xieyi_yonghu_cha['salemode'] protocolid_yonghu = xieyi_yonghu_cha['id'] # 查看子表 xieyi_zibiao_cha_li = await sor.R('product_salemode', {'protocolid': protocolid_yonghu, 'del_flg': '0'}) xieyi_zibiao_cha_li_ = sorted(xieyi_zibiao_cha_li, key=lambda x: (x['productid'] == '*', x['productid'])) if salemode_yonghu == '0' or salemode_yonghu == '2': for xieyi_zibiao in xieyi_zibiao_cha_li_: providerid_yonghu = xieyi_zibiao['providerid'] productid_yonghu = xieyi_zibiao['productid'] discount_yonghu = xieyi_zibiao['discount'] price_yonghu = xieyi_zibiao['price'] zibiao_id_yonghu = xieyi_zibiao['id'] if productid_yonghu in product_all_ids: # 产品id已经存在 continue # 折扣表中 产品id不为* 就展示对应的产品信息和折扣信息和折扣后的价格 底价表中展示对应产品信息和售价信息 if salemode_yonghu == '0': if productid_yonghu != '*': # 筛选id product_all_ids.append(productid_yonghu) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid_yonghu product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['offer_orgid'] = orgid product_info['bid_orgid'] = user_org_id product_info['zibiao_id'] = zibiao_id_yonghu product_info['protocolid'] = protocolid_yonghu product_info['discount'] = discount_yonghu # TODO 从供应商那里获取对应的产品价格 product_info['price'] = round(random.random() * 100, 2) product_info['discount_price'] = round(product_info['price'] * product_info['discount'], 2) # 追加产品信息 product_all_infos.append(product_info) elif productid_yonghu == '*': # 折扣表中 产品id为* 就向上级机构查找有对应providerid的多少产品 依次迭代 shangji_chanpin_li = await shangji_provider_product_search({'providerid': providerid_yonghu, 'offer_orgid': orgid}) for shangji_chanpin in shangji_chanpin_li: shangji_chanpin_id = shangji_chanpin['id'] # 筛选id if shangji_chanpin_id in product_all_ids: continue product_all_ids.append(shangji_chanpin_id) shangji_chanpin['discount'] = discount_yonghu shangji_chanpin['zibiao_id'] = '*' shangji_chanpin['protocolid'] = protocolid_yonghu shangji_chanpin['offer_orgid'] = orgid shangji_chanpin['bid_orgid'] = user_org_id product_all_infos.append(shangji_chanpin) elif salemode_yonghu == '2': # 筛选id product_all_ids.append(productid_yonghu) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid_yonghu product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['price'] = price_yonghu product_info['zibiao_id'] = zibiao_id_yonghu product_info['protocolid'] = protocolid_yonghu product_info['offer_orgid'] = orgid product_info['bid_orgid'] = user_org_id # 查看折扣不再追加产品信息 # product_all_infos.append(product_info) # 查找offer对应的机构id bid为* 的所有配置 时间限制 xieyi_cha_sql = """select * from saleprotocol where offer_orgid = '%s' and bid_orgid = '*' and del_flg = '0' and CURRENT_DATE between start_date and end_date;""" % orgid xieyi_cha_res_li = await sor.sqlExe(xieyi_cha_sql, {}) for xieyi_cha in xieyi_cha_res_li: salemode = xieyi_cha['salemode'] protocolid = xieyi_cha['id'] # 查看子表 xieyi_zibiao_cha_li = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) xieyi_zibiao_cha_li_ = sorted(xieyi_zibiao_cha_li, key=lambda x: (x['productid'] == '*', x['productid'])) if salemode == '0' or salemode == '2': for xieyi_zibiao in xieyi_zibiao_cha_li_: providerid = xieyi_zibiao['providerid'] productid = xieyi_zibiao['productid'] discount = xieyi_zibiao['discount'] price = xieyi_zibiao['price'] zibiao_id_ = xieyi_zibiao['id'] if productid in product_all_ids: # 产品id已经存在 continue # 折扣表中 产品id不为* 就展示对应的产品信息和折扣信息和折扣后的价格 底价表中展示对应产品信息和售价信息 if salemode == '0': if productid != '*': # 筛选id product_all_ids.append(productid) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['offer_orgid'] = orgid product_info['bid_orgid'] = user_org_id product_info['protocolid'] = protocolid product_info['zibiao_id'] = zibiao_id_ product_info['discount'] = discount # TODO 从供应商那里获取对应的产品价格 product_info['price'] = round(random.random() * 100, 2) product_info['discount_price'] = round(product_info['price'] * product_info['discount'], 2) # 追加产品信息 product_all_infos.append(product_info) elif productid == '*': # 折扣表中 产品id为* 就向上级机构查找有对应providerid的多少产品 依次迭代 shangji_chanpin_li = await shangji_provider_product_search({'providerid': providerid, 'offer_orgid': orgid}) for shangji_chanpin in shangji_chanpin_li: shangji_chanpin_id = shangji_chanpin['id'] # 筛选id if shangji_chanpin_id in product_all_ids: continue product_all_ids.append(shangji_chanpin_id) shangji_chanpin['discount'] = discount shangji_chanpin['zibiao_id'] = zibiao_id_ shangji_chanpin['protocolid'] = protocolid shangji_chanpin['offer_orgid'] = orgid shangji_chanpin['bid_orgid'] = user_org_id product_all_infos.append(shangji_chanpin) elif salemode == '2': # 筛选id product_all_ids.append(productid) product_info_sql = """select * from product where id = '%s' and del_flg = '0' and CURRENT_DATE between effect_date and expire_date;""" % productid product_info_li = await sor.sqlExe(product_info_sql, {}) product_info = product_info_li[0] if product_info_li else {} if product_info: product_info['offer_orgid'] = orgid product_info['bid_orgid'] = user_org_id product_info['price'] = price product_info['zibiao_id'] = zibiao_id_ product_info['protocolid'] = protocolid # 查看折扣不再追加产品信息 # product_all_infos.append(product_info) # 获取本机构所有产品 provider_products = await self_product_search({'bid_orgid': orgid, 'sor': sor}) for prd in provider_products: prdid = prd.get('id') if prdid not in product_all_ids: prd['offer_orgid'] = orgid prd['bid_orgid'] = user_org_id prd['productid'] = prdid product_all_infos.append(prd) return { 'status': True, 'msg': '销售获取商品折扣信息成功', 'data': product_all_infos } except Exception as e: return { 'status': False, 'msg': '销售获取商品折扣信息出错', 'err_msg': e } async def discount_guanli_jia(ns={}): """ 折扣管理加 :return: """ ns = { 'bid_orgid': 'mIWUHBeeDM8mwAFPIQ8pS', 'providerid': 'E0RFifplwLEphX5ckLLtj', 'productid': 'mbSMhLef1RdVWVxf_H9Xs', 'discount': 0.8 } db = DBPools() async with db.sqlorContext('kboss') as sor: try: # 首先查找协议是否已经存在 ns_shoujia = { 'offer_orgid': ns.get('bid_orgid'), 'bid_orgid': '*', 'salemode': '0', 'del_flg': '0' } shoujia_res_li = await sor.R('saleprotocol', ns_shoujia) if shoujia_res_li: protocolid = shoujia_res_li[0]['id'] else: # 协议表不存在 就创建 ns_zhubiao = { 'id': uuid(), 'offer_orgid': ns.get('bid_orgid'), 'bid_orgid': '*', 'salemode': '0', 'start_date': '2010-10-01', 'end_date': '2099-10-01' } await sor.C('saleprotocol', ns_zhubiao) protocolid = ns_zhubiao['id'] # 根据protocolid创建折扣 ns_zibiao_shoujia = { 'id': uuid(), 'protocolid': protocolid, 'providerid': ns.get('providerid'), 'productid': ns.get('productid'), 'discount': ns.get('discount') } await sor.C('product_salemode', ns_zibiao_shoujia) return { 'status': True, 'msg': '创建折扣成功' } except Exception as e: raise e return { 'status': False, 'msg': '创建折扣失败' } async def discount_guanli_no_config(ns={}): """ 折扣管理查 :return: """ ns = { 'bid_orgid': 'mIWUHBeeDM8mwAFPIQ8pS' } bid_orgid = ns.get('bid_orgid') db = DBPools() async with db.sqlorContext('kboss') as sor: try: ns_kehu_zhekou = { 'offer_orgid': bid_orgid, 'bid_orgid': '*', 'salemode': '0', 'del_flg': '0' } ns_dijia = { 'bid_orgid': ns.get('bid_orgid'), 'salemode': '2', 'del_flg': '0' } # 本机构所有产品 self_product_all = await self_product_search({'bid_orgid': bid_orgid}) # 筛选所有是底价的产品 dijia_res_li = await sor.R('saleprotocol', ns_dijia) dijia_res_ids = [item_.get('productid') for item_ in dijia_res_li if item_.get('productid')] # 本机构折扣和回佣产品 jigou_zhekou_huiyong = [item_prd for item_prd in self_product_all if item_prd.get('id') not in dijia_res_ids] zhekou_exist = [] # 本机构所有设置过统一折扣的产品 kehu_zhekou_res_li = await sor.R('product_salemode', ns_kehu_zhekou) for kehu_zhekou in kehu_zhekou_res_li: protocolid = kehu_zhekou['id'] zi_biao_cha_li = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) for zibiao in zi_biao_cha_li: zhekou_exist.append(zibiao['productid']) product_all = [prd_item for prd_item in jigou_zhekou_huiyong if prd_item.get('id') not in zhekou_exist] return { 'status': True, 'msg': '销售看折扣成功', 'data': product_all } except Exception as e: return { 'status': False, 'msg': '销售看折扣失败', 'err_msg': e } async def discount_guanli_cha(ns={}): """ 折扣管理查 :return: """ bid_orgid = ns.get('bid_orgid') db = DBPools() async with db.sqlorContext('kboss') as sor: try: ns_shangji_zhekou = { 'bid_orgid': ns.get('bid_orgid'), 'salemode': '0', 'del_flg': '0' } ns_kehu_zhekou = { 'offer_orgid': ns.get('bid_orgid'), 'bid_orgid': '*', 'salemode': '0', 'del_flg': '0' } # 查找所有bid是本机构的协议表 sql_find_bid = """select * from saleprotocol where bid_orgid = '%s' and (salemode='0' or salemode='1') and del_flg = '0';""" % bid_orgid sql_find_bid_li = await sor.sqlExe(sql_find_bid, {}) # 查找所有针对本机构salemode为折扣(和回佣)的所有产品并显示折扣 all_product = [] all_product_ids = [] # 判断是业主机构还是分销商 yezhu_li = await sor.R('organization', {'id': bid_orgid}) yezhu_judge = yezhu_li[0]['org_type'] # 如果是业主机构 if yezhu_judge == '0': for sett in sql_find_bid_li: salemode = sett['salemode'] offer_orgid = sett['offer_orgid'] protocolid = sett['id'] # 如果salemode是1 就是供应商所有产品 if salemode == '1': provider_product_sql = """select * from product WHERE providerid = '%s' and del_flg = '0';""" % offer_orgid provider_product_li = await sor.sqlExe(provider_product_sql, {}) all_product.extend(provider_product_li) # 如果salemode是折扣 如果有* 就是供应商所有产品 如果salemode是底价就获取所有productid if salemode == '0': # 查找子表 zi_table = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) zi_table = [zi_table_item for zi_table_item in zi_table if zi_table_item.get('productid')] xieyi_zibiao_cha_li_ = sorted(zi_table, key=lambda x: (x['productid'] == '*', x['productid'])) for xieyi_zibiao in xieyi_zibiao_cha_li_: product_id_ = xieyi_zibiao['productid'] product_discount_ = xieyi_zibiao['discount'] if product_id_ != '*': single_sql = """select * from product where id = '%s' and del_flg = '0';""" % product_id_ single_res_li = await sor.sqlExe(single_sql, {}) if single_res_li: single_res = single_res_li[0] single_res['shangji_discount'] = product_discount_ all_product_ids.append(product_id_) all_product.append(single_res) # else: # # 如果折扣设置为* 获取上级机构所有产品 # shangji_suoyou_chanpin = await self_product_search({'bid_orgid': bid_orgid}) # for shangji_suoyou in shangji_suoyou_chanpin: # shangji_prd_id = shangji_suoyou.get('id') # if shangji_prd_id not in all_product_ids: # shangji_suoyou['shangji_discount'] = product_discount_ # all_product_ids.append(shangji_prd_id) # all_product.append(shangji_suoyou) # 如果是分销商 if yezhu_judge == '1': for sett in sql_find_bid_li: salemode = sett['salemode'] offer_orgid = sett['offer_orgid'] protocolid = sett['id'] # 判断是业主机构0/供应商4/分销商1 (产品id取消* 不再做判断) # yezhu_li = await sor.R('organization', {'id': offer_orgid}) # yezhu_judge = yezhu_li[0]['org_type'] # 如果salemode是1 就是offer所有产品 (产品id取消* 不再做判断) # if salemode == '1': # prodcut_one = await self_product_search({'bid_orgid': offer_orgid}) # all_product.extend(prodcut_one) # 如果salemode是折扣 如果有* 就是供应商所有产品 不是* 就获取所有productid if salemode == '0' or salemode == '1': zi_table = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'}) # xieyi_zibiao_cha_li_ = sorted(zi_table, key=lambda x: (x['productid'] == '*', x['productid'])) # 如果有* 就获取providerid所有产品 for xieyi_zibiao in zi_table: product_id_ = xieyi_zibiao['productid'] product_discount_ = xieyi_zibiao['discount'] if product_id_ != '*': single_sql = """select * from product where id = '%s' and del_flg = '0'""" % product_id_ single_res_li = await sor.sqlExe(single_sql, {}) if single_res_li: single_res = single_res_li[0] single_res['shangji_discount'] = product_discount_ all_product_ids.append(product_id_) all_product.append(single_res) # else: # # 如果折扣设置为* 获取上级机构所有产品 # shangji_suoyou_chanpin = await self_product_search({'bid_orgid': bid_orgid}) # for shangji_suoyou in shangji_suoyou_chanpin: # shangji_prd_id = shangji_suoyou.get('id') # if shangji_prd_id not in all_product_ids: # shangji_suoyou['shangji_discount'] = product_discount_ # all_product_ids.append(product_id_) # all_product.append(shangji_suoyou) # 查找本机构对客户设置过的折扣 benjigou_kehu_zhekou_dic = { 'offer_orgid': bid_orgid, 'bid_orgid': '*', 'salemode': '0', 'del_flg': '0' } exist_yijing_peizhi = [] benjigou_kehu_zhekou_li = await sor.R('saleprotocol', benjigou_kehu_zhekou_dic) for benjigou_kehu_zhekou in benjigou_kehu_zhekou_li: benjigou_kehu_zhekou_protocolid = benjigou_kehu_zhekou.get('id') benjigou_kehu_zhekou_zibiao_li_ = await sor.R('product_salemode', {'protocolid': benjigou_kehu_zhekou_protocolid, 'del_flg': '0'}) benjigou_kehu_zhekou_zibiao_li = sorted(benjigou_kehu_zhekou_zibiao_li_, key=lambda x: (x['productid'] == '*', x['productid'])) for kehu_zhekou in benjigou_kehu_zhekou_zibiao_li: zi_biao_id = kehu_zhekou.get('id') prd_id = kehu_zhekou.get('productid') exist_yijing_peizhi.append(prd_id) prd_discount = kehu_zhekou.get('discount') # 数据覆盖 all_product_bck = [ites for ites in all_product] # 单独配置过的折扣产品 if prd_id != '*': for aa in all_product_bck: aaid = aa.get('id') if aaid == prd_id: all_product.remove(aa) aa['kehu_discount'] = prd_discount aa['zi_biao_id'] = zi_biao_id all_product.append(aa) # else: # # 配置为*的统一折扣产品 # for aa in all_product_bck: # if not aa.get('kehu_discount'): # all_product.remove(aa) # aa['kehu_discount'] = prd_discount # aa['zi_biao_id'] = zi_biao_id # all_product.append(aa) # 只有设置了客户折扣才会展示 # all_product = [itemm for itemm in all_product if itemm.get('kehu_discount')] # 查找供应商名称 赋值给每个产品 pro_name_li = await sor.R('organization', {'del_flg': '0'}) for aeo in all_product: for aoe_i in pro_name_li: if aeo['providerid'] == aoe_i['id']: aeo['providername'] = aoe_i['orgname'] # # 获取供应商和ptype拼接 # cc = {} # bb = {} # for item in all_product: # providerid = item['providerid'] # providername = item['providername'] # ptype = item['ptype'] # if providername in cc.keys(): # ptypes = cc[providername]['ptype'] # if not ptype in ptypes: # ptypes.append(ptype) # cc[providername]['ptype'] = ptypes # else: # bb[providername] = {'providerid': providerid, 'ptype': [ptype]} # cc.update(bb) # yugo_list = [] # for yugo in cc.keys(): # yugo_dic = {} # yugo_dic['providername'] = yugo # yugo_dic['providerid'] = cc[yugo]['providerid'] # yugo_dic['ptype'] = [{'ptype': yg} for yg in cc[yugo]['ptype']] # yugo_list.append(yugo_dic) # # 指定供应商和供应商的所有产品 # if ns.get('providerid'): # ptypes = [] # prid_res = [] # # 找到对应供应商的ptype # for daer in yugo_list: # daer_providerid = daer['providerid'] # if daer_providerid == ns.get('providerid'): # ptypes = daer['ptype'] # prid_res = [item for item in all_product if item['providerid'] == ns.get('providerid')] # # 如果既有供应商id 又有ptype指定的类型 # if ns.get('ptype'): # prid_res = [item for item in all_product if item['providerid'] == ns.get('providerid') # and item['ptype'] == ns.get('ptype')] # return { # 'status': True, # 'msg': '获取供应商对应的产品成功', # 'provider_info': ptypes, # 'data': prid_res # } # if ns.get('ptype'): # # ptype指定的类型 # if ns.get('ptype'): # prid_res = [item for item in all_product if item['ptype'] == ns.get('ptype')] # return { # 'status': True, # 'msg': '获取供应商对应的产品成功', # 'data': prid_res # } # if ns.get('keyword'): # return { # 'status': True, # 'msg': '通过关键字查找产品成功', # 'provider_info': yugo_list, # 'data': [item for item in all_product if ns.get('keyword') in item['name']] # } # if ns.get('kv') == 'ptype': # # 找所有ptype # all_ptype = [] # for juli in yugo_list: # juli_ptype = juli['ptype'] # all_ptype.extend(juli_ptype) # filter_all_ptype = [] # for item in all_ptype: # if not item in filter_all_ptype: # filter_all_ptype.append(item) # return { # 'status': True, # 'msg': '获取所有产品类型成功', # 'data': filter_all_ptype # } # # return { # 'status': True, # 'msg': '销售看折扣成功', # 'provider_info': yugo_list, # 'data': all_product # } provider_info = [] for i in all_product: provider_dic = {'providername': i.get('providername'), 'providerid': i.get('providerid')} if not provider_dic in provider_info: provider_info.append(provider_dic) if ns.get('start_discount') and ns.get('end_discount'): temp_list = [] for i in all_product: if i.get('shangji_discount'): if float(ns['start_discount']) <= float(i['shangji_discount']) <= float(ns['end_discount']): temp_list.append(i) all_product = temp_list if ns.get('providerid'): list2 = [] for i in all_product: if i['providerid'] == ns.get('providerid'): list2.append(i) all_product = list2 # 专属分类 classify_list_temp = list(set([i.get('classify') for i in all_product])) classify_list = [{'class_key': '未配置'} if x == None else {'class_key': x} for x in classify_list_temp] if ns.get('classify'): if ns['classify'] == '未配置': classify_filter = None else: classify_filter = ns['classify'] all_product = [i for i in all_product if i.get('classify') == classify_filter] if ns.get('keyword'): all_product = [i for i in all_product if ns['keyword'] in i['name']] return { 'status': True, 'msg': '查找产品成功', 'classify_list': classify_list, 'provider_info': provider_info, 'data': all_product } except Exception as e: raise e return { 'status': False, 'msg': '销售看折扣失败', # 'err_msg': e } ret = await discount_guanli_cha(params_kw) return ret