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

210 lines
11 KiB
Plaintext

async def floorPriceAdd(ns={}):
"""
`id` VARCHAR(32) comment 'id',
`offer_orgid` VARCHAR(32) comment '售方机构id',
`bid_orgid` VARCHAR(32) comment '买方机构id',
`productid` double(18,2) comment '产品id',
`price` double(18,2) comment '价格',
`begin_date` date comment '起效日期',
`end_date` date comment '失效日期',
入参: 销售机构id和分销商机构id -> 通过产品id查供应商 -> 查找供应商最低价 -> 比对最低价 -> 增加数据
入参: 销售机构id和分销商机构id -> 通过销售机构id找parentid 机构id -> 查找floorprice 筛选 parentid最低价 -> 比对最低价 -> 增加数据
入参: 销售机构id和分销商机构id -> 通过销售机构id找底价表中 销售机构id=买方机构id -> 查查找floorprice 筛选 parentid最低价 -> 比对最低价 -> 增加数据
:param ns:
:return:
"""
ns['id'] = uuid()
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
if ns.get('floorprice') and ns.get('selling'):
if float(ns.get('selling')) <= float(ns.get('floorprice')):
return {
'status': False,
'msg': '售价不能低于底价'
}
ns_floor = {
'id': uuid(),
'offer_orgid': ns.get('offer_orgid'),
'bid_orgid': ns.get('bid_orgid'),
'productid': ns.get('productid'),
'price': ns.get('floorprice'),
'begin_date': ns.get('begin_date'),
}
ns_selling = {
'id': uuid(),
'offer_orgid': ns.get('bid_orgid'),
'bid_orgid': '',
'productid': ns.get('productid'),
'price': ns.get('selling'),
'begin_date': ns.get('begin_date'),
}
ns_exists_floor = {
'offer_orgid': ns.get('offer_orgid'),
'bid_orgid': ns.get('bid_orgid'),
'productid': ns.get('productid'),
'sort': ['begin_date'],
'del_flg': '0'
}
ns['del_flg'] = 0
same_productid = await sor.R('floorprice', ns_exists_floor)
insert_date = datetime.datetime.strptime(ns['begin_date'], '%Y-%m-%d').date()
for index, prd in enumerate(same_productid):
begin_date = datetime.datetime.strptime(prd['begin_date'], '%Y-%m-%d').date()
end_date = datetime.datetime.strptime(prd['end_date'], '%Y-%m-%d').date()
if index == 0 and insert_date < begin_date:
ns_floor['end_date'] = begin_date
ns_selling['end_date'] = begin_date
await sor.C('floorprice', ns_floor)
await sor.C('floorprice', ns_selling)
break
if index == len(same_productid) - 1 and insert_date > begin_date:
prd['end_date'] = ns.get('begin_date')
await sor.U('floorprice', prd)
sql_1 = """select * from floorprice where offer_orgid='%s' and bid_orgid='' and productid='%s' and del_flg='0' order by begin_date desc;""" % (ns.get('bid_orgid'), ns.get('productid'))
sql_res = await sor.sqlExe(sql_1, {})
await sor.U('floorprice', {'id': sql_res[0]['id'], 'end_date': ns.get('begin_date')})
ns_floor['end_date'] = '9999-12-31'
ns_selling['end_date'] = '9999-12-31'
await sor.C('floorprice', ns_floor)
await sor.C('floorprice', ns_selling)
break
if begin_date < insert_date < end_date:
ns_floor['end_date'] = prd.get('end_date')
prd['end_date'] = ns_floor.get('begin_date')
await sor.U('floorprice', prd)
await sor.C('floorprice', ns_floor)
sql_2 = """select * from floorprice where offer_orgid='%s' and bid_orgid='' and productid='%s' and del_flg='0' and '%s' between begin_date and end_date""" % (ns.get('bid_orgid'), ns.get('productid'), insert_date)
sql_res = await sor.sqlExe(sql_2, {})
ns_selling['end_date'] = sql_res[0].get('end_date')
sql_res[0]['end_date'] = ns_selling.get('begin_date')
await sor.U('floorprice', sql_res[0])
await sor.C('floorprice', ns_selling)
break
if begin_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('floorprice', ns)
elif ns.get('orgid') and ns.get('selling'):
ns_exists_discount = {
'offer_orgid': ns.get('orgid'),
'bid_orgid': '',
'productid': ns.get('productid'),
'sort': ['begin_date'],
'del_flg': '0'
}
ns = {
'id': uuid(),
'offer_orgid': ns.get('orgid'),
'bid_orgid': '',
'productid': ns.get('productid'),
'price': ns.get('selling'),
'begin_date': ns.get('begin_date')
}
ns['del_flg'] = 0
same_productid = await sor.R('floorprice', ns_exists_discount)
insert_date = datetime.datetime.strptime(ns['begin_date'], '%Y-%m-%d').date()
for index, prd in enumerate(same_productid):
begin_date = datetime.datetime.strptime(prd['begin_date'], '%Y-%m-%d').date()
end_date = datetime.datetime.strptime(prd['end_date'], '%Y-%m-%d').date()
if index == 0 and insert_date < begin_date:
ns['end_date'] = begin_date
await sor.C('floorprice', ns)
break
if index == len(same_productid) - 1 and insert_date > begin_date:
prd['end_date'] = ns.get('begin_date')
await sor.U('floorprice', prd)
ns['end_date'] = '9999-12-31'
await sor.C('floorprice', ns)
break
if begin_date < insert_date < end_date:
ns['end_date'] = prd.get('end_date')
prd['end_date'] = ns.get('begin_date')
await sor.U('floorprice', prd)
await sor.C('floorprice', ns)
break
if begin_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('floorprice', ns)
else:
parent_org_price_sql = """select * from floorprice where bid_orgid = ${bid_orgid}$
and productid = ${productid}$ and ${begin_date}$ between begin_date and end_date and del_flg = '0'"""
nss = {
'bid_orgid': ns.get('offer_orgid'),
'productid': ns.get('productid'),
'price': ns.get('price'),
'begin_date': ns.get('begin_date'),
'del_flg': '0'
}
parent_org_price_li = await sor.sqlExe(parent_org_price_sql, nss)
parent_org_price_di = parent_org_price_li[0] if parent_org_price_li else {}
parent_org_price = parent_org_price_di.get('price') or 0
if float(ns.get('price')) <= float(parent_org_price):
return {
'status': False,
'msg': 'price can not lower parent floor price, current floor price is %s' % parent_org_price
}
ns_exists_discount = {
'offer_orgid': ns.get('offer_orgid'),
'bid_orgid': ns.get('bid_orgid'),
'productid': ns.get('productid'),
'sort': ['begin_date'],
'del_flg': '0'
}
ns['del_flg'] = 0
same_productid = await sor.R('floorprice', ns_exists_discount)
insert_date = datetime.datetime.strptime(ns['begin_date'], '%Y-%m-%d').date()
for index, prd in enumerate(same_productid):
begin_date = datetime.datetime.strptime(prd['begin_date'], '%Y-%m-%d').date()
end_date = datetime.datetime.strptime(prd['end_date'], '%Y-%m-%d').date()
if index == 0 and insert_date < begin_date:
ns['end_date'] = begin_date
await sor.C('floorprice', ns)
break
if index == len(same_productid) - 1 and insert_date > begin_date:
prd['end_date'] = ns.get('begin_date')
await sor.U('floorprice', prd)
ns['end_date'] = '9999-12-31'
await sor.C('floorprice', ns)
break
if begin_date < insert_date < end_date:
ns['end_date'] = prd.get('end_date')
prd['end_date'] = ns.get('begin_date')
await sor.U('floorprice', prd)
await sor.C('floorprice', ns)
break
if begin_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('floorprice', ns)
return {
"status": True,
"msg": "floorprice add success"
}
except Exception as e:
raise e
return {
"status": False,
"msg": "floorprice add failed"
}
ret = await floorPriceAdd(params_kw)
return ret