Files
bj_power/bj_power_mes/internal/logic/dailyplan_attainment.go
T
SunYF f8482b18f6 feat: 移除工位模板、AGV 链路增强与产品BOM/混产模板完善
- 移除工位模板体系(handler/logic 全删),同步路由与 seed
- AGV 链路增强:点位锁、车辆状态、退料派车调整,新增海康 RCS Postman 调试集合
- MES 产品BOM/混产模板完善:新增 ProductBOMDialog 组件,调整 handler/logic;日排产达成对比调整
- WMS 业务完善:出入库、检验、备料、退料、成品入库、缺料代报等 handler 与前端页面调整
- 前端通用:双端新增 RemotePageSelect 组件,统一 nextStep 规则/帮助文档/布局/请求拦截
- 文档:更新产线API/案例/部署手册/问题记录等
2026-11-18 09:53:42 +08:00

96 lines
3.4 KiB
Go

package logic
import (
"context"
"errors"
"sort"
"time"
"bj_power_mes/ent/dailyplan"
"bj_power_mes/ent/workpiecelifecyclelog"
)
// DailyPlanAttainmentRow 计划 vs 实际对比行(2026-11-16 死结6 定案)。
// 用户口径:不是历史排产对比,是「排了 10 个,那天实际做出了几个」的实际执行结果;
// 维度=现有日排产字段(排产日期/工单/计划数/实际数/差异),要看明细跳生产流程卡。
type DailyPlanAttainmentRow struct {
Date string `json:"date"` // 排产/完工日期
OrderNo string `json:"orderNo"` // 工单号(工单号)
PlanQty int `json:"planQty"` // 该日期该工单排产计划合计
DoneQty int `json:"doneQty"` // 该日期该工单实际完工台数(完工流水聚合)
Diff int `json:"diff"` // 实际 - 计划(负=欠产,正=超产)
}
// DailyPlanAttainment 计划 vs 实际(不建新表,直接汇聚,2026-11-16):
// - 计划 = daily_plan 各状态排产行按 (plan_date, order_no) 合计 plan_qty(含已完成/已取消,看的是历史全量);
// - 实际 = workpiece_lifecycle_log(action=DONE) 完工流水按 (完工当天, work_order_no) 聚合计数。
//
// 实际口径取完工流水而非 daily_plan.completed_qty:流水与生产流程卡同源(真流水),
// 不受「当天无排产行实绩漏记」影响(bumpDailyPlanCompleted 只往匹配到的排产行累加)。
// 窗口上限 92 天,防全表扫描。
func (s *Service) DailyPlanAttainment(ctx context.Context, start, end string) ([]DailyPlanAttainmentRow, error) {
if start == "" || end == "" {
return nil, errors.New("请选择日期范围")
}
st, e1 := time.Parse("2006-01-02", start)
en, e2 := time.Parse("2006-01-02", end)
if e1 != nil || e2 != nil {
return nil, errors.New("日期格式应为 YYYY-MM-DD")
}
if en.Before(st) {
return nil, errors.New("结束日期不能早于开始日期")
}
if en.Sub(st).Hours() > 92*24 {
return nil, errors.New("对比窗口最长 92 天,请缩小日期范围")
}
rows := map[string]*DailyPlanAttainmentRow{}
outOrder := make([]string, 0, 64)
touch := func(date, orderNo string) *DailyPlanAttainmentRow {
key := date + "|" + orderNo
r, ok := rows[key]
if !ok {
r = &DailyPlanAttainmentRow{Date: date, OrderNo: orderNo}
rows[key] = r
outOrder = append(outOrder, key)
}
return r
}
// 计划:排产行合计(全状态,历史全量)
plans, err := s.ctx.EntClient.DailyPlan.Query().
Where(dailyplan.PlanDateGTE(start), dailyplan.PlanDateLTE(end)).All(ctx)
if err != nil {
return nil, err
}
for _, p := range plans {
touch(p.PlanDate, p.OrderNo).PlanQty += p.PlanQty
}
// 实际:完工流水(action=DONE)按完工当天聚合
logs, err := s.ctx.EntClient.WorkpieceLifecycleLog.Query().
Where(workpiecelifecyclelog.ActionEQ("DONE"),
workpiecelifecyclelog.CreatedAtGTE(st),
workpiecelifecyclelog.CreatedAtLT(en.Add(24*time.Hour))).All(ctx)
if err != nil {
return nil, err
}
for _, l := range logs {
touch(l.CreatedAt.Format("2006-01-02"), l.WorkOrderNo).DoneQty++
}
out := make([]DailyPlanAttainmentRow, 0, len(rows))
for _, key := range outOrder {
r := rows[key]
r.Diff = r.DoneQty - r.PlanQty
out = append(out, *r)
}
sort.Slice(out, func(i, j int) bool {
if out[i].Date != out[j].Date {
return out[i].Date < out[j].Date
}
return out[i].OrderNo < out[j].OrderNo
})
return out, nil
}