async def determine_product_launch(ns={}): sor = ns['sor'] productid = ns['productid'] orgid = ns['orgid'] parentid = ns['parentid'] # 获取协议 saleprotocol_to_person = await sor.R('saleprotocol', {'bid_orgid': orgid, 'offer_orgid': parentid, 'del_flg': '0'}) # 等于空就代表这个客户没有特殊折扣,就要找到买方为*的协议 saleprotocol_to_all = await sor.R('saleprotocol', {'bid_orgid': '*', 'offer_orgid': parentid, 'del_flg': '0', 'salemode': '0'}) if saleprotocol_to_person: product_salemode = await sor.R('product_salemode', {'protocolid': saleprotocol_to_person[0]['id'], 'productid': productid, 'del_flg': '0'}) if not product_salemode: product_salemode = await sor.R('product_salemode', {'protocolid': saleprotocol_to_all[0]['id'], 'productid': productid, 'del_flg': '0'}) else: product_salemode = await sor.R('product_salemode', {'protocolid': saleprotocol_to_all[0]['id'], 'productid': productid, 'del_flg': '0'}) if not product_salemode: # print('product_salemode', '还未上线这个产品的协议配置') return { 'status': False, 'msg': '还未上线这个产品的协议配置' } else: return { 'status': True, 'msg': 'success' } async def cpcc_kehu_kan_chanpin(ns={}): """ mIWUHBeeDM8mwAFPIQ8pS :param ns: :return: """ if ns.get('userid'): userid = ns.get('userid') else: userid = await get_user() db = DBPools() async with db.sqlorContext('kboss') as sor: data = {} data['products'] = [] # 获取用户orgid orgid_li = await sor.R('users', {'id': userid}) orgid = orgid_li[0]['orgid'] # 获取用户parentid parentid_li = await sor.R('organization', {'id': orgid, 'del_flg': '0'}) parentid = parentid_li[0]['parentid'] # 获取这个parentid下所有可用 已配置算力中心 cpcc_find_sql = """select o.id, o.orgname, o.orgcode from cpclist as c left join organization as o on c.id = o.orgcode where c.orgid = '%s' and o.del_flg = '0';""" % parentid cpcc_li = await sor.sqlExe(cpcc_find_sql, {}) domain_url = ns.get('domain_url') if ('https://www.kaiyuancloud.cn/dev' in domain_url) or ('localhost' in domain_url): base_url = 'https://www.kaiyuancloud.cn/dev' elif 'https://www.kaiyuancloud.cn' in domain_url: base_url = 'https://www.kaiyuancloud.cn' else: base_url = domain_url.split('://')[0] + '://' + domain_url.split('://')[1].split('/')[0] # 一次性查找所有集群名称 cluster_data = await sor.R('cpccluster', {}) clusters = {item['id']: item['name'] for item in cluster_data} for cpcc in cpcc_li: provider_info = {} provider_info['cpccid'] = cpcc['orgcode'] provider_info['cpccname'] = cpcc['orgname'] provider_info['providerid'] = cpcc['id'] provider_info['products'] = await sor.R('product', {'providerid': provider_info['providerid'], 'classify': 'CPCC-COMBINATION', 'del_flg': '0'}) for product in provider_info['products']: # 查看产品是否配置了公共折扣 determine_ns = { 'sor': sor, 'productid': product['id'], 'orgid': orgid, 'parentid': parentid } determine_prd = await determine_product_launch(determine_ns) if not determine_prd['status']: # provider_info['products'].remove(product) continue clusterid = product['name'].split('.')[-1] product['clusterid'] = clusterid product['clustername'] = clusters.get(clusterid) # 没有找到集群名称 可能是集群重装ID变了 删除对应产品 并在后台重新组合新产品 if not product['clustername']: product_detele_sql = """UPDATE product set del_flg = '1' WHERE id = '%s';""" % product['id'] await sor.sqlExe(product_detele_sql, {}) continue product['cpccname'] = provider_info['cpccname'] rs_type = product['name'].split('.')[0] if ('-' != rs_type) and ('-' in rs_type): product['gpumemory'] = rs_type.split('-')[1] else: product['gpumemory'] = None # 先询价 再验证有没有库存 price_inquiry_url = base_url + '/cpcc/cpcwidget/accrual_price.dspy' price_payload = { 'cpcid': cpcc['orgcode'], 'resources': json.loads(product['spec_note']), 'duration': ns['duration'], 'duration_unit': ns['duration_unit'] } try: async with aiohttp_client.request( method='POST', url=price_inquiry_url, json=price_payload) as res: price_inquiry = await res.json() if price_inquiry.get('status') and price_inquiry['data']['status'] and price_inquiry['data']['message'] == '汇算正常': product['price'] = price_inquiry['data']['total_price'] product['duration'] = ns['duration'] product['duration_unit'] = ns['duration_unit'] else: product['price'] = '售罄' data['products'].append(product) continue # 验证库存 stock_url = base_url + '/cpcc/cpcbuy/determine_accommodat.dspy' payload = { 'cpcid': provider_info['cpccid'], 'clusterid': clusterid, 'resources': {'aa': json.loads(product['spec_note'])} } async with aiohttp_client.request( method='POST', url=stock_url, json=payload) as res: data_stock = await res.json() if data_stock.get('data'): product['price'] = '售罄' except Exception as e: product['price'] = '售罄' data['products'].append(product) # 筛选出所有产品相关站点 data['cluster_info'] = list({item['clusterid']: {'clusterid': item['clusterid'], 'clustername': item['cpccname'] + '-' + item['clustername'], 'resource_type': []} for item in data['products']}.values()) for i in data['cluster_info']: for j in data['products']: if j['clusterid'] == i['clusterid']: r_type = j['name'].split('.')[0] j['resource_type'] = r_type resource_type = {'name': r_type, 'type': 'GPU'} if r_type == '-' or r_type == 'STANDARD': r_type = 'CPU计算型' j['resource_type'] = r_type resource_type = {'name': r_type, 'type': 'CPU'} if resource_type in i['resource_type']: j['resource_type'] = r_type continue i['resource_type'].append(resource_type) # 第一次查找 默认第一个集群及相关产品 if (not ns.get('clusterid')) and (not ns.get('resource_type')): clusterid_fir = data['cluster_info'][0]['clusterid'] resource_tp = data['cluster_info'][0]['resource_type'][0]['name'] data['products'] = [i for i in data['products'] if i['clusterid'] == clusterid_fir and i['resource_type'] == resource_tp] if ns.get('clusterid') and ns.get('resource_type'): data['products'] = [i for i in data['products'] if i['clusterid'] == ns.get('clusterid') and i['resource_type'] == ns.get('resource_type')] if ns.get('clusterid'): data['products'] = [i for i in data['products'] if i['clusterid'] == ns.get('clusterid')] for i in range(len(data['cluster_info'])): if data['cluster_info'][i]["clusterid"] == ns.get('clusterid'): # 找到元素,移除并插入到开头 element = data['cluster_info'].pop(i) data['cluster_info'].insert(0, element) break return { 'status': True, 'msg': 'get cpcc product success', 'data': data } ret = await cpcc_kehu_kan_chanpin(params_kw) return ret