55 lines
3.0 KiB
Python
55 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from sqlor.dbpools import DBPools
|
|
|
|
async def get_discount(ns={}):
|
|
db = DBPools()
|
|
async with db.sqlorContext('kboss') as sor:
|
|
try:
|
|
# 查找用户id, orgid, parentid
|
|
if ns.get('user_orgid'):
|
|
user_orgid = ns.get('user_orgid')
|
|
else:
|
|
user_orgid = (await sor.R('users', {'id': await get_user(), 'del_flg': '0'}))[0]['orgid']
|
|
user_parentid = (await sor.R('organization', {'id': user_orgid, 'del_flg': '0'}))[0]['parentid']
|
|
|
|
# 查找对应的产品id
|
|
if ns.get('prefix'):
|
|
productid_li = await sor.R('product', {'providerid': ns.get('providerid'), 'providerpid': ns.get('prefix') + '_' + ns.get('servicetype'), 'del_flg': '0'})
|
|
if productid_li:
|
|
productid_soure = productid_li[0]['id']
|
|
else:
|
|
return {'discount': 1.0}
|
|
else:
|
|
productid_soure = ns.get('productid')
|
|
|
|
real_discount = 1.0
|
|
person_discount_sql = """select * from saleprotocol where offer_orgid = '%s' and bid_orgid = '%s' and
|
|
salemode = '0' and del_flg = '0' and CURRENT_DATE <= end_date and
|
|
CURRENT_DATE >= start_date""" % (user_parentid, user_orgid)
|
|
tongyi_discount_sql = """select * from saleprotocol where offer_orgid = '%s' and bid_orgid = '*' and
|
|
salemode = '0' and del_flg = '0' and CURRENT_DATE <= end_date and
|
|
CURRENT_DATE >= start_date""" % user_parentid
|
|
# 没有个人配置 获取产品统一配置折扣
|
|
for sql_detail in [person_discount_sql, tongyi_discount_sql]:
|
|
person_discount_li = await sor.sqlExe(sql_detail, {})
|
|
for person_discount in person_discount_li:
|
|
protocolid = person_discount['id']
|
|
xieyi_zi_search_li = await sor.R('product_salemode', {'protocolid': protocolid, 'del_flg': '0'})
|
|
for xieyi_zi in xieyi_zi_search_li:
|
|
provider_zi_id = xieyi_zi['providerid']
|
|
zi_discount = xieyi_zi['discount']
|
|
zi_productid = xieyi_zi['productid']
|
|
if provider_zi_id == ns.get('providerid') and zi_productid == productid_soure:
|
|
# 判断产品是否存在 有可能在产品表已经删除
|
|
prd_res_exists_li = await sor.R('product', {'id': xieyi_zi['productid'], 'del_flg': '0'})
|
|
if not prd_res_exists_li:
|
|
continue
|
|
real_discount = float(zi_discount)
|
|
break
|
|
if real_discount != 1.0:
|
|
break
|
|
return {'discount': real_discount}
|
|
except Exception as e:
|
|
return {'discount': 1.0, 'error_msg': str(e)} |