kboss/b/provider/providerDiscountAdd.dspy
2025-07-16 14:27:17 +08:00

68 lines
2.6 KiB
Plaintext

async def providerDiscountAdd(ns={}):
"""
add reseller discount
`id` VARCHAR(32) comment 'id',
`resellerid` VARCHAR(32) comment '分销商id',
`productid` VARCHAR(32) comment '产品id',
`discount` double(18,2) comment '折扣率',
`income_pzt` double(18,2) comment '收入百分比',
`start_date` date comment '起效日期',
`end_date` date comment '失效日期',
:param ns:
:return:
"""
ns['id'] = uuid()
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
ns_exists_discount = {
'resellerid': ns.get('resellerid'),
'productid': ns.get('productid'),
'sort': ['start_date']
}
ns['del_flg'] = 0
same_productid = await sor.R('rp_discount', 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('rp_discount', ns)
break
if index == len(same_productid) - 1 and insert_date > start_date:
prd['end_date'] = ns.get('start_date')
await sor.U('rp_discount', prd)
ns['end_date'] = '9999-12-31'
await sor.C('rp_discount', 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('rp_discount', prd)
await sor.C('rp_discount', 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('rp_discount', ns)
return {
"status": True,
"msg": "provider discount add success"
}
except Exception as e:
raise e
return {
"status": False,
"msg": "provider discount add failed"
}
ret = await providerDiscountAdd(params_kw)
return ret