2026-08-31 08:06:55 +08:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"errors"
|
2026-09-08 11:44:04 +08:00
|
|
|
|
"strings"
|
2026-08-31 08:06:55 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"bj_power_mes/ent"
|
|
|
|
|
|
"bj_power_mes/ent/inspectionrecord"
|
2026-09-30 17:33:11 +08:00
|
|
|
|
"bj_power_mes/ent/material"
|
2026-09-29 14:55:03 +08:00
|
|
|
|
"bj_power_mes/internal/wmsclient"
|
2026-08-31 08:06:55 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-09-08 11:44:04 +08:00
|
|
|
|
// FlexStr 兼容客户端把数字字段传成 number 或 string(前端 stationNo 用数字,
|
|
|
|
|
|
// 标准 JSON 反序列化进 string 会报错 → 400 请求体解析失败)
|
|
|
|
|
|
type FlexStr string
|
|
|
|
|
|
|
|
|
|
|
|
func (f *FlexStr) UnmarshalJSON(b []byte) error {
|
|
|
|
|
|
s := strings.TrimSpace(string(b))
|
|
|
|
|
|
s = strings.Trim(s, `"`)
|
|
|
|
|
|
if s == "null" {
|
|
|
|
|
|
s = ""
|
|
|
|
|
|
}
|
|
|
|
|
|
*f = FlexStr(s)
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-31 08:06:55 +08:00
|
|
|
|
// InspectionReq 巡检记录请求(PAD 巡检终端,块8)
|
|
|
|
|
|
type InspectionReq struct {
|
|
|
|
|
|
Category string `json:"category"` // CHECKIN/POINT/PROCESS/DONE/ALARM
|
2026-09-08 11:44:04 +08:00
|
|
|
|
StationNo FlexStr `json:"stationNo"`
|
2026-08-31 08:06:55 +08:00
|
|
|
|
Shift string `json:"shift"` // 早/中/晚
|
|
|
|
|
|
OrderNo string `json:"orderNo"` // 工单号
|
|
|
|
|
|
Sn string `json:"sn"` // 工件SN
|
|
|
|
|
|
Result string `json:"result"` // OK/NG
|
|
|
|
|
|
Items []string `json:"items"` // 点检项/异常类型
|
|
|
|
|
|
Photo string `json:"photo"` // 拍照文件名
|
|
|
|
|
|
Remark string `json:"remark"` // 文字描述/签字
|
|
|
|
|
|
Operator string `json:"operator"` // 操作人
|
2026-09-15 16:37:39 +08:00
|
|
|
|
// P0-4 过程巡检按步骤维度
|
|
|
|
|
|
ProcessCode int `json:"processCode"` // 所属工位号
|
|
|
|
|
|
StepId int `json:"stepId"` // 工艺步骤ID
|
|
|
|
|
|
StepName string `json:"stepName"` // 工艺步骤名称
|
|
|
|
|
|
MeasuredValue string `json:"measuredValue"` // 实测值
|
2026-09-17 12:49:07 +08:00
|
|
|
|
// 质量检验(过程检/完工检/来料检)扩展字段
|
2026-09-23 11:41:28 +08:00
|
|
|
|
ReportNo string `json:"reportNo"` // 报检单号
|
|
|
|
|
|
MaterialCode string `json:"materialCode"` // 物料编码(图号)
|
|
|
|
|
|
MaterialName string `json:"materialName"` // 物料名称
|
|
|
|
|
|
Manufacturer string `json:"manufacturer"` // 生产厂家
|
|
|
|
|
|
InspectionNo string `json:"inspectionNo"` // 检验单号
|
|
|
|
|
|
AttachmentIds string `json:"attachmentIds"` // 附件ID,逗号分隔
|
2026-09-29 14:55:03 +08:00
|
|
|
|
DisposalType string `json:"disposalType"` // 处置类型 退货/返修/退换/报废
|
2026-09-23 11:41:28 +08:00
|
|
|
|
DisposalRemark string `json:"disposalRemark"` // 处置说明
|
2026-09-29 14:55:03 +08:00
|
|
|
|
// 判不合格联动 WMS 在库处置的定位字段(可空=不联动):
|
2026-09-30 17:33:11 +08:00
|
|
|
|
// 序列号件(sn_mode=1)填 SN、批次件(sn_mode=2)填批次号;
|
|
|
|
|
|
// 定位口径由后端按物料档案 sn_mode 判定(SN=按序列号定位 / 批次=按批次定位)
|
2026-09-29 14:55:03 +08:00
|
|
|
|
BatchSn string `json:"batchSn"`
|
|
|
|
|
|
ReturnTrackingNo string `json:"returnTrackingNo"` // 退货运单号(退货/退换处置必填)
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-29 14:55:03 +08:00
|
|
|
|
// wmsDisposalType MES 中文处置方式 → WMS 内部处置枚举(RETURN/REWORK/SCRAP);
|
|
|
|
|
|
// 无法映射返回空串(不联动 WMS,仅留痕)。
|
|
|
|
|
|
func wmsDisposalType(v string) string {
|
|
|
|
|
|
switch v {
|
|
|
|
|
|
case "退货", "退换":
|
|
|
|
|
|
return "RETURN"
|
|
|
|
|
|
case "返修":
|
|
|
|
|
|
return "REWORK"
|
|
|
|
|
|
case "报废":
|
|
|
|
|
|
return "SCRAP"
|
|
|
|
|
|
}
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CreateInspection 提交巡检记录;ALARM 类型同时触发看板报警(SSE + 清缓存)。
|
|
|
|
|
|
// 质量检验(过程检/完工检)判不合格且填了批次号/SN 时,联动 WMS 在库处置:
|
|
|
|
|
|
// 检验留痕优先——WMS 调用失败不阻断保存,仅在返回信息中提示「库存联动失败: 原因」。
|
|
|
|
|
|
func (s *Service) CreateInspection(ctx context.Context, req InspectionReq, operator string) (string, error) {
|
2026-08-31 08:06:55 +08:00
|
|
|
|
if req.Category == "" {
|
2026-09-29 14:55:03 +08:00
|
|
|
|
return "", errors.New("记录类型必填")
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
category := req.Category
|
2026-09-17 12:49:07 +08:00
|
|
|
|
// PROCESS 为 PAD 巡检终端「过程巡检」专用;MES 车间质检「过程检」用 QC_PROCESS 隔离,
|
|
|
|
|
|
// 避免同枚举导致两个列表互混、统计口径乱(P1-8)。
|
|
|
|
|
|
if !oneOf(category, "CHECKIN", "POINT", "PROCESS", "DONE", "ALARM", "INCOMING", "FINAL", "QC_PROCESS") {
|
2026-09-29 14:55:03 +08:00
|
|
|
|
return "", errors.New("记录类型不合法")
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
result := req.Result
|
|
|
|
|
|
if result == "" {
|
|
|
|
|
|
result = "OK"
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.Operator == "" {
|
|
|
|
|
|
req.Operator = operator
|
|
|
|
|
|
}
|
2026-09-17 12:49:07 +08:00
|
|
|
|
create := s.ctx.EntClient.InspectionRecord.Create().
|
2026-08-31 08:06:55 +08:00
|
|
|
|
SetCategory(category).
|
2026-09-08 11:44:04 +08:00
|
|
|
|
SetStationNo(string(req.StationNo)).
|
2026-08-31 08:06:55 +08:00
|
|
|
|
SetShift(req.Shift).
|
|
|
|
|
|
SetOrderNo(req.OrderNo).
|
|
|
|
|
|
SetSn(req.Sn).
|
|
|
|
|
|
SetResult(result).
|
|
|
|
|
|
SetItems(req.Items).
|
|
|
|
|
|
SetPhoto(req.Photo).
|
|
|
|
|
|
SetRemark(req.Remark).
|
|
|
|
|
|
SetOperator(req.Operator).
|
2026-09-15 16:37:39 +08:00
|
|
|
|
SetProcessCode(req.ProcessCode).
|
|
|
|
|
|
SetStepId(req.StepId).
|
|
|
|
|
|
SetStepName(req.StepName).
|
2026-09-17 12:49:07 +08:00
|
|
|
|
SetMeasuredValue(req.MeasuredValue)
|
|
|
|
|
|
if req.ReportNo != "" {
|
|
|
|
|
|
create.SetReportNo(req.ReportNo)
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.MaterialCode != "" {
|
|
|
|
|
|
create.SetMaterialCode(req.MaterialCode)
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.MaterialName != "" {
|
|
|
|
|
|
create.SetMaterialName(req.MaterialName)
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.Manufacturer != "" {
|
|
|
|
|
|
create.SetManufacturer(req.Manufacturer)
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.InspectionNo != "" {
|
|
|
|
|
|
create.SetInspectionNo(req.InspectionNo)
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.AttachmentIds != "" {
|
|
|
|
|
|
create.SetAttachmentIds(req.AttachmentIds)
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.DisposalType != "" {
|
|
|
|
|
|
create.SetDisposalType(req.DisposalType)
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.DisposalRemark != "" {
|
|
|
|
|
|
create.SetDisposalRemark(req.DisposalRemark)
|
|
|
|
|
|
}
|
2026-09-29 14:55:03 +08:00
|
|
|
|
if req.BatchSn != "" {
|
|
|
|
|
|
create.SetBatchSn(req.BatchSn)
|
|
|
|
|
|
}
|
2026-09-17 12:49:07 +08:00
|
|
|
|
_, err := create.Save(ctx)
|
2026-08-31 08:06:55 +08:00
|
|
|
|
if err != nil {
|
2026-09-29 14:55:03 +08:00
|
|
|
|
return "", err
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
s.ctx.EventLog.Write(ctx, "inspection."+category, req.OrderNo, req.Operator, "inspection_record",
|
2026-09-08 11:44:04 +08:00
|
|
|
|
req.Sn, "巡检终端提交", map[string]any{"category": category, "stationNo": string(req.StationNo), "result": result})
|
2026-08-31 08:06:55 +08:00
|
|
|
|
|
2026-09-29 14:55:03 +08:00
|
|
|
|
// 判不合格 → 联动 WMS 在库处置(缺口 B):
|
|
|
|
|
|
// 前提:结论 NG + 已选可映射的处置方式 + 物料编码与批次号/SN 齐全;
|
2026-09-30 17:33:11 +08:00
|
|
|
|
// 定位口径由后端按物料档案 sn_mode 判定:序列号件按 SN(material_code+序列号)、批次件按批次(material_code+批次号)。
|
2026-09-29 14:55:03 +08:00
|
|
|
|
// WMS 调用失败不阻断检验留痕,返回信息提示「库存联动失败」,并写事件日志便于追溯。
|
|
|
|
|
|
msg := "巡检记录已提交"
|
|
|
|
|
|
if result == "NG" && req.MaterialCode != "" && req.BatchSn != "" {
|
|
|
|
|
|
if dt := wmsDisposalType(req.DisposalType); dt != "" {
|
2026-09-30 17:33:11 +08:00
|
|
|
|
snMode := 2
|
|
|
|
|
|
if m, err := s.ctx.EntClient.Material.Query().Where(material.CodeEQ(req.MaterialCode)).First(ctx); err == nil {
|
|
|
|
|
|
snMode = m.SnMode
|
|
|
|
|
|
}
|
2026-09-29 14:55:03 +08:00
|
|
|
|
wreq := wmsclient.DisposeInventoryReq{
|
|
|
|
|
|
MaterialCode: req.MaterialCode,
|
|
|
|
|
|
DisposalType: dt,
|
|
|
|
|
|
ReturnTrackingNo: req.ReturnTrackingNo,
|
|
|
|
|
|
Remark: req.DisposalRemark,
|
|
|
|
|
|
Operator: req.Operator,
|
|
|
|
|
|
}
|
2026-09-30 17:33:11 +08:00
|
|
|
|
if snMode == 1 {
|
2026-09-29 14:55:03 +08:00
|
|
|
|
wreq.Sn = req.BatchSn
|
|
|
|
|
|
} else {
|
|
|
|
|
|
wreq.BatchNo = req.BatchSn
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := s.ctx.Wms.DisposeInventory(ctx, wreq); err != nil {
|
|
|
|
|
|
msg = "检验记录已保存;库存联动失败: " + err.Error()
|
|
|
|
|
|
s.ctx.EventLog.Write(ctx, "inspection.wms_dispose_failed", req.OrderNo, req.Operator,
|
|
|
|
|
|
"inspection_record", req.Sn, "库存联动失败 "+err.Error(),
|
|
|
|
|
|
map[string]any{"materialCode": req.MaterialCode, "batchSn": req.BatchSn,
|
2026-09-30 17:33:11 +08:00
|
|
|
|
"disposalType": dt, "snMode": snMode})
|
2026-09-29 14:55:03 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
msg = "检验记录已保存,已联动 WMS 在库处置(" + req.DisposalType + ")"
|
|
|
|
|
|
s.ctx.EventLog.Write(ctx, "inspection.wms_disposed", req.OrderNo, req.Operator,
|
|
|
|
|
|
"inspection_record", req.Sn, "已联动 WMS 在库处置 "+dt,
|
2026-09-30 17:33:11 +08:00
|
|
|
|
map[string]any{"materialCode": req.MaterialCode, "batchSn": req.BatchSn,
|
|
|
|
|
|
"disposalType": dt, "snMode": snMode})
|
2026-09-29 14:55:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-31 08:06:55 +08:00
|
|
|
|
if category == "ALARM" {
|
|
|
|
|
|
// 触发看板报警:SSE 推送 + 使 Redis 看板报警缓存失效
|
|
|
|
|
|
alarmJSON, _ := json.Marshal(map[string]any{
|
|
|
|
|
|
"level": "CRITICAL",
|
|
|
|
|
|
"type": "inspection_alarm",
|
|
|
|
|
|
"message": "巡检异常上报:" + req.Remark,
|
2026-09-08 11:44:04 +08:00
|
|
|
|
"station": string(req.StationNo),
|
2026-08-31 08:06:55 +08:00
|
|
|
|
"time": time.Now().Format("2006-01-02 15:04:05"),
|
|
|
|
|
|
})
|
|
|
|
|
|
if s.ctx.SSE != nil {
|
|
|
|
|
|
s.ctx.SSE.Publish("dashboard.alarm", string(alarmJSON))
|
|
|
|
|
|
}
|
|
|
|
|
|
if s.ctx.RedisClient != nil {
|
|
|
|
|
|
_, _ = s.ctx.RedisClient.Del("dashboard:alarms")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-09-04 17:31:18 +08:00
|
|
|
|
s.notifyDashboard()
|
2026-09-29 14:55:03 +08:00
|
|
|
|
return msg, nil
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-08 11:44:04 +08:00
|
|
|
|
// ListInspections 查询巡检记录(不分页,供内部/导出用,封顶 1000)
|
2026-08-31 08:06:55 +08:00
|
|
|
|
func (s *Service) ListInspections(ctx context.Context, category, operator, from, to string) ([]*ent.InspectionRecord, error) {
|
2026-09-17 12:49:07 +08:00
|
|
|
|
rows, _, err := s.ListInspectionsPaged(ctx, category, operator, from, to, "", "", "", "", "", 1, 1000)
|
2026-09-08 11:44:04 +08:00
|
|
|
|
return rows, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ListInspectionsPaged 分页查询巡检记录,返回 {rows, total};排序 id desc 最新置顶
|
|
|
|
|
|
// orderNo/stationNo:2026-09-08 补充筛选(大小写不敏感)
|
2026-09-17 12:49:07 +08:00
|
|
|
|
// materialCode/materialName/reportNo:质量检验模糊查询(Item H,每框内模糊、多框取交集)
|
|
|
|
|
|
func (s *Service) ListInspectionsPaged(ctx context.Context, category, operator, from, to, orderNo, stationNo, materialCode, materialName, reportNo string, page, pageSize int) ([]*ent.InspectionRecord, int, error) {
|
2026-09-08 11:44:04 +08:00
|
|
|
|
q := s.ctx.EntClient.InspectionRecord.Query()
|
2026-08-31 08:06:55 +08:00
|
|
|
|
if category != "" {
|
|
|
|
|
|
q = q.Where(inspectionrecord.Category(category))
|
|
|
|
|
|
}
|
2026-09-08 11:44:04 +08:00
|
|
|
|
if orderNo != "" {
|
|
|
|
|
|
q = q.Where(inspectionrecord.OrderNoEqualFold(orderNo))
|
|
|
|
|
|
}
|
|
|
|
|
|
if stationNo != "" {
|
|
|
|
|
|
q = q.Where(inspectionrecord.StationNoEqualFold(stationNo))
|
|
|
|
|
|
}
|
2026-09-17 12:49:07 +08:00
|
|
|
|
if materialCode != "" {
|
|
|
|
|
|
q = q.Where(inspectionrecord.MaterialCodeContainsFold(materialCode))
|
|
|
|
|
|
}
|
|
|
|
|
|
if materialName != "" {
|
|
|
|
|
|
q = q.Where(inspectionrecord.MaterialNameContainsFold(materialName))
|
|
|
|
|
|
}
|
|
|
|
|
|
if reportNo != "" {
|
|
|
|
|
|
q = q.Where(inspectionrecord.ReportNoContainsFold(reportNo))
|
|
|
|
|
|
}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
if operator != "" {
|
|
|
|
|
|
q = q.Where(inspectionrecord.Operator(operator))
|
|
|
|
|
|
}
|
|
|
|
|
|
if from != "" {
|
|
|
|
|
|
if f, err := time.Parse("2006-01-02", from); err == nil {
|
|
|
|
|
|
q = q.Where(inspectionrecord.CreatedAtGTE(f))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if to != "" {
|
|
|
|
|
|
if t, err := time.Parse("2006-01-02", to); err == nil {
|
|
|
|
|
|
q = q.Where(inspectionrecord.CreatedAtLT(t.Add(24 * time.Hour)))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-09-08 11:44:04 +08:00
|
|
|
|
total, err := q.Count(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
rows, err := q.Order(ent.Desc(inspectionrecord.FieldID)).
|
|
|
|
|
|
Offset((page - 1) * pageSize).Limit(pageSize).All(ctx)
|
|
|
|
|
|
return rows, total, err
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func oneOf(v string, items ...string) bool {
|
|
|
|
|
|
for _, it := range items {
|
|
|
|
|
|
if v == it {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
2026-09-17 12:49:07 +08:00
|
|
|
|
|
|
|
|
|
|
// GetInspectionRecord 按 id 查询一条检验记录(质量检验处置/不合格单用)
|
|
|
|
|
|
func (s *Service) GetInspectionRecord(ctx context.Context, id int) (*ent.InspectionRecord, error) {
|
|
|
|
|
|
if id <= 0 {
|
|
|
|
|
|
return nil, errors.New("缺少 id")
|
|
|
|
|
|
}
|
|
|
|
|
|
rec, err := s.ctx.EntClient.InspectionRecord.Get(ctx, id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errors.New("检验记录不存在")
|
|
|
|
|
|
}
|
|
|
|
|
|
return rec, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetInspectionDisposal 质量检验处置:区分 退货/返修/退换,写处置说明
|
|
|
|
|
|
func (s *Service) SetInspectionDisposal(ctx context.Context, id int, disposalType, disposalRemark, operator string) error {
|
|
|
|
|
|
if id <= 0 {
|
|
|
|
|
|
return errors.New("缺少 id")
|
|
|
|
|
|
}
|
|
|
|
|
|
if !oneOf(disposalType, "退货", "返修", "退换") {
|
|
|
|
|
|
return errors.New("处置类型仅支持 退货/返修/退换")
|
|
|
|
|
|
}
|
|
|
|
|
|
rec, err := s.ctx.EntClient.InspectionRecord.Get(ctx, id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errors.New("检验记录不存在")
|
|
|
|
|
|
}
|
|
|
|
|
|
_, err = s.ctx.EntClient.InspectionRecord.UpdateOneID(id).
|
|
|
|
|
|
SetDisposalType(disposalType).SetDisposalRemark(disposalRemark).Save(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
s.ctx.EventLog.Write(ctx, "inspection.disposal", rec.OrderNo, operator, "inspection_record", rec.Sn,
|
|
|
|
|
|
"质量检验处置 "+disposalType, map[string]any{"id": id, "remark": disposalRemark})
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|