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: settle_mode = sett['settle_mode'] offer_orgid = sett['offer_orgid'] protocolid = sett['id'] # 如果settle_mode是1 就是供应商所有产品 if settle_mode == '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) # 如果settle_mode是折扣 如果有* 就是供应商所有产品 如果settle_mode是底价就获取所有productid if settle_mode == '0' or settle_mode == '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: settle_mode = sett['settle_mode'] offer_orgid = sett['offer_orgid'] protocolid = sett['id'] # 判断是业主机构还是分销商 yezhu_li = await sor.R('organization', {'id': offer_orgid}) yezhu_judge = yezhu_li[0]['org_type'] # 如果settle_mode是1 就是offer所有产品 if settle_mode == '1': prodcut_one = await self_product_search({'bid_orgid': offer_orgid}) all_product.extend(prodcut_one) # 如果settle_mode是折扣 如果有* 就是供应商所有产品 不是* 就获取所有productid if settle_mode == '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) # 如果settle_mode是底价 就获取所有productid if settle_mode == '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 { "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 no_cofig_get(ns={}): """ 1. 供应商添加过产品 没有配置 2. 给分销商的配置 :return: """ 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 } ret = await no_cofig_get(params_kw) return ret