100 lines
3.2 KiB
Go
100 lines
3.2 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"bj_power_mes/ent"
|
|
"bj_power_mes/ent/stepcriterion"
|
|
"bj_power_mes/ent/torquerecord"
|
|
"bj_power_mes/ent/workpieceprocess"
|
|
)
|
|
|
|
type TorqueReq struct {
|
|
Sn string `json:"sn"`
|
|
WorkOrder string `json:"workOrder"`
|
|
WorkOrderNo string `json:"workOrderNo"`
|
|
StationNo string `json:"stationNo"`
|
|
ScrewNo string `json:"screwNo"`
|
|
Strain float64 `json:"strain"` // 扭矩
|
|
Torque float64 `json:"torque"`
|
|
Angle float64 `json:"angle"`
|
|
Result string `json:"result"`
|
|
Operator string `json:"operator"`
|
|
Time time.Time `json:"time"`
|
|
}
|
|
|
|
// ReportTorque 接收拧紧数据(工位终端/内部接口上报),入库并做扭矩 RANGE 判定写 step_data
|
|
func (s *Service) ReportTorque(ctx context.Context, req TorqueReq, operator string) error {
|
|
if req.Sn == "" {
|
|
return errors.New("sn 不能为空")
|
|
}
|
|
if req.Strain == 0 {
|
|
req.Strain = req.Torque
|
|
}
|
|
result := req.Result
|
|
if result == "" {
|
|
result = "OK"
|
|
}
|
|
workOrderNo := req.WorkOrder
|
|
if workOrderNo == "" {
|
|
workOrderNo = req.WorkOrderNo
|
|
}
|
|
if req.Time.IsZero() {
|
|
req.Time = time.Now()
|
|
}
|
|
_, err := s.ctx.EntClient.TorqueRecord.Create().
|
|
SetWorkOrderNo(workOrderNo).SetSn(req.Sn).SetScrewNo(req.ScrewNo).
|
|
SetStrain(req.Strain).SetTorque(req.Strain).SetAngle(req.Angle).
|
|
SetResult(result).SetOperator(req.Operator).SetStationNo(req.StationNo).
|
|
SetTime(req.Time).Save(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.ctx.EventLog.Write(ctx, "torque.report", workOrderNo, operator, "torque_record", req.Sn, "接收拧紧数据上报", map[string]any{"strain": req.Strain, "angle": req.Angle, "result": result})
|
|
s.evaluateTorqueCriterion(ctx, req.Sn, req.StationNo, req.Strain, req.Angle, operator)
|
|
return nil
|
|
}
|
|
|
|
// evaluateTorqueCriterion 依据 step_criterion RANGE 自动判定 OK/NG 写入 step_data
|
|
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)
|
|
for _, c := range crits {
|
|
if c.Logic != "RANGE" {
|
|
continue
|
|
}
|
|
ok := true
|
|
if c.Min != nil && strain < *c.Min {
|
|
ok = false
|
|
}
|
|
if c.Max != nil && strain > *c.Max {
|
|
ok = false
|
|
}
|
|
_, _ = s.ctx.EntClient.StepData.Create().
|
|
SetSn(sn).SetStepId(c.StepId).SetCriterionId(c.ID).
|
|
SetCriterionName(c.Name).SetCriterionLogic(c.Logic).
|
|
SetValue(strain).SetIsOK(ok).SetOperator(operator).Save(ctx)
|
|
}
|
|
}
|
|
|
|
// ListTorqueRecords 查询拧紧数据
|
|
func (s *Service) ListTorqueRecords(ctx context.Context, sn, workOrderNo string, from, to time.Time) ([]*ent.TorqueRecord, error) {
|
|
q := s.ctx.EntClient.TorqueRecord.Query()
|
|
if sn != "" {
|
|
q = q.Where(torquerecord.Sn(sn))
|
|
}
|
|
if workOrderNo != "" {
|
|
q = q.Where(torquerecord.WorkOrderNo(workOrderNo))
|
|
}
|
|
return q.Order(ent.Desc(torquerecord.FieldTime)).Limit(500).All(ctx)
|
|
}
|
|
|
|
// 计算一块工件某工序的扭矩步骤是否已报
|
|
func (s *Service) hasWorkpieceProcess(ctx context.Context, sn string, processCode int) bool {
|
|
cnt, _ := s.ctx.EntClient.WorkpieceProcess.Query().
|
|
Where(workpieceprocess.Sn(sn), workpieceprocess.ProcessCode(processCode)).Count(ctx)
|
|
return cnt > 0
|
|
}
|