async def get_discount_or_price(ns={}): 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, 'productid': productid} # 如果签属的协议是折扣 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 {'status': True, 'discount': real_discount, 'productid': productid} if init_protocol == '2': pass except Exception as e: return {'status': False, 'discount_price': 1.0, 'error_msg': str(e)} async def get_ecs_price(ns={}): import aiohttp """ 获取云服务器价格 """ ecs_url = 'http://api.capitalonline.net/ecs/v1' action = "DescribePrice" method = "POST" param = {} if ns.get('IsToMonth') == 0 or ns.get('IsToMonth') == '0': istomonth = 0 else: istomonth = 1 if ns.get('BillingMethod') == 0 or ns.get('BillingMethod') == '0': time_unit = '天' else: time_unit = '个月' systemdiskinfo = ns.get('SystemDiskInfo') if isinstance(systemdiskinfo, str): systemdiskinfo = json.loads(systemdiskinfo) datadiskinfo = ns.get('DataDiskInfo') if datadiskinfo: if isinstance(datadiskinfo, str): datadiskinfo = json.loads(datadiskinfo) db = DBPools() async with db.sqlorContext('kboss') as sor: # 查找供应商id providerid_li = await sor.R('organization', {'orgname': '北京首都在线科技股份有限公司', 'del_flg': '0'}) if providerid_li: providerid = providerid_li[0]['id'] provider_parentid = providerid_li[0]['parentid'] else: return { 'status': False, 'msg': '没有找到指定名字的供应商' } exist_prducts = await sor.R('product', {'providerid': providerid, 'name': ns['EcsFamilyName'], 'del_flg': '0'}) if exist_prducts: productid = None for exist_prduct in exist_prducts: spec_notes = json.loads(exist_prduct['spec_note']) spec_dic = {} for note in spec_notes: spec_dic.update({note['configName']: note['value']}) if str(ns['Cpu']) == spec_dic['CPU'] and str(ns['Ram']) == spec_dic['内存']: productid = exist_prduct['id'] nss = { 'orgname': '北京首都在线科技股份有限公司', 'user_orgid': ns.get('user_orgid'), 'productname': ns['EcsFamilyName'] } ecs_price_dic = await get_discount_or_price(nss) if ecs_price_dic['status']: ecs_productid = ecs_price_dic['productid'] ecs_price = float(ecs_price_dic['price']) * int(ns.get('Duration')) else: return { 'status': False, 'msg': '无法获取配置的ecs价格 %s' % ecs_price_dic.get('msg') } nss['productname'] = '云硬盘' disk_price_dic = await get_discount_or_price(nss) if disk_price_dic['status']: disk_productid = disk_price_dic['productid'] disk_price = float(disk_price_dic['price']) * int(ns.get('Duration')) else: return { 'status': False, 'msg': '无法获取配置的云硬盘价格 %s' % disk_price_dic.get('msg') } datadisk_total = sum([int(datadisk['Size']) for datadisk in datadiskinfo]) disk_total = int(systemdiskinfo['Size']) + int(datadisk_total) return { "RequestId": "7eedccd5-4c14-43a9-9afb-ef3a66dc1af0", "status": True, "data": { "PriceUnit": "%s%s" % (ns.get('Duration'), time_unit), "PriceSymbol": "¥", "TotalPrice": float(ecs_price) + (float(disk_price) * disk_total), "EcsPrice": ecs_price, 'price_detail': [ { 'config_name': 'disk_unit', 'productid': disk_productid, 'price': float(disk_price), }, { 'config_name': 'disk', 'productid': disk_productid, 'price': float(disk_price) * disk_total, #'orgin': disk_price_dic['origin_price'] }, { 'config_name': 'ecs', 'productid': ecs_productid, 'price': ecs_price, #'orgin': ecs_price_dic['origin_price'] } ] }, "msg": "获取计费信息成功!" } # else: # return { # 'status': False, # 'msg': '有产品名称, 没有对应配置' # } # else: # return { # 'status': False, # 'msg': '没有找到对应产品' # } body = { "AvailableZoneCode": ns.get("AvailableZoneCode"), "EcsFamilyName": ns.get("EcsFamilyName") or '', "Cpu": int(ns.get("Cpu")), "Ram": int(ns.get("Ram")), "Gpu": int(ns.get("Gpu")) if ns.get("Gpu") else 0, "BillingMethod": ns.get('BillingMethod'), "Duration": int(ns.get("Duration")) if ns.get("Duration") else 1, "IsToMonth": istomonth, "SystemDiskInfo": systemdiskinfo, "Number": int(ns.get("Number")) if ns.get("Number") else 1, "DataDiskInfo": datadiskinfo if datadiskinfo else [], } url = get_signature(action, method, ecs_url, param) async with aiohttp.ClientSession() as session: async with session.request(method, url, json=body) as response: resp = await response.text() result = json.loads(resp) if 'Message' in result.keys(): key_word = 'Message' else: key_word = 'Msg' if result['Code'] == 'Success': result['status'] = True result.pop('Code') result['data'] = result.pop('Data') result['msg'] = result.pop(key_word) else: result['status'] = False result['msg'] = result.pop(key_word) return result ret = await get_ecs_price(params_kw) return ret