'use strict'; module.exports = app => { const { BIGINT, STRING, DATE, TINYINT, INTEGER } = app.Sequelize; const Store = app.model.define('store', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '商家ID', }, company_name: { type: STRING(200), allowNull: false, unique: true, validate: { notEmpty: { msg: '公司全称不能为空', }, len: { args: [ 1, 200 ], msg: '公司全称长度应在1-200个字符之间', }, }, comment: '公司全称', }, business_license: { type: STRING(100), allowNull: true, unique: true, comment: '营业执照号', }, legal_person: { type: STRING(50), allowNull: true, comment: '法人代表', }, contact_phone: { type: STRING(20), allowNull: false, validate: { notEmpty: { msg: '联系电话不能为空', }, is: { args: /^[0-9+\-()\s]+$/, msg: '请输入有效的联系电话', }, }, comment: '联系电话', }, contact_email: { type: STRING(100), allowNull: true, comment: '联系邮箱', }, contact_person: { type: STRING(50), allowNull: false, validate: { notEmpty: { msg: '联系人不能为空', }, len: { args: [ 1, 50 ], msg: '联系人姓名长度应在1-50个字符之间', }, }, comment: '联系人', }, address: { type: STRING(500), allowNull: true, comment: '公司地址', }, industry: { type: STRING(100), allowNull: true, comment: '所属行业', }, account_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '关联账号ID(商家管理员)', }, status: { type: TINYINT(4), allowNull: false, defaultValue: 1, validate: { isIn: { args: [[ 0, 1, 2 ]], msg: '状态值不合法,0-禁用,1-启用,2-审核中', }, }, comment: '状态:0-禁用,1-启用,2-审核中', }, created_by: { type: BIGINT.UNSIGNED, allowNull: false, comment: '创建人', }, created_at: { type: DATE, allowNull: false, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: DATE, allowNull: false, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), comment: '更新时间', }, logo_url: { type: STRING(255), allowNull: true, comment: 'logo链接', }, project: { type: STRING(20), allowNull: true, comment: '应用', }, domain: { type: STRING(100), allowNull: true, comment: '绑定域名', }, auth_end: { type: DATE, allowNull: true, comment: '授权结束时间', }, deleted_at: { type: DATE, allowNull: true, comment: '删除时间', }, quota_bytes: { type: BIGINT.UNSIGNED, allowNull: false, defaultValue: 107374182400, comment: '仓库文件存储配额', }, }, { tableName: 'wms_store', freezeTableName: true, underscored: true, timestamps: true, createdAt: 'created_at', updatedAt: 'updated_at', paranoid: true, deletedAt: 'deleted_at', comment: '商家表', indexes: [ { name: 'uk_company_name', fields: [ 'company_name' ], unique: true, }, { name: 'uk_business_license', fields: [ 'business_license' ], unique: true, }, { name: 'idx_status', fields: [ 'status' ], }, { name: 'idx_account_id', fields: [ 'account_id' ], }, ], }); // 关联关系 - 使用您推荐的方式 Store.associate = function() { const { Account, Role, AccountStoreEmployee, ProhibitedType } = app.model; // 关联创建人 Store.belongsTo(Account, { foreignKey: 'created_by', as: 'creator', }); // 关联商家管理员账号 Store.belongsTo(Account, { foreignKey: 'account_id', as: 'adminAccount', }); // 关联商家拥有的角色 Store.hasMany(Role, { foreignKey: 'store_id', as: 'roles', }); // 关联商家拥有的账号角色关联 Store.hasMany(AccountStoreEmployee, { foreignKey: 'store_id', as: 'accountStoreEmployee', }); Store.hasMany(ProhibitedType, { foreignKey: 'warehouse_id', as: 'prohibitedTypes', }); }; // 实例方法:检查商家是否启用 Store.prototype.isEnabled = function() { return this.status === 1; }; // 实例方法:检查商家是否审核中 Store.prototype.isReviewing = function() { return this.status === 2; }; // 实例方法:检查是否是仓库 Store.prototype.isWarehouse = function() { return this.project === 'warehouse'; }; // 实例方法:检查授权是否有效 Store.prototype.isAuthValid = function() { if (!this.auth_start || !this.auth_end) { return true; // 如果没有设置授权时间,默认有效 } const now = new Date(); return now >= new Date(this.auth_start) && now <= new Date(this.auth_end); }; // 实例方法:检查邮箱是否有效 Store.prototype.hasValidEmail = function() { if (!this.contact_email) { return false; } const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(this.contact_email); }; // 类方法:根据账号ID查找商家 Store.findByAccountId = async function(accountId) { return await this.findOne({ where: { account_id: accountId, status: 1, }, }); }; // 类方法:获取所有启用的商家 Store.findAllEnabled = async function() { return await this.findAll({ where: { status: 1, }, order: [[ 'company_name', 'ASC' ]], }); }; // 类方法:获取所有仓库类型的商家 Store.findAllWarehouses = async function() { return await this.findAll({ where: { status: 1, project: 'warehouse', }, order: [[ 'company_name', 'ASC' ]], }); }; // 类方法:检查公司名称是否存在 Store.checkCompanyNameExists = async function(companyName, excludeId = null) { const where = { company_name: companyName, }; if (excludeId) { where.id = { [app.Sequelize.Op.ne]: excludeId, }; } const store = await this.findOne({ where }); return !!store; }; // 类方法:创建商家时的数据验证 Store.validateStoreData = function(data) { const errors = []; // 必填字段检查 const requiredFields = [ 'company_name', 'contact_person', 'contact_phone' ]; for (const field of requiredFields) { if (!data[field]) { errors.push(`${field} 不能为空`); } } // 公司名称长度检查 if (data.company_name && (data.company_name.length < 1 || data.company_name.length > 200)) { errors.push('公司名称长度应在1-200个字符之间'); } // 邮箱格式检查(如果提供了邮箱) if (data.contact_email && data.contact_email.trim() !== '') { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(data.contact_email)) { errors.push('联系邮箱格式不正确'); } } return errors; }; return Store; };