177 lines
8.9 KiB
Plaintext
177 lines
8.9 KiB
Plaintext
async def get_discount_or_price(ns={}):
|
|
# ns = {
|
|
# 'orgname': '北京首都在线科技股份有限公司',
|
|
# 'productname': '云硬盘',
|
|
# 'user_orgid': 'gKG8UaYPjA_29K7puzaTM'
|
|
# }
|
|
db = DBPools()
|
|
async with db.sqlorContext('kboss') as sor:
|
|
try:
|
|
# 查找供应商id
|
|
providerid_li = await sor.R('organization', {'orgname': ns.get('orgname'), 'del_flg': '0'})
|
|
if providerid_li:
|
|
providerid = providerid_li[0]['id']
|
|
provider_parentid = providerid_li[0]['parentid']
|
|
else:
|
|
return {
|
|
'status': False,
|
|
'msg': '没有找到指定名字的供应商'
|
|
}
|
|
# 查找产品id
|
|
productid_li = await sor.R('product', {'providerid': providerid, 'name': ns.get('productname')})
|
|
if productid_li:
|
|
productid = productid_li[0]['id']
|
|
else:
|
|
return {
|
|
'status': False,
|
|
'msg': '没有找到provider: %s对应的产品' % providerid
|
|
}
|
|
# 查找用户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_li = await sor.R('organization', {'id': user_orgid, 'del_flg': '0'})
|
|
if user_parentid_li:
|
|
user_parentid = user_parentid_li[0]['parentid']
|
|
else:
|
|
return {
|
|
'status': False,
|
|
'msg': '没有找到对应用户的机构'
|
|
}
|
|
# 查找供应商和所属机构得协议
|
|
init_protocol_li = await sor.R('saleprotocol', {'offer_orgid': providerid, 'bid_orgid': provider_parentid, 'del_flg': '0'})
|
|
if init_protocol_li:
|
|
init_protocol = init_protocol_li[0]['salemode']
|
|
else:
|
|
return {
|
|
'status': False,
|
|
'msg': '没有找到供应商: %s和用户所在机构: %s的协议' % (providerid, user_parentid)
|
|
}
|
|
if init_protocol == '2':
|
|
real_price = 1000000000000.0
|
|
person_price_sql = """select * from saleprotocol where offer_orgid = '%s' and bid_orgid = '%s' and
|
|
salemode = '2' and del_flg = '0' and CURRENT_DATE <= end_date and
|
|
CURRENT_DATE >= start_date""" % (user_parentid, user_orgid)
|
|
tongyi_price_sql = """select * from saleprotocol where offer_orgid = '%s' and bid_orgid = '*' and
|
|
salemode = '2' and del_flg = '0' and CURRENT_DATE <= end_date and
|
|
CURRENT_DATE >= start_date""" % user_parentid
|
|
for sql_detail in [person_price_sql, tongyi_price_sql]:
|
|
person_price_li = await sor.sqlExe(sql_detail, {})
|
|
for person_price in person_price_li:
|
|
protocolid = person_price['id']
|
|
xieyi_zi_search_li = await sor.R('product_salemode', {'protocolid': protocolid, 'providerid': providerid, 'del_flg': '0'})
|
|
for xieyi_zi in xieyi_zi_search_li:
|
|
provider_zi_id = xieyi_zi['providerid']
|
|
zi_price = xieyi_zi['price']
|
|
zi_productid = xieyi_zi['productid']
|
|
if provider_zi_id == providerid and zi_productid == productid:
|
|
# 判断产品是否存在 有可能在产品表已经删除
|
|
prd_res_exists_li = await sor.R('product', {'id': xieyi_zi['productid'], 'del_flg': '0'})
|
|
if not prd_res_exists_li:
|
|
continue
|
|
real_price = float(zi_price)
|
|
break
|
|
if real_price != 1000000000000.0:
|
|
break
|
|
if real_price == 1000000000000.0:
|
|
return {'status': False, 'msg': '没有找到产品价格'}
|
|
else:
|
|
return {'status': True, 'price': real_price}
|
|
|
|
# 如果签属的协议是折扣
|
|
if init_protocol == '0':
|
|
# 查找对应的产品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}
|
|
if init_protocol == '2':
|
|
pass
|
|
except Exception as e:
|
|
return {'status': False, 'discount_price': 1.0, 'error_msg': str(e)}
|
|
|
|
|
|
async def get_eip_price(ns={}):
|
|
uc_client = None
|
|
db = DBPools()
|
|
async with db.sqlorContext('kboss') as sor:
|
|
# 查找主账号密钥
|
|
main_user_li = await sor.R('ucloud_users', {'orgid': 'main_user', 'del_flg': '0'})
|
|
public_key = main_user_li[0]['accesskey']
|
|
private_key = main_user_li[0]['accesskeysecret']
|
|
uc_client = U_Client({
|
|
"public_key": public_key,
|
|
"private_key": private_key
|
|
})
|
|
nss = {
|
|
'orgname': '优刻得科技股份有限公司',
|
|
'user_orgid': ns.get('user_orgid'),
|
|
'productname': '外网弹性IP'
|
|
}
|
|
eip_price = 0
|
|
if ns.get('ChargeType') != 'Dynamic':
|
|
eip_price_dic = await get_discount_or_price(nss)
|
|
if eip_price_dic['status']:
|
|
eip_price = float(eip_price_dic['price']) * int(ns.get('Quantity')) * int(ns.get('Bandwidth'))
|
|
else:
|
|
return {
|
|
'status': False,
|
|
'msg': '无法获取配置的EIP价格 %s' % eip_price_dic.get('msg')
|
|
}
|
|
try:
|
|
resp = uc_client.unet().get_eip_price({
|
|
'Region': ns.get('Region'),
|
|
'Bandwidth': int(ns.get('Bandwidth')),
|
|
'OperatorName': ns.get('OperatorName'),
|
|
'ChargeType': ns.get('ChargeType'),
|
|
'PayMode': 'Bandwidth',
|
|
'Quantity': ns.get('Quantity')
|
|
})
|
|
if ns.get('ChargeType') != 'Dynamic':
|
|
resp['PriceSet'][0]['Price'] = eip_price
|
|
return {
|
|
'status': True,
|
|
'msg': 'get eip price success',
|
|
'data': resp
|
|
}
|
|
except exc.UCloudException as e:
|
|
return {
|
|
'status': False,
|
|
'msg': 'get u cloud eip price failed, %s' % str(e)
|
|
}
|
|
|
|
ret = await get_eip_price(params_kw)
|
|
return ret |