oms/backend/database/migrations/20261104071400-add-subsidiary-to-orders.js
liufumingren 2d2f781f09 feat(order): 订单关联货主与越界操作拦截
- order_index 与各订单状态表新增 subsidiary_id(跟随店铺货主),历史数据按店铺回填
- 订单列表/详情/导出按货主范围过滤,货主权限仅可见自己所属公司的订单
- 订单 id 级操作统一在 _loadIndexRows 校验货主:审核/推送/驳回/取消/作废/恢复/改仓库物流/删除/明细维护越界返回 403
- 新建订单货主跟随店铺,货主权限只能为自己公司店铺建单;平台拉单写入时同步店铺货主
- 内部任务/平台回调无登录态时按系统操作处理,不受货主范围限制
- 订单列表与导出新增货主列

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-05 02:15:46 +08:00

75 lines
2.7 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';
/**
* 订单绑定下属公司(货主):order_index 与各订单状态表增加 subsidiary_id。
*
* 取值跟随订单所属店铺的货主(店铺 = 货主),历史数据按店铺回填;
* 货主权限仅可见自己所属公司的订单,且不可越界操作。
*/
const STATUS_TABLES = [
'order_status_pending',
'order_status_queued',
'order_status_exception',
'order_status_outsourcing',
'order_status_delivery',
'order_status_void',
'order_status_canceled',
'order_status_history',
];
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn('order_index', 'subsidiary_id', {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: true,
comment: '所属货主(下属公司ID),跟随店铺',
references: { model: 'principal_subsidiaries', key: 'id' },
after: 'principal_id',
});
await queryInterface.addIndex('order_index', [ 'principal_id', 'subsidiary_id' ], {
name: 'idx_order_index_principal_subsidiary',
});
for (const table of STATUS_TABLES) {
await queryInterface.addColumn(table, 'subsidiary_id', {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: true,
comment: '所属货主(下属公司ID),跟随店铺',
references: { model: 'principal_subsidiaries', key: 'id' },
after: 'principal_id',
});
await queryInterface.addIndex(table, [ 'principal_id', 'subsidiary_id' ], {
name: `idx_${table}_principal_subsidiary`,
});
}
// 按订单所属店铺的货主回填历史数据(状态表自带 shop_id;索引表按状态表回填)
for (const table of STATUS_TABLES) {
await queryInterface.sequelize.query(
`UPDATE ${table} t`
+ ' JOIN shops s ON s.id = t.shop_id'
+ ' SET t.subsidiary_id = s.subsidiary_id'
+ ' WHERE t.subsidiary_id IS NULL AND s.subsidiary_id IS NOT NULL'
);
}
const unionSql = STATUS_TABLES
.map(table => `SELECT id, subsidiary_id FROM ${table} WHERE subsidiary_id IS NOT NULL`)
.join(' UNION ALL ');
await queryInterface.sequelize.query(
'UPDATE order_index oi'
+ ` JOIN (${unionSql}) st ON st.id = oi.id`
+ ' SET oi.subsidiary_id = st.subsidiary_id'
+ ' WHERE oi.subsidiary_id IS NULL'
);
},
async down(queryInterface) {
for (const table of STATUS_TABLES) {
await queryInterface.removeIndex(table, `idx_${table}_principal_subsidiary`);
await queryInterface.removeColumn(table, 'subsidiary_id');
}
await queryInterface.removeIndex('order_index', 'idx_order_index_principal_subsidiary');
await queryInterface.removeColumn('order_index', 'subsidiary_id');
},
};