'use strict'; module.exports = app => { const { STRING, INTEGER, DATE, BOOLEAN, ENUM, TEXT, BIGINT } = app.Sequelize; const LocationZone = app.model.define('wms_location_zone', { id: { type: INTEGER(10).UNSIGNED, primaryKey: true, autoIncrement: true, comment: '分区ID', }, warehouse_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '仓库ID', }, zone_code: { type: STRING(5), allowNull: true, comment: '分区编码(最多5位字母或数字)', }, zone_name: { type: STRING(100), allowNull: false, comment: '分区名称', }, zone_type: { type: ENUM('warehouse', 'area', 'rack', 'pick_start'), allowNull: false, comment: '分区类型', }, parent_id: { type: INTEGER(10).UNSIGNED, allowNull: true, comment: '父分区ID', }, is_active: { type: BOOLEAN, allowNull: false, defaultValue: true, comment: '是否启用', }, x: { type: INTEGER(11), allowNull: false, comment: '左下角x坐标(mm)', }, y: { type: INTEGER(11), allowNull: false, comment: '左下角y坐标(mm)', }, z: { type: INTEGER(11), allowNull: false, comment: '左下角z坐标(mm)', }, length: { type: INTEGER(11), allowNull: false, comment: '长度(mm)', }, width: { type: INTEGER(11), allowNull: false, comment: '宽度(mm)', }, height: { type: INTEGER(11), allowNull: false, comment: '高度(mm)', }, rack_columns: { type: INTEGER(3).UNSIGNED, allowNull: true, defaultValue: 1, comment: '货架列数', }, rack_rows: { type: INTEGER(3).UNSIGNED, allowNull: true, comment: '货架层数', }, rack_levels: { type: INTEGER(3).UNSIGNED, allowNull: true, comment: '货架分格数', }, cell_width: { type: INTEGER(11).UNSIGNED, allowNull: true, comment: '货架长度', }, cell_height: { type: INTEGER(11).UNSIGNED, allowNull: true, comment: '货架高度', }, cell_length: { type: INTEGER(11).UNSIGNED, allowNull: true, comment: '货架深度', }, direction: { type: ENUM('north', 'south', 'east', 'west'), allowNull: true, comment: '货架朝向', }, color: { type: STRING(20), allowNull: true, defaultValue: '#3B82F6', comment: '显示颜色', }, path_sequence: { type: INTEGER(10).UNSIGNED, allowNull: true, comment: '拣货路径排序序号', }, pick_distance: { type: INTEGER(11), allowNull: true, comment: '距离起始点的拣货距离(mm)', }, aisles: { type: STRING(255), allowNull: true, comment: '通道在货架的列的位置', get() { const rawValue = this.getDataValue('aisles'); try { return rawValue ? rawValue.split(',') : []; } catch { return []; } }, set(value) { const normalized = value ? value.join(',') : null; this.setDataValue('aisles', normalized); }, }, neighbor_zone_ids: { type: TEXT, allowNull: true, comment: '相邻分区ID列表(JSON数组)', get() { const rawValue = this.getDataValue('neighbor_zone_ids'); try { return rawValue ? JSON.parse(rawValue) : []; } catch { return []; } }, set(value) { this.setDataValue('neighbor_zone_ids', value ? JSON.stringify(value) : null ); }, }, is_passable: { type: BOOLEAN, allowNull: true, defaultValue: true, comment: '是否可通行', }, zone_category: { type: ENUM('storage', 'operational', 'obstacle'), allowNull: true, defaultValue: 'storage', comment: '区域分类', }, deleted_at: { type: DATE, allowNull: true, comment: '软删除时间', }, }, { tableName: 'wms_location_zone', timestamps: true, createdAt: 'created_at', updatedAt: 'updated_at', paranoid: true, // 软删除 deletedAt: 'deleted_at', comment: '库位分区表', indexes: [ { name: 'uk_warehouse_zone_code', unique: true, fields: [ 'warehouse_id', 'zone_code', 'deleted_at' ], }, { fields: [ 'warehouse_id', 'parent_id' ] }, { fields: [ 'deleted_at' ] }, { fields: [ 'zone_type' ] }, { fields: [ 'is_active' ] }, { fields: [ 'parent_id' ] }, { fields: [ 'warehouse_id', 'path_sequence', 'is_active' ] }, { fields: [ 'zone_category' ] }, { fields: [ 'warehouse_id', 'pick_distance' ] }, ], }); // 关联关系 LocationZone.associate = function() { // 自关联 - 父分区 app.model.LocationZone.belongsTo(app.model.LocationZone, { as: 'parent', foreignKey: 'parent_id', }); // 自关联 - 子分区 app.model.LocationZone.hasMany(app.model.LocationZone, { as: 'children', foreignKey: 'parent_id', }); // 关联仓库 app.model.LocationZone.belongsTo(app.model.Store, { as: 'warehouse', foreignKey: 'warehouse_id', }); // 关联仓位 app.model.LocationZone.hasMany(app.model.LocationSpace, { as: 'spaces', foreignKey: 'zone_id', }); // 关联起始拣货点 app.model.LocationZone.belongsTo(app.model.LocationZone, { as: 'pickStartPoint', foreignKey: 'warehouse_id', targetKey: 'warehouse_id', scope: { zone_type: 'pick_start', is_active: true, }, }); }; return LocationZone; };