- MES 新增损耗台账 loss_ledger,差异确认损耗接口 ConfirmReceiveLoss(按工单归集) - 备料清单 preparation:MES 按 日计划×工单×工位×物料 实时计算,WMS 只读展示 PreparationList - 退料闭环:N5 工位托盘守卫(未完成禁止退料)、退料 AGV 自动派车(JBT21 空位判定 + 托盘回收链路)、退料参考剩余量接口(应剩余=实备-已耗-已退-已损) - 新增 AGV 车辆状态表 agv_vehicle_state 及查询 - 整合接驳台/叫料池/库存锁既有闭环,清理 sql 目录注释生成脚本与 catalog,更新文档
215 lines
7.4 KiB
Go
215 lines
7.4 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
"sort"
|
|
"strconv"
|
|
|
|
"bj_power_mes/ent"
|
|
"bj_power_mes/ent/agvtask"
|
|
"bj_power_mes/ent/dailyplan"
|
|
"bj_power_mes/ent/flowmaterial"
|
|
"bj_power_mes/ent/workorder"
|
|
)
|
|
|
|
// ---------- P1 备料清单(2026-11-04 用户拍板:WMS 只读展示页,数据源 MES 日排产) ----------
|
|
//
|
|
// 维度:日期 × 工单 × 工位 × 物料 × 数量。物料实时算(不加快照字段):
|
|
// 工单工艺路线(工位→物料→单台用量)× 该工位分产量 = 该工位物料需求;
|
|
// 未配工位分产量则按同工艺工位平均分(复用 splitQtyToStations,与叫料池同规则)。
|
|
// 折损率不参与计算(R7 2026-11-03 拍板,与 call.go 备料量口径一致)。
|
|
// 状态排除 DONE/CANCELLED(只显示未完成/已启用/执行中);只看今天及以后;不管库存。
|
|
|
|
// PreparationRow 备料清单行:一行 = 某排产日 × 某工单 × 某工位 × 某物料。
|
|
type PreparationRow struct {
|
|
PlanDate string `json:"planDate"` // 排产日期 yyyy-MM-dd
|
|
OrderNo string `json:"orderNo"` // 工单号
|
|
ProductName string `json:"productName"` // 产品名称(工单产品,展示用)
|
|
StationNo int `json:"stationNo"` // 工位号
|
|
StationQty int `json:"stationQty"` // 该工位分产量(台)
|
|
MaterialCode string `json:"materialCode"` // 物料编码(图号)
|
|
MaterialName string `json:"materialName"` // 物料名称
|
|
Spec string `json:"spec"` // 规格
|
|
Unit string `json:"unit"` // 单位
|
|
Qty int `json:"qty"` // 该工位该物料需求量 = ceil(分产量×单台用量)
|
|
UnitQty float64 `json:"unitQty"` // 单台用量(展示,便于核对)
|
|
Status string `json:"status"` // 排产状态 PENDING/CONFIRMED/PROCESSING
|
|
}
|
|
|
|
// PreparationList 备料清单返回结构。
|
|
type PreparationList struct {
|
|
Rows []PreparationRow `json:"rows"` // 明细行(日期×工单×工位×物料)
|
|
Total int `json:"total"` // 明细行数
|
|
Range string `json:"range"` // 覆盖日期范围说明(今天~最远排产日)
|
|
PlanCnt int `json:"planCnt"` // 覆盖排产行数
|
|
}
|
|
|
|
// ComputePreparationList 计算备料清单:今天及以后、未完成/已启用/执行中的日排产 → 展开到工位×物料。
|
|
func (s *Service) ComputePreparationList(ctx context.Context) (*PreparationList, error) {
|
|
today := TodayStr()
|
|
plans, err := s.ctx.EntClient.DailyPlan.Query().
|
|
Where(
|
|
dailyplan.PlanDateGTE(today),
|
|
dailyplan.StatusNEQ("DONE"),
|
|
dailyplan.StatusNEQ("CANCELLED"),
|
|
).
|
|
Order(dailyplan.ByPlanDate(), dailyplan.ByOrderNo()).
|
|
All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]PreparationRow, 0, 64)
|
|
productNameByOrder := map[string]string{}
|
|
farDate := today
|
|
for _, p := range plans {
|
|
wo, werr := s.ctx.EntClient.WorkOrder.Query().
|
|
Where(workorder.WorkOrderNo(p.OrderNo)).First(ctx)
|
|
if werr != nil || wo == nil {
|
|
continue
|
|
}
|
|
items, rerr := RouteStationsFromWO(ctx, s.ctx.EntClient, wo)
|
|
if rerr != nil || len(items) == 0 {
|
|
continue // 工单无工艺组合 → 无法展开物料,跳过(与叫料池同口径)
|
|
}
|
|
if pn := wo.ProductName; pn != "" {
|
|
productNameByOrder[p.OrderNo] = pn
|
|
}
|
|
// 工艺→物料清单(同工单内同 flowId 只查一次)
|
|
fmByFlow := map[int][]*ent.FlowMaterial{}
|
|
for _, it := range items {
|
|
if _, done := fmByFlow[it.FlowId]; done {
|
|
continue
|
|
}
|
|
fms, ferr := s.ctx.EntClient.FlowMaterial.Query().
|
|
Where(flowmaterial.FlowId(it.FlowId)).All(ctx)
|
|
if ferr != nil {
|
|
continue
|
|
}
|
|
fmByFlow[it.FlowId] = fms
|
|
}
|
|
// 工艺→工位序列(与池子重算同结构)
|
|
stationsByFlow := map[int][]int{}
|
|
for _, it := range items {
|
|
stationsByFlow[it.FlowId] = append(stationsByFlow[it.FlowId], it.StationNo)
|
|
}
|
|
stationQty := map[int]int{}
|
|
for k, v := range p.StationPlanQty {
|
|
if n, e := strconv.Atoi(k); e == nil {
|
|
stationQty[n] = v
|
|
}
|
|
}
|
|
for flowId, stations := range stationsByFlow {
|
|
fms := fmByFlow[flowId]
|
|
if len(fms) == 0 {
|
|
continue
|
|
}
|
|
qtys := splitQtyToStations(stations, p.PlanQty, stationQty)
|
|
for i, stNo := range stations {
|
|
q := qtys[i]
|
|
if q <= 0 {
|
|
continue
|
|
}
|
|
for _, fm := range fms {
|
|
if fm.MaterialCode == "" {
|
|
continue
|
|
}
|
|
need := int(math.Ceil(float64(q) * fm.UnitQty)) // R7:不乘折损率
|
|
if need <= 0 {
|
|
continue
|
|
}
|
|
out = append(out, PreparationRow{
|
|
PlanDate: p.PlanDate, OrderNo: p.OrderNo,
|
|
ProductName: productNameByOrder[p.OrderNo],
|
|
StationNo: stNo, StationQty: q,
|
|
MaterialCode: fm.MaterialCode, MaterialName: fm.MaterialName,
|
|
Spec: fm.Spec, Unit: fm.Unit,
|
|
Qty: need, UnitQty: fm.UnitQty, Status: p.Status,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
if p.PlanDate > farDate {
|
|
farDate = p.PlanDate
|
|
}
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool {
|
|
if out[i].PlanDate != out[j].PlanDate {
|
|
return out[i].PlanDate < out[j].PlanDate
|
|
}
|
|
if out[i].OrderNo != out[j].OrderNo {
|
|
return out[i].OrderNo < out[j].OrderNo
|
|
}
|
|
if out[i].StationNo != out[j].StationNo {
|
|
return out[i].StationNo < out[j].StationNo
|
|
}
|
|
return out[i].MaterialCode < out[j].MaterialCode
|
|
})
|
|
return &PreparationList{
|
|
Rows: out, Total: len(out),
|
|
Range: today + " ~ " + farDate,
|
|
PlanCnt: len(plans),
|
|
}, nil
|
|
}
|
|
|
|
// ---------- E4 托盘余料明细(2026-11-04 拍板:代退料自动带出该接驳台余料) ----------
|
|
|
|
// PalletResidualRow 接驳台托盘余料行(来自最近送达该接驳台的 AGV 运输段 materials_json)。
|
|
type PalletResidualRow struct {
|
|
RequestNo string `json:"requestNo"` // 关联叫料单号
|
|
MaterialCode string `json:"materialCode"` // 物料编码
|
|
MaterialName string `json:"materialName"` // 物料名称
|
|
Unit string `json:"unit"` // 单位
|
|
Qty float64 `json:"qty"` // 数量
|
|
}
|
|
|
|
// PalletResidual 查指定接驳台托盘上的余料明细:取最近一条送达该接驳台(target_dock=dock)、
|
|
// 带物料清单的 TRANSPORT 运输段(AGV 背托盘送达即托盘现存物料),同物料合并求和。
|
|
func (s *Service) PalletResidual(ctx context.Context, dockCode string) ([]PalletResidualRow, error) {
|
|
if dockCode == "" {
|
|
return nil, nil
|
|
}
|
|
tasks, err := s.ctx.EntClient.AgvTask.Query().
|
|
Where(
|
|
agvtask.TargetDockEQ(dockCode),
|
|
).
|
|
Order(ent.Desc(agvtask.FieldID)).
|
|
Limit(30).
|
|
All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
merged := map[string]*PalletResidualRow{} // key: requestNo|materialCode
|
|
order := []string{} // 保持首次出现顺序(最新段在前)
|
|
for _, t := range tasks {
|
|
if t.MaterialsJSON == "" {
|
|
continue
|
|
}
|
|
// 只认重载运输段(GO_LOAD 段目标是装货点 JBT21,不代表托盘现存内容)
|
|
if t.Kind != "TRANSPORT" {
|
|
continue
|
|
}
|
|
for _, r := range decodeSegMaterials(t.MaterialsJSON) {
|
|
if r.MaterialCode == "" {
|
|
continue
|
|
}
|
|
key := r.RequestNo + "|" + r.MaterialCode
|
|
if m, ok := merged[key]; ok {
|
|
m.Qty += r.Qty
|
|
continue
|
|
}
|
|
merged[key] = &PalletResidualRow{
|
|
RequestNo: r.RequestNo, MaterialCode: r.MaterialCode,
|
|
MaterialName: r.MaterialName, Unit: r.Unit, Qty: r.Qty,
|
|
}
|
|
order = append(order, key)
|
|
}
|
|
break // 只取最近一条送达运输段:托盘现存内容以最后一次送达为准
|
|
}
|
|
out := make([]PalletResidualRow, 0, len(order))
|
|
for _, k := range order {
|
|
out = append(out, *merged[k])
|
|
}
|
|
return out, nil
|
|
}
|