来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
125 lines
3.9 KiB
TypeScript
125 lines
3.9 KiB
TypeScript
import { pool } from '../db';
|
|
import type { TenantContext } from './tenantScope';
|
|
|
|
export interface CustomerRecipient {
|
|
id: string;
|
|
customer_code: string;
|
|
recipient_name: string;
|
|
contact_phone: string | null;
|
|
address: string | null;
|
|
is_default: boolean;
|
|
created_at: string;
|
|
}
|
|
|
|
function branchFilter(ctx: TenantContext | null | undefined, alias: string, params: unknown[]) {
|
|
if (ctx?.branchCode && !ctx.isSuperAdmin) {
|
|
params.push(ctx.branchCode);
|
|
return `AND EXISTS (SELECT 1 FROM customers c WHERE c.customer_code = ${alias}.customer_code AND c.branch_code = $${params.length})`;
|
|
}
|
|
return '';
|
|
}
|
|
|
|
export async function listCustomerRecipients(
|
|
customerCode: string,
|
|
ctx?: TenantContext | null
|
|
): Promise<CustomerRecipient[]> {
|
|
const params: unknown[] = [customerCode];
|
|
const branchSql = branchFilter(ctx, 'cr', params);
|
|
const result = await pool.query(
|
|
`SELECT id, customer_code, recipient_name, contact_phone, address, is_default, created_at
|
|
FROM customer_recipients cr
|
|
WHERE customer_code = $1 ${branchSql}
|
|
ORDER BY is_default DESC, created_at ASC`,
|
|
params
|
|
);
|
|
return result.rows;
|
|
}
|
|
|
|
export async function createCustomerRecipient(input: {
|
|
customerCode: string;
|
|
recipientName: string;
|
|
contactPhone?: string;
|
|
address?: string;
|
|
isDefault?: boolean;
|
|
ctx?: TenantContext | null;
|
|
}) {
|
|
if (!input.recipientName.trim()) throw Object.assign(new Error('收货人姓名不能为空'), { status: 400 });
|
|
|
|
if (input.isDefault) {
|
|
await pool.query(`UPDATE customer_recipients SET is_default = false WHERE customer_code = $1`, [
|
|
input.customerCode,
|
|
]);
|
|
}
|
|
|
|
const hasDefault = await pool.query(
|
|
`SELECT 1 FROM customer_recipients WHERE customer_code = $1 AND is_default = true LIMIT 1`,
|
|
[input.customerCode]
|
|
);
|
|
const isDefault = input.isDefault ?? !hasDefault.rowCount;
|
|
|
|
const result = await pool.query(
|
|
`INSERT INTO customer_recipients (customer_code, recipient_name, contact_phone, address, is_default)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING *`,
|
|
[
|
|
input.customerCode,
|
|
input.recipientName.trim(),
|
|
input.contactPhone?.trim() || null,
|
|
input.address?.trim() || null,
|
|
isDefault,
|
|
]
|
|
);
|
|
return result.rows[0];
|
|
}
|
|
|
|
export async function updateCustomerRecipient(input: {
|
|
id: string;
|
|
recipientName?: string;
|
|
contactPhone?: string;
|
|
address?: string;
|
|
isDefault?: boolean;
|
|
ctx?: TenantContext | null;
|
|
}) {
|
|
const existing = await pool.query(`SELECT * FROM customer_recipients WHERE id = $1`, [input.id]);
|
|
if (!existing.rows.length) throw Object.assign(new Error('收货人不存在'), { status: 404 });
|
|
const row = existing.rows[0];
|
|
|
|
if (input.isDefault) {
|
|
await pool.query(`UPDATE customer_recipients SET is_default = false WHERE customer_code = $1`, [
|
|
row.customer_code,
|
|
]);
|
|
}
|
|
|
|
const result = await pool.query(
|
|
`UPDATE customer_recipients SET
|
|
recipient_name = COALESCE($2, recipient_name),
|
|
contact_phone = COALESCE($3, contact_phone),
|
|
address = COALESCE($4, address),
|
|
is_default = COALESCE($5, is_default)
|
|
WHERE id = $1
|
|
RETURNING *`,
|
|
[
|
|
input.id,
|
|
input.recipientName?.trim() || null,
|
|
input.contactPhone !== undefined ? input.contactPhone?.trim() || null : null,
|
|
input.address !== undefined ? input.address?.trim() || null : null,
|
|
input.isDefault ?? null,
|
|
]
|
|
);
|
|
return result.rows[0];
|
|
}
|
|
|
|
export async function deleteCustomerRecipient(id: string) {
|
|
const result = await pool.query(`DELETE FROM customer_recipients WHERE id = $1 RETURNING *`, [id]);
|
|
if (!result.rows.length) throw Object.assign(new Error('收货人不存在'), { status: 404 });
|
|
const deleted = result.rows[0];
|
|
if (deleted.is_default) {
|
|
await pool.query(
|
|
`UPDATE customer_recipients SET is_default = true
|
|
WHERE id = (SELECT id FROM customer_recipients WHERE customer_code = $1 ORDER BY created_at ASC LIMIT 1)`,
|
|
[deleted.customer_code]
|
|
);
|
|
}
|
|
return deleted;
|
|
}
|