// app/model/file.js 'use strict'; module.exports = app => { const { BIGINT, INTEGER, STRING, DATE, BOOLEAN } = app.Sequelize; const File = app.model.define('wms_file', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '文件唯一ID', }, warehouse_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '仓库ID(核心统计维度)', references: { model: 'wms_store', key: 'id', }, }, file_key: { type: STRING(500), allowNull: false, unique: true, comment: '存储路径/对象键(如cos/2024/03/file.jpg)', }, file_url: { type: STRING(500), allowNull: false, comment: '完整访问URL', }, file_name: { type: STRING(255), allowNull: false, comment: '原始文件名', }, file_size: { type: INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '文件大小(字节)', }, file_type: { type: STRING(50), defaultValue: '', comment: '文件类型:inventory_image, delivery_note, inspection_video', }, mime_type: { type: STRING(100), defaultValue: '', comment: 'MIME类型', }, md5_hash: { type: STRING(32), allowNull: false, defaultValue: '', unique: true, comment: '文件MD5(去重依据)', }, sha1_hash: { type: STRING(40), allowNull: false, defaultValue: '', comment: '文件SHA1(二次校验)', }, storage_mode: { type: STRING(20), allowNull: false, defaultValue: 'cos', comment: '存储方式:cos/oss/local', }, storage_class: { type: STRING(20), defaultValue: 'standard', comment: '存储级别:standard/ia/archive', }, uploader_id: { type: STRING(64), allowNull: false, comment: '上传者ID(用户ID/员工工号)', }, uploader_name: { type: STRING(100), allowNull: false, comment: '上传者姓名(快照)', }, uploader_dept: { type: STRING(100), defaultValue: '', comment: '上传者部门(快照)', }, created_at: { type: DATE, allowNull: false, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: DATE, defaultValue: null, comment: '更新时间', }, expire_time: { type: DATE, allowNull: true, defaultValue: null, comment: '过期时间,为空表示永不过期', }, is_deleted: { type: BOOLEAN, allowNull: false, defaultValue: false, field: 'is_deleted', comment: '软删除标记', }, deleted_at: { type: DATE, defaultValue: null, comment: '删除时间', }, deleted_by: { type: STRING(64), defaultValue: null, comment: '删除操作人', }, }, { tableName: 'wms_file', comment: 'WMS文件资源中枢表', timestamps: false, // 使用自定义的created_at/updated_at paranoid: false, // 使用自定义的软删除字段 indexes: [ { name: 'idx_warehouse', fields: [ 'warehouse_id' ], }, { name: 'idx_uploader', fields: [ 'uploader_type', 'uploader_id' ], }, { name: 'idx_created', fields: [ 'created_at' ], }, { name: 'idx_type_warehouse', fields: [ 'file_type', 'warehouse_id' ], }, ], }); // 关联定义(如果需要) File.associate = function() { const { Store, FileReference } = app.model; // 属于一个仓库 File.belongsTo(Store, { foreignKey: 'warehouse_id', as: 'warehouse', }); // 有多个业务引用 File.hasMany(FileReference, { foreignKey: 'file_id', as: 'references', }); }; return File; };