// app/model/print_item.js module.exports = app => { const { STRING, INTEGER, BOOLEAN, JSON, DATE } = app.Sequelize; const SUPPORTED_BUSINESS_TYPES = [ 'STOCK', 'RECEIPT', 'PICKING', 'OUTBOUND', 'ENTRY_ORDER' ]; const PrintItem = app.model.define('print_item', { id: { type: INTEGER, primaryKey: true, autoIncrement: true, comment: '主键ID', }, business_type: { type: STRING(50), allowNull: false, comment: '业务类型', validate: { notEmpty: true, isIn: [ SUPPORTED_BUSINESS_TYPES ], }, }, platform: { type: STRING(30), allowNull: false, comment: '平台', validate: { notEmpty: true, }, }, field_name: { type: STRING(100), allowNull: false, comment: '字段名(显示名称)', validate: { notEmpty: true, }, }, field_code: { type: STRING(100), allowNull: false, comment: '字段代码(英文标识)', validate: { notEmpty: true, is: /^[a-zA-Z_][a-zA-Z0-9_]*$/, // 验证字段代码格式 }, }, field_type: { type: STRING(30), allowNull: false, comment: '字段类型', validate: { isIn: [[ 'text', 'number', 'date', 'datetime', 'select', 'checkbox', 'textarea', 'image', 'barcode', 'qrcode' ]], }, }, // 新增:是否渲染为表格 is_table: { type: BOOLEAN, defaultValue: false, comment: '是否渲染为表格:0-否,1-是', field: 'is_table', }, // 新增:表格配置 table_config: { type: JSON, comment: '表格配置', }, // 新增:分组字段 group_field: { type: STRING(100), comment: '分组字段', validate: { is: /^[a-zA-Z_][a-zA-Z0-9_]*$/, }, }, // 新增:分组标题(国际化key) group_label: { type: STRING(100), comment: '分组标题', }, // 新增:分组排序 group_sort: { type: INTEGER, defaultValue: 0, comment: '分组排序', }, required: { type: BOOLEAN, defaultValue: false, comment: '是否必填:0-否,1-是', field: 'required', }, default_value: { type: STRING(500), comment: '默认值', }, sort_order: { type: INTEGER, defaultValue: 0, comment: '排序序号', }, regex_pattern: { type: STRING(200), comment: '正则验证规则', }, max_length: { type: INTEGER, comment: '最大长度', }, min_length: { type: INTEGER, comment: '最小长度', }, options: { type: JSON, comment: '选项(用于下拉框等)', }, description: { type: STRING(500), comment: '字段描述', }, status: { type: INTEGER, defaultValue: 1, comment: '状态:0-禁用,1-启用', validate: { isIn: [[ 0, 1 ]], }, }, created_by: { type: STRING(50), comment: '创建人', }, updated_by: { type: STRING(50), comment: '更新人', }, created_at: { type: DATE, comment: '创建时间', get() { const rawValue = this.getDataValue('created_at'); return rawValue ? rawValue : null; }, }, updated_at: { type: DATE, comment: '更新时间', get() { const rawValue = this.getDataValue('updated_at'); return rawValue ? rawValue : null; }, }, }, { tableName: 'wms_print_items', timestamps: false, // 因为表里已经有 created_at 和 updated_at 字段,但我们手动管理 createdAt: 'created_at', updatedAt: 'updated_at', paranoid: false, deletedAt: false, comment: '打印项配置表', indexes: [ { unique: true, fields: [ 'business_type', 'platform', 'field_code' ], name: 'uk_business_platform_field', }, { fields: [ 'business_type' ], name: 'idx_business_type', }, { fields: [ 'platform' ], name: 'idx_platform', }, { fields: [ 'status' ], name: 'idx_status', }, ], // 添加作用域 scopes: { // 表格字段 tableFields: { where: { is_table: true, }, }, // 分组字段 groupFields: { where: { group_field: { [app.Sequelize.Op.ne]: null, }, }, }, // 有效字段 active: { where: { status: 1, }, }, // 按业务类型 byBusiness(businessType) { return { where: { business_type: businessType, }, }; }, // 按平台 byPlatform(platform) { return { where: { platform, }, }; }, }, }); // 实例方法:获取分组配置 PrintItem.prototype.getGroupConfig = function() { if (!this.group_field) return null; return { field: this.group_field, label: this.group_label, sort: this.group_sort, }; }; // 实例方法:获取表格配置 PrintItem.prototype.getTableConfig = function() { if (!this.is_table || !this.table_config) return null; return this.table_config; }; // 关联方法 PrintItem.associate = function() { // 如果需要关联其他表,在这里添加 // 例如:PrintItem.belongsTo(app.model.OtherModel, { foreignKey: 'xxx' }); }; return PrintItem; };