package logic import ( "context" "errors" "strings" "time" "bj_power_mes/ent" "bj_power_mes/ent/stepsignrecord" ) // StepSignVO 签字记录视图对象,返回给前端(巡检终端「待签任务」/追溯展示)。 type StepSignVO struct { ID int `json:"id"` Sn string `json:"sn"` OrderNo string `json:"orderNo"` FlowId int `json:"flowId"` StationNo string `json:"stationNo"` StepId int `json:"stepId"` StepName string `json:"stepName"` SignRole string `json:"signRole"` Result string `json:"result"` SignedBy string `json:"signedBy"` SignedAt string `json:"signedAt"` Round int `json:"round"` Source string `json:"source"` Revoked bool `json:"revoked"` OperatorSigner string `json:"operatorSigner"` // 操作工签字人(只读展示) OperatorSignedAt string `json:"operatorSignedAt"` // 操作工签字时间 HasInspectorSign bool `json:"hasInspectorSign"` // 是否已有检验员 OK 签字 } // InspectorSignReq 检验员签字确认请求(需求B:第二签)。 type InspectorSignReq struct { Sn string `json:"sn"` OrderNo string `json:"orderNo"` StationNo string `json:"stationNo"` StepId int `json:"stepId"` Result string `json:"result"` // OK / NG Remark string `json:"remark"` } // InspectorSign 落检验员签字(signRole=INSPECTOR);轮次=该(sn,stationNo,stepId)已签字最大轮次+1。 // 前置:步骤须 needInspectorSign=true,且操作工已签字(OPERATOR,未撤销,有签字人)——双签顺序先 OPERATOR 后 INSPECTOR。 // result=NG 表示检验不合格,不通过流转(报工门禁 validateNeedSign 仅认 OK)。 func (s *Service) InspectorSign(ctx context.Context, req InspectorSignReq, 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.NeedInspectorSign { return errors.New("该步骤未配置「需检验员签字」,无需签字确认") } // 操作工必须先签字(双签顺序约束) 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("INSPECTOR")).All(ctx) if e != nil { return e } round := 1 for _, r := range recs { if r.Round >= round { round = r.Round + 1 } } result := strings.ToUpper(strings.TrimSpace(req.Result)) if result != "OK" && result != "NG" { result = "OK" } _, err = client.StepSignRecord.Create(). SetSn(req.Sn).SetOrderNo(req.OrderNo).SetFlowId(ps.FlowId). SetStationNo(req.StationNo).SetStepId(req.StepId).SetStepName(ps.Name). SetSignRole("INSPECTOR").SetResult(result). SetSignedBy(operator).SetRound(round). SetSource("inspect").SetRemark(req.Remark).Save(ctx) if err != nil { if isUniqueSignViolation(err) { // 并发签字竞态:唯一索引(sn,stationNo,stepId,signRole,round)冲突 return errors.New("该步骤已签字,请勿重复提交") } return err } 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 { return false } msg := strings.ToLower(err.Error()) return strings.Contains(msg, "duplicate key") || strings.Contains(msg, "violates unique constraint") || strings.Contains(msg, "step_sign_record") } // PendingSignFilter 待检验员签字查询过滤。 type PendingSignFilter struct { Sn string `json:"sn"` OrderNo string `json:"orderNo"` StationNo string `json:"stationNo"` Page int `json:"page"` PageSize int `json:"pageSize"` } // PendingSigns 聚合「操作工已签字、待检验员签字确认」的工艺步骤(需求B 巡检终端「待签任务」数据源)。 // 基数=操作工已签字(未撤销)的步骤;剔除其中已有检验员 OK 签字(未撤销)的步骤,剩余即待签。 func (s *Service) PendingSigns(ctx context.Context, f PendingSignFilter) ([]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 } // 标记哪些 (sn,stationNo,stepId) 已有检验员 OK 签字 type signKey struct { sn, stationNo string stepId int } inspectorOK := map[signKey]bool{} insp, e := client.StepSignRecord.Query(). Where(stepsignrecord.SignRole("INSPECTOR"), stepsignrecord.Revoked(false), stepsignrecord.Result("OK"), stepsignrecord.SignedByNEQ("")).All(ctx) if e != nil { return nil, 0, e } for _, r := range insp { inspectorOK[signKey{r.Sn, r.StationNo, r.StepId}] = true } list := make([]StepSignVO, 0, len(ops)) for _, r := range ops { if inspectorOK[signKey{r.Sn, r.StationNo, r.StepId}] { 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, HasInspectorSign: false, }) } 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 } // 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 }