来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
238 lines
7.4 KiB
JavaScript
238 lines
7.4 KiB
JavaScript
/**
|
|
* 自贡分公司:将指定干杂自采采购单转给干杂供应商备货
|
|
* 用法: DATABASE_URL=... node scripts/reassign-dry-goods-to-supplier.cjs [--dry-run]
|
|
*/
|
|
const { Pool } = require('pg');
|
|
|
|
const BRANCH = process.env.BRANCH_CODE || 'BR002';
|
|
const DRY_CATEGORY = process.env.DRY_CATEGORY || 'CAT78659053039';
|
|
const SUPPLIER_CODE = process.env.DRY_SUPPLIER || 'SUP37831443';
|
|
|
|
const ITEMS = [
|
|
{ orderName: '干海椒段', skuCode: 'SKU78662174130', qty: 3 },
|
|
{ orderName: '干海椒丝', skuCode: null, qty: 3 },
|
|
{ orderName: '干辣椒筒筒', skuCode: 'SKU78662157625', qty: 5 },
|
|
{ orderName: '黄灯笼盖辣椒酱', skuCode: 'SKU78668982766', qty: 1 },
|
|
{ orderName: '辣椒段辣的', skuCode: 'SKU78663542330', qty: 3 },
|
|
];
|
|
|
|
const NAME_ALIASES = {
|
|
黄灯笼盖辣椒酱: '黄灯笼黄辣椒酱',
|
|
干辣椒筒筒: '干海椒筒(中辣)',
|
|
干海椒段: '干海椒节',
|
|
辣椒段辣的: '海椒节(辣)',
|
|
};
|
|
|
|
function trim(v) {
|
|
return String(v || '').trim();
|
|
}
|
|
|
|
async function resolveSkuCode(client, item) {
|
|
if (item.skuCode) return item.skuCode;
|
|
const names = [item.orderName];
|
|
if (NAME_ALIASES[item.orderName]) names.push(NAME_ALIASES[item.orderName]);
|
|
for (const name of names) {
|
|
const r = await client.query(
|
|
`SELECT code FROM product_catalog
|
|
WHERE item_type = 'product' AND TRIM(name) = TRIM($1)
|
|
AND branch_code = $2
|
|
ORDER BY updated_at DESC NULLS LAST LIMIT 1`,
|
|
[name, BRANCH]
|
|
);
|
|
if (r.rowCount) return r.rows[0].code;
|
|
}
|
|
const fuzzy = await client.query(
|
|
`SELECT code FROM product_catalog
|
|
WHERE item_type = 'product' AND branch_code = $1 AND name ILIKE $2
|
|
ORDER BY length(name) ASC LIMIT 1`,
|
|
[BRANCH, `%${item.orderName.replace(/[%_]/g, '')}%`]
|
|
);
|
|
return fuzzy.rows[0]?.code || null;
|
|
}
|
|
|
|
async function findPurchaseOrder(client, orderName) {
|
|
const names = [orderName];
|
|
if (NAME_ALIASES[orderName]) names.push(NAME_ALIASES[orderName]);
|
|
const r = await client.query(
|
|
`SELECT po.purchase_code, po.order_code, po.source_id, po.purchase_status, po.sku_code,
|
|
oi.id AS order_item_id, oi.item_name, oi.category_code
|
|
FROM purchase_orders po
|
|
LEFT JOIN order_items oi ON oi.id::text = po.source_id AND oi.order_code = po.order_code
|
|
WHERE po.branch_code = $1
|
|
AND po.purchase_status IN (10, 32)
|
|
AND (
|
|
TRIM(COALESCE(oi.item_name, '')) = ANY($2::text[])
|
|
OR po.remark ILIKE $3
|
|
)
|
|
ORDER BY po.created_at DESC NULLS LAST
|
|
LIMIT 1`,
|
|
[BRANCH, names, `%${orderName}%`]
|
|
);
|
|
return r.rows[0] || null;
|
|
}
|
|
|
|
async function ensurePreparation(client, input) {
|
|
const skuCode = trim(input.skuCode) || null;
|
|
const itemName = trim(input.itemName);
|
|
const existing = skuCode
|
|
? await client.query(
|
|
`SELECT id FROM supplier_preparations
|
|
WHERE order_code = $1 AND supplier_code = $2 AND sku_code = $3 LIMIT 1`,
|
|
[input.orderCode, input.supplierCode, skuCode]
|
|
)
|
|
: await client.query(
|
|
`SELECT id FROM supplier_preparations
|
|
WHERE order_code = $1 AND supplier_code = $2
|
|
AND TRIM(COALESCE(item_name, '')) = $3 LIMIT 1`,
|
|
[input.orderCode, input.supplierCode, itemName]
|
|
);
|
|
if (existing.rowCount) return false;
|
|
|
|
await client.query(
|
|
`INSERT INTO supplier_preparations (
|
|
order_code, supplier_code, sku_code, item_name, prepare_qty, purchase_price,
|
|
prepare_status, branch_code, category_code, processing_method, processing_owner
|
|
) VALUES ($1, $2, $3, $4, $5, $6, 10, $7, $8, '原材料', NULL)`,
|
|
[
|
|
input.orderCode,
|
|
input.supplierCode,
|
|
skuCode,
|
|
itemName,
|
|
input.prepareQty,
|
|
input.purchasePrice,
|
|
BRANCH,
|
|
DRY_CATEGORY,
|
|
]
|
|
);
|
|
|
|
if (skuCode) {
|
|
const taskCode = `AT${Date.now()}${String(skuCode).slice(-4)}`;
|
|
try {
|
|
await client.query(
|
|
`INSERT INTO allocation_tasks (
|
|
task_code, order_code, sku_code, supplier_code, allocated_price, allocated_qty, task_status
|
|
) VALUES ($1, $2, $3, $4, $5, $6, 20)`,
|
|
[taskCode, input.orderCode, skuCode, input.supplierCode, input.purchasePrice, input.prepareQty]
|
|
);
|
|
} catch {
|
|
// optional
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
async function assignOne(client, item, dryRun) {
|
|
const po = await findPurchaseOrder(client, item.orderName);
|
|
if (!po) {
|
|
return { orderName: item.orderName, status: 'purchase_not_found' };
|
|
}
|
|
|
|
const skuCode = await resolveSkuCode(client, item);
|
|
const supplier = await client.query(
|
|
`SELECT company_name FROM suppliers WHERE supplier_code = $1 LIMIT 1`,
|
|
[SUPPLIER_CODE]
|
|
);
|
|
const supplierName = supplier.rows[0]?.company_name || SUPPLIER_CODE;
|
|
|
|
if (dryRun) {
|
|
return {
|
|
orderName: item.orderName,
|
|
status: 'would_assign',
|
|
purchase_code: po.purchase_code,
|
|
order_code: po.order_code,
|
|
sku_code: skuCode,
|
|
old_category: po.category_code,
|
|
supplier: SUPPLIER_CODE,
|
|
};
|
|
}
|
|
|
|
if (po.order_item_id) {
|
|
await client.query(
|
|
`UPDATE order_items
|
|
SET sku_code = COALESCE($2, sku_code),
|
|
category_code = $3,
|
|
category_name = '干杂',
|
|
supplier_code = $4
|
|
WHERE id = $1`,
|
|
[po.order_item_id, skuCode, DRY_CATEGORY, SUPPLIER_CODE]
|
|
);
|
|
}
|
|
|
|
await client.query(
|
|
`UPDATE purchase_orders
|
|
SET sku_code = COALESCE($2, sku_code),
|
|
supplier_code = $3,
|
|
purchase_status = 40,
|
|
remark = COALESCE(remark, '') || $4
|
|
WHERE purchase_code = $1`,
|
|
[
|
|
po.purchase_code,
|
|
skuCode,
|
|
SUPPLIER_CODE,
|
|
`;系统批量转供应商「${supplierName}」备货,取消自采`,
|
|
]
|
|
);
|
|
|
|
const priceRow = await client.query(
|
|
`SELECT purchase_price FROM purchase_orders WHERE purchase_code = $1`,
|
|
[po.purchase_code]
|
|
);
|
|
const prepCreated = await ensurePreparation(client, {
|
|
orderCode: po.order_code,
|
|
supplierCode: SUPPLIER_CODE,
|
|
skuCode,
|
|
itemName: po.item_name || item.orderName,
|
|
prepareQty: Number(item.qty) || Number(po.purchase_qty) || 1,
|
|
purchasePrice: Number(priceRow.rows[0]?.purchase_price) || 0,
|
|
});
|
|
|
|
if (skuCode) {
|
|
await client.query(
|
|
`UPDATE product_catalog SET parent_code = $2, updated_at = NOW()
|
|
WHERE item_type = 'product' AND code = $1`,
|
|
[skuCode, DRY_CATEGORY]
|
|
);
|
|
await client.query(`UPDATE skus SET category_code = $2 WHERE sku_code = $1`, [skuCode, DRY_CATEGORY]);
|
|
}
|
|
|
|
return {
|
|
orderName: item.orderName,
|
|
status: 'assigned',
|
|
purchase_code: po.purchase_code,
|
|
order_code: po.order_code,
|
|
sku_code: skuCode,
|
|
preparation_created: prepCreated,
|
|
supplier: supplierName,
|
|
};
|
|
}
|
|
|
|
async function main() {
|
|
const dryRun = process.argv.includes('--dry-run');
|
|
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
const client = await pool.connect();
|
|
|
|
try {
|
|
await client.query('BEGIN');
|
|
const results = [];
|
|
for (const item of ITEMS) {
|
|
results.push(await assignOne(client, item, dryRun));
|
|
}
|
|
if (dryRun) await client.query('ROLLBACK');
|
|
else await client.query('COMMIT');
|
|
console.log(JSON.stringify(results, null, 2));
|
|
const failed = results.filter((r) => r.status === 'purchase_not_found');
|
|
if (failed.length) process.exitCode = 1;
|
|
} catch (err) {
|
|
await client.query('ROLLBACK').catch(() => undefined);
|
|
throw err;
|
|
} finally {
|
|
client.release();
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|