feat&refactor: 2026-09-22 半成品业务模型重构与功能完善
- 重构半成品流转逻辑:进度权威源改为 workpiece.currentStationNo,新增 completedText 快照字段,废除 doneProcessCodes - 清理物料品类中半成品类型,存量数据幂等清理,调整物料管理方式文案为批次/序列号 - 新增日排产状态专用更新接口,修复工单工艺校验逻辑 - 新增物料档案代理接口、深路径文件下载接口 - 优化前端界面文案与工位选择逻辑,补充追溯页半成品流转展示 - 调整WMS端物料品类校验与入库接口契约 - 新增/更新数据库表结构与实体类代码
This commit is contained in:
@@ -199,6 +199,10 @@ var schemaPatchSQL = []string{
|
||||
`ALTER TABLE workpiece_process ADD COLUMN IF NOT EXISTS flow_id integer NOT NULL DEFAULT 0`,
|
||||
`ALTER TABLE workpiece_bind ADD COLUMN IF NOT EXISTS flow_id integer NOT NULL DEFAULT 0`,
|
||||
`ALTER TABLE material_request ADD COLUMN IF NOT EXISTS flow_id integer NOT NULL DEFAULT 0`,
|
||||
|
||||
// ---- 半成品流转新模型(2026-09-22 定稿):进度唯一权威源=workpiece.currentStationNo ----
|
||||
// semi_flow 补进度快照列 completed_text("已完成至工位N");旧 done_process_codes 保留历史数据、不再写入
|
||||
`ALTER TABLE semi_flow ADD COLUMN IF NOT EXISTS completed_text varchar(64) NOT NULL DEFAULT ''`,
|
||||
}
|
||||
|
||||
// EnsureSchema ent 建表 + 幂等列补丁(只增,不删数据)
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package production
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"bj_power_mes/common/httpx"
|
||||
"bj_power_mes/internal/logic"
|
||||
"bj_power_mes/internal/svc"
|
||||
)
|
||||
|
||||
// DailyPlanStatusHandler POST /daily-plans/:id/status 日排产启用/停用专用状态接口。
|
||||
// body: {"status":"PENDING"|"CONFIRMED"};只更新 status 字段(启用前校验工单已下发),
|
||||
// 前端此前误用 PUT /daily-plans/{id} 会 405,且全量保存会覆盖分产量。
|
||||
func DailyPlanStatusHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
if err := httpx.ParseJSON(r, &req); err != nil {
|
||||
httpx.BadRequest(w, "请求体解析失败")
|
||||
return
|
||||
}
|
||||
id := pathID(r)
|
||||
if err := logic.New(svcCtx).SetDailyPlanStatus(r.Context(), id, req.Status, operator(r, "")); err != nil {
|
||||
httpx.Fail(w, 3012, err.Error())
|
||||
return
|
||||
}
|
||||
httpx.OkMessage(w, "操作成功", nil)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package production
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"bj_power_mes/common/httpx"
|
||||
"bj_power_mes/internal/svc"
|
||||
)
|
||||
|
||||
// ListMaterialsHandler GET /api/v1/materials 物料档案(代理 WMS GET /api/internal/materials)。
|
||||
// WMS 是物料主数据源(item_type=1原材料/2半成品/3成品/4其他),MES 管理端经 JWT 接口只读代理。
|
||||
// 响应 [{code,name,spec,unit,manageMode}],manageMode "1"=批次(结构件)/"2"=序列号(电气件),
|
||||
// 与 WMS manage_mode 口径一致(参考 wmsclient.MaterialRow.ManageMode)。
|
||||
// WMS 不可达/异常时返回空列表(前端自行降级),不报错。
|
||||
func ListMaterialsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
list := []map[string]string{}
|
||||
_, rows, err := svcCtx.Wms.MaterialPage(r.Context(), "", "", "", 0, 0, 1, 1000)
|
||||
if err == nil {
|
||||
for _, m := range rows {
|
||||
list = append(list, map[string]string{
|
||||
"code": m.Code,
|
||||
"name": m.Name,
|
||||
"spec": m.Spec,
|
||||
"unit": m.Unit,
|
||||
"manageMode": strconv.Itoa(m.ManageMode),
|
||||
})
|
||||
}
|
||||
}
|
||||
httpx.Ok(w, list)
|
||||
}
|
||||
}
|
||||
@@ -128,11 +128,12 @@ func OnlineWorkpieceInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc
|
||||
httpx.BadRequest(w, "请求体解析失败")
|
||||
return
|
||||
}
|
||||
if err := logic.New(svcCtx).OnlineWorkpiece(r.Context(), req, operator(r, r.URL.Query().Get("operator"))); err != nil {
|
||||
if msg, err := logic.New(svcCtx).OnlineWorkpiece(r.Context(), req, operator(r, r.URL.Query().Get("operator"))); err != nil {
|
||||
httpx.Fail(w, 2007, err.Error())
|
||||
return
|
||||
} else {
|
||||
httpx.OkMessage(w, msg, nil)
|
||||
}
|
||||
httpx.OkMessage(w, "进线登记成功", nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,11 +23,12 @@ func OnlineWorkpieceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
httpx.BadRequest(w, "请求体解析失败")
|
||||
return
|
||||
}
|
||||
if err := logic.New(svcCtx).OnlineWorkpiece(r.Context(), req, operator(r, "")); err != nil {
|
||||
if msg, err := logic.New(svcCtx).OnlineWorkpiece(r.Context(), req, operator(r, "")); err != nil {
|
||||
httpx.Fail(w, 3301, err.Error())
|
||||
return
|
||||
} else {
|
||||
httpx.OkMessage(w, msg, nil)
|
||||
}
|
||||
httpx.OkMessage(w, "进线登记成功", nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -172,6 +172,9 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
|
||||
{Method: http.MethodPut, Path: "/daily-plans", Handler: production.SaveDailyPlanHandler(serverCtx)},
|
||||
{Method: http.MethodGet, Path: "/daily-plans", Handler: production.ListDailyPlansHandler(serverCtx)},
|
||||
{Method: http.MethodDelete, Path: "/daily-plans/:id", Handler: production.DeleteDailyPlanHandler(serverCtx)},
|
||||
// 日排产启用/停用专用状态接口(body {"status":"PENDING"|"CONFIRMED"}):
|
||||
// 只改 status 列,避免前端此前误用 PUT /daily-plans/{id}(405)或全量保存覆盖分产量
|
||||
{Method: http.MethodPost, Path: "/daily-plans/:id/status", Handler: production.DailyPlanStatusHandler(serverCtx)},
|
||||
{Method: http.MethodGet, Path: "/daily-plans/suggest-docks", Handler: production.SuggestedDockCodesHandler(serverCtx)},
|
||||
|
||||
{Method: http.MethodPut, Path: "/bom", Handler: production.SaveBomHandler(serverCtx)},
|
||||
@@ -182,6 +185,8 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
|
||||
|
||||
{Method: http.MethodPost, Path: "/material-requests/generate", Handler: production.GenerateMaterialHandler(serverCtx)},
|
||||
{Method: http.MethodGet, Path: "/material-requests", Handler: production.ListMaterialRequestsHandler(serverCtx)},
|
||||
// 物料档案(代理 WMS /api/internal/materials,WMS 是物料主数据源;WMS 不可达返回空列表)
|
||||
{Method: http.MethodGet, Path: "/materials", Handler: production.ListMaterialsHandler(serverCtx)},
|
||||
{Method: http.MethodPost, Path: "/material-requests/receive", Handler: production.ReceiveMaterialRequestHandler(serverCtx)},
|
||||
{Method: http.MethodPost, Path: "/material-requests/auto-outbound", Handler: production.AutoOutboundHandler(serverCtx)},
|
||||
// 配送进度(只读聚合:WMS 台账四段量 + MES 已接料 → 在途/线边)
|
||||
@@ -239,6 +244,9 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
|
||||
{Method: http.MethodPost, Path: "/process-flows/status", Handler: SetFlowStatusHandler(serverCtx)},
|
||||
{Method: http.MethodDelete, Path: "/process-flows/:id", Handler: DeleteProcessFlowHandler(serverCtx)},
|
||||
{Method: http.MethodPost, Path: "/process-flows/upload", Handler: UploadPdfHandler(serverCtx)},
|
||||
// 统一存储深路径文件 URL(年/月/日/文件类型/uuid.ext,如 /api/v1/files/2026/09/22/PROCESS_PDF/xxx.pdf),
|
||||
// 上传接口返回的 url 即此格式,未注册前直接访问 404
|
||||
{Method: http.MethodGet, Path: "/files/:y/:m/:d/:t/:f", Handler: FileDeepPathHandler(serverCtx)},
|
||||
{Method: http.MethodGet, Path: "/stations", Handler: ListStationsHandler(serverCtx)},
|
||||
{Method: http.MethodPost, Path: "/stations", Handler: SaveStationHandler(serverCtx)},
|
||||
{Method: http.MethodDelete, Path: "/stations/:id", Handler: DeleteStationHandler(serverCtx)},
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -118,7 +118,7 @@ func StationTaskInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// StationReportInternalHandler 工位终端工序报工(写工序实绩 + 步骤考核)
|
||||
// StationReportInternalHandler 工位终端工艺报工(写工位实绩 + 步骤考核)
|
||||
func StationReportInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req logic.ReportProcessReq
|
||||
@@ -159,15 +159,14 @@ func FileDownloadInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
|
||||
// StationCheckinInternalHandler 工位终端暂存退库(半成品入库,块4/5)
|
||||
// 记录已完成工序后按半成品 INBOUND 流转,并通知 WMS 入库。
|
||||
// 进度由 MES 服务端自取(workpiece.currentStationNo),按半成品 INBOUND 流转并通知 WMS 入库。
|
||||
func StationCheckinInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Sn string `json:"sn"`
|
||||
OrderNo string `json:"orderNo"`
|
||||
StationNo int `json:"stationNo"`
|
||||
ProcessCode int `json:"processCode"`
|
||||
Operator string `json:"operator"`
|
||||
Sn string `json:"sn"`
|
||||
OrderNo string `json:"orderNo"`
|
||||
StationNo int `json:"stationNo"`
|
||||
Operator string `json:"operator"`
|
||||
}
|
||||
if err := httpx.ParseJSON(r, &req); err != nil {
|
||||
httpx.BadRequest(w, "请求体解析失败")
|
||||
@@ -177,19 +176,14 @@ func StationCheckinInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc
|
||||
httpx.BadRequest(w, "缺少 sn")
|
||||
return
|
||||
}
|
||||
doneCodes := []int{}
|
||||
if req.ProcessCode > 0 {
|
||||
doneCodes = append(doneCodes, req.ProcessCode)
|
||||
}
|
||||
op := req.Operator
|
||||
if op == "" {
|
||||
op = operator(r, "")
|
||||
}
|
||||
if err := logic.New(svcCtx).SemiFlow(r.Context(), logic.SemiReq{
|
||||
Sn: req.Sn,
|
||||
OrderNo: req.OrderNo,
|
||||
DoneProcessCodes: doneCodes,
|
||||
Action: "INBOUND",
|
||||
Sn: req.Sn,
|
||||
OrderNo: req.OrderNo,
|
||||
Action: "INBOUND",
|
||||
}, op); err != nil {
|
||||
httpx.Fail(w, 2106, err.Error())
|
||||
return
|
||||
@@ -198,7 +192,6 @@ func StationCheckinInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// PerformanceExportHandler GET /performance/export 绩效报表导出(一个 xlsx 三个 sheet:按人/按工位/明细)
|
||||
// 执行全项目统一导出时间硬规则:起止必填(默认近3个月)、间隔≤1年
|
||||
func PerformanceExportHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
@@ -260,7 +253,6 @@ func PerformanceExportHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// normalizeExportRange MES 侧导出时间硬规则(与 WMS 同一套):
|
||||
// ① 起止都空 → 默认近 3 个月;② 只传一个 → 报错;③ 间隔 > 1 年 → 报错;④ 结束早于起始 → 报错。
|
||||
func normalizeExportRange(startDate, endDate string) (string, string, error) {
|
||||
|
||||
@@ -261,6 +261,9 @@ func serveUploadFile(w http.ResponseWriter, r *http.Request, dir, name string) {
|
||||
ct = "application/vnd.ms-excel"
|
||||
}
|
||||
w.Header().Set("Content-Type", ct)
|
||||
// 统一强制下载(Content-Disposition: attachment):文件名按 RFC 5987 编码(中文安全),
|
||||
// 浏览器直接下载不预览;<img>/<iframe> 内嵌展示不受此头影响
|
||||
w.Header().Set("Content-Disposition", "attachment; filename*=UTF-8''"+url.PathEscape(filepath.Base(abs)))
|
||||
http.ServeContent(w, r, filepath.Base(abs), time.Time{}, f)
|
||||
}
|
||||
|
||||
|
||||
@@ -328,6 +328,45 @@ func (s *Service) DeleteDailyPlan(ctx context.Context, id int) error {
|
||||
return s.ctx.EntClient.DailyPlan.DeleteOneID(id).Exec(ctx)
|
||||
}
|
||||
|
||||
// SetDailyPlanStatus 日排产启用/停用(人工专用入口,POST /daily-plans/:id/status):
|
||||
// 仅允许 PENDING(待执行)↔CONFIRMED(已启用) 之间切换;执行中/已完成/已取消由系统联动维护,无人工入口。
|
||||
// 启用(→CONFIRMED)前校验所属工单(按排产 orderNo 查)状态必须为 RELEASED 或 IN_PROGRESS。
|
||||
// 只 UPDATE status 字段,绝不触碰计划数量/分产量等其它列。
|
||||
func (s *Service) SetDailyPlanStatus(ctx context.Context, id int, target, operator string) error {
|
||||
if id <= 0 {
|
||||
return errors.New("缺少排产 id")
|
||||
}
|
||||
if target != "PENDING" && target != "CONFIRMED" {
|
||||
return errors.New("仅支持 启用/停用 操作")
|
||||
}
|
||||
p, err := s.ctx.EntClient.DailyPlan.Get(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New("日排产不存在")
|
||||
}
|
||||
if p.Status != "PENDING" && p.Status != "CONFIRMED" {
|
||||
return errors.New("仅支持 启用/停用 操作")
|
||||
}
|
||||
if p.Status == target {
|
||||
return nil // 幂等:状态相同直接成功
|
||||
}
|
||||
if target == "CONFIRMED" {
|
||||
wo, err := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNo(p.OrderNo)).First(ctx)
|
||||
if err != nil {
|
||||
return errors.New("工单不存在")
|
||||
}
|
||||
if wo.Status != "RELEASED" && wo.Status != "IN_PROGRESS" {
|
||||
return errors.New("工单未下发,不能启用排产")
|
||||
}
|
||||
}
|
||||
if err := s.ctx.EntClient.DailyPlan.UpdateOneID(id).SetStatus(target).Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "daily.plan.status", p.OrderNo, operator, "daily_plan", p.OrderNo,
|
||||
"排产启用/停用", map[string]any{"id": id, "from": p.Status, "to": target})
|
||||
s.notifyDashboard()
|
||||
return nil
|
||||
}
|
||||
|
||||
// ---------- 自动排产(引导式) ----------
|
||||
|
||||
// DateSegment 日期区间 [start, end](含两端),yyyy-MM-dd
|
||||
|
||||
@@ -83,7 +83,7 @@ func StationFlowMapOfWO(items []FlowItem) map[int]int {
|
||||
}
|
||||
|
||||
// ValidateFlowCombination 保存工单时校验工艺组合:
|
||||
// 非空、工艺存在且启用(ACTIVE)、工位存在、工位号不重复;返回按工位号升序的条目。
|
||||
// 非空、工艺存在且启用(ACTIVE)、工位存在且 ENABLED 且当前绑定工艺与工单一致、工位号不重复;返回按工位号升序的条目。
|
||||
func (s *Service) ValidateFlowCombination(ctx context.Context, raw []map[string]int) ([]FlowItem, error) {
|
||||
items := flowMapsToItems(raw)
|
||||
if len(items) == 0 {
|
||||
@@ -103,13 +103,15 @@ func (s *Service) ValidateFlowCombination(ctx context.Context, raw []map[string]
|
||||
if flow.Status != "ACTIVE" {
|
||||
return nil, fmt.Errorf("工艺「%s」已停用", flow.Name)
|
||||
}
|
||||
exist, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(it.StationNo)).Exist(ctx)
|
||||
exist, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(it.StationNo)).First(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exist {
|
||||
return nil, fmt.Errorf("工位 %d 不存在", it.StationNo)
|
||||
}
|
||||
// 工位当前绑定工艺必须与工单一致且工位启用(与排产校验 ValidateWOCombinationForSchedule 同口径同文案),
|
||||
// 否则工单下发后工位被改绑/停用会导致路线漂移
|
||||
if exist.FlowId != it.FlowId || exist.Status != "ENABLED" {
|
||||
return nil, fmt.Errorf("工位%d当前工艺与工单不匹配", it.StationNo)
|
||||
}
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
@@ -6,17 +6,19 @@ import (
|
||||
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/semiflow"
|
||||
"bj_power_mes/ent/workpiece"
|
||||
)
|
||||
|
||||
type SemiReq struct {
|
||||
Sn string `json:"sn"`
|
||||
OrderNo string `json:"orderNo"`
|
||||
DoneProcessCodes []int `json:"doneProcessCodes"`
|
||||
Action string `json:"action"` // INBOUND / OUTBOUND
|
||||
TargetDock string `json:"targetDock"`
|
||||
Sn string `json:"sn"`
|
||||
OrderNo string `json:"orderNo"`
|
||||
Action string `json:"action"` // INBOUND / OUTBOUND
|
||||
TargetDock string `json:"targetDock"`
|
||||
}
|
||||
|
||||
// SemiFlow 半成品流转:记录已完成工序,并调用 WMS /api/internal/semi/*
|
||||
// SemiFlow 半成品流转:进度唯一权威源=workpiece.currentStationNo(最后报工工位号),
|
||||
// 服务端按 sn 自取进度生成 completedText("已完成至工位N";未报工传空串),
|
||||
// 并调用 WMS /api/internal/semi/*(payload={sn,orderNo,completedText,operator},旧 doneProcessCodes 已废除)。
|
||||
func (s *Service) SemiFlow(ctx context.Context, req SemiReq, operator string) error {
|
||||
if req.Sn == "" {
|
||||
return errors.New("sn 不能为空")
|
||||
@@ -25,8 +27,17 @@ func (s *Service) SemiFlow(ctx context.Context, req SemiReq, operator string) er
|
||||
if action == "" {
|
||||
action = "INBOUND"
|
||||
}
|
||||
_, err := s.ctx.EntClient.SemiFlow.Create().
|
||||
SetSn(req.Sn).SetOrderNo(req.OrderNo).SetDoneProcessCodes(req.DoneProcessCodes).
|
||||
// 服务端自取进度:工件未上线(无 workpiece 记录)一律拒绝流转
|
||||
wp, err := s.ctx.EntClient.Workpiece.Query().Where(workpiece.Sn(req.Sn)).First(ctx)
|
||||
if err != nil {
|
||||
return errors.New("工件未上线,无法暂存退库")
|
||||
}
|
||||
completedText := ""
|
||||
if wp.CurrentStationNo > 0 {
|
||||
completedText = "已完成至工位" + itoa(wp.CurrentStationNo)
|
||||
}
|
||||
_, err = s.ctx.EntClient.SemiFlow.Create().
|
||||
SetSn(req.Sn).SetOrderNo(req.OrderNo).SetCompletedText(completedText).
|
||||
SetAction(action).SetTargetDock(req.TargetDock).SetOperator(operator).Save(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -35,9 +46,9 @@ func (s *Service) SemiFlow(ctx context.Context, req SemiReq, operator string) er
|
||||
// 不再静默吞掉——WMS 不可达或校验失败时必须让调用方感知,才能形成闭环)。
|
||||
var wmsErr error
|
||||
if action == "INBOUND" {
|
||||
wmsErr = s.ctx.Wms.SemiInbound(ctx, req.Sn, req.OrderNo, req.DoneProcessCodes, operator)
|
||||
wmsErr = s.ctx.Wms.SemiInbound(ctx, req.Sn, req.OrderNo, completedText, operator)
|
||||
} else {
|
||||
wmsErr = s.ctx.Wms.SemiOutbound(ctx, req.Sn, req.OrderNo, req.DoneProcessCodes, operator)
|
||||
wmsErr = s.ctx.Wms.SemiOutbound(ctx, req.Sn, req.OrderNo, completedText, operator)
|
||||
}
|
||||
if wmsErr != nil {
|
||||
s.ctx.EventLog.Write(ctx, "semi.flow.wms_error", req.OrderNo, operator, "semi_flow", req.Sn,
|
||||
@@ -45,7 +56,7 @@ func (s *Service) SemiFlow(ctx context.Context, req SemiReq, operator string) er
|
||||
return wmsErr
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "semi.flow", req.OrderNo, operator, "semi_flow", req.Sn, "半成品流转",
|
||||
map[string]any{"action": action, "doneProcessCodes": req.DoneProcessCodes})
|
||||
map[string]any{"action": action, "completedText": completedText})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -64,4 +75,4 @@ func (s *Service) ListSemiFlows(ctx context.Context, sn, orderNo string) ([]*ent
|
||||
// ListEventLogs 操作日志查询(按工单号/操作人;operators 为候选列表,任一命中即算)
|
||||
func (s *Service) ListEventLogs(ctx context.Context, orderNo string, operators []string, eventType string, fromMs, toMs int64, page, pageSize int) ([]*ent.EventLog, int, error) {
|
||||
return s.ctx.EventLog.Query(ctx, eventType, orderNo, operators, fromMs, toMs, page, pageSize)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/associationtrace"
|
||||
"bj_power_mes/ent/processstep"
|
||||
"bj_power_mes/ent/semiflow"
|
||||
"bj_power_mes/ent/station"
|
||||
"bj_power_mes/ent/stepcriterion"
|
||||
"bj_power_mes/ent/stepdata"
|
||||
@@ -66,25 +67,86 @@ type OnlineReq struct {
|
||||
WorkOrderId int `json:"workOrderId"`
|
||||
}
|
||||
|
||||
// OnlineWorkpiece 工件进线登记
|
||||
// 执行路线 = 工单工艺组合(唯一路线源头),工件只需按组合逐工位报工推进
|
||||
func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator string) error {
|
||||
// OnlineWorkpiece 工件进线登记(返回提示文案,供响应透出)。
|
||||
// 执行路线 = 工单工艺组合(唯一路线源头),工件只需按组合逐工位报工推进。
|
||||
// 重上线识别:workpiece 已存在且未完工 → 先调 SemiFlow(OUTBOUND)(WMS 半成品出库,
|
||||
// 失败报错不继续、文案含 WMS 原文),再按工单工艺组合计算下一站并提示"继续执行:工位N·工艺名";
|
||||
// 新件路径不变。
|
||||
func (s *Service) OnlineWorkpiece(ctx context.Context, req OnlineReq, operator string) (string, error) {
|
||||
if req.Sn == "" {
|
||||
return errors.New("sn 不能为空")
|
||||
return "", errors.New("sn 不能为空")
|
||||
}
|
||||
exist, err := s.ctx.EntClient.Workpiece.Query().Where(workpiece.Sn(req.Sn)).First(ctx)
|
||||
if err != nil && !ent.IsNotFound(err) {
|
||||
return "", err
|
||||
}
|
||||
if exist != nil {
|
||||
if exist.Status == "DONE" || exist.Status == "SCRAPPED" {
|
||||
return "", errors.New("工件已完工/报废,不可重复上线登记")
|
||||
}
|
||||
return s.reonlineWorkpiece(ctx, exist, operator)
|
||||
}
|
||||
if req.WorkOrderId == 0 {
|
||||
if wo, err := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNo(req.OrderNo)).First(ctx); err == nil {
|
||||
req.WorkOrderId = wo.ID
|
||||
}
|
||||
}
|
||||
err := s.ctx.EntClient.Workpiece.Create().
|
||||
if err := s.ctx.EntClient.Workpiece.Create().
|
||||
SetSn(req.Sn).SetOrderNo(req.OrderNo).SetWorkOrderId(req.WorkOrderId).
|
||||
SetStatus("ONLINE").SetOnlineAt(time.Now()).Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
SetStatus("ONLINE").SetOnlineAt(time.Now()).Exec(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "workpiece.online", req.OrderNo, operator, "workpiece", req.Sn, "工件进线登记", nil)
|
||||
s.notifyDashboard()
|
||||
return "进线登记成功", nil
|
||||
}
|
||||
|
||||
// reonlineWorkpiece 重上线(暂存退库后再次进线):
|
||||
// ① 先校验:工单组合非空且存在下一站(stationNo > currentStationNo),无副作用不通过即拒绝;
|
||||
// ② 再调 SemiFlow(OUTBOUND) 让 WMS 半成品出库,失败则报错不继续(文案含 WMS 原文)——
|
||||
//
|
||||
// 顺序保证:校验全部通过后才产生出库这一副作用,避免"已出库却登记失败"的悬挂状态;
|
||||
//
|
||||
// ③ 下一站工位当天停单时提示"工位N已停止接单"(复用 GetStationPaused 既有停单判定,不阻断登记)。
|
||||
func (s *Service) reonlineWorkpiece(ctx context.Context, wp *ent.Workpiece, operator string) (string, error) {
|
||||
items, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, s.workpieceWorkOrder(ctx, wp))
|
||||
if len(items) == 0 {
|
||||
return "", errors.New("工单未配置工艺组合")
|
||||
}
|
||||
next, nextFlow := 0, 0
|
||||
for _, it := range items {
|
||||
if it.StationNo > wp.CurrentStationNo && (next == 0 || it.StationNo < next) {
|
||||
next, nextFlow = it.StationNo, it.FlowId
|
||||
}
|
||||
}
|
||||
if next == 0 {
|
||||
return "", fmt.Errorf("工件已报工至工位%d,工艺组合中无下一站", wp.CurrentStationNo)
|
||||
}
|
||||
if err := s.SemiFlow(ctx, SemiReq{Sn: wp.Sn, OrderNo: wp.OrderNo, Action: "OUTBOUND"}, operator); err != nil {
|
||||
return "", fmt.Errorf("重上线失败:%s", err.Error())
|
||||
}
|
||||
msg := fmt.Sprintf("重上线成功,继续执行:工位%d·%s", next, s.flowNameById(ctx, nextFlow))
|
||||
if s.GetStationPaused(ctx, next) {
|
||||
msg += "(注意:工位" + itoa(next) + "已停止接单)"
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "workpiece.reonline", wp.OrderNo, operator, "workpiece", wp.Sn,
|
||||
"工件重上线(暂存退库后出库继续)", map[string]any{"nextStationNo": next, "nextFlowId": nextFlow})
|
||||
s.notifyDashboard()
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
// workpieceWorkOrder 取工件所属工单:优先 workOrderId,缺失按 orderNo 兜底解析
|
||||
func (s *Service) workpieceWorkOrder(ctx context.Context, wp *ent.Workpiece) *ent.WorkOrder {
|
||||
if wp.WorkOrderId > 0 {
|
||||
if wo, err := s.ctx.EntClient.WorkOrder.Get(ctx, wp.WorkOrderId); err == nil {
|
||||
return wo
|
||||
}
|
||||
}
|
||||
if wp.OrderNo != "" {
|
||||
if wo, err := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNo(wp.OrderNo)).First(ctx); err == nil {
|
||||
return wo
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -457,6 +519,9 @@ func (s *Service) Trace(ctx context.Context, sn string) (map[string]any, error)
|
||||
torque, _ := s.ctx.EntClient.TorqueRecord.Query().
|
||||
Where(torquerecord.Sn(sn)).Order(ent.Desc(torquerecord.FieldTime)).Limit(200).All(ctx)
|
||||
bindVO, _ := s.BuildBindTrace(ctx, sn)
|
||||
// 半成品流转记录(IN=暂存退库 / OUT=重上线),追溯页展示完成至工位快照
|
||||
semiFlows, _ := s.ctx.EntClient.SemiFlow.Query().
|
||||
Where(semiflow.Sn(sn)).Order(ent.Desc(semiflow.FieldID)).All(ctx)
|
||||
|
||||
return map[string]any{
|
||||
"workpiece": wp,
|
||||
@@ -465,6 +530,7 @@ func (s *Service) Trace(ctx context.Context, sn string) (map[string]any, error)
|
||||
"associationTrace": assoc,
|
||||
"torqueRecords": torque,
|
||||
"bindTrace": bindVO,
|
||||
"semiFlows": semiFlows,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package wmsclient
|
||||
package wmsclient
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -31,20 +31,56 @@ func New(baseURL, token string) *Client {
|
||||
}
|
||||
}
|
||||
|
||||
// SemiInbound 调 WMS /api/internal/semi/inbound 半成品入库
|
||||
func (c *Client) SemiInbound(ctx context.Context, sn, orderNo string, doneProcessCodes []int, operator string) error {
|
||||
return c.post(ctx, "/api/internal/semi/inbound", map[string]any{
|
||||
"sn": sn, "orderNo": orderNo, "doneProcessCodes": doneProcessCodes, "operator": operator,
|
||||
// SemiInbound 调 WMS /api/internal/semi/inbound 半成品入库。
|
||||
// 统一契约:payload={sn,orderNo,completedText,operator},completedText 由 MES 生成
|
||||
// ("已完成至工位N",N=workpiece.currentStationNo;0/无记录传空串),旧 doneProcessCodes 已废除。
|
||||
func (c *Client) SemiInbound(ctx context.Context, sn, orderNo, completedText, operator string) error {
|
||||
return c.postSemi(ctx, "/api/internal/semi/inbound", map[string]any{
|
||||
"sn": sn, "orderNo": orderNo, "completedText": completedText, "operator": operator,
|
||||
})
|
||||
}
|
||||
|
||||
// SemiOutbound 调 WMS /api/internal/semi/outbound 半成品出库(重上线)
|
||||
func (c *Client) SemiOutbound(ctx context.Context, sn, orderNo string, doneProcessCodes []int, operator string) error {
|
||||
return c.post(ctx, "/api/internal/semi/outbound", map[string]any{
|
||||
"sn": sn, "orderNo": orderNo, "doneProcessCodes": doneProcessCodes, "operator": operator,
|
||||
// SemiOutbound 调 WMS /api/internal/semi/outbound 半成品出库(重上线),契约同 SemiInbound。
|
||||
func (c *Client) SemiOutbound(ctx context.Context, sn, orderNo, completedText, operator string) error {
|
||||
return c.postSemi(ctx, "/api/internal/semi/outbound", map[string]any{
|
||||
"sn": sn, "orderNo": orderNo, "completedText": completedText, "operator": operator,
|
||||
})
|
||||
}
|
||||
|
||||
// postSemi 调 WMS 半成品接口并解析业务响应 {code,message}:
|
||||
// 网络/超时、非2xx、业务码非0 均返回 error,且优先携带 WMS 原文 message,
|
||||
// 供上线登记重上线路径直接透出(WMS 出库失败必须阻断并让操作工看到原因)。
|
||||
func (c *Client) postSemi(ctx context.Context, path string, body any) error {
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+path, bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-API-TOKEN", c.token)
|
||||
resp, err := c.hc.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
var br struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
_ = json.Unmarshal(raw, &br)
|
||||
if resp.StatusCode >= 300 || br.Code != 0 {
|
||||
if br.Message != "" {
|
||||
return errors.New(br.Message)
|
||||
}
|
||||
return &RespError{Status: resp.StatusCode}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FinishedInbound 成品完工回流入库:调 WMS /api/internal/finished/inbound。
|
||||
// 成品 SN 由 MES 系统生成(工件SN),统一库存主表 category=3;失败返回 err 由调用方降级。
|
||||
func (c *Client) FinishedInbound(ctx context.Context, productCode, productName, sn, orderNo, operator string) error {
|
||||
|
||||
Reference in New Issue
Block a user