feat: 新增拧紧待签任务集中签字功能

新增了巡检终端的拧紧待签任务聚合查询和批量审核签字接口,支持检验员对拧紧任务进行第二签;在权限系统中新增对应按钮权限,前端页面新增指标选择下拉和绩效报表的上机记录页签,同时完善相关帮助文档和配置项。
This commit is contained in:
SunYF
2026-09-29 17:37:45 +08:00
parent 76c9b6a7e2
commit 58f67240d7
67 changed files with 939 additions and 86 deletions
+142 -13
View File
@@ -64,15 +64,14 @@ func (s *Service) ReportTorque(ctx context.Context, req TorqueReq, operator stri
return nil
}
// evaluateTorqueCriterion 依据 step_criterion RANGE 自动判定 OK/NG 写入 step_data
// evaluateTorqueCriterion 依据「isTorque 步骤 + RANGE 考核标准」自动判定 OK/NG 写入 step_data。
// 判定完全依据步骤 isTorque 标记与区间逻辑,不再依赖指标名(如「扭矩」),
// 避免误命名(如「时间」)导致整段判定静默失效;与 dashboard.loadTorqueCriterion 共用
// torqueRangeCriteria,口径统一。仅取上报工位所绑定工艺流程内的扭矩标准,跨流程/无关步骤不会误判。
func (s *Service) evaluateTorqueCriterion(ctx context.Context, sn, stationNo string, strain, angle float64, operator string) {
q := s.ctx.EntClient.StepCriterion.Query().Where(stepcriterion.Name("扭矩"))
crits, _ := q.All(ctx)
crits, _ := s.torqueRangeCriteria(ctx, s.stationFlowId(ctx, stationNo))
ng := false
for _, c := range crits {
if c.Logic != "RANGE" {
continue
}
ok := true
if c.Min != nil && strain < *c.Min {
ok = false
@@ -98,6 +97,43 @@ func (s *Service) evaluateTorqueCriterion(ctx context.Context, sn, stationNo str
}
}
// stationFlowId 由工位号取绑定工艺流程 ID;未绑定/非法工位返回 0。
func (s *Service) stationFlowId(ctx context.Context, stationNo string) int {
no, err := strconv.Atoi(strings.TrimSpace(stationNo))
if err != nil || no <= 0 {
return 0
}
st, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(no)).First(ctx)
if err != nil || st.FlowId <= 0 {
return 0
}
return st.FlowId
}
// torqueRangeCriteria 取「isTorque 步骤 + RANGE 逻辑」的考核标准(即扭矩规格)。
// flowId>0 限定某个工艺流程;flowId<=0 取全部流程(供看板全局控制图使用)。
// 判定完全依据步骤 isTorque 标记与区间逻辑,不依赖指标名,杜绝「时间」等误命名导致漏判。
func (s *Service) torqueRangeCriteria(ctx context.Context, flowId int) ([]*ent.StepCriterion, error) {
stepQ := s.ctx.EntClient.ProcessStep.Query().Where(processstep.IsTorque(true))
if flowId > 0 {
stepQ = stepQ.Where(processstep.FlowId(flowId))
}
steps, err := stepQ.All(ctx)
if err != nil {
return nil, err
}
if len(steps) == 0 {
return nil, nil
}
ids := make([]int, 0, len(steps))
for _, st := range steps {
ids = append(ids, st.ID)
}
return s.ctx.EntClient.StepCriterion.Query().
Where(stepcriterion.StepIdIn(ids...), stepcriterion.Logic("RANGE")).
All(ctx)
}
// ListTorqueRecords 查询拧紧数据(四条件复合取交集:工单号/SN/日期区间/工位)
func (s *Service) ListTorqueRecords(ctx context.Context, sn, workOrderNo, stationNo string, from, to time.Time) ([]*ent.TorqueRecord, error) {
q := s.ctx.EntClient.TorqueRecord.Query()
@@ -123,16 +159,12 @@ func (s *Service) ListTorqueRecords(ctx context.Context, sn, workOrderNo, statio
// requiredScrews 按工位号取该工位绑定工艺流程中 isTorque=true 的步骤数 = 应拧数量。
// 工位未绑定流程或流程无扭矩步骤时返回 0(表示无工艺基准,前端标注为「未配置」)。
func (s *Service) requiredScrews(ctx context.Context, stationNo string) int {
no, err := strconv.Atoi(strings.TrimSpace(stationNo))
if err != nil || no <= 0 {
return 0
}
st, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(no)).First(ctx)
if err != nil || st.FlowId <= 0 {
flowId := s.stationFlowId(ctx, stationNo)
if flowId <= 0 {
return 0
}
cnt, _ := s.ctx.EntClient.ProcessStep.Query().
Where(processstep.FlowId(st.FlowId), processstep.IsTorque(true)).Count(ctx)
Where(processstep.FlowId(flowId), processstep.IsTorque(true)).Count(ctx)
return cnt
}
@@ -284,6 +316,103 @@ func (s *Service) ListTorqueAuditLog(ctx context.Context, recordId int) ([]*ent.
Order(ent.Desc(torqueauditlog.FieldCreatedAt)).All(ctx)
}
// ListTorquePendingGroups 巡检终端「待签任务」:聚合 AuditBy==""(未审核)的拧紧记录,
// 按 工单号+SN+工位 分组(口径同 ListTorqueGroups),供检验员集中第二签。
// Actual/OkCount/NgCount 只统计未审核子集,LastTime/Operator 取子集中最近采集记录;
// 按 LastTime 倒序,后端真分页,返回 {"total","list","page","pageSize"}。
func (s *Service) ListTorquePendingGroups(ctx context.Context, page, pageSize int) (map[string]any, error) {
recs, err := s.ctx.EntClient.TorqueRecord.Query().
Where(torquerecord.AuditByEqualFold("")).
Order(ent.Desc(torquerecord.FieldTime)).
All(ctx)
if err != nil {
return nil, err
}
type gkey struct{ wo, sn, st string }
agg := map[gkey]*TorqueGroupVO{}
reqCache := map[string]int{}
for _, r := range recs {
k := gkey{r.WorkOrderNo, r.Sn, r.StationNo}
g, ok := agg[k]
if !ok {
g = &TorqueGroupVO{WorkOrderNo: r.WorkOrderNo, Sn: r.Sn, StationNo: r.StationNo}
agg[k] = g
}
g.Actual++
if r.Result == "OK" {
g.OkCount++
} else {
g.NgCount++
}
if r.Time.After(g.LastTime) {
g.LastTime = r.Time
g.Operator = r.Operator
}
}
list := make([]*TorqueGroupVO, 0, len(agg))
for _, g := range agg {
req, ok := reqCache[g.StationNo]
if !ok {
req = s.requiredScrews(ctx, g.StationNo)
reqCache[g.StationNo] = req
}
g.Required = req
g.Satisfied = req > 0 && g.Actual >= req && g.NgCount == 0
g.Audited = false // 待签列表只含未审核记录,整组必然未审核
list = append(list, g)
}
sort.Slice(list, func(i, j int) bool { return list[i].LastTime.After(list[j].LastTime) })
total := len(list)
if page <= 0 {
page = 1
}
if pageSize <= 0 {
pageSize = 20
}
start := (page - 1) * pageSize
if start > total {
start = total
}
end := start + pageSize
if end > total {
end = total
}
return map[string]any{"total": total, "list": list[start:end], "page": page, "pageSize": pageSize}, nil
}
// AuditTorqueGroup 拧紧分组批量审核签字(巡检终端「待签任务」):把该组(工单号+SN+工位)
// 全部未审核记录一次性标记审核人/时间,并逐条写 TorqueAuditLog 留痕,返回签字条数。
// 组内没有未审核记录时返回明确错误,避免前端对已签字任务重复提交静默成功。
func (s *Service) AuditTorqueGroup(ctx context.Context, workOrderNo, sn, stationNo, remark, operator string) (int, error) {
if strings.TrimSpace(sn) == "" {
return 0, errors.New("sn 不能为空")
}
recs, err := s.ctx.EntClient.TorqueRecord.Query().
Where(
torquerecord.WorkOrderNoEqualFold(workOrderNo),
torquerecord.SnEqualFold(sn),
torquerecord.StationNoEqualFold(stationNo),
torquerecord.AuditByEqualFold(""),
).All(ctx)
if err != nil {
return 0, err
}
if len(recs) == 0 {
return 0, errors.New("该任务没有待审核的拧紧记录,可能已签字")
}
now := time.Now()
for _, rec := range recs {
if _, err := rec.Update().SetAuditBy(operator).SetAuditAt(now).Save(ctx); err != nil {
return 0, err
}
_, _ = s.ctx.EntClient.TorqueAuditLog.Create().
SetTorqueRecordId(rec.ID).SetAction("审核").SetOperator(operator).
SetRemark(remark).SetOldVal(rec.AuditBy).SetNewVal(operator).Save(ctx)
}
return len(recs), nil
}
// ProcessStepTemplate 下发给前端渲染的参数采集模板
type ProcessStepTemplate struct {
ID int `json:"id"`