package logic import ( "context" "time" "bj_power_mes/ent/stepsignrecord" ) // ScanSignTimeout 定时巡检:查出「已报检、未撤销、无检验员签字、超过阈值」的 // 待签步骤,落 sign_timeout 预警中心 + 异常告警角标(不进铃铛,遵守 11-25 铃铛与待办分家)。 // // 由 bj_power_mes.go 每分钟调用一次,不挂在巡检端待签查询上——没人打开巡检端也必须报警。 // 阈值来自基础设置 CfgSignTimeoutMinutes(默认 30 分钟,1~1440 可调); // 去重走 CreateAlertDedup(60 分钟窗口),避免每次扫描重复落同一条。 // // 与待签列表同口径:基数改 CALL;只处理 needInspectorSign=true 的步骤;批量取 ProcessStep 避免 N+1。 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 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 } // 基数=CALL:未撤销、有呼叫人的报检记录 calls, err := client.StepSignRecord.Query(). Where(stepsignrecord.SignRole("CALL"), stepsignrecord.Revoked(false), stepsignrecord.SignedByNEQ(""), stepsignrecord.SignedAtLTE(cutoff)).All(ctx) if err != nil { return 0, err } // 批量取步骤配置(替代逐条 Get) ids := make([]int, 0, len(calls)) for _, r := range calls { ids = append(ids, r.StepId) } stepMap, err := processStepsByIDs(ctx, client, ids) if err != nil { return 0, err } cnt := 0 for _, r := range calls { k := signKey{r.Sn, r.StationNo, r.StepId, r.Round} if inspectorDone[k] { continue } ps, ok := stepMap[r.StepId] if !ok || !ps.NeedInspectorSign { continue } 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 }