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_uhost_instance_price(ns={}): db = DBPools() async with db.sqlorContext('kboss') as sor: ucloud_user_li = await sor.R('ucloud_users', {'orgid': 'main_user', 'del_flg': '0'}) if ucloud_user_li: uc_client = U_Client({ "project_id": ucloud_user_li[0]['projectid'], "public_key": ucloud_user_li[0]['accesskey'], "private_key": ucloud_user_li[0]['accesskeysecret'], }) else: return { 'status': False, 'msg': 'can not find u cloud user' } nss = { 'orgname': '优刻得科技股份有限公司', 'user_orgid': ns.get('user_orgid'), 'productname': '4090' } ecs_price = 0 if ns.get('ChargeType'): ecs_price_dic = await get_discount_or_price(nss) if ecs_price_dic['status']: ecs_price = float(ecs_price_dic['price']) * int(ns.get('Quantity')) else: return { 'status': False, 'msg': '无法获取配置的ecs价格 %s' % ecs_price_dic.get('msg') } try: Disks = ns.get('Disks') resp = uc_client.uhost().get_uhost_instance_price({ "Region": ns.get('Region'), "Zone": ns.get('Zone'), "CPU": ns['CPU'], "Memory": ns['Memory'], "Count": ns['Count'], "ImageId": ns.get('ImageId'), "GPU": ns['GPU'] if ns.get('GPU') else 0, "ChargeType": ns.get('ChargeType'), "NetCapability": ns.get('NetCapability'), "MachineType": ns.get('MachineType'), "GpuType": ns.get('GpuType'), "Quantity": ns['Quantity'] if ns.get('Quantity') else 1, "UDSetUHostInstance": False, "ShowPriceDetails": True, "Disks": json.loads(Disks) if isinstance(Disks, str) else Disks }) if ns.get('ChargeType'): resp['PriceSet'][0]['PriceDetail']['UHost'] = float(ecs_price) resp['PriceSet'][0]['PriceDetail']['UDisk'] = resp['PriceSet'][0]['PriceDetail']['UDisk'] * 2 resp['PriceSet'][0]['Price'] = resp['PriceSet'][0]['PriceDetail']['UDisk'] + float(ecs_price) return { 'status': True, 'msg': 'get instance price success', 'data': resp } except exc.UCloudException as e: return { 'status': False, 'msg': 'get instance price failed, %s' % str(e) } ret = await get_uhost_instance_price(params_kw) return ret