async def cpcc_product_add(ns={}): """ 自定义产品 组合产品 {type: "cpu", model: "STANDARD", amount: 1} customize/combination :param ns: :return: """ db = DBPools() async with db.sqlorContext('kboss') as sor: if ns.get('sor'): sor = ns.get('sor') try: # 拼接自定义产品名称 gpu.cores.memory.cpu if ns.get('product_type') == 'combination' and ns.get('spec_note'): spec_li = json.loads(ns['spec_note']) if isinstance(ns['spec_note'], str) else ns['spec_note'] # 创建一个字典来存储每种类型的信息 components = {} for item in spec_li: comp_type = item["type"] if comp_type == 'disk' and item["model"] == 'SYS': comp_type = 'disk-sys' if comp_type == 'disk' and item["model"] == 'DATA': comp_type = 'disk-data' components[comp_type] = { "model": item["model"].replace(" ", ""), # 去除空格 "amount": item["amount"] } # 按照指定顺序提取信息并拼接字符串 gpu_model = components['gpu']['model'] if components.get('gpu') else '-' cpu_amount = components['cpu']['amount'] cpu_model = components['cpu']['model'] memory_amount = components['memory']['amount'] clusterid = ns.get('clusterid') if not clusterid: return { 'status': False, 'msg': '添加产品失败, 缺少参数站点ID:clusterid' } sys_disk_amount = 10 data_disk_amount = 0 if components.get('disk-sys'): sys_disk_amount = components['disk-sys']['amount'] if components.get('disk-data'): data_disk_amount = components['disk-data']['amount'] # 拼接最终字符串 ns['name'] = f"{gpu_model}.cpu{cpu_amount}memory{memory_amount}.disks{sys_disk_amount}diskd{data_disk_amount}.{cpu_model}.{clusterid}" ns['ptype'] = 20 ns['unit'] = 'hour' ns['discount'] = 1 ns['classify'] = 'CPCC-COMBINATION' # k8s product # 自定义产品 if ns.get('product_type') == 'customize': ns['name'] = 'bcc.mncn.ia.customize' ns['classify'] = 'CPCC-CUSTOMIZE' # init product ns['product_area'] = 'customize-area' # 获取协议 saleprotocol = await sor.R('saleprotocol', {'bid_orgid': ns.get('reseller_orgid'), 'offer_orgid': ns.get('providerid'), 'del_flg': '0'}) if not saleprotocol: return { 'status': False, 'msg': '添加失败, 请把算力中心转换成供应商' } ns['protocolid'] = saleprotocol[0]['id'] ns['spec_note'] = json.dumps(ns.get('spec_note')) if ns.get('spec_note') and isinstance(ns.get('spec_note'), list) else ns.get('spec_note') nss = { 'id': uuid(), 'providerid': ns.get('providerid') or '', 'providerpid': ns.get('providerpid') or '', 'name': ns.get('name') or '', 'description': ns.get('description') or '', 'ptype': ns.get('ptype') or '', 'state': ns.get('state') or '', # 'effect_date': ns.get('start_date') or '', # 'expire_date': ns.get('end_date') or '', 'salemode': ns.get('salemode') or '', 'product_code': ns.get('product_code') or '', 'spec_note': ns.get('spec_note') or '', 'product_area': ns.get('product_area') or '', 'specific_pattern': ns.get('specific_pattern') or '', 'reseller_orgid': ns.get('reseller_orgid') or '', 'classify': ns.get('classify'), 'productgroup': ns.get('productgroup'), 'label': ns.get('label') } nss_product_salemode = { 'id': uuid(), 'protocolid': ns.get('protocolid'), 'providerid': ns.get('providerid'), 'productid': nss['id'], 'discount': ns.get('discount'), 'price': ns.get('floorprice'), 'unit': ns.get('unit') } nss_product_salemode_copy = { 'id': uuid(), 'protocolid': ns.get('protocolid'), 'providerid': ns.get('providerid'), 'productid': '*', 'discount': ns.get('discount'), 'price': ns.get('floorprice'), 'unit': ns.get('unit') } # if not ns.get('effect_date'): # return { # "status": False, # "message": "product effect date is empty" # } # # if not ns.get('expire_date'): # nss['expire_date'] = "9999-12-31" # 添加产品 如果有底价 触发插入底价表, 同时插入两条数据 # 1. 买方售方都设置则为底价 2.售方为本机构 买方为空则为售价 if ns.get('selling') and ns.get('floorprice'): if int(ns.get('selling')) <= int(ns.get('floorprice')): return { 'status': False, 'msg': '售价不能低于底价' } ns_floor = { 'id': uuid(), 'offer_orgid': ns.get('providerid'), 'bid_orgid': ns.get('reseller_orgid'), 'productid': nss.get('id'), 'price': ns.get('floorprice'), 'begin_date': ns.get('effect_date'), 'end_date': '9999-12-31' } ns_selling = { 'id': uuid(), 'offer_orgid': ns.get('reseller_orgid'), 'bid_orgid': '', 'productid': nss.get('id'), 'price': ns.get('selling'), 'begin_date': ns.get('effect_date'), 'end_date': '9999-12-31' } await sor.C('floorprice', ns_floor) await sor.C('floorprice', ns_selling) if ns.get('protocolid'): # 获取供应商名称 用于筛选中金设置映射产品 provider_name_li = await sor.R('organization', {'id': ns.get('providerid')}) provider_name = provider_name_li[0]['orgname'] # 获取协议起始时间 time_line_li = await sor.R('saleprotocol', {'id': ns.get('protocolid'), 'del_flg': '0'}) start_time = time_line_li[0]['start_date'] end_time = time_line_li[0]['end_date'] nss['effect_date'] = start_time nss['expire_date'] = end_time nss['salemode'] = time_line_li[0]['salemode'] # 检查产品表是否已经存在 ns_exist = { 'providerid': ns.get('providerid'), 'name': ns.get('name'), 'del_flg': '0' } product_exist_li = await sor.R('product', ns_exist) # 如果已经存在就更新 if product_exist_li: product_table_exits_id = product_exist_li[0]['id'] nss['id'] = product_table_exits_id await sor.U('product', nss) # 此时默认协议子表已经存在并更新 # 查找协议子表id 用于更新 exists_zibiao = { 'protocolid': ns.get('protocolid'), 'providerid': ns.get('providerid'), 'productid': nss['id'] } exists_zibiao_li = await sor.R('product_salemode', exists_zibiao) if exists_zibiao_li: zibiao_id = exists_zibiao_li[0]['id'] nss_product_salemode['id'] = zibiao_id nss_product_salemode['productid'] = nss['id'] if provider_name == '中金超算': await sor.U('product_salemode', nss_product_salemode_copy) await sor.U('product_salemode', nss_product_salemode) else: await sor.C('product', nss) # 如果时中金协议子表创建两个 一个productid正常用于展示 一个productid为* if provider_name == '中金超算': zj_offer_yezhu_bind_exist = { 'protocolid': ns.get('protocolid'), 'providerid': ns.get('providerid'), 'productid': '*', 'del_flg': '0' } # 如果中金offer bind业主机构 已经配置过* 更新 zj_exist_li = await sor.R('product_salemode', zj_offer_yezhu_bind_exist) if zj_exist_li: nss_product_salemode_copy['id'] = zj_exist_li[0]['id'] await sor.U('product_salemode', nss_product_salemode_copy) else: await sor.C('product_salemode', nss_product_salemode_copy) await sor.C('product_salemode', nss_product_salemode) # 如果是批量添加 salemode=1就是添加产品 不添加统一客户配置 if ns.get('batch'): ns_batch_dic = { 'bid_orgid': nss['reseller_orgid'], 'providerid': nss['providerid'], 'productid': nss['id'], 'sor': sor } if ns.get('salemode') == '0': ns_batch_dic['discount'] = ns.get('kehu_discount') await discount_guanli_jia(ns_batch_dic) elif ns.get('salemode') == '2': ns_batch_dic['price'] = ns.get('kehu_price') await price_guanli_jia(ns_batch_dic) else: if ns.get('classify') == 'E': nss['hotshow'] = '0' await sor.C('product',nss) return { "status": True, "msg": "product add success", } except Exception as e: await sor.rollback() import traceback traceback.print_exc() return { "status": False, "msg": "product add failed, %s" % str(e) + traceback.format_exc() } ret = await cpcc_product_add(params_kw) return ret