Files
bj_power/bj_power_mes/internal/logic/sign_timeout.go
T

68 lines
2.4 KiB
Go
Raw Normal View History

package logic
import (
"context"
"time"
"bj_power_mes/ent/stepsignrecord"
)
// ScanSignTimeout 定时巡检(2026-11-30 P1-2):查出「已呼叫、未撤销、无检验员签字、超过阈值」的
// 待签步骤,落 sign_timeout 预警中心 + 异常告警角标(不进铃铛,遵守 11-25 铃铛与待办分家)。
//
// 由 bj_power_mes.go 每分钟调用一次,不挂在巡检端待签查询上——没人打开巡检端也必须报警。
// 阈值来自基础设置 CfgSignTimeoutMinutes(默认 30 分钟,1~1440 可调);
// 去重走 CreateAlertDedup(60 分钟窗口),避免每次扫描重复落同一条。
//
// 与 P1-1 同口径:只处理 needInspectorSign=true 的步骤,只配操作工签字的步骤不产生假超时。
func (s *Service) ScanSignTimeout(ctx context.Context, minutes int) (int, error) {
if minutes <= 0 {
minutes = signTimeoutMinutes
}
cutoff := time.Now().Add(-time.Duration(minutes) * time.Minute)
client := s.ctx.EntClient
// 有检验员签字(OK 或 NG 均已处理)的 (sn,stationNo,stepId,round) 集合
type signKey struct {
sn, stationNo string
stepId, round int
}
inspectorDone := map[signKey]bool{}
insp, e := client.StepSignRecord.Query().
Where(stepsignrecord.SignRole("INSPECTOR"), stepsignrecord.Revoked(false),
stepsignrecord.SignedByNEQ("")).All(ctx)
if e != nil {
return 0, e
}
for _, r := range insp {
inspectorDone[signKey{r.Sn, r.StationNo, r.StepId, r.Round}] = true
}
// 基数=未撤销、有签字人的 OPERATOR 呼叫记录
ops, err := client.StepSignRecord.Query().
Where(stepsignrecord.SignRole("OPERATOR"), stepsignrecord.Revoked(false),
stepsignrecord.SignedByNEQ(""), stepsignrecord.SignedAtLTE(cutoff)).All(ctx)
if err != nil {
return 0, err
}
cnt := 0
for _, r := range ops {
k := signKey{r.Sn, r.StationNo, r.StepId, r.Round}
if inspectorDone[k] {
continue
}
ps, pe := client.ProcessStep.Get(ctx, r.StepId)
if pe != nil || !ps.NeedInspectorSign {
continue // P1-1:只配操作工签字的步骤不产生假超时
}
msg := "工位 " + r.StationNo + " 工件 " + r.Sn + " 步骤「" + r.StepName +
"」呼叫签字已超时 " + itoa(minutes) + " 分钟,请检验员尽快签字确认"
created, aerr := s.CreateAlertDedup(ctx, "sign_timeout", "警告", r.StationNo, msg, 60)
if aerr != nil || !created {
continue
}
cnt++
}
return cnt, nil
}