kboss/b/product/publish_product_add.dspy
2025-08-20 14:30:58 +08:00

110 lines
4.1 KiB
Plaintext

async def get_user_role(ns={}):
sor = ns['sor']
# get role
ns['del_flg'] = '0'
res_role = await sor.R('userrole', ns)
if res_role:
user_role = res_role[0]
else:
return {
"status": False,
"msg": "userrole table, user id can not find..."
}
roleid = user_role.get('roleid')
# get role name
role_name = await sor.R('role', {'id': roleid})
if role_name:
role = role_name[0].get('role')
else:
return {
"status": False,
"msg": "role table, can not get role name"
}
return role
async def publish_product_add(ns={}):
if ns.get('userid'):
userid = ns.get('userid')
else:
userid = await get_user()
db = DBPools()
async with db.sqlorContext('kboss') as sor:
# 区分运营和普通客户
user_list = await sor.R('users', {'id': userid})
if not user_list:
return {
'status': False,
'msg': '没有找到匹配的用户'
}
orgid = user_list[0]['orgid']
domain_name = ns.get('url_link').split("//")[1].split("/")[0]
if 'localhost' in domain_name:
domain_name = 'dev.opencomputing.cn'
product_category = ns.get('product_category')
company_type = ns.get('company_type')
ns_dic = {
"id": uuid(),
"publish_type": ns.get("publish_type"),
"cart_flag": ns.get("cart_flag"),
"orgid": orgid,
"img": ns.get('img') if ns.get('img') else None,
"domain_name": domain_name,
"product_name": ns.get("product_name"),
"product_category": product_category,
"company_name": ns.get("company_name"),
"company_type": json.dumps(company_type) if isinstance(company_type, list) else company_type,
"contact_person": ns.get("contact_person"),
"job_title": ns.get("job_title"),
"phone_number": ns.get("phone_number"),
"email": ns.get("email"),
"cpu": ns.get("cpu"),
"memory": ns.get("memory"),
"gpu": ns.get("gpu"),
"sys_disk": ns.get("sys_disk"),
"data_disk": ns.get("data_disk"),
"net_card": ns.get("net_card"),
"price": ns.get("price"),
"unit": ns.get("unit"),
"discount": ns.get("discount"),
"service_charge": ns.get("service_charge"),
"short_term": ns.get("short_term"),
"priority": ns.get("priority") if ns.get("priority") else 1,
"status": ns.get("status") if ns.get("status") else '0',
"title": ns.get("title"),
"label": ns.get("label"),
"first_page": ns.get("first_page") if ns.get("first_page") else '0',
"requirement_summary": ns.get("requirement_summary"),
"related_parameters": ns.get("related_parameters"),
"application_scenario": ns.get("application_scenario"),
"audit_status": ns.get('audit_status', 'pending'),
}
if ns.get('discount') == '0':
ns['discount'] = None
ns_dic['discount'] = None
if ns.get('price') and ns.get('discount'):
ns_dic['discount_price'] = float(ns.get('price')) * float(ns['discount']) / 10
else:
ns_dic['discount_price'] = ns['price']
user_role = await get_user_role({'userid': userid, 'sor': sor})
# 非客户角色不需要审批
if user_role != '客户' and user_role != '管理员':
ns['status'] = '1'
ns_dic['audit_status'] = 'approved'
ns_dic['listing_status'] = 'listing'
ns_dic['first_page'] = '1'
try:
await sor.C('user_publish_product', ns_dic)
return {
'status': True,
'msg': 'publish product created successfully'
}
except Exception as e:
return {
'status': False,
'msg': 'Failed to publish product, %s' % str(e)
}
ret = await publish_product_add(params_kw)
return ret