async def DiscontProductSearch(ns={}): """ search discount product if ns is None, return all product Can be queried using separate fields 销售查看所有产品 销售salemanid -> 查找orgid org_type=1 -> 用户查看所有产品 userid -> orgid -> parentid=用户的销售人员机构id = resellerid 来源供应商/来源上级机构 resellerid 折扣底价查找 rp_discount floor_price cp_discount(优先) 获取底价 获取折扣 :param ns: :return: """ db = DBPools() async with db.sqlorContext('kboss') as sor: try: ns['del_flg'] = '0' ns['sort'] = 'create_at' ns['order'] = 'desc' ns['page'] = ns.get('page') if not ns.get('page'): return { 'status': False, 'msg': 'page is null' } orgid_find_ = await sor.R('users', {'id': await get_user(), 'del_flg': '0'}) # TODO 根据org_type判断类别 org_type_flag = 0 if orgid_find_: orgid_ = orgid_find_[0].get('orgid') orgid_find = await sor.R('organization', {'id': orgid_}) # 销售查询 orgid org_type = 1 即分销商机构id if orgid_find[0].get('org_type') == '1': org_type_flag = 1 orgid = orgid_ else: # 客户查询 (客户user 客户org) -> 客户parentid 即分销商机构id orgid = orgid_find[0].get('parentid') else: return { 'status': False, 'msg': 'can not find orgid' } if org_type_flag == 1: # 查找售价 floor_sql = """select fl.offer_orgid, fl.bid_orgid, fl.productid, fl.price, p.id, p.providerid, p.providerpid, p.name, p.description, p.ptype, p.state, p.effect_date, p.expire_date, p.salemode, p.product_code, p.spec_note, p.product_area, p.specific_pattern, p.reseller_orgid FROM floorprice as fl LEFT JOIN product as p on fl.productid=p.id where fl.offer_orgid='%s' and fl.bid_orgid='' and fl.del_flg='0';""" % orgid floor_sql_res_li = await sor.sqlExe(floor_sql, {}) # filter discount and rebate ns.pop('userid') # 分销商查询折扣底价回佣 if org_type_flag == 1: cp_discount_sql = """select cp.customerid, cp.productid, cp.discount, p.id, p.providerid, p.providerpid, p.name, p.description, p.ptype, p.state, p.effect_date, p.expire_date, p.salemode, p.product_code, p.spec_note, p.product_area, p.specific_pattern, p.reseller_orgid from cp_discount as cp left join product as p on cp.productid=p.id where customerid = '%s' and cp.del_flg = '0' and CURRENT_DATE between cp.start_date and cp.end_date;""" % orgid # 客户只能查看折扣底价 else: # 查询对应分销商的折扣 discounts = await sor.R('rp_discount', {'resellerid': orgid, 'del_flg': '0'}) cp_discount_sql = """select cp.customerid, cp.productid, cp.discount, p.id, p.providerid, p.providerpid, p.name, p.description, p.ptype, p.state, p.effect_date, p.expire_date, p.salemode, p.product_code, p.spec_note, p.product_area, p.specific_pattern, p.reseller_orgid from cp_discount as cp left join product as p on cp.productid=p.id where customerid = '%s' and cp.del_flg = '0' and CURRENT_DATE between cp.start_date and cp.end_date;""" % orgid cp_discount_list = await sor.sqlExe(cp_discount_sql, {'customerid': orgid}) # cp_dict = {} # result = [] # for cp in cp_discount_list: # cp_dict[cp.get('productid')] = cp.get('discount') # keys_list = list(cp_dict.keys()) # for product in product_res_list.get('rows'): # productid = product.get('id') # if productid in keys_list: # product['discount'] = cp_dict[productid] # # product['discount_price'] = round(product['price'] * cp_dict[productid], 2) # result.append(product) if cp_discount_list: # result = sorted(cp_discount_list, key=lambda x: x['start_date'], reverse=True) result = cp_discount_list else: result = [] result.extend(floor_sql_res_li) else: # 查找售价 floor_signal_sql = """select * from floorprice where offer_orgid = '%s' and bid_orgid = '' and del_flg = '0' and CURRENT_DATE BETWEEN begin_date AND end_date;""" % orgid floor_signal_li = await sor.sqlExe(floor_signal_sql, {}) floor_signal_productids = [item.get('productid') for item in floor_signal_li] # 查找折扣 discount_signal_sql = """select * from rp_discount where resellerid = '%s' and del_flg = '0' and CURRENT_DATE BETWEEN start_date AND end_date;""" % orgid discount_signal_li = await sor.sqlExe(discount_signal_sql, {}) discount_signal_productids = [item.get('productid') for item in discount_signal_li] # 查找针对客户设置的折扣 discount_user_sql = """select * from cp_discount where customerid = '%s' and del_flg = '0' and CURRENT_DATE BETWEEN start_date AND end_date;""" % orgid_find[0].get('id') discount_user_li = await sor.sqlExe(discount_user_sql, {}) discount_user_productids = [item.get('productid') for item in discount_user_li] # 即有售价也有折扣的产品ids bind_floor_discount_li = list(set(floor_signal_productids) & set(discount_user_productids)) # 如果只有底价和折扣 获取底价和折扣的产品查询 floor_count = [] for marid in set(floor_signal_productids): # 如果底价 if marid not in bind_floor_discount_li: floor_sql = """select fl.offer_orgid, fl.bid_orgid, fl.productid, fl.price, p.id, p.providerid, p.providerpid, p.name, p.description, p.ptype, p.state, p.effect_date, p.expire_date, p.salemode, p.product_code, p.spec_note, p.product_area, p.specific_pattern, p.reseller_orgid FROM floorprice as fl LEFT JOIN product as p on fl.productid=p.id where fl.offer_orgid='%s' and fl.bid_orgid='' and productid = '%s' and fl.del_flg='0' and CURRENT_DATE between begin_date and end_date;""" % (orgid, marid) floor_sql_res_li = await sor.sqlExe(floor_sql, {}) floor_count.extend(floor_sql_res_li) discount_count = [] for jine in set(discount_signal_productids): # 如果只有折扣 if jine not in bind_floor_discount_li: discou_sql = """SELECT rpdisc.*, p.id AS product_id, p.providerid, p.providerpid, p.NAME, p.description, p.ptype, p.state, p.effect_date, p.expire_date, p.salemode, p.product_code, p.spec_note, p.product_area, p.specific_pattern, p.reseller_orgid FROM rp_discount AS rpdisc LEFT JOIN product AS p ON rpdisc.productid = p.id WHERE rpdisc.resellerid = '%s' AND rpdisc.productid = '%s' AND rpdisc.del_flg = '0' AND CURRENT_DATE BETWEEN start_date AND end_date;""" % (orgid, jine) discou_sql_res_li = await sor.sqlExe(discou_sql, {}) discount_count.extend(discou_sql_res_li) floor_dis_count = [] for floor_dis in set(bind_floor_discount_li): # 既有折扣也有售价 # 查找当前时间内折扣 floor_dis_dict = {} shou_sql = """select * from cp_discount where customerid = '%s' and productid = '%s' and CURRENT_DATE between start_date and end_date and del_flg = '0';""" % (orgid_find_[0].get('orgid'), floor_dis) shou_res_li = await sor.sqlExe(shou_sql, {}) shou_res = shou_res_li[0] shou_res['cp_discount_id'] = shou_res_li[0].pop('id') shou_res['cp_discount_start_date'] = shou_res_li[0].pop('start_date') shou_res['cp_discount_end_date'] = shou_res_li[0].pop('end_date') # 查找当前时间内底价 zhe_sql = """select * from floorprice where offer_orgid = '%s' and bid_orgid = '' and productid = '%s' and del_flg = '0' and CURRENT_DATE between begin_date and end_date;""" % (orgid, floor_dis) zhe_res_li = await sor.sqlExe(zhe_sql, {}) zhe_res = zhe_res_li[0] zhe_res['floor_price_id'] = zhe_res_li[0].pop('id') zhe_res['floor_price_begin_date'] = zhe_res_li[0].pop('begin_date') zhe_res['floor_price_end_date'] = zhe_res_li[0].pop('end_date') zhe_res['discount_price'] = round(float(zhe_res_li[0].get('price')) * float(shou_res_li[0].get('discount')), 2) # 查找产品内容 chan_res_li = await sor.R('product', {'id': floor_dis, 'del_flg': '0'}) chan_res = chan_res_li[0] # chan_res['product_id'] = chan_res.pop('id') floor_dis_dict.update(shou_res) floor_dis_dict.update(zhe_res) floor_dis_dict.update(chan_res) floor_dis_count.append(floor_dis_dict) floor_count.extend(discount_count) floor_count.extend(floor_dis_count) result = floor_count return { "status": True, "msg": "product search success", "data": result } except Exception as e: return { "status": False, "msg": "product search failed", 'err_msg': e } ret = await DiscontProductSearch(params_kw) return ret