204 lines
8.3 KiB
Go
204 lines
8.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
"bj_power_mes/ent"
|
|
"bj_power_mes/ent/plcsendlog"
|
|
"bj_power_mes/ent/workorder"
|
|
"bj_power_mes/ent/workpiece"
|
|
)
|
|
|
|
type PlcSendReq struct {
|
|
OrderNo string `json:"orderNo"`
|
|
Sn string `json:"sn"`
|
|
StationNo int `json:"stationNo"`
|
|
}
|
|
|
|
// manualConfirmStationNos 人工确认工位(遗留4 PLC 标记):11/12 为下料检测位,
|
|
// 无 PLC 装配动作。流程流转到这两个工位时不做 PLC 工序码下发、不等完成信号,
|
|
// 走人工确认分支:下发日志直接置 DONE(人工确认即完成),流转由人工在工位终端推进。
|
|
var manualConfirmStationNos = []int{11, 12}
|
|
|
|
// isManualConfirmStation 判断工位是否走人工确认分支(11/12 下料检测位)
|
|
func isManualConfirmStation(no int) bool {
|
|
for _, n := range manualConfirmStationNos {
|
|
if no == n {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// SendProcess 下发 PLC 工序码。
|
|
// 路线 = 工单工艺组合(唯一路线源头,工位号升序),不再按日派工派生。
|
|
// 约束:① SN 必须属于所选工单且已进线(未完工);② 上一条工序未收到完成信号,不下发下一条(模拟 S7 握手)。
|
|
func (s *Service) SendProcess(ctx context.Context, req PlcSendReq, operator string) (*ent.PlcSendLog, error) {
|
|
if req.OrderNo == "" {
|
|
return nil, errors.New("请选择工单号")
|
|
}
|
|
if req.Sn == "" {
|
|
return nil, errors.New("请填写/扫描工件 SN")
|
|
}
|
|
// 校验工单并取工艺组合(唯一路线源头)
|
|
wo, err := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNo(req.OrderNo)).First(ctx)
|
|
if err != nil {
|
|
return nil, errors.New("工单不存在:" + req.OrderNo)
|
|
}
|
|
if wo.Status == "DONE" || wo.Status == "CANCELLED" {
|
|
return nil, errors.New("该工单已完成或已取消,不能下发")
|
|
}
|
|
items, _ := RouteStationsFromWO(ctx, s.ctx.EntClient, wo)
|
|
if len(items) == 0 {
|
|
return nil, errors.New("该工单未配置工艺组合,无法下发 PLC 工序码")
|
|
}
|
|
combination := RouteStationsStrOfWO(items)
|
|
route := RouteStationsOfWO(items)
|
|
// 校验①:SN 归属工单且已进线
|
|
wp, err := s.ctx.EntClient.Workpiece.Query().
|
|
Where(workpiece.Sn(req.Sn), workpiece.OrderNo(req.OrderNo)).First(ctx)
|
|
if err != nil {
|
|
return nil, errors.New("该 SN 不属于当前工单或未进线登记(请先在工件进线中登记)")
|
|
}
|
|
if wp.Status == "DONE" {
|
|
return nil, errors.New("该工件已完工,无需再次下发")
|
|
}
|
|
// 检查是否存在未完成的同工位下发
|
|
last, err := s.ctx.EntClient.PlcSendLog.Query().
|
|
Where(plcsendlog.Status("SENT")).Order(plcsendlog.ByID()).First(ctx)
|
|
if err == nil && last != nil {
|
|
done, qErr := s.ctx.PLC.Get().QueryDone(ctx)
|
|
if qErr != nil || !done {
|
|
return nil, errors.New("上一条工序尚未收到PLC完成信号,不能下发下一条")
|
|
}
|
|
// 标记上一条为完成
|
|
_, _ = s.ctx.EntClient.PlcSendLog.UpdateOneID(last.ID).
|
|
SetStatus("DONE").SetDoneTime(time.Now()).SetAck(true).Save(ctx)
|
|
}
|
|
now := time.Now()
|
|
rec, err := s.ctx.EntClient.PlcSendLog.Create().
|
|
SetOrderNo(req.OrderNo).SetSn(req.Sn).SetStationNo(req.StationNo).
|
|
SetProcessCombination(combination).SetProcessCodes(route).
|
|
SetStatus("SENT").SetSendTime(now).SetOperator(operator).Save(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// 标记(遗留4,仅记录、不改下发流程):11/12 为下料检测位「人工确认位」——仅打点日志,
|
|
// 仍走下方通用下发路径。本次已撤回此前"跳过下发直接 DONE"的自作主张改动:下发流程恢复原样,
|
|
// 11/12 是否走专用分支待 PLC 方提供「物理点位定义书」后由用户决策,不在代码侧擅改业务规则。
|
|
if isManualConfirmStation(req.StationNo) {
|
|
s.ctx.EventLog.Write(ctx, "plc.send.mark", req.OrderNo, operator, "plc_send_log", req.Sn,
|
|
"下料检测位人工确认位(按普通工位下发,待PLC点位定义书)", map[string]any{"stationNo": req.StationNo, "combination": combination})
|
|
}
|
|
// 模拟下行下发
|
|
_, err = s.ctx.PLC.Get().SendProcessCode(ctx, combination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
s.ctx.EventLog.Write(ctx, "plc.send", req.OrderNo, operator, "plc_send_log", req.Sn, "工位下发", map[string]any{"combination": combination})
|
|
|
|
// mock 完成信号:延时后置为完成(演示"未完成不下发下一条"握手)。
|
|
// 条件更新(仅 SENT 才置 DONE):若记录已被人工撤回/收口,不得被模拟信号覆盖。
|
|
go func(id int) {
|
|
time.Sleep(5 * time.Second)
|
|
s.ctx.PLC.Get().AckDone(context.Background(), true)
|
|
_, _ = s.ctx.EntClient.PlcSendLog.Update().
|
|
Where(plcsendlog.ID(id), plcsendlog.StatusEQ("SENT")).
|
|
SetStatus("DONE").SetDoneTime(time.Now()).SetAck(true).Save(context.Background())
|
|
}(rec.ID)
|
|
|
|
return rec, nil
|
|
}
|
|
|
|
// WithdrawSend 撤回下发(2026-11-03 断头路补全):把误下发的指令收回来。
|
|
// 仅「已下发(SENT)」可撤回;已完成(DONE)/已撤回(CANCELLED) 不可撤。
|
|
// 撤回后该条不再占用"上一条未完成不能下发下一条"的握手位,可立即重新下发正确工位码。
|
|
func (s *Service) WithdrawSend(ctx context.Context, id int, reason, operator string) (*ent.PlcSendLog, error) {
|
|
if id <= 0 {
|
|
return nil, errors.New("下发记录 ID 无效")
|
|
}
|
|
if reason == "" {
|
|
return nil, errors.New("撤回原因必填(写清楚为什么撤,如:选错工件/工位)")
|
|
}
|
|
rec, err := s.ctx.EntClient.PlcSendLog.Query().Where(plcsendlog.ID(id)).First(ctx)
|
|
if err != nil {
|
|
return nil, errors.New("下发记录不存在")
|
|
}
|
|
if rec.Status != "SENT" {
|
|
return nil, fmt.Errorf("仅「已下发」状态可撤回(当前状态:%s)", plcSendStatusText(rec.Status))
|
|
}
|
|
updated, err := s.ctx.EntClient.PlcSendLog.Update().
|
|
Where(plcsendlog.ID(id), plcsendlog.StatusEQ("SENT")).
|
|
SetStatus("CANCELLED").SetRemark("撤回:" + reason).
|
|
SetDoneTime(time.Now()).Save(ctx)
|
|
if err != nil || updated == 0 {
|
|
return nil, errors.New("撤回失败:记录状态已变化,请刷新后重试")
|
|
}
|
|
s.ctx.EventLog.Write(ctx, "plc.send.withdraw", rec.OrderNo, operator, "plc_send_log", rec.Sn,
|
|
"撤回工位下发(工位"+strconv.Itoa(rec.StationNo)+"):"+reason, nil)
|
|
return s.ctx.EntClient.PlcSendLog.Get(ctx, id)
|
|
}
|
|
|
|
// ManualComplete 人工完成收口(2026-11-03 断头路补全):PLC 完成信号丢失/产线未接入时,
|
|
// 现场确认工件实际已到位,由人工把「已下发」置为「已完成」,解除握手、放行下一条下发。
|
|
func (s *Service) ManualComplete(ctx context.Context, id int, operator string) (*ent.PlcSendLog, error) {
|
|
if id <= 0 {
|
|
return nil, errors.New("下发记录 ID 无效")
|
|
}
|
|
rec, err := s.ctx.EntClient.PlcSendLog.Query().Where(plcsendlog.ID(id)).First(ctx)
|
|
if err != nil {
|
|
return nil, errors.New("下发记录不存在")
|
|
}
|
|
if rec.Status != "SENT" {
|
|
return nil, fmt.Errorf("仅「已下发」状态可人工完成(当前状态:%s)", plcSendStatusText(rec.Status))
|
|
}
|
|
remark := "人工确认完成"
|
|
if rec.Remark == "" {
|
|
remark = "人工确认完成(PLC 完成信号丢失,现场确认到位)"
|
|
}
|
|
updated, err := s.ctx.EntClient.PlcSendLog.Update().
|
|
Where(plcsendlog.ID(id), plcsendlog.StatusEQ("SENT")).
|
|
SetStatus("DONE").SetAck(true).SetDoneTime(time.Now()).
|
|
SetRemark(remark).
|
|
Save(ctx)
|
|
if err != nil || updated == 0 {
|
|
return nil, errors.New("收口失败:记录状态已变化,请刷新后重试")
|
|
}
|
|
s.ctx.EventLog.Write(ctx, "plc.send.manual_done", rec.OrderNo, operator, "plc_send_log", rec.Sn,
|
|
"人工确认工位"+strconv.Itoa(rec.StationNo)+"流转完成(PLC 信号未回,现场收口)", nil)
|
|
return s.ctx.EntClient.PlcSendLog.Get(ctx, id)
|
|
}
|
|
|
|
// plcSendStatusText 下发状态中文(前端展示同口径)
|
|
func plcSendStatusText(s string) string {
|
|
switch s {
|
|
case "SENT":
|
|
return "已下发"
|
|
case "DONE":
|
|
return "已完成"
|
|
case "CANCELLED":
|
|
return "已撤回"
|
|
case "TIMEOUT":
|
|
return "已超时"
|
|
case "REPLY_NG":
|
|
return "回复异常"
|
|
default:
|
|
return s
|
|
}
|
|
}
|
|
|
|
// ListPlcSendLogs 查询 PLC 下发日志
|
|
func (s *Service) ListPlcSendLogs(ctx context.Context, orderNo, status string) ([]*ent.PlcSendLog, error) {
|
|
q := s.ctx.EntClient.PlcSendLog.Query()
|
|
if orderNo != "" {
|
|
q = q.Where(plcsendlog.OrderNo(orderNo))
|
|
}
|
|
if status != "" {
|
|
q = q.Where(plcsendlog.Status(status))
|
|
}
|
|
return q.Order(ent.Desc(plcsendlog.FieldID)).All(ctx)
|
|
} |