feat: 重构BOM架构,新增基础设置与虚拟工位管理功能

本次重构删除原有BOM物料清单表,改用工单工艺组合×工艺物料清单作为唯一用料来源;新增系统基础设置表支持WMS地址、日志保留天数等配置,新增虚拟工位作业管理后台接口与前端页签控制功能,同时优化工位号校验逻辑、事件日志自动清理与工单用料查询能力。
This commit is contained in:
SunYF
2026-09-23 11:41:28 +08:00
parent d3eda6b588
commit f274da044c
74 changed files with 3637 additions and 5586 deletions
+175 -251
View File
@@ -15,14 +15,6 @@ import (
"bj_power_mes/internal/wmsclient"
)
// producibleBomItem 可生产数量计算用的精简 BOM 行
type producibleBomItem struct {
materialCode string
materialName string
unit string // 计量单位(来自 BOM,与物料档案单位一致)
unitQty float64 // 单台用量
}
// ProducibleResult 可生产数量计算结果(供 WMS 按需实时拉取)
type ProducibleResult struct {
Items []wmsclient.ProducibleItem `json:"items"`
@@ -37,20 +29,21 @@ type MaterialDemandRow struct {
MaterialCode string `json:"materialCode"`
MaterialName string `json:"materialName"`
Unit string `json:"unit"`
DemandQty int `json:"demandQty"` // 未来 N 天排产总需求(台数×单台用量,向上取整)
DemandQty int `json:"demandQty"` // 未来 N 天排产总需求(台数×单台含损耗用量,向上取整)
StartDate string `json:"startDate"` // 需求窗口起(YYYY-MM-DD)
EndDate string `json:"endDate"` // 需求窗口止(YYYY-MM-DD)
}
// ProducibleSupportItem 排产支撑:单个成品的可生产套数与短板物料说明
// ProducibleSupportItem 排产支撑:单张工单的可生产套数与短板物料说明
type ProducibleSupportItem struct {
OrderNo string `json:"orderNo"`
ProductCode string `json:"productCode"`
ProductName string `json:"productName"`
ProducibleQty int `json:"producibleQty"`
ShortText string `json:"shortText"`
}
// ProducibleSupportResult 排产支撑总览:逐成品可生产套数 + 整体还能满产多少天/支撑到哪天/最先告罄短板物料
// ProducibleSupportResult 排产支撑总览:逐工单可生产套数 + 整体还能满产多少天/支撑到哪天/最先告罄短板物料
type ProducibleSupportResult struct {
Items []ProducibleSupportItem `json:"items"`
SupportDays int `json:"supportDays"`
@@ -59,36 +52,88 @@ type ProducibleSupportResult struct {
ComputedAt int64 `json:"computedAt"`
}
// planOrdersBetween 取 [start,end] 窗口内(按排产日期 PlanDate,非工单起止日期)未取消日排产:
// 返回 工单号→排产台数 合并映射(同工单多日排产累加)。
func (s *Service) planOrdersBetween(ctx context.Context, start, end string) (map[string]int, error) {
plans, err := s.ctx.EntClient.DailyPlan.Query().
Where(dailyplan.PlanDateGTE(start), dailyplan.PlanDateLTE(end)).All(ctx)
if err != nil {
return nil, err
}
qty := map[string]int{}
for _, p := range plans {
if p.Status == "CANCELLED" {
continue
}
qty[p.OrderNo] += p.PlanQty
}
for no, q := range qty {
if q <= 0 {
delete(qty, no)
}
}
return qty, nil
}
// loadOrderUsage 批量加载工单(工单号→单台用料行)。工单号不存在或组合无物料的不进映射。
func (s *Service) loadOrderUsage(ctx context.Context, orderNos []string) map[string][]UsageRow {
usageByOrder := map[string][]UsageRow{}
if len(orderNos) == 0 {
return usageByOrder
}
wos, err := s.ctx.EntClient.WorkOrder.Query().
Where(workorder.WorkOrderNoIn(orderNos...)).All(ctx)
if err != nil {
return usageByOrder
}
for _, wo := range wos {
if u := s.WorkOrderUnitUsage(ctx, wo); len(u) > 0 {
usageByOrder[wo.WorkOrderNo] = u
}
}
return usageByOrder
}
// ComputeProducible 计算「可生产数量」+「未来5天物料需求」,结果直接返回(不做定时推送)。
//
// 业务口径(问题记录 L470-474 + L256):
// - 可生产套数 = min(各物料可用量 ÷ 单台用量)。BOM 单台用量在 MES、库存可用量在 WMS,
// 故由 MES 拉取 WMS 可用量(/api/internal/stock/check)后本地计算,WMS 打开页面时实时来拉。
// - 未来5天需求量 = 近5天(含今日)日排产台数 × 单台用量,按物料汇总,供 WMS 备料四态「预警」判定。
// 业务口径(BOM 已删除,物料跟着工单走):
// - 可生产套数 = 按"有未取消日排产的工单"逐单计算:min(各物料可用量 ÷ 该工单单台含损耗用量)。
// 单台用量来自该工单工艺组合×工艺物料清单;库存可用量在 WMS,由 MES 拉取后本地计算。
// - 未来5天需求量 = 近5天(含今日)日排产台数 × 工单单台含损耗用量,按物料汇总(按排产日期算),
// 供 WMS 备料四态「预警」判定(客户诉求 问题记录 L256)。
//
// 两系统独立运行约束:未配置 WMS 或不可达时返回错误,由调用方(WMS)提示,绝不阻塞 MES。
func (s *Service) ComputeProducible(ctx context.Context) (*ProducibleResult, error) {
if s.ctx.Wms == nil || s.ctx.EntClient == nil {
return nil, errors.New("未配置 WMS 地址,无法计算可生产数量")
}
chosen, materialSet, err := s.buildChosenBom(ctx)
today := time.Now().Format("2006-01-02")
planQty, err := s.planOrdersBetween(ctx, today, addDays(today, 59))
if err != nil {
return nil, err
}
if len(chosen) == 0 {
if len(planQty) == 0 {
return &ProducibleResult{
Items: []wmsclient.ProducibleItem{}, Demands: []wmsclient.ProducibleDemand{},
Avail: map[string]int{}, ComputedAt: time.Now().Unix(),
}, nil
}
orderNos := make([]string, 0, len(planQty))
for no := range planQty {
orderNos = append(orderNos, no)
}
sort.Strings(orderNos)
usageByOrder := s.loadOrderUsage(ctx, orderNos)
// §A2:可生产套数只算启用成品(停用产品不再展示产能)。
// materialSet 保持完整——排产消耗模拟(simulateSupport)按真实排产消耗全部 BOM 物料。
s.dropInactiveProducts(ctx, chosen)
// 3) 拉取 WMS 各物料可用量
codes := make([]string, 0, len(materialSet))
for c := range materialSet {
// 拉取 WMS 各物料可用量(仅工单用料涉及的物料)
codeSet := map[string]bool{}
for _, usage := range usageByOrder {
for _, it := range usage {
codeSet[it.MaterialCode] = true
}
}
codes := make([]string, 0, len(codeSet))
for c := range codeSet {
codes = append(codes, c)
}
sort.Strings(codes)
@@ -97,62 +142,58 @@ func (s *Service) ComputeProducible(ctx context.Context) (*ProducibleResult, err
return nil, errors.New("拉取 WMS 可用量失败: " + err.Error())
}
// 4) 产品名称映射(product_type 优先,work_order 兜底)
// 产品名称映射(product_type 优先,工单快照兜底)
nameMap := map[string]string{}
if rows, e := s.ctx.Wms.ProductTypes(ctx, ""); e == nil {
for _, p := range rows {
nameMap[p.Code] = p.Name
}
}
if wos, e := s.ctx.EntClient.WorkOrder.Query().All(ctx); e == nil {
prodCodeByOrder := map[string]string{}
if wos, e := s.ctx.EntClient.WorkOrder.Query().
Where(workorder.WorkOrderNoIn(orderNos...)).All(ctx); e == nil {
for _, wo := range wos {
if wo.ProductCode != "" && wo.ProductName != "" {
if _, has := nameMap[wo.ProductCode]; !has {
nameMap[wo.ProductCode] = wo.ProductName
}
}
prodCodeByOrder[wo.WorkOrderNo] = wo.ProductCode
}
}
// 5) 逐产品计算可生产套数 = min(floor(可用量 ÷ 单台用量)),短板物料取达最小值的前3
// 逐工单计算可生产套数 = min(floor(可用量 ÷ 单台含损耗用量)),短板物料取最小值前3
now := time.Now().Unix()
prodCodes := make([]string, 0, len(chosen))
for pc := range chosen {
prodCodes = append(prodCodes, pc)
}
sort.Strings(prodCodes)
items := make([]wmsclient.ProducibleItem, 0, len(prodCodes))
for _, pc := range prodCodes {
bom := chosen[pc]
items := make([]wmsclient.ProducibleItem, 0, len(orderNos))
for _, no := range orderNos {
usage, ok := usageByOrder[no]
if !ok {
continue // 工单组合无物料清单,无法评估
}
minSets := -1
type shortRow struct {
text string
set int
}
shorts := make([]shortRow, 0, len(bom))
for _, it := range bom {
if it.unitQty <= 0 {
shorts := make([]shortRow, 0, len(usage))
for _, it := range usage {
if it.EffQty <= 0 {
continue
}
a := avail[it.materialCode]
sets := int(math.Floor(float64(a) / it.unitQty))
a := avail[it.MaterialCode]
sets := int(math.Floor(float64(a) / it.EffQty))
if sets < 0 {
sets = 0
}
if minSets < 0 || sets < minSets {
minSets = sets
}
name := it.materialName
name := it.MaterialName
if name == "" {
name = it.materialCode
name = it.MaterialCode
}
shorts = append(shorts, shortRow{
text: fmt.Sprintf("%s(%s) 可用%d÷单台%s=%d套", name, it.materialCode, a, trimNum(it.unitQty), sets),
text: fmt.Sprintf("%s(%s) 可用%d÷单台%s=%d套", name, it.MaterialCode, a, trimNum(it.EffQty), sets),
set: sets,
})
}
if minSets < 0 {
minSets = 0 // BOM 全无有效单台用量
minSets = 0
}
sort.SliceStable(shorts, func(i, j int) bool { return shorts[i].set < shorts[j].set })
pick := make([]string, 0, 3)
@@ -161,7 +202,9 @@ func (s *Service) ComputeProducible(ctx context.Context) (*ProducibleResult, err
pick = append(pick, sr.text)
}
}
pc := prodCodeByOrder[no]
items = append(items, wmsclient.ProducibleItem{
OrderNo: no,
ProductCode: pc,
ProductName: nameMap[pc],
ProducibleQty: minSets,
@@ -170,98 +213,57 @@ func (s *Service) ComputeProducible(ctx context.Context) (*ProducibleResult, err
})
}
// 6) 未来5天物料需求(备料四态「预警」数据源)
demands := s.computeFuture5Demand(ctx, chosen)
return &ProducibleResult{Items: items, Demands: demands, Avail: avail, ComputedAt: now}, nil
// 未来 N 天物料需求(备料四态「预警」数据源;窗口天数在「基础设置」页配置,默认 5)
demands := s.computeFutureDemand(ctx, s.CfgInt(ctx, CfgDemandDays, 5))
return &ProducibleResult{
Items: items, Demands: demands,
Avail: avail, ComputedAt: time.Now().Unix(),
}, nil
}
// dropInactiveProducts 剔除停用产品(product_type.isActive=false)的 BOM 条目,
// 使「可生产套数」(排产支撑视图 Items)只统计启用成品(§A2:只算启用成品)。
// 注意:只影响可生产套数计算;逐日消耗模拟(simulateSupport)仍用完整 BOM——
// 停用产品若有遗留排产,物料消耗是真实的,不能漏算。
func (s *Service) dropInactiveProducts(ctx context.Context, chosen map[string][]producibleBomItem) {
if len(chosen) == 0 {
return
}
rows, err := s.ctx.Wms.ProductTypes(ctx, "false")
if err != nil {
return
}
for _, p := range rows {
delete(chosen, p.Code)
}
}
// buildChosenBom 按产品编码分组 BOM 明细,并为每个产品选定一份 BOM(优先“默认”,否则按名排序取第一份)。
// 返回 chosen(产品编码→明细行) 与 materialSet(BOM 涉及的全部物料编码)。
func (s *Service) buildChosenBom(ctx context.Context) (map[string][]producibleBomItem, map[string]bool, error) {
all, err := s.ctx.EntClient.BomItem.Query().All(ctx)
if err != nil {
return nil, nil, err
}
byProd := map[string]map[string][]producibleBomItem{}
for _, b := range all {
if b.ProductCode == "" || b.MaterialCode == "" {
continue
}
bn := b.BomName
if bn == "" {
bn = DefaultBOMName
}
if byProd[b.ProductCode] == nil {
byProd[b.ProductCode] = map[string][]producibleBomItem{}
}
byProd[b.ProductCode][bn] = append(byProd[b.ProductCode][bn], producibleBomItem{
materialCode: b.MaterialCode, materialName: b.MaterialName, unit: b.Unit, unitQty: b.UnitQty,
})
}
chosen := map[string][]producibleBomItem{}
materialSet := map[string]bool{}
for pc, boms := range byProd {
var bn string
if _, has := boms[DefaultBOMName]; has {
bn = DefaultBOMName
} else {
names := make([]string, 0, len(boms))
for n := range boms {
names = append(names, n)
}
sort.Strings(names)
bn = names[0]
}
chosen[pc] = boms[bn]
for _, it := range boms[bn] {
materialSet[it.materialCode] = true
}
}
return chosen, materialSet, nil
}
// ComputeMaterialDemand 按物料汇总未来 N 天(含今日)排产需求量,供 WMS 缺料补货视图实时拉取。
// 需求 = Σ(逐日排产台数 × 单台用量),向上取整,按物料汇总;返回需求窗口起止日期。
// 与 ComputeProducible 共享 BOM 选定逻辑,各自只算自己视角:MES 只产出"需求量",可用量由 WMS 自己比。
// ComputeMaterialDemand WMS 缺料补货数据源:未来 N 天(按排产日期)排产物料需求,按物料汇总。
// 与 ComputeProducible 共享工单用料汇总:MES 只产出"需求量",可用量由 WMS 自己比。
func (s *Service) ComputeMaterialDemand(ctx context.Context, days int) ([]MaterialDemandRow, error) {
if s.ctx.EntClient == nil {
return nil, errors.New("未配置数据库连接")
}
if days <= 0 {
days = 5
}
chosen, _, err := s.buildChosenBom(ctx)
if err != nil {
return nil, err
}
if len(chosen) == 0 {
return []MaterialDemandRow{}, nil
days = s.CfgInt(ctx, CfgDemandDays, 5) // 未传窗口 → 基础设置需求窗口天数
}
today := time.Now()
start := today.Format("2006-01-02")
end := today.AddDate(0, 0, days-1).Format("2006-01-02")
demand, err := s.dailyMaterialDemand(ctx, chosen, start, end)
planQty, err := s.planOrdersBetween(ctx, start, end)
if err != nil {
return nil, err
}
if len(planQty) == 0 {
return []MaterialDemandRow{}, nil
}
orderNos := make([]string, 0, len(planQty))
for no := range planQty {
orderNos = append(orderNos, no)
}
usageByOrder := s.loadOrderUsage(ctx, orderNos)
if len(usageByOrder) == 0 {
return []MaterialDemandRow{}, nil
}
// 需求 = Σ 排产台数 × 单台含损耗用量(向上取整);名称/单位取自工艺物料快照
demand := map[string]int{}
nameUnit := map[string]struct{ name, unit string }{}
for no, qty := range planQty {
usage, ok := usageByOrder[no]
if !ok || qty <= 0 {
continue
}
for _, it := range usage {
if it.EffQty <= 0 {
continue
}
demand[it.MaterialCode] += int(math.Ceil(float64(qty) * it.EffQty))
nameUnit[it.MaterialCode] = struct{ name, unit string }{it.MaterialName, it.Unit}
}
}
if len(demand) == 0 {
return []MaterialDemandRow{}, nil
}
@@ -270,13 +272,6 @@ func (s *Service) ComputeMaterialDemand(ctx context.Context, days int) ([]Materi
mcodes = append(mcodes, c)
}
sort.Strings(mcodes)
// 物料名称/单位取自选定 BOM
nameUnit := map[string]struct{ name, unit string }{}
for _, bom := range chosen {
for _, it := range bom {
nameUnit[it.materialCode] = struct{ name, unit string }{it.materialName, it.unit}
}
}
out := make([]MaterialDemandRow, 0, len(mcodes))
for _, c := range mcodes {
nu := nameUnit[c]
@@ -288,83 +283,22 @@ func (s *Service) ComputeMaterialDemand(ctx context.Context, days int) ([]Materi
return out, nil
}
// dailyMaterialDemand 累计 [start,end] 窗口内各物料排产需求量(向上取整)。
// 数据链路:daily_plan(工单号→台数) → work_order(工单号→产品编码) → BOM(产品编码→物料单台用量)。
func (s *Service) dailyMaterialDemand(ctx context.Context, chosen map[string][]producibleBomItem, start, end string) (map[string]int, error) {
plans, err := s.ctx.EntClient.DailyPlan.Query().
Where(dailyplan.PlanDateGTE(start), dailyplan.PlanDateLTE(end)).All(ctx)
if err != nil {
return nil, err
}
if len(plans) == 0 {
return nil, nil
}
orderByNo := map[string]int{}
for _, p := range plans {
if p.Status == "CANCELLED" {
continue
}
orderByNo[p.OrderNo] += p.PlanQty
}
if len(orderByNo) == 0 {
return nil, nil
}
nos := make([]string, 0, len(orderByNo))
for no := range orderByNo {
nos = append(nos, no)
}
wos, err := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNoIn(nos...)).All(ctx)
if err != nil {
return nil, err
}
// 产品编码 → 窗口内总台数
prodQty := map[string]int{}
for _, wo := range wos {
if wo.ProductCode == "" {
continue
}
prodQty[wo.ProductCode] += orderByNo[wo.WorkOrderNo]
}
matQty := map[string]int{}
for pc, qty := range prodQty {
bom := chosen[pc]
if qty <= 0 || len(bom) == 0 {
continue
}
for _, it := range bom {
if it.unitQty <= 0 {
continue
}
matQty[it.materialCode] += int(math.Ceil(float64(qty) * it.unitQty))
}
}
return matQty, nil
}
// computeFuture5Demand 近5天(含今日)日排产台数 × 单台用量,按物料汇总为未来5天需求量。
// 数据链路:daily_plan(工单号→台数) → work_order(工单号→产品编码) → BOM(产品编码→物料单台用量)。
func (s *Service) computeFuture5Demand(ctx context.Context, chosen map[string][]producibleBomItem) []wmsclient.ProducibleDemand {
today := time.Now()
start := today.Format("2006-01-02")
end := today.AddDate(0, 0, 4).Format("2006-01-02")
matQty, err := s.dailyMaterialDemand(ctx, chosen, start, end)
if err != nil || len(matQty) == 0 {
// computeFutureDemand 近 N 天(含今日)日排产台数 × 工单单台含损耗用量,按物料汇总为未来需求量。
// 窗口按排产日期算(工单起止日期≠排产日期,一律以排产日期为准);N 在「基础设置」页配置(默认 5)。
func (s *Service) computeFutureDemand(ctx context.Context, days int) []wmsclient.ProducibleDemand {
rows, err := s.ComputeMaterialDemand(ctx, days)
if err != nil || len(rows) == 0 {
return nil
}
mcodes := make([]string, 0, len(matQty))
for c := range matQty {
mcodes = append(mcodes, c)
}
sort.Strings(mcodes)
out := make([]wmsclient.ProducibleDemand, 0, len(mcodes))
for _, c := range mcodes {
out = append(out, wmsclient.ProducibleDemand{MaterialCode: c, Future5Qty: matQty[c]})
out := make([]wmsclient.ProducibleDemand, 0, len(rows))
for _, r := range rows {
out = append(out, wmsclient.ProducibleDemand{MaterialCode: r.MaterialCode, Future5Qty: r.DemandQty})
}
return out
}
// ComputeProducibleSupport 计算排产支撑视图(§A2 / §D1)。
// 复用 ComputeProducible 得到逐成品可生产套数,再用 WMS 可用量对日排产做逐日消耗模拟,
// 复用 ComputeProducible 得到逐工单可生产套数,再用 WMS 可用量对日排产做逐日消耗模拟,
// 得出整体还能满产多少天、支撑到哪天、最先告罄的短板物料;排产只排 N 天时,第 N+1 天起用近3天平均日需求外推。
func (s *Service) ComputeProducibleSupport(ctx context.Context) (*ProducibleSupportResult, error) {
base, err := s.ComputeProducible(ctx)
@@ -378,24 +312,32 @@ func (s *Service) ComputeProducibleSupport(ctx context.Context) (*ProducibleSupp
}
for _, it := range base.Items {
res.Items = append(res.Items, ProducibleSupportItem{
ProductCode: it.ProductCode, ProductName: it.ProductName,
OrderNo: it.OrderNo, ProductCode: it.ProductCode, ProductName: it.ProductName,
ProducibleQty: it.ProducibleQty, ShortText: it.ShortText,
})
}
chosen, _, err := s.buildChosenBom(ctx)
if err != nil {
return nil, err
}
days, until, shortBoard := s.simulateSupport(ctx, chosen, base.Avail)
usageByOrder := s.loadOrderUsage(ctx, orderNosOf(base.Items))
days, until, shortBoard := s.simulateSupport(ctx, usageByOrder, base.Avail)
res.SupportDays = days
res.SupportUntil = until
res.ShortBoardMaterial = shortBoard
return res, nil
}
// orderNosOf 从可生产结果提取工单号(保持顺序)
func orderNosOf(items []wmsclient.ProducibleItem) []string {
out := make([]string, 0, len(items))
for _, it := range items {
if it.OrderNo != "" {
out = append(out, it.OrderNo)
}
}
return out
}
// simulateSupport 逐日消耗模拟:用 WMS 可用量 avail 对日排产做逐日扣减,直到某物料不足。
// 返回 支撑满产天数、支撑到哪天、最先告罄短板物料(名称+编码,无排产返回"无排产计划")。
func (s *Service) simulateSupport(ctx context.Context, chosen map[string][]producibleBomItem, avail map[string]int) (int, string, string) {
func (s *Service) simulateSupport(ctx context.Context, usageByOrder map[string][]UsageRow, avail map[string]int) (int, string, string) {
today := time.Now()
start := today.Format("2006-01-02")
end := today.AddDate(0, 0, 59).Format("2006-01-02")
@@ -404,42 +346,27 @@ func (s *Service) simulateSupport(ctx context.Context, chosen map[string][]produ
if err != nil {
return 0, start, "无排产计划"
}
// 工单号 → 产品编码
orderNoSet := map[string]bool{}
planByDate := map[string]map[string]int{} // date -> orderNo -> qty
for _, p := range plans {
if p.Status == "CANCELLED" || p.PlanQty <= 0 {
continue
}
if _, has := usageByOrder[p.OrderNo]; !has {
continue // 工单组合无物料清单,不参与消耗模拟
}
if planByDate[p.PlanDate] == nil {
planByDate[p.PlanDate] = map[string]int{}
}
planByDate[p.PlanDate][p.OrderNo] += p.PlanQty
orderNoSet[p.OrderNo] = true
}
if len(planByDate) == 0 {
return 0, start, "无排产计划"
}
// 拉工单产品编码
orderNos := make([]string, 0, len(orderNoSet))
for no := range orderNoSet {
orderNos = append(orderNos, no)
}
wos, err := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNoIn(orderNos...)).All(ctx)
if err != nil {
return 0, start, "无排产计划"
}
orderProduct := map[string]string{}
for _, wo := range wos {
if wo.ProductCode != "" {
orderProduct[wo.WorkOrderNo] = wo.ProductCode
}
}
// 物料名称
matName := map[string]string{}
for _, bom := range chosen {
for _, it := range bom {
matName[it.materialCode] = it.materialName
for _, usage := range usageByOrder {
for _, it := range usage {
matName[it.MaterialCode] = it.MaterialName
}
}
// 排序排产日
@@ -460,7 +387,7 @@ func (s *Service) simulateSupport(ctx context.Context, chosen map[string][]produ
supportDays := 0
shortBoard := ""
for _, d := range dates {
dayDemand := dayMaterialDemand(planByDate[d], orderProduct, chosen)
dayDemand := dayMaterialDemand(planByDate[d], usageByOrder)
history = append(history, dayDemand)
// 扣减
fail := false
@@ -481,16 +408,17 @@ func (s *Service) simulateSupport(ctx context.Context, chosen map[string][]produ
supportDays++
}
// 排产日已耗尽仍有余量 → 用近3天平均日需求外推
// 排产日已耗尽仍有余量 → 用近 N 天平均日需求外推(N 在「基础设置」页配置,默认 3)
if shortBoard == "" {
// 计算均值(取最近 min(3,len) 天)
// 计算均值(取最近 min(N,len) 天)
n := len(history)
if n == 0 {
return supportDays, addDays(start, supportDays), ""
}
window := s.CfgInt(ctx, CfgExtrapWindow, 3)
lo := 0
if n > 3 {
lo = n - 3
if n > window {
lo = n - window
}
avg := map[string]float64{}
cnt := float64(n - lo)
@@ -502,7 +430,7 @@ func (s *Service) simulateSupport(ctx context.Context, chosen map[string][]produ
for mat := range avg {
avg[mat] /= cnt
}
// 近3天平均日需求全为0(无有效 BOM 用量)→ 无更多需求,不再外推
// 近 N 天平均日需求全为0(无有效单台用量)→ 无更多需求,不再外推
totalAvg := 0.0
for _, q := range avg {
totalAvg += q
@@ -510,8 +438,8 @@ func (s *Service) simulateSupport(ctx context.Context, chosen map[string][]produ
if totalAvg <= 0 {
return supportDays, addDays(start, supportDays), ""
}
// 外推:每日按 avg 扣减,直到某物料不足(封顶 365 天防死循环)
capDays := 365
// 外推:每日按 avg 扣减,直到某物料不足(封顶天数在「基础设置」页配置,防死循环)
capDays := s.CfgInt(ctx, CfgExtrapCap, 365)
for supportDays < capDays {
fail := false
for mat, q := range avg {
@@ -537,23 +465,19 @@ func (s *Service) simulateSupport(ctx context.Context, chosen map[string][]produ
return supportDays, addDays(start, supportDays), shortBoard
}
// dayMaterialDemand 计算某日各物料需求(逐工单台数 × 单台用量,向上取整)
func dayMaterialDemand(planOfDay map[string]int, orderProduct map[string]string, chosen map[string][]producibleBomItem) map[string]int {
// dayMaterialDemand 计算某日各物料需求(逐工单台数 × 工单单台含损耗用量,向上取整)
func dayMaterialDemand(planOfDay map[string]int, usageByOrder map[string][]UsageRow) map[string]int {
demand := map[string]int{}
for orderNo, qty := range planOfDay {
pc := orderProduct[orderNo]
if pc == "" || qty <= 0 {
usage := usageByOrder[orderNo]
if len(usage) == 0 || qty <= 0 {
continue
}
bom := chosen[pc]
if len(bom) == 0 {
continue
}
for _, it := range bom {
if it.unitQty <= 0 {
for _, it := range usage {
if it.EffQty <= 0 {
continue
}
demand[it.materialCode] += int(math.Ceil(float64(qty) * it.unitQty))
demand[it.MaterialCode] += int(math.Ceil(float64(qty) * it.EffQty))
}
}
return demand