perf: P1 性能优化批量修复 + kefu 视图拆分
P1 性能优化(避免循环内 N+1 / N 次 DB 查询): - shop_order_views._batch_images: 移除 ThreadPoolExecutor 掩盖的 N 次查询,改单次批量 + Python 侧分组 - product_query.DashouHuiyuanList: 5N 查询 -> 3 次批量预取 + 本地闭包判断 - roles.GetRolePermissionView / 用户列表: 循环内 RolePermission/Permission/UserRole/Role -> 批量 __in 预取 - guanli / zuzhang 杜次分红 4.2/4.3: 循环内 update_or_create + 单条 delete -> bulk_create + bulk_update + 批量 delete - admin_config QQ 群配置: 循环内 exists/first/save/create -> 批量预取 + bulk_create + bulk_update - jituan 服务层多处合并统计查询、批量预取 map - rank 多处循环 N+1 改批量预取 - backend 多处循环内 count/create 改批量 - config/orders/merchant_ops 多处循环 N+1 改批量预取 其他改动: - users/views/kefu.py 拆分为 kefu_base/kefu_dashou/kefu_orders/kefu_punishment/kefu_withdraw 5 个文件 - 删除遗留脚本 check_prod_uid.py / create_rbac_tables.sql
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import io
|
||||
import io
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
@@ -394,40 +394,46 @@ class AdminUpdateConfigView(APIView):
|
||||
# 🔴 修复:更新QQ群配置 - 确保根据peizhiid正确更新对应的记录
|
||||
qq_groups = request.data.get('qq_groups')
|
||||
if qq_groups and isinstance(qq_groups, list):
|
||||
# 收集所有 peizhiid,批量预取已有记录(避免循环内 N+1)
|
||||
_peizhiids = [g.get('peizhiid') for g in qq_groups if g.get('peizhiid')]
|
||||
_existing_map = {}
|
||||
if _peizhiids:
|
||||
for q in Qunpeizhi.query.filter(GroupType__in=_peizhiids):
|
||||
# 如果存在多个相同 peizhiid 的记录,取第一个(应该只有一个)
|
||||
if q.GroupType not in _existing_map:
|
||||
_existing_map[q.GroupType] = q
|
||||
|
||||
_to_create = []
|
||||
_to_update = []
|
||||
updated_groups = []
|
||||
for group_data in qq_groups:
|
||||
peizhiid = group_data.get('peizhiid')
|
||||
if not peizhiid:
|
||||
continue
|
||||
|
||||
# 🔴 修复:使用filter()确保查询到正确的记录
|
||||
qun_configs = Qunpeizhi.query.filter(GroupType=peizhiid)
|
||||
|
||||
if qun_configs.exists():
|
||||
# 如果存在多个相同peizhiid的记录,取第一个(应该只有一个)
|
||||
qun_config = qun_configs.first()
|
||||
|
||||
# 更新字段
|
||||
neirong = group_data.get('neirong')
|
||||
qunid = group_data.get('qunid')
|
||||
jieshao = group_data.get('jieshao')
|
||||
neirong = group_data.get('neirong')
|
||||
qunid = group_data.get('qunid')
|
||||
jieshao = group_data.get('jieshao')
|
||||
|
||||
qun_config = _existing_map.get(peizhiid)
|
||||
if qun_config is not None:
|
||||
# 更新已有记录
|
||||
if neirong is not None:
|
||||
qun_config.GroupContent = neirong or ''
|
||||
if qunid is not None:
|
||||
qun_config.GroupID = qunid or ''
|
||||
if jieshao is not None:
|
||||
qun_config.Description = jieshao or ''
|
||||
|
||||
qun_config.save()
|
||||
_to_update.append(qun_config)
|
||||
else:
|
||||
# 如果不存在,创建新的(使用提供的peizhiid)
|
||||
qun_config = Qunpeizhi.query.create(
|
||||
# 不存在则创建新的
|
||||
qun_config = Qunpeizhi(
|
||||
GroupType=peizhiid,
|
||||
GroupContent=group_data.get('neirong', '') or '',
|
||||
GroupID=group_data.get('qunid', '') or '',
|
||||
Description=group_data.get('jieshao', '') or ''
|
||||
GroupContent=neirong or '',
|
||||
GroupID=qunid or '',
|
||||
Description=jieshao or '',
|
||||
)
|
||||
_to_create.append(qun_config)
|
||||
|
||||
updated_groups.append({
|
||||
'peizhiid': qun_config.GroupType,
|
||||
@@ -436,6 +442,13 @@ class AdminUpdateConfigView(APIView):
|
||||
'jieshao': qun_config.Description
|
||||
})
|
||||
|
||||
if _to_create:
|
||||
Qunpeizhi.objects.bulk_create(_to_create)
|
||||
if _to_update:
|
||||
Qunpeizhi.objects.bulk_update(
|
||||
_to_update, ['GroupContent', 'GroupID', 'Description']
|
||||
)
|
||||
|
||||
if updated_groups:
|
||||
response_data['qq_groups'] = updated_groups
|
||||
|
||||
|
||||
Reference in New Issue
Block a user