Files
order_site/apps/backend/src/routes/admin/kuaishou-industry.ts
T
yml2213 5092e88618 perf(admin): 打手等级筛选用 UNION 形式、概览加短缓存、静态配置接口加缓存头
三处都出自审计清单,改动互不重叠。

1) 打手等级筛选(生产实测 1119ms)
   原写法是对 worker_users 的两个相关 EXISTS(接单人 / 拼单参与人),规划器无法用它
   驱动索引,只能顺序扫完整张 work_orders。改成先把等级解析成打手 id
   (worker_users(level_id) 有索引,等级只有 6 个、解析出几百个 id),再走索引。
   注意:**不能**写成 `assigned_worker_id = ANY(...) OR id IN (...)` —— 实测 1852ms,
   比原写法还慢(OR 右支要逐行回表取 id)。用 UNION 形式:
     wo.id IN (SELECT id FROM work_orders WHERE assigned_worker_id = ANY(...) UNION
               SELECT work_order_id FROM work_order_shares WHERE worker_id = ANY(...) AND status != 'cancelled')
   实测 61ms(count),语义与原来完全一致(含"空数组=过滤掉全部")。
   三个入口 listWorkOrders / countWorkOrders / getWorkOrderStatistics 都先解析;
   未解析时保留原相关 EXISTS 兜底。

2) 平台概览短缓存(5 秒)
   一次并发 5 条 count(4 条打 work_orders,一条是运营态 CASE),而池只有 8 条连接,
   后台刷新时与其他请求互相争抢(抓包显示整批 9 个请求同时发出、它自身 167ms)。
   看板计数滞后几秒无感,用 WorkerListCache 缓存并合并并发。

3) 三个静态配置接口加 HTTP 缓存头(private, max-age=300)
   接单分类、快手行业店铺、自定义店铺此前没有任何 Cache-Control,每次刷新都真发一次
   请求(抓包里 4 个这类请求)。带 private 是因为这些是带鉴权的后台接口。
   **dev-mock/status 故意不加**:它是可切换状态,缓存会让"切了开关界面没变"。

验证:后端 547 项单测全绿(新增 4 项守卫,分别锁住 UNION 形式、三个入口都解析、
概览缓存、以及 dev-mock 不得加缓存)。
2026-10-01 15:36:46 +08:00

220 lines
7.2 KiB
TypeScript

import { Router } from 'express'
import { setHttpCache } from '../../middleware/http-cache.js'
import {
approveAdminKuaishouIndustryRefund,
checkAdminKuaishouIndustryVoucherAvailable,
consumeAdminKuaishouIndustryVoucherByCode,
destroyAdminKuaishouIndustryVoucherByCode,
disagreeAdminKuaishouIndustryRefund,
listAdminKuaishouIndustryRefunds,
listAdminKuaishouIndustryShops,
listAdminKuaishouIndustryVouchers,
resendAdminKuaishouIndustryVoucherCode,
reverseAdminKuaishouIndustryVoucher,
} from '../../services/admin/kuaishou-industry-admin-service.js'
import { createJsonHandler, requireAdminRoles } from './session.js'
import type {
AdminKuaishouIndustryRefundApproveRouteBody,
AdminKuaishouIndustryRefundDisagreeRouteBody,
AdminKuaishouIndustryRefundListRouteBody,
AdminKuaishouIndustryVoucherCheckAvailableRouteBody,
AdminKuaishouIndustryVoucherConsumeRouteBody,
AdminKuaishouIndustryVoucherDestroyRouteBody,
AdminKuaishouIndustryVoucherResendRouteBody,
AdminKuaishouIndustryVoucherReverseRouteBody,
} from '../../types/admin/route-inputs.js'
import type { KuaishouIndustryVoucherAdminListQuery } from '../../repositories/kuaishou-industry-voucher-repo.js'
const router = Router()
router.get(
'/kuaishou-industry/vouchers',
createJsonHandler(
(req) => listAdminKuaishouIndustryVouchers(req.query as KuaishouIndustryVoucherAdminListQuery),
{
successMessage: 'ok',
errorMessage: '查询快手行业电子凭证失败',
scope: '[admin/kuaishou-industry/vouchers]',
},
),
)
router.get(
'/kuaishou-industry/shops',
requireAdminRoles(['admin', 'operator', 'support']),
setHttpCache(300),
createJsonHandler(() => listAdminKuaishouIndustryShops(), {
successMessage: 'ok',
errorMessage: '查询快手行业店铺失败',
scope: '[admin/kuaishou-industry/shops]',
}),
)
router.post(
'/kuaishou-industry/refunds/list',
requireAdminRoles(['admin', 'operator']),
createJsonHandler(
(req) => listAdminKuaishouIndustryRefunds(req.body as AdminKuaishouIndustryRefundListRouteBody),
{
successMessage: '售后单列表已查询',
errorMessage: '查询快手售后单列表失败',
scope: '[admin/kuaishou-industry/refunds/list]',
audit: (req, data) =>
buildKuaishouIndustryAudit('kuaishou_industry_refund_list', req.body, data),
},
),
)
router.post(
'/kuaishou-industry/refunds/approve',
requireAdminRoles(['admin', 'operator']),
createJsonHandler(
(req) =>
approveAdminKuaishouIndustryRefund(req.body as AdminKuaishouIndustryRefundApproveRouteBody),
{
successMessage: '同意退款接口已执行',
errorMessage: '执行同意退款失败',
scope: '[admin/kuaishou-industry/refunds/approve]',
audit: (req, data) =>
buildKuaishouIndustryAudit('kuaishou_industry_refund_approve', req.body, data),
},
),
)
router.post(
'/kuaishou-industry/refunds/disagree',
requireAdminRoles(['admin', 'operator']),
createJsonHandler(
(req) =>
disagreeAdminKuaishouIndustryRefund(req.body as AdminKuaishouIndustryRefundDisagreeRouteBody),
{
successMessage: '不同意退款接口已执行',
errorMessage: '执行不同意退款失败',
scope: '[admin/kuaishou-industry/refunds/disagree]',
audit: (req, data) =>
buildKuaishouIndustryAudit('kuaishou_industry_refund_disagree', req.body, data),
},
),
)
router.post(
'/kuaishou-industry/vouchers/check-available',
requireAdminRoles(['admin', 'operator', 'support']),
createJsonHandler(
(req) =>
checkAdminKuaishouIndustryVoucherAvailable(
req.body as AdminKuaishouIndustryVoucherCheckAvailableRouteBody,
),
{
successMessage: '电子凭证有效性已检查',
errorMessage: '检查电子凭证有效性失败',
scope: '[admin/kuaishou-industry/vouchers/check-available]',
audit: (req, data) =>
buildKuaishouIndustryAudit('kuaishou_industry_voucher_check_available', req.body, data),
},
),
)
router.post(
'/kuaishou-industry/vouchers/reverse',
requireAdminRoles(['admin', 'operator', 'support']),
createJsonHandler(
(req) =>
reverseAdminKuaishouIndustryVoucher(req.body as AdminKuaishouIndustryVoucherReverseRouteBody),
{
successMessage: '电子凭证冲正回调已执行',
errorMessage: '执行电子凭证冲正失败',
scope: '[admin/kuaishou-industry/vouchers/reverse]',
audit: (req, data) =>
buildKuaishouIndustryAudit('kuaishou_industry_voucher_reverse', req.body, data),
},
),
)
router.post(
'/kuaishou-industry/vouchers/consume',
requireAdminRoles(['admin', 'operator', 'support']),
createJsonHandler(
(req) =>
consumeAdminKuaishouIndustryVoucherByCode(
req.body as AdminKuaishouIndustryVoucherConsumeRouteBody,
),
{
successMessage: '电子凭证已手动核销',
errorMessage: '手动核销电子凭证失败',
scope: '[admin/kuaishou-industry/vouchers/consume]',
audit: (req, data) =>
buildKuaishouIndustryAudit('kuaishou_industry_voucher_consume', req.body, data),
},
),
)
router.post(
'/kuaishou-industry/vouchers/resend-code',
requireAdminRoles(['admin', 'operator', 'support']),
createJsonHandler(
(req) =>
resendAdminKuaishouIndustryVoucherCode(
req.body as AdminKuaishouIndustryVoucherResendRouteBody,
),
{
successMessage: '电子凭证发码回调已重发',
errorMessage: '重发电子凭证发码回调失败',
scope: '[admin/kuaishou-industry/vouchers/resend-code]',
audit: (req, data) =>
buildKuaishouIndustryAudit('kuaishou_industry_voucher_resend_code', req.body, data),
},
),
)
router.post(
'/kuaishou-industry/vouchers/destroy',
requireAdminRoles(['admin', 'operator']),
createJsonHandler(
(req) =>
destroyAdminKuaishouIndustryVoucherByCode(
req.body as AdminKuaishouIndustryVoucherDestroyRouteBody,
),
{
successMessage: '电子凭证已手动销毁',
errorMessage: '手动销毁电子凭证失败',
scope: '[admin/kuaishou-industry/vouchers/destroy]',
audit: (req, data) =>
buildKuaishouIndustryAudit('kuaishou_industry_voucher_destroy', req.body, data),
},
),
)
function buildKuaishouIndustryAudit(action: string, body: unknown, data: unknown) {
const payload = body && typeof body === 'object' ? (body as Record<string, unknown>) : {}
const result = data && typeof data === 'object' ? (data as Record<string, unknown>) : {}
return {
action,
targetType: 'kuaishou_industry',
targetId: resolveAuditTargetId(payload),
data: {
sellerId: String(payload.sellerId || ''),
oid: String(payload.oid || payload.orderId || ''),
voucherCode: String(payload.voucherCode || ''),
refundId: String(payload.refundId || ''),
success: Boolean(result.success),
httpStatus: Number(result.httpStatus || 0) || 0,
error: String(result.error || ''),
},
}
}
function resolveAuditTargetId(payload: Record<string, unknown>): string {
return String(
payload.voucherCode ||
payload.refundId ||
payload.oid ||
payload.orderId ||
payload.sellerId ||
'kuaishou-industry',
)
}
export default router