本次 commit 统一修正了全项目范围内关于 SN 管理的术语表述,将原“SN管理”、“序列号件”、“批次件”等表述调整为“序列号”、“序列号=是”、“序列号=否”,同时同步更新了相关文档、代码注释、前端提示文本、数据库 schema 注释、API 文档等,确保全链路术语统一,消除理解歧义。
242 lines
9.2 KiB
Go
242 lines
9.2 KiB
Go
package handler
|
||
|
||
import (
|
||
"errors"
|
||
"net/http"
|
||
"strings"
|
||
"time"
|
||
|
||
"bj_power_wms/ent"
|
||
"bj_power_wms/ent/inventory"
|
||
"bj_power_wms/ent/material"
|
||
"bj_power_wms/internal/svc"
|
||
)
|
||
|
||
// 手工成品入库(方案A 定稿 2026-10-04):完工检合格才入成品库,
|
||
// 成品入库与「完工自动回流」解耦为双通道——MES 回流走 internalapi.go 内部接口,
|
||
// 库房手工录入走本文件 POST /api/finished/inbound(登录态 + wms.finished:edit 权限)。
|
||
// 两通道共享 createFinishedInventory 创建逻辑,字段口径一致(客户问题记录 L218 成品入库九字段)。
|
||
|
||
// 成品入库来源(共享创建逻辑按来源区分 入库单号/备注/事件日志描述 留痕)
|
||
const (
|
||
sourceMES = "MES" // 完工检合格自动回流(内部接口)
|
||
sourceManual = "manual" // 手工成品入库页
|
||
)
|
||
|
||
// createFinishedInventory 成品入库共享创建逻辑(MES 回流与手工入库两处调用)。
|
||
// 返回 (库存行, http状态码, 错误):失败时调用方 fail(w, code, err) 回包。
|
||
// 质量状态:默认「合格」;MES 完工检按产品状态传入(已通过→合格/部分通过→合格/返修→不合格),
|
||
// 后端尊重传入值、不重复映射。
|
||
// SN 全局防重沿用:跨 sn_mode 查重(成品+半成品+序列号=是 不允许撞号),409 文案两通道一致。
|
||
func createFinishedInventory(ctx *svc.ServiceContext, req *finishedInboundRequest, source string) (*ent.Inventory, int, error) {
|
||
if req.Sn == "" || req.ProductCode == "" {
|
||
return nil, http.StatusBadRequest, errors.New("sn 与 productCode 必填")
|
||
}
|
||
// 质量状态归一:合法枚举值原样尊重;空值或非枚举值(如 MES 误传「已通过/部分通过」)按产品状态派生兜底,
|
||
// 避免脏值落库(库存质量状态仅认 合格/不合格/未检/待退)。
|
||
quality := normalizeQualityStatus(req.QualityStatus, req.ProductStatus)
|
||
|
||
// SN 全局防重(同物料类别成品 + 半成品 + 序列号=是 SN 都不允许撞号)
|
||
dup, err := ctx.EntClient.Inventory.Query().
|
||
Where(inventory.SnModeEQ(snModeSN), inventory.SnCodeEQ(req.Sn)).Exist(ctx0())
|
||
if err != nil {
|
||
return nil, http.StatusInternalServerError, err
|
||
}
|
||
if dup {
|
||
return nil, http.StatusConflict, errors.New("成品 SN 已存在库存: " + req.Sn)
|
||
}
|
||
|
||
// 物料档案补名称/类型;缺失时不阻塞入库(名称用请求传入兜底),记 eventlog 提示补建档
|
||
materialName := req.ProductName
|
||
matType := 1
|
||
archiveMiss := false
|
||
if m, err := ctx.EntClient.Material.Query().Where(material.CodeEQ(req.ProductCode)).First(ctx0()); err == nil && m != nil {
|
||
materialName = m.Name
|
||
matType = m.ManageMode
|
||
} else {
|
||
archiveMiss = true
|
||
}
|
||
|
||
// 记录日期 YYYY-MM-DD → unix 秒(空/解析失败 = 不落)
|
||
var recordedAt int64
|
||
if t, e := time.ParseInLocation("2006-01-02", strings.TrimSpace(req.RecordDate), time.Local); e == nil {
|
||
recordedAt = t.Unix()
|
||
}
|
||
|
||
// 入库单号与备注按来源留痕:MES 回流=MES:工单号(沿用原口径);手工=工单号(可空)+「手工成品入库」备注
|
||
inboundNo, remark := "", ""
|
||
desc := "成品完工回流入库 " + req.Sn
|
||
if source == sourceManual {
|
||
desc = "手工成品入库 " + req.Sn
|
||
remark = "手工成品入库"
|
||
if r := strings.TrimSpace(req.Remark); r != "" {
|
||
remark += ":" + r
|
||
}
|
||
inboundNo = strings.TrimSpace(req.OrderNo)
|
||
} else {
|
||
inboundNo = "MES:" + req.OrderNo
|
||
remark = "成品完工回流(SN来源:" + snSourceLabel(req.SnSource) + ")"
|
||
}
|
||
|
||
rec, err := ctx.EntClient.Inventory.Create().
|
||
SetManageMode(matType).
|
||
SetSnMode(snModeSN).
|
||
SetCategory(invCatFinished).
|
||
SetMaterialCode(req.ProductCode).
|
||
SetMaterialName(materialName).
|
||
SetSnCode(req.Sn).
|
||
SetQuantity(1).
|
||
SetQualityStatus(quality).
|
||
SetNillableZoneCode(strPtr(req.ZoneCode)).
|
||
SetStatus("在库").
|
||
SetNillableInboundNo(strPtr(inboundNo)).
|
||
SetOrderNo(req.OrderNo).
|
||
// 归属三字段:照 semiInboundHandler 的归属落库写法(类型归一化,编号/工号 Nillable 空=不落)
|
||
SetNillableOwnershipType(strPtr(ownershipTypeOrNone(req.OwnershipType))).
|
||
SetNillableOwnershipNo(strPtr(req.OwnershipNo)).
|
||
SetNillableProductWorkNo(strPtr(req.ProductWorkNo)).
|
||
SetNillableProductStatus(strPtr(req.ProductStatus)).
|
||
SetNillableRelatedStandard(strPtr(req.RelatedStandard)).
|
||
SetNillableTestRecordNo(strPtr(req.TestRecordNo)).
|
||
SetNillableRecordedBy(strPtr(req.RecordedBy)).
|
||
SetNillableRecordedAt(int64Ptr(recordedAt)).
|
||
SetRemark(remark).
|
||
Save(ctx0())
|
||
if err != nil {
|
||
if isUniqueViolation(err) {
|
||
return nil, http.StatusConflict, errors.New("成品 SN 已存在库存: " + req.Sn)
|
||
}
|
||
return nil, http.StatusInternalServerError, err
|
||
}
|
||
|
||
ctx.EventLog.Write(ctx0(), "finished.inbound", req.Operator, "finished", req.Sn,
|
||
desc, map[string]any{
|
||
"productCode": req.ProductCode, "orderNo": req.OrderNo,
|
||
"snSource": req.SnSource, "archiveMiss": archiveMiss,
|
||
"productStatus": req.ProductStatus, "testRecordNo": req.TestRecordNo,
|
||
"qualityStatus": quality, "source": source,
|
||
})
|
||
return rec, 0, nil
|
||
}
|
||
|
||
// normalizeQualityStatus 归一成品入库质量状态:合法枚举值原样尊重;空值或非库存枚举值
|
||
// (如 MES 误传「已通过/部分通过」等业务态)按产品状态派生兜底,防止脏值落库。
|
||
func normalizeQualityStatus(quality, productStatus string) string {
|
||
switch quality {
|
||
case QStatusPending, QStatusPass, QStatusFail, QStatusReturn:
|
||
return quality
|
||
}
|
||
switch productStatus {
|
||
case "部分通过", "已通过":
|
||
return QStatusPass
|
||
case "返修":
|
||
return QStatusFail
|
||
default:
|
||
return QStatusPass
|
||
}
|
||
}
|
||
|
||
// isUniqueViolation 判定 DB 错误是否唯一约束冲突(部分唯一索引 ux_inventories_sn_code 等)。
|
||
// 用错误文本匹配,避免强依赖 lib/pq(当前为 indirect 依赖)。
|
||
func isUniqueViolation(err error) bool {
|
||
if err == nil {
|
||
return false
|
||
}
|
||
msg := strings.ToLower(err.Error())
|
||
return strings.Contains(msg, "ux_inventories_sn_code") ||
|
||
strings.Contains(msg, "duplicate key") ||
|
||
strings.Contains(msg, "violates unique constraint")
|
||
}
|
||
|
||
// manualFinishedInboundHandler POST /api/finished/inbound 手工成品入库(登录态,权限 wms.finished:edit)。
|
||
// 与 MES 完工回流共享创建逻辑(createFinishedInventory);差异仅在必填校验:
|
||
// SN/产品编码/产品状态/记录员必填,记录日期默认今天;成功返回库存行。
|
||
func manualFinishedInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
var req finishedInboundRequest
|
||
if err := parseJSON(r, &req); err != nil {
|
||
fail(w, http.StatusBadRequest, "参数错误: "+err.Error())
|
||
return
|
||
}
|
||
req.Sn = strings.TrimSpace(req.Sn)
|
||
req.ProductCode = strings.TrimSpace(req.ProductCode)
|
||
if req.Sn == "" {
|
||
fail(w, http.StatusBadRequest, "SN 必填")
|
||
return
|
||
}
|
||
if req.ProductCode == "" {
|
||
fail(w, http.StatusBadRequest, "产品编码必填")
|
||
return
|
||
}
|
||
if strings.TrimSpace(req.ProductStatus) == "" {
|
||
fail(w, http.StatusBadRequest, "产品状态必填(已通过/部分通过/返修)")
|
||
return
|
||
}
|
||
if strings.TrimSpace(req.RecordedBy) == "" {
|
||
fail(w, http.StatusBadRequest, "记录员必填")
|
||
return
|
||
}
|
||
// 归属与入库/半成品同口径:合同号/科技项目号必须填归属编号
|
||
if (req.OwnershipType == "workorder" || req.OwnershipType == "project") && strings.TrimSpace(req.OwnershipNo) == "" {
|
||
fail(w, http.StatusBadRequest, "归属类型为合同号/科技项目号时,归属编号必填")
|
||
return
|
||
}
|
||
// 记录日期默认今天
|
||
if strings.TrimSpace(req.RecordDate) == "" {
|
||
req.RecordDate = time.Now().Format("2006-01-02")
|
||
}
|
||
// 操作人取当前登录用户(事件日志留痕)
|
||
req.Operator = r.Header.Get("X-Username")
|
||
|
||
rec, code, err := createFinishedInventory(ctx, &req, sourceManual)
|
||
if err != nil {
|
||
fail(w, code, err.Error())
|
||
return
|
||
}
|
||
ok(w, semiView(rec))
|
||
}
|
||
}
|
||
|
||
// finishedInboundTodayHandler GET /api/finished/inbound/today
|
||
// 手工成品入库页「今日成品入库记录」只读表:统一库存主表 category=3 当天入库的行
|
||
// (双通道同表——MES 完工回流与手工入库都在内,备注列可区分来源),按创建时间倒序分页。
|
||
func finishedInboundTodayHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
page := atoi(r.URL.Query().Get("page"), 1)
|
||
pageSize := atoi(r.URL.Query().Get("pageSize"), 20)
|
||
if page < 1 {
|
||
page = 1
|
||
}
|
||
if pageSize < 1 || pageSize > 200 {
|
||
pageSize = 20
|
||
}
|
||
now := time.Now()
|
||
dayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
|
||
q := ctx.EntClient.Inventory.Query().Where(
|
||
inventory.CategoryEQ(invCatFinished),
|
||
inventory.CreatedAtGTE(dayStart),
|
||
)
|
||
total, err := q.Count(ctx0())
|
||
if err != nil {
|
||
fail(w, http.StatusInternalServerError, err.Error())
|
||
return
|
||
}
|
||
list, err := q.Order(ent.Desc("created_at"), ent.Desc("id")).
|
||
Offset((page - 1) * pageSize).Limit(pageSize).All(ctx0())
|
||
if err != nil {
|
||
fail(w, http.StatusInternalServerError, err.Error())
|
||
return
|
||
}
|
||
rows := make([]*semiViewRow, 0, len(list))
|
||
for _, inv := range list {
|
||
rows = append(rows, semiView(inv))
|
||
}
|
||
ok(w, map[string]any{
|
||
"total": total,
|
||
"list": rows,
|
||
"page": page,
|
||
"pageSize": pageSize,
|
||
})
|
||
}
|
||
}
|