来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
120 lines
3.3 KiB
JavaScript
120 lines
3.3 KiB
JavaScript
/**
|
|
* 将纯牛奶、豆浆从干杂供应商改回自采(自贡分公司)
|
|
* 用法: DATABASE_URL=... node scripts/revert-milk-soy-to-self-purchase.cjs [--dry-run]
|
|
*/
|
|
const { Pool } = require('pg');
|
|
|
|
const BRANCH = process.env.BRANCH_CODE || 'BR002';
|
|
const SELF_PURCHASE_CATEGORY = process.env.SELF_PURCHASE_CATEGORY || 'CAT86944033392';
|
|
const DRY_SUPPLIER = process.env.DRY_SUPPLIER || 'SUP37831443';
|
|
|
|
const TARGETS = [
|
|
{ orderName: '纯牛奶', skuCode: 'SKU78665503388' },
|
|
{ orderName: '豆浆', skuCode: 'SKU78667003452' },
|
|
];
|
|
|
|
async function revertOne(client, target, dryRun) {
|
|
const po = await client.query(
|
|
`SELECT po.purchase_code, po.order_code, po.source_id, po.purchase_status, po.supplier_code,
|
|
oi.id AS order_item_id, oi.item_name
|
|
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 = 40
|
|
AND po.supplier_code = $2
|
|
AND (
|
|
TRIM(COALESCE(oi.item_name, '')) = $3
|
|
OR po.sku_code = $4
|
|
)
|
|
ORDER BY po.created_at DESC NULLS LAST
|
|
LIMIT 1`,
|
|
[BRANCH, DRY_SUPPLIER, target.orderName, target.skuCode]
|
|
);
|
|
|
|
if (!po.rowCount) {
|
|
return { orderName: target.orderName, status: 'not_found' };
|
|
}
|
|
|
|
const row = po.rows[0];
|
|
if (dryRun) {
|
|
return {
|
|
orderName: target.orderName,
|
|
status: 'would_revert',
|
|
purchase_code: row.purchase_code,
|
|
order_code: row.order_code,
|
|
};
|
|
}
|
|
|
|
await client.query(
|
|
`DELETE FROM allocation_tasks
|
|
WHERE order_code = $1 AND sku_code = $2 AND supplier_code = $3`,
|
|
[row.order_code, target.skuCode, DRY_SUPPLIER]
|
|
);
|
|
|
|
await client.query(
|
|
`DELETE FROM supplier_preparations
|
|
WHERE order_code = $1 AND supplier_code = $2 AND sku_code = $3`,
|
|
[row.order_code, DRY_SUPPLIER, target.skuCode]
|
|
);
|
|
|
|
if (row.order_item_id) {
|
|
await client.query(
|
|
`UPDATE order_items
|
|
SET supplier_code = NULL,
|
|
category_code = $2,
|
|
category_name = '自采'
|
|
WHERE id = $1 AND order_code = $3`,
|
|
[row.order_item_id, SELF_PURCHASE_CATEGORY, row.order_code]
|
|
);
|
|
}
|
|
|
|
await client.query(
|
|
`UPDATE purchase_orders
|
|
SET purchase_status = 10,
|
|
supplier_code = NULL,
|
|
remark = regexp_replace(
|
|
COALESCE(remark, ''),
|
|
';系统批量转供应商「[^」]+」备货,取消自采',
|
|
'',
|
|
'g'
|
|
)
|
|
WHERE purchase_code = $1`,
|
|
[row.purchase_code]
|
|
);
|
|
|
|
return {
|
|
orderName: target.orderName,
|
|
status: 'reverted_to_self_purchase',
|
|
purchase_code: row.purchase_code,
|
|
order_code: row.order_code,
|
|
};
|
|
}
|
|
|
|
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 target of TARGETS) {
|
|
results.push(await revertOne(client, target, dryRun));
|
|
}
|
|
if (dryRun) await client.query('ROLLBACK');
|
|
else await client.query('COMMIT');
|
|
console.log(JSON.stringify(results, null, 2));
|
|
} 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);
|
|
});
|