'use strict'; /** * 仓库前端个性化配置模型(仓库伪独立部署,单表)。 * - 每仓库一行(一仓一域一证书):保存仓库前端 Logo / 标题 / 登录页图 / * 主题等个性化配置,以及证书元信息与证书 PEM 内容(cert_pem / cert_key,由定时 * 任务读取后写文件部署,禁止写入日志/接口响应)。 * - sync_status 状态机:0-待同步,1-同步成功,2-同步失败; * 管理后台保存后置为待同步,由定时任务同步到 nginx 后回写状态。 * - 证书启用/停用由整行删除或 sync_status 表达;证书到期由 cert_expire_at * 判断,不单独保留证书 status 列。 * - domain 唯一索引(uk_domain)为 MySQL 可空列唯一索引:允许多个 NULL, * 应用层在写入非空 domain 前必须做唯一性校验,不能依赖数据库约束报错。 * - store_id 唯一索引(uk_store_id):每仓一行,由数据库层保证。 * - 不启用 paranoid:配置为"每仓库一行"的元数据,删除语义由业务层 * (清空/停用配置)表达,表结构不含 deleted_at 列。 * * @param {Object} app Egg.js Application 实例。 * @return {Object} Sequelize Model 定义。 */ module.exports = app => { const { BIGINT, STRING, INTEGER, TINYINT, DATE, TEXT, JSON: JSON_TYPE } = app.Sequelize; const WarehouseFrontConfig = app.model.define('warehouse_front_config', { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, store_id: { type: BIGINT(20).UNSIGNED, allowNull: false, references: { model: 'store', key: 'id', }, comment: '仓库ID,关联 wms_store.id(is_warehouse=1),每仓一行', }, domain: { type: STRING(100), allowNull: true, comment: '仓库自持域名;可空列唯一索引允许多个 NULL,非空值唯一性需应用层校验', }, title: { type: STRING(100), allowNull: true, comment: '站点标题', }, logo_url: { type: STRING(255), allowNull: true, comment: 'Logo地址', }, favicon_url: { type: STRING(255), allowNull: true, comment: '站点 favicon 图标地址', }, login_bg_url: { type: STRING(255), allowNull: true, comment: '登录页背景图地址', }, theme: { type: JSON_TYPE, allowNull: true, defaultValue: null, comment: '主题配置JSON(颜色/布局等)', }, cert_path: { type: STRING(500), allowNull: true, comment: '证书文件路径(只存路径不存内容)', }, cert_pem: { type: TEXT, allowNull: true, comment: '证书 PEM 内容(私钥不在内);内容仅由定时任务读取,禁止写入日志/接口响应', }, key_path: { type: STRING(500), allowNull: true, comment: '私钥文件路径(只存路径不存内容)', }, cert_key: { type: TEXT, allowNull: true, comment: '私钥 PEM 内容;内容仅由定时任务读取,禁止写入日志/接口响应', }, cert_expire_at: { type: DATE, allowNull: true, comment: '证书到期时间', }, sync_status: { type: TINYINT, allowNull: false, defaultValue: 0, validate: { isIn: { args: [[ 0, 1, 2 ]], msg: '同步状态不合法:0-待同步,1-同步成功,2-同步失败', }, }, comment: '同步状态:0-待同步,1-同步成功,2-同步失败', }, sync_attempts: { type: INTEGER(11), allowNull: false, defaultValue: 0, comment: '同步尝试次数', }, last_sync_error: { type: STRING(500), allowNull: true, comment: '最近一次同步失败原因', }, last_synced_at: { type: DATE, allowNull: true, 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: '更新时间', }, }, { tableName: 'wms_warehouse_front_config', freezeTableName: true, underscored: true, timestamps: true, createdAt: 'created_at', updatedAt: 'updated_at', paranoid: false, comment: '仓库前端个性化配置表(含证书内容与元信息)', indexes: [ { name: 'uk_store_id', unique: true, fields: [ 'store_id' ], }, { name: 'uk_domain', unique: true, fields: [ 'domain' ], }, { name: 'idx_sync_status', fields: [ 'sync_status' ], }, ], }); return WarehouseFrontConfig; };