来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
188 lines
6.9 KiB
TypeScript
188 lines
6.9 KiB
TypeScript
import { pool } from '../db';
|
|
import { signedExistingPhotos } from './inspectionReviewService';
|
|
|
|
export type InboundPhoto = { path: string; url: string };
|
|
|
|
export type InboundPhotoInspectionRow = {
|
|
sku_code?: string | null;
|
|
supplier_code?: string | null;
|
|
source_type?: string | null;
|
|
actual_qty?: number | null;
|
|
inbound_batch_no?: string | null;
|
|
inspection_time?: string | null;
|
|
quality_rating?: string | null;
|
|
compliance_status?: number | null;
|
|
inspection_photos?: unknown;
|
|
};
|
|
|
|
/** 自采验收常空供应商或 source_type=purchase;订单行是 INTERNAL。统一成 _none。 */
|
|
export function normalizeInboundPhotoSupplier(
|
|
supplierCode: string | null | undefined,
|
|
sourceType?: string | null
|
|
): string {
|
|
if (String(sourceType || '').trim().toLowerCase() === 'purchase') return '_none';
|
|
const code = String(supplierCode || '').trim();
|
|
if (!code || code === '_none' || code === 'UNASSIGNED' || code === 'INTERNAL') return '_none';
|
|
return code;
|
|
}
|
|
|
|
export function inboundPhotoGroupKey(
|
|
skuCode: string,
|
|
supplierCode: string | null | undefined,
|
|
sourceType?: string | null
|
|
): string {
|
|
const sku = String(skuCode || '').trim();
|
|
return `${sku}|${normalizeInboundPhotoSupplier(supplierCode, sourceType)}`;
|
|
}
|
|
|
|
/** 仅已入库批次;排除作废 30、D/不合格。无批次=未入库,订单详情不挂照。 */
|
|
export function shouldIncludeInspectionForInboundPhotos(row: InboundPhotoInspectionRow): boolean {
|
|
const sku = String(row.sku_code || '').trim();
|
|
if (!sku) return false;
|
|
if (!String(row.inbound_batch_no || '').trim()) return false;
|
|
if (!row.inspection_time) return false;
|
|
if (Number(row.compliance_status) === 30) return false;
|
|
const rating = String(row.quality_rating || '').trim();
|
|
if (rating === 'D' || rating === '不合格') return false;
|
|
return true;
|
|
}
|
|
|
|
export function collectInboundPhotoPathsBySkuSupplier(
|
|
rows: InboundPhotoInspectionRow[]
|
|
): Array<{ sku_code: string; supplier_code: string; paths: string[] }> {
|
|
const grouped = new Map<string, { sku_code: string; supplier_code: string; paths: string[]; seen: Set<string> }>();
|
|
for (const row of rows) {
|
|
if (!shouldIncludeInspectionForInboundPhotos(row)) continue;
|
|
const sku = String(row.sku_code || '').trim();
|
|
const supplier = normalizeInboundPhotoSupplier(row.supplier_code, row.source_type);
|
|
const key = `${sku}|${supplier}`;
|
|
let bucket = grouped.get(key);
|
|
if (!bucket) {
|
|
bucket = { sku_code: sku, supplier_code: supplier, paths: [], seen: new Set() };
|
|
grouped.set(key, bucket);
|
|
}
|
|
const raw = Array.isArray(row.inspection_photos)
|
|
? row.inspection_photos
|
|
: row.inspection_photos != null
|
|
? [row.inspection_photos]
|
|
: [];
|
|
for (const item of raw) {
|
|
const path = String(item || '').trim();
|
|
if (!path || bucket.seen.has(path)) continue;
|
|
bucket.seen.add(path);
|
|
bucket.paths.push(path);
|
|
}
|
|
}
|
|
return [...grouped.values()]
|
|
.filter((row) => row.paths.length > 0)
|
|
.map(({ sku_code, supplier_code, paths }) => ({ sku_code, supplier_code, paths }));
|
|
}
|
|
|
|
export type LoadingPhotoRow = {
|
|
sku_code?: string | null;
|
|
supplier_code?: string | null;
|
|
loading_photos?: unknown;
|
|
};
|
|
|
|
/** 配送补拍按 SKU+供应商成组;不过滤合格/不合格。 */
|
|
export function collectLoadingPhotoPathsBySkuSupplier(
|
|
rows: LoadingPhotoRow[]
|
|
): Array<{ sku_code: string; supplier_code: string; paths: string[] }> {
|
|
const grouped = new Map<string, { sku_code: string; supplier_code: string; paths: string[]; seen: Set<string> }>();
|
|
for (const row of rows) {
|
|
const sku = String(row.sku_code || '').trim();
|
|
if (!sku) continue;
|
|
const supplier = normalizeInboundPhotoSupplier(row.supplier_code);
|
|
const key = `${sku}|${supplier}`;
|
|
let bucket = grouped.get(key);
|
|
if (!bucket) {
|
|
bucket = { sku_code: sku, supplier_code: supplier, paths: [], seen: new Set() };
|
|
grouped.set(key, bucket);
|
|
}
|
|
const raw = Array.isArray(row.loading_photos)
|
|
? row.loading_photos
|
|
: row.loading_photos != null
|
|
? [row.loading_photos]
|
|
: [];
|
|
for (const item of raw) {
|
|
const path = String(item || '').trim();
|
|
if (!path || bucket.seen.has(path)) continue;
|
|
bucket.seen.add(path);
|
|
bucket.paths.push(path);
|
|
}
|
|
}
|
|
return [...grouped.values()]
|
|
.filter((row) => row.paths.length > 0)
|
|
.map(({ sku_code, supplier_code, paths }) => ({ sku_code, supplier_code, paths }));
|
|
}
|
|
|
|
export function mergeInboundAndLoadingPhotoGroups(
|
|
inbound: Array<{ sku_code: string; supplier_code: string; paths: string[] }>,
|
|
loading: Array<{ sku_code: string; supplier_code: string; paths: string[] }>
|
|
): Array<{ sku_code: string; supplier_code: string; paths: string[] }> {
|
|
const grouped = new Map<string, { sku_code: string; supplier_code: string; paths: string[]; seen: Set<string> }>();
|
|
for (const group of [...inbound, ...loading]) {
|
|
const key = `${group.sku_code}|${group.supplier_code}`;
|
|
let bucket = grouped.get(key);
|
|
if (!bucket) {
|
|
bucket = {
|
|
sku_code: group.sku_code,
|
|
supplier_code: group.supplier_code,
|
|
paths: [],
|
|
seen: new Set(),
|
|
};
|
|
grouped.set(key, bucket);
|
|
}
|
|
for (const path of group.paths) {
|
|
if (!path || bucket.seen.has(path)) continue;
|
|
bucket.seen.add(path);
|
|
bucket.paths.push(path);
|
|
}
|
|
}
|
|
return [...grouped.values()].map(({ sku_code, supplier_code, paths }) => ({
|
|
sku_code,
|
|
supplier_code,
|
|
paths,
|
|
}));
|
|
}
|
|
|
|
export async function listOrderInboundPhotos(orderCode: string): Promise<{
|
|
items: Array<{ sku_code: string; supplier_code: string; photos: InboundPhoto[] }>;
|
|
}> {
|
|
const code = String(orderCode || '').trim();
|
|
if (!code) {
|
|
throw Object.assign(new Error('缺少订单号'), { status: 400, code: 'invalid_request' });
|
|
}
|
|
const result = await pool.query(
|
|
`SELECT sku_code, supplier_code, source_type, actual_qty, inspection_time, quality_rating,
|
|
compliance_status, inspection_photos, inbound_batch_no
|
|
FROM inspections
|
|
WHERE order_code = $1
|
|
ORDER BY COALESCE(inspection_time, created_at) ASC, created_at ASC`,
|
|
[code]
|
|
);
|
|
const loadingResult = await pool.query(
|
|
`SELECT COALESCE(NULLIF(TRIM(pi.sku_code), ''), NULLIF(TRIM(oi.sku_code), '')) AS sku_code,
|
|
oi.supplier_code,
|
|
pi.loading_photos
|
|
FROM picking_items pi
|
|
LEFT JOIN order_items oi ON oi.id = pi.order_item_id
|
|
WHERE pi.order_code = $1
|
|
AND COALESCE(array_length(pi.loading_photos, 1), 0) > 0
|
|
ORDER BY pi.created_at ASC`,
|
|
[code]
|
|
);
|
|
const groups = mergeInboundAndLoadingPhotoGroups(
|
|
collectInboundPhotoPathsBySkuSupplier(result.rows || []),
|
|
collectLoadingPhotoPathsBySkuSupplier(loadingResult.rows || [])
|
|
);
|
|
const items = await Promise.all(
|
|
groups.map(async (group) => ({
|
|
sku_code: group.sku_code,
|
|
supplier_code: group.supplier_code,
|
|
photos: await signedExistingPhotos(group.paths),
|
|
}))
|
|
);
|
|
return { items: items.filter((row) => row.photos.length > 0) };
|
|
}
|