'use strict'; /** * 出库单运单号补充回填:从确认摘要的「单值运单号」补齐 waybill_codes * * 背景:`20260915001700` 的回填只认摘要 JSON 里的运单号**列表**(confirm.waybill_codes / * confirm.express_codes 两个键),但 20260915001400~01700 之间写的早期摘要里只有根节点 * **单值** `confirm.express_code`(当时还没有列表键),这些单的 `waybill_codes` 仍未回填。 * * 回填规则(只在能确认「运单号集合完整」时才写,避免以偏概全): * 摘要 `confirm.packages` 数组**恰好 1 个包裹**,且根节点 `confirm.express_code` 非空 * → waybill_codes = [ 该单值运单号 ](唯一包裹即全部运单号) * 多包裹的历史摘要若缺列表键则不动(信息不全,宁可留空等 WMS 重推时由回调补写)。 * * 幂等:只更新 waybill_codes IS NULL 的行;可重复执行。 */ module.exports = { async up(queryInterface) { await queryInterface.sequelize.query(` UPDATE outbound_orders SET waybill_codes = JSON_ARRAY(JSON_UNQUOTE(JSON_EXTRACT(wms_push_result, '$.confirm.express_code'))) WHERE waybill_codes IS NULL AND wms_push_result IS NOT NULL AND JSON_LENGTH(JSON_EXTRACT(wms_push_result, '$.confirm.packages')) = 1 AND JSON_EXTRACT(wms_push_result, '$.confirm.express_code') IS NOT NULL AND JSON_UNQUOTE(JSON_EXTRACT(wms_push_result, '$.confirm.express_code')) <> '' `); }, async down() { // 回填属数据修正,不反向撤销(撤销只会丢掉运单号,无业务意义) }, };