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 { 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; }