'use strict'; module.exports = app => { const { STRING, INTEGER, TEXT, DATE } = app.Sequelize; const FastWhere = app.model.define('fast_where', { id: { type: INTEGER.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, label: { type: STRING(50), allowNull: true, comment: '条件/组名称', }, value: { type: TEXT, allowNull: true, comment: '查询条件,json需要序列化', get() { const rawValue = this.getDataValue('value'); if (!rawValue) return null; try { return JSON.parse(rawValue); } catch (error) { app.logger.error('[FastWhere] JSON parse error:', error); return null; } }, set(value) { if (value) { this.setDataValue('value', JSON.stringify(value)); } else { this.setDataValue('value', null); } }, }, pid: { type: INTEGER, allowNull: true, defaultValue: 0, comment: '父级id,方便树结构', }, icon: { type: STRING(50), allowNull: true, comment: 'icon编码', }, type: { type: STRING(50), allowNull: true, comment: '查询类别', }, createdAt: { type: DATE, allowNull: true, field: 'created_at', comment: '创建时间', }, updatedAt: { type: DATE, allowNull: true, field: 'updated_at', comment: '更新时间', }, }, { // 模型配置 tableName: 'wms_fast_where', // 数据库表名 timestamps: true, // 自动维护时间戳 freezeTableName: true, // 禁用表名复数化 underscored: true, // 字段使用下划线命名 comment: '快速查询条件表', }); // 定义模型关联关系(如果需要) FastWhere.associate = function() { // 自关联:父级关系 app.model.FastWhere.belongsTo(app.model.FastWhere, { foreignKey: 'pid', as: 'parent', }); // 自关联:子级关系 app.model.FastWhere.hasMany(app.model.FastWhere, { foreignKey: 'pid', as: 'children', }); }; return FastWhere; };