oms/backend/app/model/orderDelivery.js
liufumingren 11e7b26ced feat(backend): 发货单模块与 WMS 定时推送队列
- 发货单主表/明细表(order_deliveries / order_delivery_items)控制器与模型
- 表头金额与明细派生字段重算(utils/recalcOrderDelivery.js)
- 状态流转:审核/驳回/取消/恢复/作废、批量审核、批量切换仓库
- 审核入队 + 定时推送:service/orderDeliveryPush.js(乐观锁 CAS 防重、失败重试上限 3 次、业务失败退回待审核)+ schedule/orderDeliveryPush.js
- 收件人 AI 智能识别(service/ai.js,DeepSeek)
- 相关迁移:建表、站内字段、明细表、订单类型默认值、快递字典、菜单种子、历史数据搬迁
2026-09-13 02:23:47 +08:00

134 lines
8.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 发货单(订单)模型,对应 order_deliveries 表(独立于出库单 outbound_orders)
*
* 字段口径对齐 WMS `wms_order_delivery` 与奇门 `deliveryorder.create` 的
* request.deliveryOrder + receiverInfo(receiverInfo 在 WMS 侧扁平落主表,此处同样扁平):
* - delivery_order_code ↔ deliveryOrderCode(必填,去重键的一部分)
* - order_type ↔ orderType、operator_name ↔ operatorName
* - need_oms_waybill ↔ needOmsWaybill:OMS 走 WMS openapi,WMS 侧按通道自行写入创建渠道(API),OMS 不落该字段
* - createTime 不落库(WMS 显式忽略),本地用 created_at
* - sourceOrderCode 不单独落列,仅作 pay_no 缺省回退;多来源用 source_order_codes
* @param {Egg.Application} app egg application
* @return {object} orderDelivery model
*/
module.exports = app => {
const { BIGINT, STRING, TINYINT, DECIMAL, INTEGER, TEXT, JSON, ENUM, DATE } = app.Sequelize;
const OrderDelivery = app.model.define('order_delivery', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
// ---------- OMS 归属与键 ----------
principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID(货主,对应 WMS store_id)' },
// 历史数据来源:orders.id(仅搬迁数据有值,正常新建为 NULL)
legacy_order_id: { type: BIGINT.UNSIGNED, comment: '历史数据来源:orders.id(非搬迁数据为 NULL)' },
warehouse_id: { type: BIGINT.UNSIGNED, comment: '仓库ID(关联 warehouses,对应 WMS warehouse_id)' },
shop_id: { type: BIGINT.UNSIGNED, comment: '店铺ID(关联 shops)' },
warehouse_code: { type: STRING(50), comment: '仓库编码(报文 warehouseCode 快照)' },
// ---------- 主键 / 类型 / 标记 ----------
delivery_order_code: { type: STRING(50), allowNull: false, comment: '发货单号(必填,唯一去重键)' },
order_type: {
type: STRING(50),
defaultValue: 'JYCK',
comment: '订单类型(ORDER 组 WMS 编码):JYCK 一般交易出库单 / HHCK 换货出库 / BFCK 补发出库,默认 JYCK',
},
order_flag: { type: TEXT, comment: '订单标记' },
pre_delivery_order_code: { type: STRING(50), comment: '预发货单号' },
pre_delivery_order_id: { type: STRING(50), comment: '预发货单ID' },
is_urgency: { type: ENUM('Y', 'N'), defaultValue: 'N', comment: '是否紧急:Y是 N否(默认 N)' },
invoice_flag: { type: ENUM('Y', 'N'), defaultValue: 'N', comment: '是否需要发票:Y是 N否(默认 N)' },
// ---------- 平台 / 店铺 ----------
source_platform_code: { type: STRING(50), comment: '来源平台编码' },
source_platform_name: { type: STRING(200), comment: '来源平台名称' },
shop_nick: { type: STRING(200), comment: '店铺名称' },
seller_nick: { type: STRING(200), comment: '卖家名称' },
buyer_nick: { type: STRING(200), comment: '买家名称' },
// ---------- 留言 / 备注 ----------
buyer_message: { type: STRING(500), comment: '买家留言(手工单不填写)' },
seller_message: { type: STRING(500), comment: '卖家留言(手工单表单「留言」写入此字段)' },
// ---------- 时间 ----------
place_order_time: { type: DATE, comment: '店铺创建订单时间' },
pay_time: { type: DATE, comment: '支付时间' },
pay_no: { type: STRING(50), comment: '支付单号(缺省回退取 sourceOrderCode)' },
operate_time: { type: DATE, comment: 'ERP 审核时间' },
// ---------- 金额 ----------
total_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '订单总金额' },
item_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '商品总金额(Σ 明细 plan_qty × actual_price,明细增删改后自动重算)' },
discount_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '订单折扣金额' },
freight: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '快递费用' },
ar_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '应收金额' },
got_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '已收金额' },
// ---------- 快递 ----------
logistics_code: { type: STRING(50), comment: '物流公司编码(WMS 标准编码,如 SF/YTO)' },
logistics_name: { type: STRING(200), comment: '物流公司名称' },
express_code: { type: STRING(50), comment: '运单号(面单号)' },
logistics_area_code: { type: STRING(50), comment: '快递区域编码(大头笔信息)' },
// ---------- 操作人 / 其他 ----------
operator_name: { type: STRING(50), comment: 'ERP 审核人(奇门 operatorName)' },
oaid_order_source_code: { type: STRING(50), comment: 'OAID 订单来源编码' },
source_order_codes: { type: TEXT, comment: '来源订单号集合(对齐 WMS 列名 source_order_codes)' },
extend_props: { type: JSON, comment: '请求级扩展属性(如 ewaybill_order_code)' },
// ---------- 渠道 / 取号 ----------
need_oms_waybill: {
type: TINYINT,
allowNull: false,
defaultValue: 0,
comment: '是否需要回 OMS 取号:0不需要 1需要(OMS 走 WMS openapi,渠道由 WMS 侧写入 API,该开关即生效)',
},
// ---------- 收件人(奇门 receiverInfo 扁平落库,列名与 WMS 一致) ----------
name: { type: STRING(100), comment: '收件人姓名(receiverInfo.name)' },
tel: { type: STRING(50), comment: '收件人电话(receiverInfo.tel)' },
mobile: { type: STRING(50), comment: '收件人移动电话(receiverInfo.mobile)' },
email: { type: STRING(100), comment: '收件人邮箱(receiverInfo.email)' },
country_code: { type: STRING(50), comment: '国家编码(receiverInfo.countryCode)' },
province: { type: STRING(100), comment: '省份(receiverInfo.province)' },
city: { type: STRING(100), comment: '城市(receiverInfo.city)' },
area: { type: STRING(100), comment: '区县(receiverInfo.area)' },
town: { type: STRING(100), comment: '街道/镇(receiverInfo.town)' },
detail_address: { type: STRING(500), comment: '详细地址(receiverInfo.detailAddress)' },
zip_code: { type: STRING(20), comment: '邮编(receiverInfo.zipCode)' },
oaid: { type: STRING(100), comment: 'OAID(receiverInfo.oaid)' },
// ---------- OMS 站内字段:统计 / 状态流转 / 推送记录 ----------
item_total: { type: DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '商品总数(明细计划数量合计)' },
item_kind: { type: INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '商品种类数(不重复商品数)' },
status: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1待处理 2已审核 3已推送 4已完成 5已作废 9已取消' },
audit_by: { type: BIGINT.UNSIGNED, comment: '审核人ID(关联 users,待处理→已审核时写入)' },
audit_time: { type: DATE, comment: '审核时间' },
void_at: { type: DATE, comment: '作废时间(状态 5=已作废)' },
void_by: { type: BIGINT.UNSIGNED, comment: '作废人ID(关联 users)' },
void_reason: { type: STRING(255), comment: '作废原因' },
created_by: { type: BIGINT.UNSIGNED, comment: '创建人ID(关联 users)' },
pushed_at: { type: DATE, comment: 'WMS 推送时间' },
wms_push_result: { type: JSON, comment: 'WMS 推送结果(外部单号/返回码/消息/原始报文)' },
created_at: { type: DATE, comment: '创建时间(对应 WMS create_at,取服务器时间)' },
updated_at: { type: DATE, comment: '更新时间' },
}, {
tableName: 'order_deliveries',
comment: '发货单(订单)表',
});
OrderDelivery.associate = () => {
app.model.OrderDelivery.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' });
app.model.OrderDelivery.belongsTo(app.model.Shop, { foreignKey: 'shop_id', as: 'shop' });
app.model.OrderDelivery.belongsTo(app.model.Warehouse, { foreignKey: 'warehouse_id', as: 'warehouse' });
app.model.OrderDelivery.belongsTo(app.model.User, { foreignKey: 'created_by', as: 'creator' });
app.model.OrderDelivery.belongsTo(app.model.User, { foreignKey: 'audit_by', as: 'auditor' });
app.model.OrderDelivery.belongsTo(app.model.User, { foreignKey: 'void_by', as: 'voider' });
app.model.OrderDelivery.hasMany(app.model.OrderDeliveryItem, { foreignKey: 'order_delivery_id', as: 'items' });
};
return OrderDelivery;
};