salescrm/b/kpi/sync_product_add.dspy
2025-10-27 15:50:44 +08:00

212 lines
9.4 KiB
Plaintext

async def productAdd(ns={}):
"""
add new product
:param ns:
:return:
"""
db = DBPools()
async with db.sqlorContext('kboss') as sor:
if ns.get('sor'):
sor = ns.get('sor')
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')
}
nss_product_salemode_copy = {
'id': uuid(),
'protocolid': ns.get('protocolid'),
'providerid': ns.get('providerid'),
'productid': '*',
'discount': ns.get('discount'),
'price': ns.get('floorprice')
}
# 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"
try:
# 添加产品 如果有底价 触发插入底价表, 同时插入两条数据
# 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:
await sor.C('product',nss)
return {
"status": True,
"msg": "product add success",
}
except Exception as e:
raise ValueError('产品添加失败: %s' % e)
return {
"status": False,
"msg": "product add failed"
}
async def sync_product_add(ns={}):
providerid = 'SCH5NAXEa01O3rwuXnkiA'
protocolid = '4vax8AlCnCbxxrqEi17f7'
name_list = ['阿里云-agency', '阿里云-reseller', '阿里云-融合云', '阿里云-返佣']
for name in name_list:
ns = {
"classify": "A",
"reseller_orgid": "mIWUHBeeDM8mwAFPIQ8pS",
"providerid": providerid,
"providerpid": "",
"name": name,
"ptype": "9",
"description": "product_sync",
"publish_method": "",
"spec_note": "[{\"configName\":\"\",\"value\":\"\"}]",
"floorprice": "1",
"product_area": "北京",
"protocolid": protocolid
}
# await productAdd(ns)
providerid_k = '2ROX9TKRem5cLAsIEmkrk'
protocolid_k = 'CCefalSmfWnRBZLjLZXqi'
name_list_k = ['超算','定制服务-SAAS','定制服务-UPS电源维护','定制服务-短信','定制服务-服务器托管','定制服务-网络安全评测服务','定制服务-驻场运维','互联网接入','互联网接入-DCI','互联网接入-国际专线加速','互联网接入-系统集成','互联网接入-智慧酒店服务','集团项目','智算-A800','智算-算力服务']
for name_k in name_list_k:
ns_k = {
"classify": "A",
"reseller_orgid": "mIWUHBeeDM8mwAFPIQ8pS",
"providerid": providerid_k,
"providerpid": "",
"name": name_k,
"ptype": "9",
"description": "product_sync",
"publish_method": "",
"spec_note": '[{\"configName\":\"\",\"value\":\"\"}]',
"floorprice": "1",
"product_area": "北京",
"protocolid": protocolid_k
}
await productAdd(ns_k)
ret = await sync_product_add(params_kw)
return ret