feat: 新增产品工艺管理与工序审核功能

- 新增产品工艺(product_process)及明细(product_process_item)数据模型
- 新增工序审核开关(need_audit)与审核角色(audit_role)字段
- 新增待审核签字页面与路由
- 优化 WMS/MES 接口交互,增加业务错误处理与友好提示
- 补充库存过滤逻辑,避免返回已出库 SN 数据
- 调整默认查询时间范围为近一个月,优化用户体验
This commit is contained in:
SunYF
2026-10-09 13:46:33 +08:00
parent e2f1dacf76
commit b032f47fc7
95 changed files with 10546 additions and 1457 deletions
+189
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"strings"
"time"
"bj_power_mes/ent"
"bj_power_mes/ent/stepsignrecord"
@@ -105,6 +106,102 @@ func (s *Service) InspectorSign(ctx context.Context, req InspectorSignReq, opera
return nil
}
// AuditorSignReq 审核人复核签字请求(工序级审核 gate,工艺配置 needAudit 决定)。
type AuditorSignReq struct {
Sn string `json:"sn"`
OrderNo string `json:"orderNo"`
StationNo string `json:"stationNo"`
StepId int `json:"stepId"`
Result string `json:"result"` // OK / NG(NG=审核不通过,退回操作工重做)
Remark string `json:"remark"`
}
// AuditorSign 落审核人复核签字(signRole=AUDITOR);前置:步骤须 needAudit=true,且前置签字
// (若 needInspectorSign 则检验员须 OK,否则操作工已签)已存在。
// result=NG 表示审核不通过,撤销该步骤已有的操作工/检验员签字,退回操作工重做(新一轮轮次)。
func (s *Service) AuditorSign(ctx context.Context, req AuditorSignReq, operator string) error {
req.Sn = strings.TrimSpace(req.Sn)
req.StationNo = strings.TrimSpace(req.StationNo)
req.OrderNo = strings.TrimSpace(req.OrderNo)
if req.Sn == "" || req.StationNo == "" || req.StepId <= 0 {
return errors.New("审核参数不完整:缺少 SN / 工位号 / 步骤")
}
if operator == "" {
return errors.New("未获取到审核人身份,请重新登录")
}
client := s.ctx.EntClient
ps, err := client.ProcessStep.Get(ctx, req.StepId)
if err != nil {
return errors.New("工艺步骤不存在:" + itoa(req.StepId))
}
if !ps.NeedAudit {
return errors.New("该步骤未配置「需审核人复核」,无需审核")
}
result := strings.ToUpper(strings.TrimSpace(req.Result))
if result != "OK" && result != "NG" {
result = "OK"
}
// 前置签字校验(审核在签字链末端)
if ps.NeedInspectorSign {
inspOK, e := client.StepSignRecord.Query().
Where(stepsignrecord.Sn(req.Sn), stepsignrecord.StationNo(req.StationNo),
stepsignrecord.StepId(req.StepId), stepsignrecord.SignRole("INSPECTOR"),
stepsignrecord.Revoked(false), stepsignrecord.Result("OK"), stepsignrecord.SignedByNEQ("")).Exist(ctx)
if e != nil {
return e
}
if !inspOK {
return errors.New("检验员尚未 OK 签字,审核人不能先于检验员复核")
}
} else {
opSigned, e := client.StepSignRecord.Query().
Where(stepsignrecord.Sn(req.Sn), stepsignrecord.StationNo(req.StationNo),
stepsignrecord.StepId(req.StepId), stepsignrecord.SignRole("OPERATOR"),
stepsignrecord.Revoked(false), stepsignrecord.SignedByNEQ("")).Exist(ctx)
if e != nil {
return e
}
if !opSigned {
return errors.New("操作工尚未签字,审核人不能复核")
}
}
// 计算轮次(每轮审核递增)
recs, e := client.StepSignRecord.Query().
Where(stepsignrecord.Sn(req.Sn), stepsignrecord.StationNo(req.StationNo),
stepsignrecord.StepId(req.StepId), stepsignrecord.SignRole("AUDITOR")).All(ctx)
if e != nil {
return e
}
round := 1
for _, r := range recs {
if r.Round >= round {
round = r.Round + 1
}
}
if result == "NG" {
// 退回操作工重做:撤销该步骤已有的操作工/检验员签字
_, _ = client.StepSignRecord.Update().
Where(stepsignrecord.Sn(req.Sn), stepsignrecord.StationNo(req.StationNo),
stepsignrecord.StepId(req.StepId),
stepsignrecord.SignRoleIn("OPERATOR", "INSPECTOR"), stepsignrecord.Revoked(false)).
SetRevoked(true).SetRevokedBy(operator).
SetRevokedAt(time.Now()).SetRevokeReason("审核不通过,退回重做").Save(ctx)
}
_, err = client.StepSignRecord.Create().
SetSn(req.Sn).SetOrderNo(req.OrderNo).SetFlowId(ps.FlowId).
SetStationNo(req.StationNo).SetStepId(req.StepId).SetStepName(ps.Name).
SetSignRole("AUDITOR").SetResult(result).
SetSignedBy(operator).SetRound(round).
SetSource("audit").SetRemark(req.Remark).Save(ctx)
if err != nil {
if isUniqueSignViolation(err) {
return errors.New("该步骤已审核,请勿重复提交")
}
return err
}
return nil
}
// isUniqueSignViolation 判定是否为签字唯一索引冲突(并发签字竞态)
func isUniqueSignViolation(err error) bool {
if err == nil {
@@ -204,3 +301,95 @@ func (s *Service) PendingSigns(ctx context.Context, f PendingSignFilter) ([]Step
}
return list[start:end], total, nil
}
// PendingAuditFilter 待审核查询过滤(审核人「待审任务」数据源)。
type PendingAuditFilter struct {
Sn string `json:"sn"`
OrderNo string `json:"orderNo"`
StationNo string `json:"stationNo"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
// PendingAudits 聚合「前置签字已满足、待审核人复核」的工艺步骤。
// 基数=操作工已签字(未撤销)的步骤;排除:已有审核 OK 的步骤;以及检验未通过(存在检验员签字记录但未 OK)的步骤。
func (s *Service) PendingAudits(ctx context.Context, f PendingAuditFilter) ([]StepSignVO, int, error) {
client := s.ctx.EntClient
q := client.StepSignRecord.Query().
Where(stepsignrecord.SignRole("OPERATOR"), stepsignrecord.Revoked(false), stepsignrecord.SignedByNEQ(""))
if f.Sn != "" {
q = q.Where(stepsignrecord.Sn(f.Sn))
}
if f.OrderNo != "" {
q = q.Where(stepsignrecord.OrderNo(f.OrderNo))
}
if f.StationNo != "" {
q = q.Where(stepsignrecord.StationNo(f.StationNo))
}
ops, err := q.Order(ent.Desc(stepsignrecord.FieldID)).All(ctx)
if err != nil {
return nil, 0, err
}
type signKey struct {
sn, stationNo string
stepId int
}
inspectorAny := map[signKey]bool{}
inspectorOK := map[signKey]bool{}
auditOK := map[signKey]bool{}
insp, e := client.StepSignRecord.Query().
Where(stepsignrecord.SignRoleIn("INSPECTOR", "AUDITOR"), stepsignrecord.Revoked(false),
stepsignrecord.SignedByNEQ("")).All(ctx)
if e != nil {
return nil, 0, e
}
for _, r := range insp {
k := signKey{r.Sn, r.StationNo, r.StepId}
if r.SignRole == "INSPECTOR" {
inspectorAny[k] = true
if r.Result == "OK" {
inspectorOK[k] = true
}
}
if r.SignRole == "AUDITOR" && r.Result == "OK" {
auditOK[k] = true
}
}
list := make([]StepSignVO, 0, len(ops))
for _, r := range ops {
k := signKey{r.Sn, r.StationNo, r.StepId}
if auditOK[k] {
continue
}
if inspectorAny[k] && !inspectorOK[k] {
// 检验环节未通过,未到审核阶段
continue
}
signedAt := r.SignedAt.Format("2006-01-02 15:04:05")
list = append(list, StepSignVO{
ID: r.ID, Sn: r.Sn, OrderNo: r.OrderNo, FlowId: r.FlowId,
StationNo: r.StationNo, StepId: r.StepId, StepName: r.StepName,
SignRole: r.SignRole, Result: r.Result, SignedBy: r.SignedBy,
SignedAt: signedAt, Round: r.Round, Source: r.Source,
OperatorSigner: r.SignedBy, OperatorSignedAt: signedAt,
})
}
total := len(list)
ps := f.PageSize
if ps <= 0 {
ps = 50
}
pg := f.Page
if pg <= 0 {
pg = 1
}
start := (pg - 1) * ps
if start >= total {
return []StepSignVO{}, total, nil
}
end := start + ps
if end > total {
end = total
}
return list[start:end], total, nil
}