'use strict'; /** * 售后主表 Model,基于 `wms_after_sale` 表。 * @param {import('egg').Application} app Egg 应用实例 * @return {import('sequelize').ModelCtor} 售后 Model(含常量挂载) */ module.exports = app => { const { STRING, BIGINT, INTEGER, DECIMAL, TEXT, TINYINT, DATE } = app.Sequelize; // ============================ // 枚举常量定义 // ============================ /** * 售后类型 + 子类型枚举,按业务单据维度分类。 * @type {Array<{name: string, type: string, subType: Array<{name: string, value: string}>}>} */ const AFTER_SALE_TYPES = [ { name: '订单', type: 'order', subType: [ { name: '破损', value: 'damage' }, { name: '少发', value: 'missing_item' }, { name: '错发', value: 'mis_shipment' }, { name: '错品', value: 'wrong_sku' }, { name: '质量问题', value: 'quality_issue' }, { name: '发货延迟', value: 'delivery_delay' }, { name: '物流异常', value: 'logistics_issue' }, { name: '客户拒收', value: 'rejected' }, { name: '拦截', value: 'intercept' }, { name: '核实签收', value: 'verify_delivery' }, { name: '催件', value: 'urge' }, { name: '多发', value: 'extra_item' }, { name: '改地址', value: 'change_address' }, { name: '换快递', value: 'change_carrier' }, { name: '揽收', value: 'pickup' }, { name: '重量异常', value: 'weight_issue' }, { name: '运费争议', value: 'freight_issue' }, { name: '其他', value: 'other' }, ], }, { name: '收货单', type: 'receiving', subType: [ { name: '少货', value: 'shortage' }, { name: '破损', value: 'damage' }, { name: '错品', value: 'wrong_sku' }, { name: '质量问题', value: 'quality_issue' }, { name: '其他', value: 'other' }, ], }, { name: '入库单', type: 'entry', subType: [ { name: '少货', value: 'shortage' }, { name: '破损', value: 'damage' }, { name: '错品', value: 'wrong_sku' }, { name: '质量问题', value: 'quality_issue' }, { name: '其他', value: 'other' }, ], }, { name: '出库单', type: 'stockout', subType: [ { name: '退货异常', value: 'return_issue' }, { name: '错发', value: 'wrong_outbound' }, { name: '少发', value: 'shortage' }, { name: '其他', value: 'other' }, ], }, ]; /** * 售后处理角色组枚举。 * @type {{customer_service: string, warehouse: string, finance: string}} */ const AFTER_SALE_ROLE_GROUPS = { customer_service: '客服组', warehouse: '仓库组', finance: '财务组', }; /** * 售后单状态枚举。 * @type {{pending: string, processing: string, waiting: string, completed: string, closed: string, cancelled: string}} */ const AFTER_SALE_STATUS = { pending: '待处理', processing: '处理中', pending_feedback: '等待外部反馈', completed: '已完成', closed: '已关闭', cancelled: '已取消', }; /** * 理赔状态枚举(仓库理赔 & 运方理赔通用)。 * @type {{pending: string, reviewing: string, approved: string, paid: string, monthly: string, rejected: string}} */ const CLAIM_STATUS = { pending: '待理赔', reviewing: '审核中', approved: '已通过', paid: '已打款', monthly: '月结', rejected: '已拒绝', }; // ============================ // Model 定义 // ============================ const AfterSale = app.model.define('after_sale', { /** 主键 */ id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键', }, /** 仓库ID */ warehouse_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', }, /** 货主ID */ store_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '货主ID', }, /** 售后单号,唯一 */ after_sale_no: { type: STRING(50), allowNull: false, comment: '售后单号,唯一。生成规则:前缀AS + 日期YYYYMMDD + 6位序号', }, /** 售后类型 */ type: { type: STRING(20), allowNull: false, comment: '售后类型:order=订单, receiving=收货单, entry=入库单, stockout=出库单', }, /** 售后子类型 */ sub_type: { type: STRING(50), allowNull: false, comment: '售后子类型,与 type 组成二级分类', }, /** 售后状态 */ status: { type: STRING(20), allowNull: false, defaultValue: 'pending', comment: '售后状态:pending=待处理, processing=处理中, pending_feedback=等待外部反馈, completed=已完成, closed=已关闭, cancelled=已取消', }, /** 优先级 */ priority: { type: INTEGER(11), allowNull: false, defaultValue: 0, comment: '优先级,数值越大越优先(0=普通,10=紧急)', }, /** 售后标题 */ title: { type: STRING(200), allowNull: false, comment: '售后标题(简要描述)', }, /** 售后详细描述 */ description: { type: TEXT, allowNull: true, comment: '售后详细描述', }, /** 创建人账户ID */ creator_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '创建人账户ID(关联 accounts 表)', }, /** 关联站内信消息ID */ inbox_message_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '关联 wms_inbox_message.id,为空表示无关联信息', }, /** 当前处理角色组 */ role_group: { type: STRING(50), allowNull: true, comment: '当前处理角色组:customer_service=客服组, warehouse=仓库组, finance=财务组。NULL 表示未分配', }, /** 最终处理结果说明 */ resolution: { type: TEXT, allowNull: true, comment: '最终处理结果说明', }, /** 完结时间 */ resolved_at: { type: DATE, allowNull: true, comment: '完结时间(状态变为 completed 时写入)', }, /** 下次跟进时间 */ followup_at: { type: DATE, allowNull: true, comment: '下次跟进时间:设为 pending_feedback 时可指定,超时后需二次跟进', }, /** 仓库理赔标记 */ claim_needed: { type: TINYINT(1), allowNull: false, defaultValue: 0, comment: '仓库理赔标记:0=不需要,1=需要', }, /** 仓库理赔金额(元) */ claim_amount: { type: DECIMAL(12, 2), allowNull: true, comment: '仓库理赔金额,单位:人民币(元),claim_needed=1 时可填写', }, /** 仓库理赔状态 */ claim_status: { type: STRING(20), allowNull: true, comment: '仓库理赔状态:pending=待理赔, reviewing=审核中, approved=已通过, paid=已打款, monthly=月结, rejected=已拒绝', }, /** 运方理赔标记 */ carrier_claim_needed: { type: TINYINT(1), allowNull: false, defaultValue: 0, comment: '运方理赔标记:0=不需要,1=需要', }, /** 运方理赔金额(元) */ carrier_claim_amount: { type: DECIMAL(12, 2), allowNull: true, comment: '运方理赔金额,单位:人民币(元),carrier_claim_needed=1 时可填写', }, /** 运方理赔状态 */ carrier_claim_status: { type: STRING(20), allowNull: true, comment: '运方理赔状态:pending=待理赔, reviewing=审核中, approved=已通过, paid=已打款, monthly=月结, rejected=已拒绝', }, // ========== 单据关联字段(替代旧版多态关联) ========== /** 运单ID */ waybill_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '运单ID(关联 wms_order_express.id)', }, /** 运单号(冗余) */ waybill_no: { type: STRING(50), allowNull: true, comment: '运单号(冗余字段,方便查询展示)', }, /** 订单ID */ order_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '订单ID(关联 wms_order_delivery.id)', }, /** 入库单ID */ entry_order_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '入库单ID(关联 wms_entry_order.id)', }, /** 入库单号(冗余) */ entry_order_no: { type: STRING(50), allowNull: true, comment: '入库单号(冗余字段)', }, /** 出库单/退货出库单ID */ order_stockout_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '出库单/退货出库单ID(关联 wms_order_stockout.id)', }, /** 出库单号(冗余) */ order_stockout_no: { type: STRING(50), allowNull: true, comment: '出库单号(冗余字段)', }, /** 收货单ID */ receiving_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '收货单ID(关联 wms_receiving.id)', }, /** 收货单号(冗余) */ receiving_no: { type: STRING(50), allowNull: true, comment: '收货单号(冗余字段)', }, /** 创建时间 */ created_at: { type: DATE, allowNull: true, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, /** 更新时间 */ updated_at: { type: DATE, allowNull: true, comment: '更新时间', }, /** 软删除时间 */ deleted_at: { type: DATE, allowNull: true, comment: '软删除时间', }, }, { tableName: 'wms_after_sale', comment: '售后主表', timestamps: false, indexes: [ { name: 'uk_after_sale_no', unique: true, fields: [ 'after_sale_no' ], }, { name: 'idx_warehouse_status', fields: [ 'warehouse_id', 'status', 'deleted_at' ], }, { name: 'idx_warehouse_type', fields: [ 'warehouse_id', 'type', 'deleted_at' ], }, { name: 'idx_type_subtype', fields: [ 'warehouse_id', 'type', 'sub_type', 'deleted_at' ], }, { name: 'idx_store_id', fields: [ 'store_id', 'deleted_at' ], }, { name: 'idx_creator_id', fields: [ 'creator_id', 'deleted_at' ], }, { name: 'idx_role_group', fields: [ 'role_group', 'status', 'deleted_at' ], }, { name: 'idx_created_at', fields: [ 'warehouse_id', 'created_at' ], }, { name: 'idx_waybill_id', fields: [ 'waybill_id' ], }, { name: 'idx_order_id', fields: [ 'order_id' ], }, { name: 'idx_entry_order_id', fields: [ 'entry_order_id' ], }, { name: 'idx_order_stockout_id', fields: [ 'order_stockout_id' ], }, { name: 'idx_receiving_id', fields: [ 'receiving_id' ], }, { name: 'idx_carrier_claim_status', fields: [ 'carrier_claim_status' ], }, { name: 'idx_inbox_message_id', fields: [ 'inbox_message_id' ], }, ], }); // ============================ // 关联关系 // ============================ AfterSale.associate = function() { const { AfterSale, AfterSaleDetail } = app.model; // 关联售后进度步骤(一对多) AfterSale.hasMany(AfterSaleDetail, { foreignKey: 'after_sale_id', as: 'details', }); }; // ============================ // 常量挂载到 Model // ============================ AfterSale.AFTER_SALE_TYPES = AFTER_SALE_TYPES; AfterSale.AFTER_SALE_ROLE_GROUPS = AFTER_SALE_ROLE_GROUPS; AfterSale.AFTER_SALE_STATUS = AFTER_SALE_STATUS; AfterSale.CLAIM_STATUS = CLAIM_STATUS; return AfterSale; };