kboss/b/product/search_user_inquiry.dspy
2026-07-07 17:56:59 +08:00

82 lines
4.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

async def search_user_inquiry(ns={}):
if not ns.get('url_link'):
return {
'status': False,
'msg': '请传递url_link'
}
domain_name = ns.get('url_link').split("//")[1].split("/")[0]
if 'localhost' in domain_name:
domain_name = 'dev.opencomputing.cn'
db = DBPools()
async with db.sqlorContext('kboss') as sor:
search_sql = """select * from product_inquiry where domain_name = '%s' and del_flg = '0' order by update_time desc;""" % domain_name
result = await sor.sqlExe(search_sql, {})
dict_sql = """select dict_type, dict_key, dict_value from product_inquiry_dict where status = 1 order by dict_type asc, sort_order asc;"""
dict_result = await sor.sqlExe(dict_sql, {})
dict_mapping = {}
for dict_item in dict_result:
dict_type = dict_item.get('dict_type')
dict_mapping.setdefault(dict_type, {})[str(dict_item.get('dict_key'))] = dict_item.get('dict_value')
value_mapping = {
'custom_type': {'0': '个人', '1': '企业'},
'enterprise_type': dict_mapping.get('enterprise_type', {}),
'region': dict_mapping.get('region', {}),
'feedback': {'0': '未反馈', '1': '已反馈'}
}
for data_dic in result:
for key, mapping in value_mapping.items():
if key in data_dic:
data_dic['%s_name' % key] = mapping.get(str(data_dic.get(key)), data_dic.get(key))
direction_mapping = dict_mapping.get('direction', {})
direction_keys = str(data_dic.get('consult_direction')).split(',') if data_dic.get('consult_direction') else []
data_dic['consult_direction_name'] = ''.join([direction_mapping.get(direction_key.strip(), direction_key.strip()) for direction_key in direction_keys if direction_key.strip()])
if ns.get('to_excel') == '1':
# 创建映射字段 导出execl
# 结果转换成 中文名称:值 的字典列表
field_mapping = {
'name': '联系人姓名',
'custom_type': '客户类型',
'phone': '联系人电话',
'email': '邮箱',
'company': '公司名称',
'enterprise_type': '企业类型',
'region': '所在区域',
'consult_direction': '咨询方向',
'content': '咨询内容',
'feedback': '反馈状态',
}
# 新增值映射字典,集中管理各字段的数值转换规则
# 转换字典键为中文
for data_dic in result:
# 拆分后:显式循环结构(便于后续处理)
new_data_dic = {}
# 按field_mapping定义的顺序处理字段确保输出顺序一致
for key in field_mapping.keys():
# 跳过数据中不存在的字段
if key not in data_dic:
continue
value = data_dic[key]
chinese_key = field_mapping[key]
if key == 'consult_direction':
direction_mapping = dict_mapping.get('direction', {})
direction_keys = str(value).split(',') if value else []
new_data_dic[chinese_key] = ''.join([direction_mapping.get(direction_key.strip(), direction_key.strip()) for direction_key in direction_keys if direction_key.strip()])
elif key in value_mapping:
mapped_value = value_mapping[key].get(str(value), value) # 若未找到对应映射,保留原始值
new_data_dic[chinese_key] = mapped_value
else:
new_data_dic[chinese_key] = value
data_dic.clear()
data_dic.update(new_data_dic)
return {
'status': True,
'msg': 'search success',
'data': result
}
ret = await search_user_inquiry(params_kw)
return ret