Files
bj_power/bj_power_mes/internal/logic/sign.go
T
SunYF 756d978900 feat: 实现工位终端滑动续签机制,强化安全与身份追溯
- 新增滑动续签功能:人工触屏触发本地心跳 + 30秒节流刷新JWT,避免作业中令牌过期
- 拆分质量检验权限:过程检/完工检使用不同权限码,细化权限粒度
- 新增操作人用户ID字段:报工、拧紧记录落库时携带可信用户ID,解决身份追溯问题
- 移除编译期默认密钥,启动期校验所有必要密钥非空,防止密钥泄露风险
- 修复WMS附件越权遍历问题:使用UUID文件名替代顺序ID访问附件
- 实现登录态过期数据隔离:仅返回当前操作人的待同步数据,避免身份混淆
2026-10-06 19:36:22 +08:00

207 lines
6.9 KiB
Go

package logic
import (
"context"
"errors"
"strings"
"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
}
// 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
}