AGV 调度: - 任务重构为 GO_LOAD/TRANSPORT/EMPTY_RETURN/FREE 三段链,到位即停(ARRIVED)需人工确认装卸 - 自由调度单点步进,每段强制弹"请确保路径畅通无杂物";托盘回收独立工作台 - 接驳台 PLC 传感器检测到托盘即自动卸货放行,传感器故障由人工按钮兜底(无轮询) - 车辆状态只读展示(海康3.1.7 queryAgvStatus),调度仍由 RCS 负责 物料闭环: - 补料管理(SHORTAGE):工位申请 + 主管审批调增叫料池额度,与借料/差异补发三轨并行 - 损耗台账(loss_ledger):退料实际 < 理论必填原因、后审核,工单不记损耗 - 退料强制指定左/右接驳台,WMS 库管可代工位退料 下班闭环: - 工位终端自检接驳台剩余物料后下班;库管收货 + 托盘拉走后事件驱动自动结班 - 退出登录未下班仅作安全提示(不强制拦截、不新增责任审批) 接驳台左右按操作员朝向建模(dock.side 字段),废除"奇左偶右"隐式约定; 看板标签 / seed / 迁移补丁 / 布局文档统一为中文左/右。 产线布局说明:C1 前线头 AGV 点、C15 后线尾 AGV 点、外部 O1/O2/O3 散点, 清理修订痕迹为最终态描述。
107 lines
4.7 KiB
Go
107 lines
4.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"bj_power_mes/ent"
|
|
"bj_power_mes/ent/dock"
|
|
"bj_power_mes/ent/stationshift"
|
|
)
|
|
|
|
// ============ 工位班次(2026-11-03 拍板「下班/结束作业」闭环) ============
|
|
// 工人登录建班;下班时工人**自己目视**左右接驳台有无剩余物料(系统不替他查)——
|
|
// 确认无 → 直接下班(CLOSED);有剩余 → 走退料流程后即可离场,班次置 CLOSING,
|
|
// 待库管收货+空托盘拉走后由系统事件驱动(ConfirmReturn / 卸货放行)自动置 CLOSED。
|
|
// 未下班退出登录 → 终端提示安全警示(不强制拦截,留痕)。
|
|
|
|
// EnsureShiftOnLogin 登录建班(无进行中班次则创建 ON_DUTY;已有则复用)。
|
|
func (s *Service) EnsureShiftOnLogin(ctx context.Context, stationNo int, operator string) error {
|
|
exists, _ := s.ctx.EntClient.StationShift.Query().
|
|
Where(stationshift.StationNo(stationNo),
|
|
stationshift.StatusIn("ON_DUTY", "CLOSING")).Count(ctx)
|
|
if exists > 0 {
|
|
return nil
|
|
}
|
|
_, err := s.ctx.EntClient.StationShift.Create().
|
|
SetStationNo(stationNo).SetOperator(operator).SetStatus("ON_DUTY").
|
|
SetStartAt(time.Now().Unix()).Save(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.ctx.EventLog.Write(ctx, "shift.start", "", operator, "station_shift",
|
|
fmt.Sprintf("station:%d", stationNo), "工位上班建班", nil)
|
|
return nil
|
|
}
|
|
|
|
// GetShift 当前班次状态(无进行中班次返回 nil)。
|
|
func (s *Service) GetShift(ctx context.Context, stationNo int) (*ent.StationShift, error) {
|
|
return s.ctx.EntClient.StationShift.Query().
|
|
Where(stationshift.StationNo(stationNo),
|
|
stationshift.StatusIn("ON_DUTY", "CLOSING")).
|
|
Order(ent.Desc(stationshift.FieldCreatedAt)).First(ctx)
|
|
}
|
|
|
|
// OffDuty 下班/结束作业:工人自检接驳台后提交。
|
|
// - hasLeftover=false:确认左右接驳台无剩余物料 → 班次置 CLOSED,可退出下班;
|
|
// - hasLeftover=true:有剩余物料 → 走退料流程(returns 复用转产按工单归集:指定接驳台/
|
|
// 实际数量/损耗原因),生成退库单后班次置 CLOSING,工人即可离场;库管收货+空托盘拉走后
|
|
// 由事件驱动自动置 CLOSED。
|
|
func (s *Service) OffDuty(ctx context.Context, stationNo int, operator string, hasLeftover bool, returns []TransferReturnInput) error {
|
|
shift, err := s.GetShift(ctx, stationNo)
|
|
if err != nil || shift == nil {
|
|
// 无班次(如异常退出后重进)→ 允许直接补记下班
|
|
if !hasLeftover {
|
|
return nil
|
|
}
|
|
return errors.New("无进行中班次,请先登录再操作")
|
|
}
|
|
if !hasLeftover {
|
|
if _, uerr := s.ctx.EntClient.StationShift.UpdateOneID(shift.ID).
|
|
SetStatus("CLOSED").SetEndApplyAt(time.Now().Unix()).SetClosedAt(time.Now().Unix()).
|
|
SetRemark("工人自检:左右接驳台无剩余物料,正常下班").Save(ctx); uerr != nil {
|
|
return uerr
|
|
}
|
|
s.ctx.EventLog.Write(ctx, "shift.close", operator, "", "station_shift",
|
|
fmt.Sprintf("station:%d", stationNo), "下班:确认接驳台无剩余物料", nil)
|
|
return nil
|
|
}
|
|
// 有剩余物料:生成退库单(指定接驳台、实际数量、损耗原因必填校验在 CreateTransferReturns 内)
|
|
if err := s.CreateTransferReturns(ctx, stationNo, operator, returns); err != nil {
|
|
return err
|
|
}
|
|
if _, uerr := s.ctx.EntClient.StationShift.UpdateOneID(shift.ID).
|
|
SetStatus("CLOSING").SetEndApplyAt(time.Now().Unix()).
|
|
SetRemark("有剩余物料已发起退料,待库管收货+空托盘拉走后自动结班").Save(ctx); uerr != nil {
|
|
return uerr
|
|
}
|
|
s.ctx.EventLog.Write(ctx, "shift.closing", operator, "", "station_shift",
|
|
fmt.Sprintf("station:%d", stationNo), "下班申请:有剩余物料退料中,工人可离场", nil)
|
|
return nil
|
|
}
|
|
|
|
// maybeCloseShifts 事件驱动收口(无轮询):在「退料收货确认」与「卸货放行」后调用——
|
|
// 工位处于 CLOSING 且该工位全部接驳台 hasPallet=false(料已退、托盘已拉走)→ 班次自动置 CLOSED。
|
|
func (s *Service) maybeCloseShifts(ctx context.Context) {
|
|
shifts, err := s.ctx.EntClient.StationShift.Query().
|
|
Where(stationshift.StatusEQ("CLOSING")).All(ctx)
|
|
if err != nil {
|
|
return
|
|
}
|
|
for _, sh := range shifts {
|
|
left, _ := s.ctx.EntClient.Dock.Query().
|
|
Where(dock.StationNo(sh.StationNo), dock.HasPallet(true)).Count(ctx)
|
|
if left > 0 {
|
|
continue // 该工位仍有接驳台带托盘,继续等待
|
|
}
|
|
if _, uerr := s.ctx.EntClient.StationShift.UpdateOneID(sh.ID).
|
|
SetStatus("CLOSED").SetClosedAt(time.Now().Unix()).
|
|
SetRemark("库管收货+托盘回收完成,班次自动结班").Save(ctx); uerr == nil {
|
|
s.ctx.EventLog.Write(ctx, "shift.auto_close", "", "系统", "station_shift",
|
|
fmt.Sprintf("station:%d", sh.StationNo), "退料收货+托盘回收完成,班次自动结班", nil)
|
|
}
|
|
}
|
|
}
|