async def resellerRebateAdd(ns={}): """ add reseller rebate `id` VARCHAR(32) comment 'id', `resellerid` VARCHAR(32) comment '分销商id', `productid` VARCHAR(32) comment '产品id', `level` int comment '级别', `sale_amount` double(18,2) comment '最低销售额', `rebate_rate` double(18,2) comment '返佣率', `start_date` date comment '起效日期', `end_date` date comment '失效日期', :param ns: :return: """ if not ns: return { "status": False, "msg": "reseller id is empty, please check" } if not ns.get('end_date'): ns['end_date'] = '9999-12-31' ns_ctl = { 'resellerid': ns.get('resellerid'), 'productid': ns.get('productid'), 'rebateid': uuid(), 'start_date': ns.get('start_date'), 'end_date': ns.get('end_date'), } ns_rebate = { 'rebate_cycle': ns.get('rebate_cycle'), 'sale_amount': ns.get('sale_amount'), 'rebate_rate': ns.get('rebate_rate'), 'rebateid': ns_ctl['rebateid'] } ns_ctl['id'] = uuid() ns_rebate['id'] = uuid() db = DBPools() async with db.sqlorContext('kboss') as sor: try: # ns_exists_rebate = { # 'resellerid': ns.get('resellerid'), # 'productid': ns.get('productid'), # 'sort': ['start_date'] # } # ns['del_flg'] = 0 # same_productid = await sor.R('rp_rebate_ctl', ns_exists_rebate) same_sql = """SELECT tl.*, te.rebate_cycle, te.sale_amount, te.rebate_rate FROM rp_rebate_ctl AS tl LEFT JOIN rp_rebate AS te ON tl.rebateid = te.rebateid WHERE resellerid = '%s' AND productid = '%s' AND tl.del_flg = '0' ORDER BY start_date ASC;""" % (ns.get('resellerid'), ns.get('productid')) same_productid = await sor.sqlExe(same_sql, {}) exists_cycle = {} exists_cycle_key = [] for same_cycle in same_productid: sdate = same_cycle['start_date'] if sdate in exists_cycle_key: exists_cycle[sdate] += same_cycle['rebate_cycle'] else: exists_cycle_key.append(sdate) exists_cycle[sdate] = same_cycle['rebate_cycle'] 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_ctl['end_date'] = start_date await sor.C('rp_rebate_ctl', ns_ctl) await sor.C('rp_rebate', ns_rebate) break if index == len(same_productid) - 1 and insert_date > start_date: prd['end_date'] = ns.get('start_date') await sor.U('rp_rebate_ctl', prd) ns_ctl['end_date'] = '9999-12-31' await sor.C('rp_rebate_ctl', ns_ctl) # 查找历史内容 并把原来时间段rebate表中内容赋值给新的时间段 sql_item = """select * from rp_rebate where rebateid = (select rebateid from rp_rebate_ctl where start_date = '%s' and resellerid = '%s' and productid = '%s') and del_flg = '0';""" % (str(start_date), prd['resellerid'], prd['productid']) sql_res = await sor.sqlExe(sql_item, {}) repeat_flg = True for reb in sql_res: ns_reb = { 'id': uuid(), 'rebate_cycle': reb.get('rebate_cycle'), 'sale_amount': reb.get('sale_amount'), 'rebate_rate': reb.get('rebate_rate'), 'rebateid': ns_ctl['rebateid'] } if ns_rebate['rebate_cycle'] == reb.get('rebate_cycle'): repeat_flg = False await sor.C('rp_rebate', ns_rebate) else: await sor.C('rp_rebate', ns_reb) if repeat_flg: await sor.C('rp_rebate', ns_rebate) break if start_date < insert_date < end_date: ns_ctl['end_date'] = prd.get('end_date') prd['end_date'] = ns_ctl.get('start_date') await sor.U('rp_rebate_ctl', prd) await sor.C('rp_rebate_ctl', ns_ctl) # 查找历史内容 并把原来时间段rebate表中内容赋值给新的时间段 sql_item = """select * from rp_rebate where rebateid = (select rebateid from rp_rebate_ctl where start_date = '%s' and resellerid = '%s' and productid = '%s') and del_flg = '0';""" % (str(start_date), prd['resellerid'], prd['productid']) sql_res = await sor.sqlExe(sql_item, {}) repeat_flg = True for reb in sql_res: ns_reb = { 'id': uuid(), 'rebate_cycle': reb.get('rebate_cycle'), 'sale_amount': reb.get('sale_amount'), 'rebate_rate': reb.get('rebate_rate'), 'rebateid': ns_ctl['rebateid'] } if ns_rebate['rebate_cycle'] == reb.get('rebate_cycle'): repeat_flg = False await sor.C('rp_rebate', ns_rebate) else: await sor.C('rp_rebate', ns_reb) if repeat_flg: await sor.C('rp_rebate', ns_rebate) break if start_date == insert_date: date_str = str(start_date) if ns['rebate_cycle'] in exists_cycle[date_str]: return { "status": False, "Warning": "The current date and rebate_cycle has already been configured" } else: ns_ctl['end_date'] = end_date # 时间一致 rebateid在rebate表中保持一致 # await sor.C('rp_rebate_ctl', ns_ctl) ns_rebate['rebateid'] = prd['rebateid'] await sor.C('rp_rebate', ns_rebate) break if not same_productid: ns_ctl['end_date'] = '9999-12-31' await sor.C('rp_rebate_ctl', ns_ctl) await sor.C('rp_rebate', ns_rebate) return { "status": True, "msg": "reseller rebate add success" } except Exception as e: raise e return { "status": False, "msg": "reseller rebate add failed" } ret = await resellerRebateAdd(params_kw) return ret