220 lines
10 KiB
Plaintext
220 lines
10 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={}):
|
|
'''
|
|
获取弹性ip价格
|
|
:return:
|
|
'''
|
|
import aiohttp
|
|
CCS_URL = 'http://cdsapi.capitalonline.net/gcw'
|
|
action = "GetEipPrice"
|
|
method = "GET"
|
|
param = {
|
|
"duration": ns.get('duration'),
|
|
"isMounth": ns.get('isMounth'),
|
|
"size": ns.get('size'),
|
|
"siteId": ns.get('siteId'),
|
|
"bandwidthConfIdStr": ns.get('bandwidthConfIdStr'),
|
|
"qos": ns.get('qos'),
|
|
"isToMonth": ns.get('isToMonth'),
|
|
"requestId": "6bca6bbd-1000-4x10-4xx1-1xxxxxxxxxx1"
|
|
}
|
|
nss = {
|
|
'orgname': '北京首都在线科技股份有限公司',
|
|
'user_orgid': ns.get('user_orgid'),
|
|
'productname': '宽带'
|
|
}
|
|
bandwidth_price_dic = await get_discount_or_price(nss)
|
|
if bandwidth_price_dic['status']:
|
|
bandwidth_price = float(bandwidth_price_dic['price']) * int(ns.get('duration')) * int(ns.get('qos'))
|
|
else:
|
|
return {
|
|
'status': False,
|
|
'msg': '无法获取配置的宽带价格 %s' % bandwidth_price_dic.get('msg')
|
|
}
|
|
nss['productname'] = '弹性IP'
|
|
eip_price_dic = await get_discount_or_price(nss)
|
|
if eip_price_dic['status']:
|
|
eip_price = float(eip_price_dic['price']) * int(ns.get('duration'))
|
|
else:
|
|
return {
|
|
'status': False,
|
|
'msg': '无法获取配置的弹性IP价格 %s' % eip_price_dic.get('msg')
|
|
}
|
|
if eip_price < 10:
|
|
price_unit = '/天'
|
|
else:
|
|
price_unit = "/%s个月" % ns.get('duration')
|
|
return {
|
|
"status": True,
|
|
"data": {
|
|
"bandwidth_price": {
|
|
"config_name": "bandwidth",
|
|
"cycle": price_unit,
|
|
"end_time": "",
|
|
"peak_qos_list": "[]",
|
|
"price": bandwidth_price,
|
|
"price_str": "%s" % bandwidth_price,
|
|
"sign": "¥"
|
|
},
|
|
"eip_price": {
|
|
"config_name": "eip",
|
|
"cycle": price_unit,
|
|
"end_time": "",
|
|
"peak_qos_list": "",
|
|
"price": eip_price,
|
|
"price_str": "%s" % eip_price,
|
|
"sign": "¥"
|
|
},
|
|
"total_price": {
|
|
"cycle": price_unit,
|
|
"end_time": "",
|
|
"peak_qos_list": "",
|
|
"price": bandwidth_price + eip_price,
|
|
"price_str": "%s" % (bandwidth_price + eip_price),
|
|
"sign": "¥"
|
|
}
|
|
},
|
|
"msg": ""
|
|
}
|
|
# url = get_signature(action, method, CCS_URL, param=param)
|
|
# async with aiohttp.ClientSession() as session:
|
|
# async with session.request(method, url) as response:
|
|
# resp = await response.text()
|
|
# result = json.loads(resp)
|
|
# if result['Code'] == 'Success':
|
|
# result['status'] = True
|
|
# result.pop('Code')
|
|
# result['data'] = result.pop('Data')
|
|
# result['msg'] = result.pop('Message')
|
|
# else:
|
|
# result['status'] = False
|
|
# result['msg'] = result.pop('Message')
|
|
# return result
|
|
|
|
ret = await get_eip_price(params_kw)
|
|
return ret |