feat: 两库对等物料管理及产品工号贯通全流程
- 新增 MES 本地物料表及双向同步接口,WMS 物料变更自动同步 - 全流程新增产品工号字段,贯通入库/库存/出库/检验环节 - 工单管理支持多工单混产,移除单工单互斥限制 - 新增批次号/SN 字段支持质量检验联动 WMS 库存处置 - 修复权限规则,仅内置管理员账号不允许修改删除 - 工位终端新增 SN 扫码自动切换所属工单功能
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/material"
|
||||
"bj_power_mes/internal/wmsclient"
|
||||
)
|
||||
|
||||
// 物料两库对等双向同步(需求一 M1-M8,2026-09-29 定稿):
|
||||
// · MES 本地 materials 表 = 与 WMS materials 对等的副本,code(图号)为两库同步唯一业务键,
|
||||
// 两库均不物理删除(只停用,M5);
|
||||
// · MES 侧写入(M3):先落本地,再推 WMS /api/internal/materials/upsert,推失败置 sync_pending
|
||||
// 待重推(不回滚本地);列表请求顺带异步重推 pending 行;
|
||||
// · WMS 侧写入(M2/M4):经内部接口按 code 幂等 upsert 到本地(本包 UpsertLocalMaterial),
|
||||
// 只落本地、不回推 WMS(防同步环);
|
||||
// · 本地为空(冷启动)异步从 WMS 全量拉取补齐(M7);失败静默记日志,不阻塞用户;
|
||||
// · 型号/物料查询全部读本地(M6):WMS 宕机时 MES 建单/排产/下拉照常可用;
|
||||
// · 备料"物料在 WMS 档案存在"校验仍直查 WMS(M8)——备料要出库,库存真源在 WMS。
|
||||
|
||||
// MaterialUpsertReq 两库物料 upsert 载荷(WMS→MES 推送 / MES 拉 WMS 补齐共用),code 为同步唯一业务键。
|
||||
type MaterialUpsertReq struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Spec string `json:"spec"`
|
||||
Unit string `json:"unit"`
|
||||
Description string `json:"description"`
|
||||
ItemType int `json:"itemType"`
|
||||
ManageMode int `json:"manageMode"`
|
||||
TemplateCode string `json:"templateCode"`
|
||||
SafetyStock int `json:"safetyStock"`
|
||||
IsActive *bool `json:"isActive"`
|
||||
}
|
||||
|
||||
// UpsertLocalMaterial 按 code 幂等 upsert MES 本地物料副本。
|
||||
// WMS→MES 推送落点(M2):WMS 是该行内容的写入方,落库成功即视为两库一致,清 sync_pending。
|
||||
// 返回 true=新建 / false=更新;code 为必填业务键,不可通过本接口修改。
|
||||
func (s *Service) UpsertLocalMaterial(ctx context.Context, req MaterialUpsertReq) (bool, error) {
|
||||
if req.Code == "" {
|
||||
return false, errors.New("code(图号)为必填项")
|
||||
}
|
||||
active := true
|
||||
if req.IsActive != nil {
|
||||
active = *req.IsActive
|
||||
}
|
||||
existing, err := s.ctx.EntClient.Material.Query().Where(material.CodeEQ(req.Code)).Only(ctx)
|
||||
if err == nil {
|
||||
_, err = s.ctx.EntClient.Material.UpdateOneID(existing.ID).
|
||||
SetName(req.Name).
|
||||
SetSpec(req.Spec).
|
||||
SetUnit(req.Unit).
|
||||
SetDescription(req.Description).
|
||||
SetItemType(req.ItemType).
|
||||
SetManageMode(req.ManageMode).
|
||||
SetTemplateCode(req.TemplateCode).
|
||||
SetSafetyStock(req.SafetyStock).
|
||||
SetIsActive(active).
|
||||
SetSyncPending(false).
|
||||
Save(ctx)
|
||||
return false, err
|
||||
}
|
||||
if !ent.IsNotFound(err) {
|
||||
return false, err
|
||||
}
|
||||
_, err = s.ctx.EntClient.Material.Create().
|
||||
SetCode(req.Code).
|
||||
SetName(req.Name).
|
||||
SetSpec(req.Spec).
|
||||
SetUnit(req.Unit).
|
||||
SetDescription(req.Description).
|
||||
SetItemType(req.ItemType).
|
||||
SetManageMode(req.ManageMode).
|
||||
SetTemplateCode(req.TemplateCode).
|
||||
SetSafetyStock(req.SafetyStock).
|
||||
SetIsActive(active).
|
||||
SetSyncPending(false).
|
||||
Save(ctx)
|
||||
return err == nil, err
|
||||
}
|
||||
|
||||
// materialUpsertOf 本地物料行 → 推 WMS 载荷。
|
||||
func materialUpsertOf(m *ent.Material) wmsclient.MaterialUpsert {
|
||||
active := m.IsActive
|
||||
return wmsclient.MaterialUpsert{
|
||||
Code: m.Code,
|
||||
Name: m.Name,
|
||||
Spec: m.Spec,
|
||||
Unit: m.Unit,
|
||||
Description: m.Description,
|
||||
ItemType: m.ItemType,
|
||||
ManageMode: m.ManageMode,
|
||||
TemplateCode: m.TemplateCode,
|
||||
SafetyStock: m.SafetyStock,
|
||||
IsActive: &active,
|
||||
}
|
||||
}
|
||||
|
||||
// pushMaterialToWMS 推单行到 WMS(同步调用):成功清 sync_pending,失败置位等待重推(不 panic)。
|
||||
func (s *Service) pushMaterialToWMS(ctx context.Context, m *ent.Material) error {
|
||||
if err := s.ctx.Wms.UpsertMaterialToWMS(ctx, materialUpsertOf(m)); err != nil {
|
||||
_ = s.ctx.EntClient.Material.UpdateOneID(m.ID).SetSyncPending(true).Exec(ctx)
|
||||
return err
|
||||
}
|
||||
_ = s.ctx.EntClient.Material.UpdateOneID(m.ID).SetSyncPending(false).Exec(ctx)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RetryPendingMaterialPushes 重推全部 sync_pending 行(M3),返回重推成功的行数。
|
||||
func (s *Service) RetryPendingMaterialPushes(ctx context.Context) int {
|
||||
pending, err := s.ctx.EntClient.Material.Query().Where(material.SyncPendingEQ(true)).All(ctx)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
okCnt := 0
|
||||
for i := range pending {
|
||||
c, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
if s.pushMaterialToWMS(c, pending[i]) == nil {
|
||||
okCnt++
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
return okCnt
|
||||
}
|
||||
|
||||
// RetryPendingMaterialPushesAsync 异步重推(列表请求顺带触发,不阻塞响应,失败静默)。
|
||||
func (s *Service) RetryPendingMaterialPushesAsync() {
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
if n := s.RetryPendingMaterialPushes(ctx); n > 0 {
|
||||
log.Printf("[material-sync] 重推 WMS 成功 %d 行", n)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// PullMaterialsFromWMSAsync 从 WMS 全量分页拉取物料 upsert 到本地(M7 冷启动补齐)。
|
||||
// 仅在本地为空时调用——若本地已有 pending 行,说明本地存在未推 WMS 的改动,禁止被 WMS 旧值覆盖。
|
||||
// 失败静默记日志,不阻塞用户。
|
||||
func (s *Service) PullMaterialsFromWMSAsync() {
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
total := 0
|
||||
for page := 1; page <= 100; page++ {
|
||||
_, rows, err := s.ctx.Wms.MaterialPage(ctx, "", "", "", 0, 0, page, 200)
|
||||
if err != nil {
|
||||
log.Printf("[material-sync] 拉取 WMS 物料失败(第 %d 页): %v", page, err)
|
||||
return
|
||||
}
|
||||
for _, r := range rows {
|
||||
active := r.IsActive
|
||||
if _, err := s.UpsertLocalMaterial(ctx, MaterialUpsertReq{
|
||||
Code: r.Code, Name: r.Name, Spec: r.Spec, Unit: r.Unit, Description: r.Description,
|
||||
ItemType: r.ItemType, ManageMode: r.ManageMode, TemplateCode: r.TemplateCode,
|
||||
SafetyStock: r.SafetyStock, IsActive: &active,
|
||||
}); err != nil {
|
||||
log.Printf("[material-sync] 物料 %s 落库失败: %v", r.Code, err)
|
||||
continue
|
||||
}
|
||||
total++
|
||||
}
|
||||
if len(rows) < 200 {
|
||||
break
|
||||
}
|
||||
}
|
||||
log.Printf("[material-sync] 自 WMS 全量同步物料 %d 行", total)
|
||||
}()
|
||||
}
|
||||
|
||||
// EnsureMaterialCacheAsync 列表请求顺带的缓存维护(M3/M7):本地为空才异步全量拉取;重推 pending 行。
|
||||
// 均为异步,不阻塞响应。
|
||||
func (s *Service) EnsureMaterialCacheAsync() {
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
n, err := s.ctx.EntClient.Material.Query().Count(ctx)
|
||||
cancel()
|
||||
if err == nil && n == 0 {
|
||||
s.PullMaterialsFromWMSAsync()
|
||||
}
|
||||
}()
|
||||
s.RetryPendingMaterialPushesAsync()
|
||||
}
|
||||
Reference in New Issue
Block a user