feat: 两库对等物料管理及产品工号贯通全流程

- 新增 MES 本地物料表及双向同步接口,WMS 物料变更自动同步
- 全流程新增产品工号字段,贯通入库/库存/出库/检验环节
- 工单管理支持多工单混产,移除单工单互斥限制
- 新增批次号/SN 字段支持质量检验联动 WMS 库存处置
- 修复权限规则,仅内置管理员账号不允许修改删除
- 工位终端新增 SN 扫码自动切换所属工单功能
This commit is contained in:
SunYF
2026-09-29 14:55:03 +08:00
parent a229b6727a
commit 76c9b6a7e2
103 changed files with 9412 additions and 1726 deletions
+6 -4
View File
@@ -37,11 +37,13 @@ func CreateInspectionHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
httpx.FailHTTP(w, http.StatusForbidden, "无操作权限:"+code)
return
}
if err := logic.New(svcCtx).CreateInspection(r.Context(), req, operator(r, "")); err != nil {
msg, err := logic.New(svcCtx).CreateInspection(r.Context(), req, operator(r, ""))
if err != nil {
httpx.Fail(w, 2301, err.Error())
return
}
httpx.OkMessage(w, "巡检记录已提交", nil)
// message 放 data 返回:质量检验页需读取「库存联动失败/已联动」提示(含 WMS 原文原因)
httpx.Ok(w, map[string]any{"message": msg})
}
}
@@ -93,8 +95,8 @@ func UploadPhotoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
func InspectionDisposalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
Id int `json:"id"`
DisposalType string `json:"disposalType"`
Id int `json:"id"`
DisposalType string `json:"disposalType"`
DisposalRemark string `json:"disposalRemark"`
}
if err := httpx.ParseJSON(r, &req); err != nil || req.Id <= 0 {
@@ -5,19 +5,24 @@ import (
"strconv"
"bj_power_mes/common/httpx"
"bj_power_mes/ent"
"bj_power_mes/internal/logic"
"bj_power_mes/internal/svc"
)
// ListMaterialsHandler GET /api/v1/materials 物料档案(代理 WMS GET /api/internal/materials)。
// WMS 是物料主数据源(item_type=1原材料/2半成品/3成品/4其他),MES 管理端经 JWT 接口只读代理。
// ListMaterialsHandler GET /api/v1/materials 物料下拉(读 MES 本地 materials 副本,需求一 M3/M6)。
// 两库对等的核心收益:WMS 宕机时本接口照常可用。
// 响应 [{code,name,spec,unit,manageMode}],manageMode "1"=批次(结构件)/"2"=序列号(电气件),
// 与 WMS manage_mode 口径一致(参考 wmsclient.MaterialRow.ManageMode)。
// WMS 不可达/异常时返回空列表(前端自行降级),不报错。
// 与 WMS manage_mode 口径一致。本地为空(冷启动)时异步自 WMS 全量拉取补齐(M7),
// 并顺带异步重推 sync_pending 行(M3);均不阻塞响应、失败静默。
func ListMaterialsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logic.New(svcCtx).EnsureMaterialCacheAsync()
list := []map[string]string{}
_, rows, err := svcCtx.Wms.MaterialPage(r.Context(), "", "", "", 0, 0, 1, 1000)
rows, err := svcCtx.EntClient.Material.Query().
Order(ent.Desc("id")).
Limit(1000).
All(r.Context())
if err == nil {
for _, m := range rows {
list = append(list, map[string]string{
@@ -0,0 +1,28 @@
package production
import (
"net/http"
"bj_power_mes/common/httpx"
"bj_power_mes/internal/logic"
"bj_power_mes/internal/svc"
)
// UpsertMaterialInternalHandler POST/PUT /api/internal/materials
// WMS→MES 物料推送落点(需求一 M2,X-API-TOKEN 保护):按 code 幂等 upsert MES 本地物料副本,
// 成功即清 sync_pending。POST/PUT 同义(均按 code upsert);本接口只落本地、不回推 WMS(防同步环)。
func UpsertMaterialInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req logic.MaterialUpsertReq
if err := httpx.ParseJSON(r, &req); err != nil {
httpx.BadRequest(w, "请求体解析失败")
return
}
created, err := logic.New(svcCtx).UpsertLocalMaterial(r.Context(), req)
if err != nil {
httpx.Fail(w, 3120, err.Error())
return
}
httpx.Ok(w, map[string]any{"created": created})
}
}
@@ -7,6 +7,7 @@ import (
"strings"
"bj_power_mes/common/httpx"
"bj_power_mes/ent/workorder"
"bj_power_mes/internal/logic"
"bj_power_mes/internal/svc"
@@ -40,7 +41,7 @@ func ListProductTypesPageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
page := atoiDefault(q.Get("page"), 1)
pageSize := atoiDefault(q.Get("pageSize"), 20)
total, list, wmsOnline, err := logic.New(svcCtx).ListProductTypesPage(r.Context(),
q.Get("code"), q.Get("name"), q.Get("category"), q.Get("isActive"), page, pageSize)
q.Get("code"), q.Get("name"), q.Get("isActive"), page, pageSize)
if err != nil {
httpx.Fail(w, 3001, err.Error())
return
@@ -96,7 +97,7 @@ func ExportProductTypesHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
b, err := svcCtx.Wms.ExportProductTypes(r.Context(),
q.Get("code"), q.Get("name"), q.Get("category"), q.Get("isActive"), q.Get("startDate"), q.Get("endDate"))
q.Get("code"), q.Get("name"), q.Get("isActive"), q.Get("startDate"), q.Get("endDate"))
if err != nil {
httpx.Fail(w, 3002, "导出失败(WMS 不可达):"+err.Error())
return
@@ -164,6 +165,29 @@ func GetWorkOrderHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
}
// WorkOrderInternalHandler GET /api/internal/work-orders/:no
// 供 WMS 解析工单归属(A2 半成品入库归属继承产线工单):返回工单号、合同编号(contractNo)、工程编号(projectNo)。
// X-API-TOKEN 保护;工单不存在返回 code!=0(调用方按"找不到"静默处理,不阻断入库)。
func WorkOrderInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
no := pathvar.Vars(r)["no"]
if no == "" {
httpx.BadRequest(w, "工单号必填")
return
}
wo, err := svcCtx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNo(no)).First(r.Context())
if err != nil {
httpx.Fail(w, 3012, "工单不存在:"+no)
return
}
httpx.Ok(w, map[string]any{
"workOrderNo": wo.WorkOrderNo,
"contractNo": wo.ContractNo,
"projectNo": wo.ProjectNo,
})
}
}
// SetWorkOrderStatusHandler POST /work-orders/status(工单状态流转,可选原因)
func SetWorkOrderStatusHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
@@ -195,19 +219,36 @@ func DeleteWorkOrderHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
}
// AutoScheduleHandler POST /work-orders/auto-schedule 引导式自动排产(生成多条 PENDING 排产单)
func AutoScheduleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
// AutoSchedulePreviewHandler POST /work-orders/auto-schedule/preview 排产预览(不落库,逐日数量可调)
func AutoSchedulePreviewHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req logic.AutoScheduleReq
if err := httpx.ParseJSON(r, &req); err != nil {
httpx.BadRequest(w, "请求体解析失败")
return
}
if err := logic.New(svcCtx).AutoSchedule(r.Context(), req, operator(r, "")); err != nil {
preview, err := logic.New(svcCtx).AutoSchedulePreview(r.Context(), req)
if err != nil {
httpx.Fail(w, 3011, err.Error())
return
}
httpx.OkMessage(w, "自动排产已生成(待人工启用)", nil)
httpx.Ok(w, preview)
}
}
// AutoScheduleApplyHandler POST /work-orders/auto-schedule/apply 确认排产(Σ逐日数量==应排总数,总数据对得上)
func AutoScheduleApplyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req logic.AutoScheduleApplyReq
if err := httpx.ParseJSON(r, &req); err != nil {
httpx.BadRequest(w, "请求体解析失败")
return
}
if err := logic.New(svcCtx).AutoScheduleApply(r.Context(), req, operator(r, "")); err != nil {
httpx.Fail(w, 3011, err.Error())
return
}
httpx.OkMessage(w, "排产已确认(待人工启用)", nil)
}
}
@@ -30,6 +30,17 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/internal/order/query", Handler: internal(production.OrderQueryInternalHandler(serverCtx)),
})
// 工单归属解析(A2 半成品入库继承产线工单):凭工单号取合同号/工程编号
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/internal/work-orders/:no", Handler: internal(production.WorkOrderInternalHandler(serverCtx)),
})
// 物料两库对等(需求一 M2):WMS→MES 推送落点,按 code 幂等 upsert 本地副本
server.AddRoute(rest.Route{
Method: http.MethodPost, Path: "/api/internal/materials", Handler: internal(production.UpsertMaterialInternalHandler(serverCtx)),
})
server.AddRoute(rest.Route{
Method: http.MethodPut, Path: "/api/internal/materials", Handler: internal(production.UpsertMaterialInternalHandler(serverCtx)),
})
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/internal/dashboard/snapshot", Handler: internal(production.DashboardSnapshotInternalHandler(serverCtx)),
})
@@ -167,7 +178,9 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
{Method: http.MethodPost, Path: "/work-orders/status", Handler: production.SetWorkOrderStatusHandler(serverCtx)},
{Method: http.MethodGet, Path: "/work-orders/:id", Handler: production.GetWorkOrderHandler(serverCtx)},
{Method: http.MethodDelete, Path: "/work-orders/:id", Handler: production.DeleteWorkOrderHandler(serverCtx)},
{Method: http.MethodPost, Path: "/work-orders/auto-schedule", Handler: production.AutoScheduleHandler(serverCtx)},
// 排产预览/确认(M9:预览逐日可调、Σ==应排总数才能确认)
{Method: http.MethodPost, Path: "/work-orders/auto-schedule/preview", Handler: production.AutoSchedulePreviewHandler(serverCtx)},
{Method: http.MethodPost, Path: "/work-orders/auto-schedule/apply", Handler: production.AutoScheduleApplyHandler(serverCtx)},
{Method: http.MethodPost, Path: "/daily-plans", Handler: production.SaveDailyPlanHandler(serverCtx)},
{Method: http.MethodPut, Path: "/daily-plans", Handler: production.SaveDailyPlanHandler(serverCtx)},
{Method: http.MethodGet, Path: "/daily-plans", Handler: production.ListDailyPlansHandler(serverCtx)},
+3 -2
View File
@@ -93,11 +93,12 @@ func StationInspectionInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFu
httpx.BadRequest(w, "请求体解析失败")
return
}
if err := logic.New(svcCtx).CreateInspection(r.Context(), req, req.Operator); err != nil {
msg, err := logic.New(svcCtx).CreateInspection(r.Context(), req, req.Operator)
if err != nil {
httpx.Fail(w, 2301, err.Error())
return
}
httpx.OkMessage(w, "巡检记录已提交", nil)
httpx.OkMessage(w, msg, nil)
}
}