82 lines
3.0 KiB
Go
82 lines
3.0 KiB
Go
package logic
|
||||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"errors"
|
|||
|
|
"fmt"
|
|||
|
|
"log"
|
|||
|
|
"strconv"
|
|||
|
|
"strings"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"bj_power_mes/ent/workpiece"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// 人工跳过(设备未连接)的步骤数据前缀:流程卡据此单独列示,不混进设备拧紧数据。
|
|||
|
|
const TorqueSkipNamePrefix = "拧紧跳过(设备未连接)"
|
|||
|
|
|
|||
|
|
// TorqueSkipReq 拧紧枪未连接时的人工跳过上报(工位终端 → MES)。
|
|||
|
|
// 与设备判 OK/NG 是两条完全不同的链路:这里不写 torque_record,绝不冒充设备结果。
|
|||
|
|
type TorqueSkipReq struct {
|
|||
|
|
Sn string `json:"sn"`
|
|||
|
|
WorkOrderNo string `json:"workOrderNo"`
|
|||
|
|
StepId string `json:"stepId"`
|
|||
|
|
StepName string `json:"stepName"`
|
|||
|
|
Reason string `json:"reason"`
|
|||
|
|
StationNo string `json:"stationNo"`
|
|||
|
|
Operator string `json:"operator"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ReportTorqueSkip 设备未连接导致无采集结果时的人工跳过留痕。
|
|||
|
|
// 口径(2026-11-27 用户拍板):
|
|||
|
|
// ① 只在拧紧前、控制器未连接时允许;终端点「跳过」须填原因,操作人取工位登录态,不可冒签。
|
|||
|
|
// ② 写 step_data(流程卡/追溯可查:哪一步、谁、什么原因)+ event_log(完整原因)+ 管理屏预警。
|
|||
|
|
// ③ 不写 torque_record、不算设备判 OK、不进合格率与扭矩统计;与 NG 人工放行(DisposeTorqueNg)分开。
|
|||
|
|
func (s *Service) ReportTorqueSkip(ctx context.Context, req TorqueSkipReq, operator string) error {
|
|||
|
|
sn := strings.TrimSpace(req.Sn)
|
|||
|
|
reason := strings.TrimSpace(req.Reason)
|
|||
|
|
stepName := strings.TrimSpace(req.StepName)
|
|||
|
|
if sn == "" {
|
|||
|
|
return errors.New("sn 必填")
|
|||
|
|
}
|
|||
|
|
if reason == "" {
|
|||
|
|
return errors.New("跳过原因必填")
|
|||
|
|
}
|
|||
|
|
if stepName == "" {
|
|||
|
|
return errors.New("拧紧步骤必填")
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
stepId, _ := strconv.Atoi(strings.TrimSpace(req.StepId))
|
|||
|
|
orderNo := strings.TrimSpace(req.WorkOrderNo)
|
|||
|
|
if orderNo == "" {
|
|||
|
|
if wp, e := s.ctx.EntClient.Workpiece.Query().Where(workpiece.Sn(sn)).First(ctx); e == nil && wp != nil {
|
|||
|
|
orderNo = wp.OrderNo
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// valueText 受 step_data 的 64 字节上限,按字节安全截断(ent MaxLen 按字节校验);
|
|||
|
|
// 完整原因不丢:event_log 里保留原文。
|
|||
|
|
if _, err := s.ctx.EntClient.StepData.Create().
|
|||
|
|
SetSn(sn).SetStepId(stepId).
|
|||
|
|
SetStepName(truncateUTF8(TorqueSkipNamePrefix+":"+stepName, 120)).
|
|||
|
|
SetValueText(truncateUTF8(reason, 60)).
|
|||
|
|
SetIsOK(true).SetOperator(operator).Save(ctx); err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
s.ctx.EventLog.Write(ctx, "torque.skip", orderNo, operator, "workpiece", sn,
|
|||
|
|
"拧紧步骤人工跳过(设备未连接)",
|
|||
|
|
map[string]any{
|
|||
|
|
"reason": reason, "stepName": stepName, "stepId": req.StepId,
|
|||
|
|
"stationNo": req.StationNo, "operator": operator, "at": time.Now().Format(time.RFC3339),
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
msg := fmt.Sprintf("拧紧步骤人工跳过:SN=%s 工位=%s 步骤=%s 原因=%s 操作人=%s",
|
|||
|
|
sn, req.StationNo, stepName, reason, operator)
|
|||
|
|
if err := s.CreateAlert(ctx, "torque_skip", "warning", "SN="+sn+" 工位"+req.StationNo, msg); err != nil {
|
|||
|
|
log.Printf("[torque] 写跳过预警失败 sn=%s: %v", sn, err)
|
|||
|
|
}
|
|||
|
|
s.notifyDashboard()
|
|||
|
|
return nil
|
|||
|
|
}
|