kboss/b/product/search_user_inquiry.dspy
2026-07-09 11:22:05 +08:00

134 lines
6.6 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:
where_conditions = ["domain_name = '%s'" % domain_name, "del_flg = '0'"]
if ns.get('name'):
where_conditions.append("name like '%%%%%s%%%%'" % ns.get('name'))
if ns.get('phone'):
where_conditions.append("phone like '%%%%%s%%%%'" % ns.get('phone'))
if ns.get('email'):
where_conditions.append("email like '%%%%%s%%%%'" % ns.get('email'))
if ns.get('source'):
if ns.get('source') == '未知':
where_conditions.append("(source is null or source = '')")
else:
where_conditions.append("source = '%s'" % ns.get('source'))
if ns.get('feedback'):
where_conditions.append("feedback = '%s'" % ns.get('feedback'))
where_clause = ' and '.join(where_conditions)
# 分页参数
page = int(ns.get('page', 1))
page_size = int(ns.get('page_size', 20))
offset = (page - 1) * page_size
# 统计查询(基于全部符合条件的数据)
count_sql = """select count(*) as cnt from product_inquiry where %s""" % where_clause
total_count = (await sor.sqlExe(count_sql, {}))[0]['cnt']
source_sql = """select source, count(*) as cnt from product_inquiry where %s group by source""" % where_clause
source_result = await sor.sqlExe(source_sql, {})
source_stats = {}
for row in source_result:
src = row.get('source') or '未知'
source_stats[src] = row.get('cnt')
pending_sql = """select count(*) as cnt from product_inquiry where %s and feedback = '0'""" % where_clause
pending_count = (await sor.sqlExe(pending_sql, {}))[0]['cnt']
source_list_sql = """select distinct source from product_inquiry where %s""" % where_clause
source_list = [row['source'] for row in (await sor.sqlExe(source_list_sql, {}))]
has_empty_source = any(s is None or s == '' for s in source_list)
source_list = [s for s in source_list if s] # 过滤空值
if has_empty_source:
source_list.append('未知')
# 分页查询
search_sql = """select * from product_inquiry where %s order by update_time desc limit %d offset %d;""" % (where_clause, page_size, offset)
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': '反馈状态',
'remark': '备注',
'create_at': '创建时间'
}
# 新增值映射字典,集中管理各字段的数值转换规则
# 转换字典键为中文
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,
'total_count': total_count,
'source_stats': source_stats,
'pending_count': pending_count,
'source_list': source_list,
'page': page,
'page_size': page_size,
'feedback_list': [{'id': 0, 'name': '待回复'},{'id': 1, 'name': '已回复'}]
}
ret = await search_user_inquiry(params_kw)
return ret