Files
SupplierSystem/server/src/services/aftersaleQueryService.ts
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

110 lines
4.2 KiB
TypeScript

import { pool } from '../db';
import type { TenantContext } from './tenantScope';
const LOCKED_TYPES = ['order_return', 'spi_void', 'inbound_switch'];
export type AfterSaleDirectoryRow = {
id: string;
aftersale_code: string;
order_code: string | null;
customer_code: string | null;
company_name: string | null;
aftersale_type: string;
status: number | null;
amount: number;
sku_code: string | null;
reason: string | null;
};
export async function listAfterSalesForManagement(input: {
ctx: TenantContext;
dateFrom: string;
dateTo: string;
dateField?: 'delivery_date' | 'created_at';
statusTab?: 'pending' | 'completed' | '';
search?: string;
type?: string;
limit?: number;
}): Promise<{ rows: AfterSaleDirectoryRow[]; total: number }> {
const branchCode = String(input.ctx.branchCode || '').trim();
if (!branchCode) {
throw Object.assign(new Error('请选择当前操作分公司'), { status: 400, code: 'invalid_request' });
}
const dateField = input.dateField === 'created_at' ? 'created_at' : 'delivery_date';
const params: unknown[] = [branchCode, input.dateFrom, input.dateTo, dateField];
const where = [
'a.branch_code = $1',
`(
CASE
WHEN a.aftersale_type = ANY($5::text[]) OR $4 = 'created_at'
THEN (a.created_at AT TIME ZONE 'Asia/Shanghai')::date
ELSE COALESCE(
NULLIF(o.delivery_date::text, '')::date,
(o.created_at AT TIME ZONE 'Asia/Shanghai')::date,
(a.created_at AT TIME ZONE 'Asia/Shanghai')::date
)
END
) BETWEEN $2::date AND $3::date`,
];
params.push(LOCKED_TYPES);
const statusTab = String(input.statusTab || 'pending').trim() || 'pending';
if (statusTab === 'pending') {
// 待办=「状态未完成」且**不是**按类型锁定的三类自动单(建单即终态、与 status 无关)。
// 只看 status 的话,一旦 status 被网关裸改成 10/20(H8),已完成的自动单会虚高进待处理。
where.push('a.status IN (10, 20)');
where.push('NOT (a.aftersale_type = ANY($5::text[]))');
} else if (statusTab === 'completed') {
// 对称口径:按类型锁定的三类自动单永远算「已完成」(即使 status 被改坏也不会漏出两个页签之外)
where.push('(a.status IN (30, 40) OR a.aftersale_type = ANY($5::text[]))');
} else if (statusTab) {
throw Object.assign(new Error('status_tab 只能是 pending、completed 或空'), {
status: 400,
code: 'invalid_request',
});
}
const type = String(input.type || '').trim();
if (type) {
params.push(type);
where.push(`a.aftersale_type = $${params.length}`);
}
const search = String(input.search || '').trim();
if (search) {
params.push(`%${search}%`);
where.push(`(
a.aftersale_code ILIKE $${params.length}
OR COALESCE(a.order_code, '') ILIKE $${params.length}
OR COALESCE(c.company_name, '') ILIKE $${params.length}
)`);
}
const limit = Math.min(Math.max(Number(input.limit) || 50, 1), 50);
params.push(limit);
const result = await pool.query(
`SELECT a.id, a.aftersale_code, a.order_code, a.customer_code, a.aftersale_type,
a.status, a.amount, a.sku_code, a.reason,
c.company_name,
COUNT(*) OVER()::int AS total
FROM after_sales a
LEFT JOIN customers c ON c.customer_code = a.customer_code
LEFT JOIN orders o ON o.order_code = a.order_code
WHERE ${where.join(' AND ')}
ORDER BY a.created_at DESC
LIMIT $${params.length}`,
params
);
const rows = result.rows.map((row) => ({
id: String(row.id),
aftersale_code: String(row.aftersale_code || ''),
order_code: row.order_code == null ? null : String(row.order_code),
customer_code: row.customer_code == null ? null : String(row.customer_code),
company_name: row.company_name == null ? null : String(row.company_name),
aftersale_type: String(row.aftersale_type || ''),
status: row.status == null ? null : Number(row.status),
amount: Number(row.amount) || 0,
sku_code: row.sku_code == null ? null : String(row.sku_code),
reason: row.reason == null ? null : String(row.reason),
}));
return { rows, total: Number(result.rows[0]?.total) || rows.length };
}