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 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': 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 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(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 } except Exception as e: raise e return { 'status': False, 'msg': '销售看折扣失败', # 'err_msg': e } async def shoujia_guanli_cha(ns={}): """ 销售看售价 :return: """ 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'}) if product_table_li: 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) # 查找供应商名称 赋值给每个产品 pro_name_li = await sor.R('organization', {'del_flg': '0'}) for aeo in product_all: for aoe_i in pro_name_li: if aeo['providerid'] == aoe_i['id']: aeo['providername'] = aoe_i['orgname'] # 获取供应商和ptype拼接 cc = {} bb = {} for item in product_all: 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 product_all if item['providerid'] == ns.get('providerid')] # 如果既有供应商id 又有ptype指定的类型 if ns.get('ptype'): prid_res = [item for item in product_all if item['providerid'] == ns.get('providerid') and item['ptype'] == ns.get('ptype')] break return { 'status': True, 'msg': '获取供应商对应的产品成功', 'provider_info': ptypes, 'data': prid_res } if ns.get('ptype'): prid_res = [item for item in product_all 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 product_all 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, 'provider_info': yugo_list, 'msg': '查看售价产品成功', 'data': product_all } except Exception as e: raise e return { 'status': False, 'msg': '查看售价产品失败' } async def reseller_discount_config_get(ns={}): """ 获取本机构所有折扣和回佣的产品 配置详情 :param ns: :return: """ ns['del_flg'] = '0' offer_orgid = ns.get('offer_orgid') bid_orgid = ns.get('bid_orgid') flag = ns.get('flag') salemode = ns.get('salemode') discount_guanli_cha_res = [] price_guanli_cha_res = [] if salemode == '0' or salemode == '1': # 获取统一折扣管理内容 discount_guanli_cha_res = await discount_guanli_cha({'bid_orgid': offer_orgid}) discount_guanli_cha_res.pop('provider_info') elif salemode == '2': # 获取统一底价管理内容 price_guanli_cha_res = await shoujia_guanli_cha({'bid_orgid': offer_orgid}) price_guanli_cha_res.pop('provider_info') db = DBPools() async with db.sqlorContext('kboss') as sor: if salemode == '0': # 首先获取回佣的产品列表 待筛选 reseller_huiyong_dic = { 'offer_orgid': offer_orgid, 'bid_orgid': bid_orgid, 'salemode': '1', 'del_flg': '0' } reseller_huiyong_li = await sor.R('saleprotocol', reseller_huiyong_dic) if reseller_huiyong_li: reseller_huiyong_protocolid = reseller_huiyong_li[0].get('id') reseller_huiyong_zibiao_li = await sor.R('product_salemode', {'protocolid': reseller_huiyong_protocolid, 'del_flg': '0'}) else: reseller_huiyong_zibiao_li = [] rebate_product_list = [] for data_detail in discount_guanli_cha_res['data']: for reseller_huiyong in reseller_huiyong_zibiao_li: if data_detail['id'] == reseller_huiyong['productid']: rebate_product_list.append(data_detail['id']) # 获取针对分销商的折扣 reseller_zhekou_dic = { 'offer_orgid': offer_orgid, 'bid_orgid': bid_orgid, 'salemode': '0', 'del_flg': '0' } reseller_zhekou_li = await sor.R('saleprotocol', reseller_zhekou_dic) if reseller_zhekou_li: reseller_zhekou_protocolid = reseller_zhekou_li[0].get('id') reseller_zhekou_zibiao_li = await sor.R('product_salemode', {'protocolid': reseller_zhekou_protocolid, 'del_flg': '0'}) # print('reseller_zhekou_zibiao_li: ', reseller_zhekou_zibiao_li) else: reseller_zhekou_zibiao_li = [] for data_detail in discount_guanli_cha_res['data']: for reseller_zhekou in reseller_zhekou_zibiao_li: if data_detail['id'] == reseller_zhekou['productid']: data_detail['reseller_discount'] = reseller_zhekou['discount'] data_detail['reseller_discount_price_id'] = reseller_zhekou['id'] if not data_detail.get('reseller_discount'): data_detail['reseller_discount'] = None available_data = [] # 筛选已经配置 if flag == '1': for detail in discount_guanli_cha_res['data']: if detail.get('reseller_discount'): available_data.append(detail) discount_guanli_cha_res['data'] = available_data # 筛选没有配置 if flag == '0': for detail in discount_guanli_cha_res['data']: if not detail.get('reseller_discount'): available_data.append(detail) discount_guanli_cha_res['data'] = available_data # 折扣区间筛选 available_data_range = [] if ns.get('range'): start_extent, end_extent = ns.get('range').split(',') for detail in discount_guanli_cha_res['data']: shangji_discount = float(detail['shangji_discount']) if detail.get('shangji_discount') else 0 if (float(end_extent) >= shangji_discount and shangji_discount >= float(start_extent)): available_data_range.append(detail) discount_guanli_cha_res['data'] = available_data_range # 筛选出回佣里没有的产品 rebate_filter_prd = [] for detail in discount_guanli_cha_res['data']: if not detail['id'] in rebate_product_list: rebate_filter_prd.append(detail) discount_guanli_cha_res['data'] = rebate_filter_prd return discount_guanli_cha_res elif salemode == '2': reseller_price_dic = { 'offer_orgid': offer_orgid, 'bid_orgid': bid_orgid, 'salemode': '2', 'del_flg': '0' } reseller_li = await sor.R('saleprotocol', reseller_price_dic) if reseller_li: reseller_protocolid = reseller_li[0].get('id') reseller_zibiao_li = await sor.R('product_salemode', {'protocolid': reseller_protocolid, 'del_flg': '0'}) # print('reseller_dijia_zibiao_li: ', reseller_zibiao_li) else: reseller_zibiao_li = [] for data_detail in price_guanli_cha_res['data']: for reseller_price in reseller_zibiao_li: if data_detail['id'] == reseller_price['productid']: data_detail['reseller_price'] = reseller_price['price'] data_detail['reseller_discount_price_id'] = reseller_price['id'] if not data_detail.get('reseller_price'): data_detail['reseller_price'] = None available_data = [] # 筛选已经配置 if flag == '1': for detail in price_guanli_cha_res['data']: if detail.get('reseller_price'): available_data.append(detail) price_guanli_cha_res['data'] = available_data # 筛选没有配置 if flag == '0': for detail in price_guanli_cha_res['data']: if not detail.get('reseller_price'): available_data.append(detail) price_guanli_cha_res['data'] = available_data return price_guanli_cha_res else: # 首先获取分销商的折扣 待筛选 reseller_zhekou_dic = { 'offer_orgid': offer_orgid, 'bid_orgid': bid_orgid, 'salemode': '0', 'del_flg': '0' } reseller_zhekou_li = await sor.R('saleprotocol', reseller_zhekou_dic) reseller_zhekou_protocolid = reseller_zhekou_li[0].get('id') if reseller_zhekou_li else '1' reseller_zhekou_zibiao_li = await sor.R('product_salemode', {'protocolid': reseller_zhekou_protocolid, 'del_flg': '0'}) # print('reseller_zhekou_zibiao_li: ', reseller_zhekou_zibiao_li) discount_product_list = [] for data_detail in discount_guanli_cha_res['data']: for reseller_zhekou in reseller_zhekou_zibiao_li: if data_detail['id'] == reseller_zhekou['productid']: discount_product_list.append(data_detail['id']) # 获取针对分销商的回佣 reseller_huiyong_dic = { 'offer_orgid': offer_orgid, 'bid_orgid': bid_orgid, 'salemode': '1', 'del_flg': '0' } reseller_huiyong_li = await sor.R('saleprotocol', reseller_huiyong_dic) reseller_huiyong_protocolid = reseller_huiyong_li[0].get('id') if reseller_huiyong_li else '1' reseller_huiyong_zibiao_li = await sor.R('product_salemode', {'protocolid': reseller_huiyong_protocolid, 'del_flg': '0'}) for data_detail in discount_guanli_cha_res['data']: for reseller_huiyong in reseller_huiyong_zibiao_li: if data_detail['id'] == reseller_huiyong['productid']: data_detail['reseller_discount_price_id'] = reseller_huiyong['id'] if not data_detail.get('reseller_discount'): data_detail['reseller_rebate'] = None rebate_all = {} # 获取回佣主表所有rebate_cycle rebate_cycle_all = await sor.R('rebate_cycle', {'protocolid': reseller_huiyong_protocolid, 'del_flg': '0'}) for rebate_cyc in rebate_cycle_all: zhu_table_id = rebate_cyc['id'] sette_dp = rebate_cyc['sette_dp'] zhu_table_cycle = rebate_cyc['rebate_cycle'] # 查找子表内容 zi_table_find = { 'rebatecycleid': zhu_table_id, 'sort': 'sale_amount', 'del_flg': '0' } zi_table_all = await sor.R('rp_rebate', zi_table_find) for zi_table in zi_table_all: zi_table_sigle = {} zi_table_sigle['rp_rebate_id'] = zi_table['id'] zi_table_sigle['sale_amount'] = zi_table['sale_amount'] zi_table_sigle['rebate_rate'] = zi_table['rebate_rate'] zi_table_sigle['rebate_cycle_id'] = zhu_table_id zi_table_sigle['sette_dp'] = sette_dp if zhu_table_cycle in rebate_all.keys(): rebate_all[zhu_table_cycle].append(zi_table_sigle) else: rebate_all[zhu_table_cycle] = [zi_table_sigle] available_data = [] # 筛选已经配置 if flag == '1': for detail in discount_guanli_cha_res['data']: if detail.get('reseller_discount_price_id'): available_data.append(detail) discount_guanli_cha_res['data'] = available_data # 筛选没有配置 if flag == '0': for detail in discount_guanli_cha_res['data']: if not detail.get('reseller_discount_price_id'): available_data.append(detail) discount_guanli_cha_res['data'] = available_data # 筛选出回佣里没有的产品 discount_filter_prd = [] for detail in discount_guanli_cha_res['data']: if not detail['id'] in discount_product_list: discount_filter_prd.append(detail) discount_guanli_cha_res['data'] = discount_filter_prd # 添加回佣字段内容 discount_guanli_cha_res['rebate'] = rebate_all return discount_guanli_cha_res ret = await reseller_discount_config_get(params_kw) return ret