本提交完成了多个核心模块的功能完善与业务对齐: 1. 工位终端:固定工位配置、移除自选工位逻辑、适配配置化工位号 2. MES核心:重构工位组合逻辑、新增工单/流程状态管理、补全报工/追溯逻辑 3. WMS客户端:新增修改密码功能、优化帮助弹窗逻辑 4. 文档与权限:补充完整操作手册、统一菜单名称与权限描述 5. 修复多业务校验:排产数量校验、工单状态校验、工位绑定规则
151 lines
5.1 KiB
Go
151 lines
5.1 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"bj_power_mes/ent"
|
|
"bj_power_mes/ent/processflow"
|
|
"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
|
|
}
|
|
|
|
// ProcessStepTemplate 下发给前端渲染的参数采集模板
|
|
type ProcessStepTemplate struct {
|
|
ID int `json:"id"`
|
|
ProcessCode int `json:"processCode"`
|
|
Seq int `json:"seq"`
|
|
Name string `json:"name"`
|
|
CollectType string `json:"collectType"` // AUTO(拧紧枪自动)/MANUAL(手填)/NONE
|
|
IsTorque bool `json:"isTorque"`
|
|
Remark string `json:"remark"`
|
|
Criteria []CriterionVO `json:"criteria"`
|
|
}
|
|
|
|
// CriterionVO 考核标准视图
|
|
type CriterionVO struct {
|
|
ID int `json:"id"`
|
|
Name string `json:"name"`
|
|
Unit string `json:"unit"`
|
|
Logic string `json:"logic"` // GE/LE/GT/LT/RANGE/EQUAL/NONE
|
|
Target float64 `json:"target"`
|
|
Min *float64 `json:"min"`
|
|
Max *float64 `json:"max"`
|
|
}
|
|
|
|
// ListStationSteps 按工位号加载该工位「启用」工艺流程的工序步骤(含考核标准)。
|
|
// 手动报工页:选工位 → 自动带出该工位的工序步骤。
|
|
func (s *Service) ListStationSteps(ctx context.Context, stationNo int) ([]*ProcessStepTemplate, error) {
|
|
if stationNo < 1 || stationNo > 12 {
|
|
return []*ProcessStepTemplate{}, nil
|
|
}
|
|
flow, err := s.ctx.EntClient.ProcessFlow.Query().
|
|
Where(processflow.ProcessCode(stationNo), processflow.Status("ACTIVE")).First(ctx)
|
|
if err != nil {
|
|
// 该工位未配置启用的工艺流程:返回空模板(前端提示)
|
|
return []*ProcessStepTemplate{}, nil
|
|
}
|
|
return s.flowSteps(ctx, flow.ID, flow.ProcessCode), nil
|
|
}
|
|
|
|
// StepItemReq 一次保存的一个步骤(含其考核标准)
|
|
type StepItemReq struct {
|
|
ID int `json:"id"`
|
|
ProcessCode int `json:"processCode"`
|
|
Seq int `json:"seq"`
|
|
Name string `json:"name"`
|
|
CollectType string `json:"collectType"`
|
|
IsTorque bool `json:"isTorque"`
|
|
Remark string `json:"remark"`
|
|
Criteria []CriterionVO `json:"criteria"`
|
|
}
|