'use strict'; module.exports = app => { const { STRING, BIGINT, INTEGER, TINYINT, TEXT, DATE, DECIMAL } = app.Sequelize; /** * 售后工单业务类型定义 * 外部可通过 require('./app/model/after_sale_work_order').BUSINESS_TYPES 访问 * 增删业务类型或子类型只需修改此常量,无需 ALTER TABLE */ const BUSINESS_TYPES = { waybill: { label: '运单', table: 'wms_order_express', subtypes: { intercept: '拦截', urge: '催件', damage: '破损', lost: '丢件', wrong_info: '信息错误', }, }, order_delivery: { label: '发货订单', table: 'wms_order_delivery', subtypes: { mis_shipment: '错发', missing_item: '少发', wrong_sku: '错品', quality_issue: '质量问题', delivery_delay: '发货延迟', }, }, entry_order: { label: '入库单', table: 'wms_entry_order', subtypes: { shortage: '少货', damage: '破损', wrong_sku: '错品', quality_issue: '质量问题', }, }, order_stockout: { label: '出库单/退货出库单', table: 'wms_order_stockout', subtypes: { return_issue: '退货异常', wrong_outbound: '错发', shortage: '少发', }, }, receiving: { label: '收货单', table: 'wms_receiving', subtypes: { shortage: '少货', damage: '破损', quality_issue: '质量问题', }, }, other: { label: '其他', table: null, subtypes: { general: '通用', }, }, }; /** * 根据 business_type 获取关联表名 * @param {string} businessType 业务类型 * @returns {string|null} 关联表名 */ function getBusinessTable(businessType) { return BUSINESS_TYPES[businessType]?.table || null; } /** * 校验 business_type + business_sub_type 组合是否合法 * @param {string} businessType 业务类型 * @param {string} businessSubType 业务子类型 * @returns {boolean} 是否合法 */ function isValidSubType(businessType, businessSubType) { const def = BUSINESS_TYPES[businessType]; return def && Object.prototype.hasOwnProperty.call(def.subtypes, businessSubType); } const AfterSaleWorkOrder = app.model.define('after_sale_work_order', { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键ID', }, warehouse_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', }, store_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '货主ID', }, work_order_no: { type: STRING(50), allowNull: false, comment: '售后工单号,唯一', }, business_type: { type: STRING(50), allowNull: false, comment: '关联业务类型', }, business_sub_type: { type: STRING(50), allowNull: false, comment: '关联业务子类型', }, business_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '关联业务单据主键ID(多态关联)', }, business_no: { type: STRING(100), allowNull: true, comment: '关联业务单据号(冗余)', }, direction: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '记账方向:0=未确定 1=入库 2=出库', }, status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '工单状态:0=待处理 1=处理中 2=已完成 3=已关闭 9=已取消', }, priority: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 50, comment: '优先级:1~100,数字越大优先级越高', }, title: { type: STRING(200), allowNull: false, comment: '工单标题/摘要', }, description: { type: TEXT, allowNull: true, comment: '问题描述', }, creator_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '创建人ID,关联 account.id;0 表示 AI 自动接待', }, creator_snapshot: { type: TEXT, allowNull: true, comment: '创建人信息快照(JSON格式);AI创建时记录模型/会话信息', }, assignee_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '当前处理人ID,关联 account.id', }, parent_work_order_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '父工单ID,自关联本表;非空时表示子工单', }, current_step: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '当前进度,值为当前正在处理的 step_order 序号', }, claim_needed: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '是否需要理赔:0=不需要 1=需要', }, claim_amount: { type: DECIMAL(12, 2), allowNull: true, defaultValue: 0.00, comment: '理赔金额(元)', }, claim_status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '理赔状态:0=未理赔 1=已理赔(单笔结清) 2=月结', }, resolution: { type: TEXT, allowNull: true, comment: '处理结果说明', }, resolved_at: { type: DATE, allowNull: true, comment: '解决时间', }, created_at: { type: DATE, allowNull: false, comment: '创建时间', }, updated_at: { type: DATE, allowNull: false, comment: '更新时间', }, deleted_at: { type: DATE, allowNull: true, comment: '软删除时间', }, }, { tableName: 'wms_after_sale_work_order', timestamps: true, createdAt: 'created_at', updatedAt: 'updated_at', paranoid: true, deletedAt: 'deleted_at', comment: '售后工单主表', }); AfterSaleWorkOrder.associate = () => { // 自关联:父工单 → 子工单 AfterSaleWorkOrder.hasMany(app.model.AfterSaleWorkOrder, { foreignKey: 'parent_work_order_id', sourceKey: 'id', as: 'children', }); AfterSaleWorkOrder.belongsTo(app.model.AfterSaleWorkOrder, { foreignKey: 'parent_work_order_id', targetKey: 'id', as: 'parent', }); // 关联进度步骤表 AfterSaleWorkOrder.hasMany(app.model.AfterSaleWorkOrderStep, { foreignKey: 'work_order_id', sourceKey: 'id', as: 'steps', }); }; // 挂载业务类型常量及辅助函数到 Model AfterSaleWorkOrder.BUSINESS_TYPES = BUSINESS_TYPES; AfterSaleWorkOrder.getBusinessTable = getBusinessTable; AfterSaleWorkOrder.isValidSubType = isValidSubType; return AfterSaleWorkOrder; };