WMS_MAIN/database/migrations/20261111203500-change-trace-weight-to-decimal.js
liufumingren e99f8fad48 fix(waybill): 适配轨迹接口返回调整(空轨迹与小数重量)
- trace 为空数组(暂无轨迹)不再计为失败:只计入 result.empty,不落库,
  任务状态仍为 success;提示语补充「无轨迹 Z 单」
- weight 支持字符串小数(如 "0.99"):新增 parseTraceWeight 统一转数字保留 3 位小数
- wms_order_express_trace.weight 由 INT(11) 改为 DECIMAL(10,3)(迁移 20261111203500,
  已在开发库与生产库执行;MySQL 不支持 INT→DECIMAL 的 INPLACE,走 COPY 重建)
- 文档:methods/system.json(0.3.327)、变更记录、设计文档与出站回调说明同步

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-11 20:37:18 +08:00

76 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* wms_order_express_trace.weight 由 INT 改为 DECIMAL(10,3)。
*
* 背景:外部轨迹接口返回的 weight 为带小数的字符串(如 "0.99"),
* 原 INT 列会把 0.99 四舍五入/截断为 1 或 0,丢失精度。
*
* 变更:INT(11) DEFAULT 0 → DECIMAL(10,3) DEFAULT 0(保留原可空性与注释)。
*
* 注意:MySQL 不支持 INT→DECIMAL 的 INPLACE 变更,只能 COPY(重建表)。
* 该表为 2026-11-11 新建(轨迹尚在接入期,数据量小),重建代价可接受。
* 幂等:已是 decimal 时跳过。
*/
const TABLE_NAME = 'wms_order_express_trace';
const COLUMN_NAME = 'weight';
module.exports = {
async up(queryInterface) {
const { sequelize } = queryInterface;
const [ rows ] = await sequelize.query(
'SELECT COLUMN_TYPE AS columnType, IS_NULLABLE AS isNullable, '
+ 'COLUMN_DEFAULT AS columnDefault, COLUMN_COMMENT AS columnComment '
+ 'FROM INFORMATION_SCHEMA.COLUMNS '
+ 'WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?',
{ replacements: [ TABLE_NAME, COLUMN_NAME ] }
);
const column = (rows || [])[0];
if (!column) {
console.log(`[${COLUMN_NAME}] skip: ${TABLE_NAME}.${COLUMN_NAME} 列不存在`);
return;
}
if (String(column.columnType || '').toLowerCase().startsWith('decimal')) {
console.log(`[${COLUMN_NAME}] skip: 已是 DECIMAL(${column.columnType})`);
return;
}
const nullable = String(column.isNullable).toUpperCase() === 'YES' ? 'NULL' : 'NOT NULL';
const defaultValue = column.columnDefault === null || column.columnDefault === undefined
? ''
: ` DEFAULT ${Number(column.columnDefault) || 0}`;
const comment = String(column.columnComment || '').replace(/'/g, "''");
await sequelize.query(
`ALTER TABLE \`${TABLE_NAME}\` MODIFY COLUMN \`${COLUMN_NAME}\` `
+ `DECIMAL(10,3) ${nullable}${defaultValue} COMMENT '${comment}'`
);
console.log(`[${COLUMN_NAME}] changed to DECIMAL(10,3) on ${TABLE_NAME}`);
},
async down(queryInterface) {
const { sequelize } = queryInterface;
const [ rows ] = await sequelize.query(
'SELECT COLUMN_TYPE AS columnType, IS_NULLABLE AS isNullable, '
+ 'COLUMN_DEFAULT AS columnDefault, COLUMN_COMMENT AS columnComment '
+ 'FROM INFORMATION_SCHEMA.COLUMNS '
+ 'WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?',
{ replacements: [ TABLE_NAME, COLUMN_NAME ] }
);
const column = (rows || [])[0];
if (!column) return;
if (!String(column.columnType || '').toLowerCase().startsWith('decimal')) return;
const nullable = String(column.isNullable).toUpperCase() === 'YES' ? 'NULL' : 'NOT NULL';
const comment = String(column.columnComment || '').replace(/'/g, "''");
await sequelize.query(
`ALTER TABLE \`${TABLE_NAME}\` MODIFY COLUMN \`${COLUMN_NAME}\` `
+ `INT(11) ${nullable} DEFAULT 0 COMMENT '${comment}'`
);
console.log(`[${COLUMN_NAME}] reverted to INT(11)`);
},
};