feat(mes/wms/终端): 工位终端状态感知 + 全厂统一账号 + 单号短号规则 + 退料SN逐颗核对闭环(2026-11-26~28 现场整改)
【MES · 工位终端状态感知(11-28 第十三节拍板)】 - 新增 station_terminal_state / station_terminal_event 两表:当前状态镜像 + 状态变化流水 - 在线判定走既有 SSE 长连接存活:建连/断连/30s ping 心跳,90s 无心跳判离线,零 HTTP 轮询 - 登录/退出/暂停/恢复由终端事件驱动上报;新增「终端状态」管理页(produce.terminalstate,仅超级管理员):看在线/登录/操作人/接单/下班判定/心跳,抽屉看状态流水 - 库管代退料前置条件②「工人已下班」据此判定:未登录或终端离线任一即下班 【工位终端 · 全厂统一账号(11-28 拍板)】 - 删除本地账号表与 seed,登录直连 MES 用户表;本地 SQLite 只剩操作日志+登录态,断网不能换人 - SSE 客户端携带本机固定工位号,MES 据连接存活映射该工位在线状态 【单号短号规则(11-27 拍板)】 - 叫料单 MR / 叫料包 PK / 转产退库 TR / 配送单 DL:改「前缀+YYMMDD+3位当天序号」,每天从 001 归零、超 999 进 4 位 - MR 与 TR 同列不同前缀独立计数不撞号;进程内互斥分配 + DB 唯一索引兜底;历史时间戳号一律不动 【退料闭环(11-28)】 - 退库单新增 sn_list:工位提交的待退 SN 清单落库,收货逐颗扫码核对(不按批次分组、允许跨批次混合,所属批次系统按 SN 带出,库管不必填) - 收货改大数量必须有实物依据:SN 件以精确扫描的最终清单为准(条数相等且不重复);批次件必须落到真实启用的四级库位,禁止凭空加库存 - 余量参考值口径定稿(N4):应剩余量 = 实际备料 − 已消耗 − 已退 − 已损耗 - 损耗台账取消「后审核批注」:删 status/comment/commenter/commented_at 字段,WMS 确认收货当场落账闭环,MES 台账只读 【日排产 · 两种 PENDING 区分(11-26 口径定稿)】 - daily_plan 新增 discarded 废弃标记:停用打标、新建不打标;额度只算 CONFIRMED + 未废弃 PENDING - 工单管「要干几台」、日排产管「按天从哪个工位领多少料」,两张账不合并不互倒推;排产合计可小于工单数(缺口靠借料/补料审批摆出,不自动兜底) 【其他】 - SN 查询大小写脱敏(11-27):存储原样不强制大小写,匹配 LOWER(sn)=LOWER(?),扫 ABC-0001 与 abc-0001 命中同一件 - 拧紧枪人工跳过链路:设备未连接时人工跳过上报,「拧紧跳过(设备未连接)」前缀单独列示,不写 torque_record 绝不冒充设备结果 - 单据流转提示规则表全量 35 条(MES 18 + WMS 17):常驻提示条、单号可复制;单号字段逐条对后端响应实测,取不到不显示复制按钮 - PLC 快照缓存/对账、产线清线、绩效与拧紧枪配置页同步修正 【文档】 - 新增《临时结束.md》2026-11-26 日排产/叫料/完工口径大讨论定稿(含状态专项结论与被推翻旧说法留档);问题临时.md 全量更新
This commit is contained in:
@@ -8,12 +8,26 @@ import (
|
||||
"time"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/callpool"
|
||||
"bj_power_mes/ent/dailyplan"
|
||||
"bj_power_mes/ent/dock"
|
||||
"bj_power_mes/ent/workorder"
|
||||
"bj_power_mes/internal/verr"
|
||||
)
|
||||
|
||||
// ============ 日排产业务口径(2026-11-26 C 方案定稿 + 本会话共识,勿按旧五态理解) ============
|
||||
// 1. 日排产只两态:PENDING(未启用)/CONFIRMED(已启用);「执行中/完成/取消」不再写排产,从工单派生。
|
||||
// 列表两列「启用状态」(可点)/「工单进度」(只读)。工单状态不再回写排产、强制完工不再自动取消排产。
|
||||
// 2. 日排产管的是「物料」:启用(CONFIRMED)=算料开关——只有启用行才贡献叫料池/备料/齐套需求;
|
||||
// 停用(PENDING)行不占额度、不参与算料。工单完工/取消后由 planOrderActive 停止算料并重算池子。
|
||||
// 3. 上线门禁:工件建档(新扫SN/预生成/重上线)必须有 CONFIRMED 排产且工单未完工/未取消,否则拒绝进线。
|
||||
// 4. 跨夜:排产按 planDate 逐日切分,工件跨夜在制按工单+当日行累计完工,日保护看 CompletedQty>0,
|
||||
// 不因跨天重置。补排只能排今天/以后(planDateNotPast),不追改历史。
|
||||
// 5. 可排额度 = 工单量 − 已完工 − Σ启用行剩余量(剩余=max(0,计划−已完成)),停用行不占额度;
|
||||
// 工单行 ForUpdate 加锁,保存/批量/启用/下拉统一调用,防并发超排。
|
||||
// 6. 废弃标记 discarded:停用行若已记完工不物理删除,只标记废弃;报表「计划」列含废弃行(逐日真实),
|
||||
// 页面注明口径,避免因补排双计造成「当日超产」假象。
|
||||
|
||||
// parseDate 解析 yyyy-MM-dd 或标准时间
|
||||
func parseDate(v string) (time.Time, error) {
|
||||
if t, err := time.ParseInLocation("2006-01-02", v, time.Local); err == nil {
|
||||
@@ -22,6 +36,106 @@ func parseDate(v string) (time.Time, error) {
|
||||
return time.ParseInLocation(time.RFC3339, v, time.Local)
|
||||
}
|
||||
|
||||
// planDateNotPast 新建/补排的日期只能是今天或以后(过去是已发生的事实,不改历史)。
|
||||
func planDateNotPast(v string) bool {
|
||||
t, err := time.ParseInLocation("2006-01-02", v, time.Local)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
now := time.Now()
|
||||
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
|
||||
return !t.Before(today)
|
||||
}
|
||||
|
||||
// dailyPlanQuota 统一可排额度 = 工单量 − 已完工数 − Σ有效行剩余量。
|
||||
// 有效行 = CONFIRMED + 未废弃 PENDING;废弃行不占额度,但沿用旧料完工仍往原行累加(scan.go 口径不变)。
|
||||
// 剩余量 = max(0, planQty − completedQty)。excludeDate 非空时排除该日期既有行(编辑同日只算新值)。
|
||||
func dailyPlanQuota(wo *ent.WorkOrder, plans []*ent.DailyPlan, excludeDate string) int {
|
||||
occupied := 0
|
||||
for _, p := range plans {
|
||||
if excludeDate != "" && p.PlanDate == excludeDate {
|
||||
continue
|
||||
}
|
||||
if p.Status != "CONFIRMED" && (p.Status != "PENDING" || p.Discarded) {
|
||||
continue
|
||||
}
|
||||
rem := p.PlanQty - p.CompletedQty
|
||||
if rem < 0 {
|
||||
rem = 0
|
||||
}
|
||||
occupied += rem
|
||||
}
|
||||
quota := wo.Quantity - wo.FinishedNum - occupied
|
||||
if quota < 0 {
|
||||
quota = 0
|
||||
}
|
||||
return quota
|
||||
}
|
||||
|
||||
// dailyPlanQuotaExcludingDates 批量/自动排产用:本次要覆盖的日期(无完工数量的行)旧值由新值替代,
|
||||
// 不该再占额度,否则「把今天 3 台改成 5 台」会被旧值重复计费而误拒。
|
||||
func dailyPlanQuotaExcludingDates(wo *ent.WorkOrder, plans []*ent.DailyPlan, dates []string) int {
|
||||
occupied := 0
|
||||
for _, p := range plans {
|
||||
skip := false
|
||||
for _, ds := range dates {
|
||||
if p.PlanDate == ds && p.CompletedQty == 0 {
|
||||
skip = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if skip {
|
||||
continue
|
||||
}
|
||||
if p.Status != "CONFIRMED" && (p.Status != "PENDING" || p.Discarded) {
|
||||
continue
|
||||
}
|
||||
rem := p.PlanQty - p.CompletedQty
|
||||
if rem < 0 {
|
||||
rem = 0
|
||||
}
|
||||
occupied += rem
|
||||
}
|
||||
quota := wo.Quantity - wo.FinishedNum - occupied
|
||||
if quota < 0 {
|
||||
quota = 0
|
||||
}
|
||||
return quota
|
||||
}
|
||||
|
||||
// withLockedWorkOrder 在事务内锁住工单行再执行额度校验与写入,避免两个计划员并发各算一次、双双通过。
|
||||
// 锁与写入必须同一连接,故所有读写都用传入的 tx。
|
||||
func (s *Service) withLockedWorkOrder(ctx context.Context, orderNo string, fn func(*ent.Tx, *ent.WorkOrder) error) error {
|
||||
tx, err := s.ctx.EntClient.Tx(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}()
|
||||
wo, err := tx.WorkOrder.Query().Where(workorder.WorkOrderNo(orderNo)).ForUpdate().Only(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := fn(tx, wo); err != nil {
|
||||
return err
|
||||
}
|
||||
committed = true
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// DailyPlanRemainQty 工单详情回填的「可排额度」,与日排产页下拉口径一致。
|
||||
func (s *Service) DailyPlanRemainQty(ctx context.Context, wo *ent.WorkOrder) int {
|
||||
plans, err := s.ctx.EntClient.DailyPlan.Query().Where(dailyplan.OrderNo(wo.WorkOrderNo)).All(ctx)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return dailyPlanQuota(wo, plans, "")
|
||||
}
|
||||
|
||||
type DailyPlanReq struct {
|
||||
Id int `json:"id"`
|
||||
OrderNo string `json:"orderNo"`
|
||||
@@ -83,31 +197,6 @@ func (s *Service) SaveDailyPlan(ctx context.Context, req DailyPlanReq, operator
|
||||
if req.PlanQty <= 0 {
|
||||
ve.Add("planQty", "排产数量需大于 0", "请填写当天的计划产量(正整数)")
|
||||
}
|
||||
// 合计护栏(2026-10-20 统一口径:累计排产合计不得超过工单总数量)
|
||||
total := req.PlanQty
|
||||
sameDateDone := false
|
||||
if req.PlanDate != "" {
|
||||
cur, _ := s.ctx.EntClient.DailyPlan.Query().
|
||||
Where(dailyplan.OrderNo(req.OrderNo)).All(ctx)
|
||||
// 本次 req.PlanQty 恒计入;若「工单+日期」已有记录,则原值作废只算新值(避免旧值+新值重复累计)
|
||||
for _, p := range cur {
|
||||
if p.PlanDate == req.PlanDate {
|
||||
if p.CompletedQty > 0 {
|
||||
sameDateDone = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
total += p.PlanQty
|
||||
}
|
||||
}
|
||||
if sameDateDone {
|
||||
ve.Add("planDate", "排产日 "+req.PlanDate+" 已有完工数量,不能修改计划数量", "该日已开工生产,数量锁定;如需调整请先与生产确认,或另排其他日期")
|
||||
}
|
||||
if req.PlanQty > 0 && total > wo.Quantity {
|
||||
over := total - wo.Quantity
|
||||
ve.Add("planQty", fmt.Sprintf("工单 %s 总量 %d 件,累计排产将达 %d 件,超出 %d 件", req.OrderNo, wo.Quantity, total, over),
|
||||
fmt.Sprintf("把本次排产数量减到 %d 件或以下,或先调整工单总量", req.PlanQty-over))
|
||||
}
|
||||
switch req.Status {
|
||||
case "", "PENDING", "CONFIRMED":
|
||||
default:
|
||||
@@ -120,29 +209,80 @@ func (s *Service) SaveDailyPlan(ctx context.Context, req DailyPlanReq, operator
|
||||
if spq == nil {
|
||||
spq = map[string]int{}
|
||||
}
|
||||
exist, err := s.ctx.EntClient.DailyPlan.Query().
|
||||
Where(dailyplan.OrderNo(req.OrderNo), dailyplan.PlanDate(req.PlanDate)).First(ctx)
|
||||
status := req.Status
|
||||
if req.Status == "" {
|
||||
if status == "" {
|
||||
status = "PENDING"
|
||||
}
|
||||
if err == nil && exist != nil {
|
||||
// 编辑时若不传 status,保留该行原有状态(避免编辑把已启用(CONFIRMED)误回退为待启用)
|
||||
if req.Status == "" {
|
||||
status = exist.Status
|
||||
}
|
||||
_, err = s.ctx.EntClient.DailyPlan.UpdateOneID(exist.ID).
|
||||
SetOrderNo(req.OrderNo).SetPlanDate(req.PlanDate).SetPlanQty(req.PlanQty).SetStatus(status).SetStationPlanQty(spq).Save(ctx)
|
||||
// 额度校验与写入同一事务,工单行 ForUpdate 防并发双通过
|
||||
err = s.withLockedWorkOrder(ctx, req.OrderNo, func(tx *ent.Tx, locked *ent.WorkOrder) error {
|
||||
qve := verr.New()
|
||||
cur, err := tx.DailyPlan.Query().Where(dailyplan.OrderNo(req.OrderNo)).All(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
_, err = s.ctx.EntClient.DailyPlan.Create().
|
||||
SetOrderNo(req.OrderNo).SetPlanDate(req.PlanDate).SetPlanQty(req.PlanQty).
|
||||
SetStatus(status).SetStationPlanQty(spq).SetOperator(operator).Save(ctx)
|
||||
if err != nil {
|
||||
// 编辑通道(PUT /daily-plans/:id)按 id 精确定位要改的行,支持改日期。
|
||||
// 2026-11-27 修复:旧逻辑无视 path id、按(工单,日期)猜行——改日期时旧行原样保留、
|
||||
// 另建新行造成重复排产且额度双计。
|
||||
var editRow *ent.DailyPlan
|
||||
if req.Id > 0 {
|
||||
editRow, err = tx.DailyPlan.Query().
|
||||
Where(dailyplan.ID(req.Id), dailyplan.OrderNo(req.OrderNo)).Only(ctx)
|
||||
if ent.IsNotFound(err) {
|
||||
return fmt.Errorf("要编辑的排产行不存在或已删除,请刷新列表后重试")
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
exist, err := tx.DailyPlan.Query().
|
||||
Where(dailyplan.OrderNo(req.OrderNo), dailyplan.PlanDate(req.PlanDate)).Only(ctx)
|
||||
if err != nil && !ent.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
if editRow != nil {
|
||||
// 改日期且目标日期已有另一行:拒绝,不合并不覆盖别人的行
|
||||
if editRow.PlanDate != req.PlanDate && exist != nil && exist.ID != editRow.ID {
|
||||
return fmt.Errorf("排产日 %s 已有另一条排产,请先停用或删除那一行,再把本行改过去", req.PlanDate)
|
||||
}
|
||||
// 已有完工量的行锁定:改数量、挪日期都不允许(完工流水挂在本行上)
|
||||
if editRow.CompletedQty > 0 {
|
||||
return fmt.Errorf("排产日 %s 已有完工 %d 件,该行已锁定不能编辑;如需调整请与生产确认后另行安排", editRow.PlanDate, editRow.CompletedQty)
|
||||
}
|
||||
exist = editRow
|
||||
}
|
||||
// 新建或改日期才拦过去;编辑既有行只改数量时保留原日期,不翻历史
|
||||
if (exist == nil || exist.PlanDate != req.PlanDate) && !planDateNotPast(req.PlanDate) {
|
||||
qve.Add("planDate", "补排日期只能是今天或以后", "过去的日期是已发生的生产事实,请选今天或以后的日期补排")
|
||||
}
|
||||
for _, p := range cur {
|
||||
if p.PlanDate == req.PlanDate && p.CompletedQty > 0 {
|
||||
qve.Add("planDate", "排产日 "+req.PlanDate+" 已有完工数量,不能修改计划数量", "该日已开工生产,数量锁定;如需调整请先与生产确认,或另排其他日期")
|
||||
}
|
||||
}
|
||||
quota := dailyPlanQuota(locked, cur, req.PlanDate)
|
||||
if req.PlanQty > quota {
|
||||
qve.Add("planQty", fmt.Sprintf("工单 %s 当前可排额度 %d 件,本次 %d 件超出 %d 件", req.OrderNo, quota, req.PlanQty, req.PlanQty-quota),
|
||||
fmt.Sprintf("把本次数量减到 %d 件或以下,或先停用/调减其它占用额度的排产", quota))
|
||||
}
|
||||
if err := qve.OrNil(); err != nil {
|
||||
return err
|
||||
}
|
||||
if exist != nil {
|
||||
// 编辑时若不传 status,保留原状态;废弃标记不由本入口改动(停用/启用走专用接口)
|
||||
if req.Status == "" {
|
||||
status = exist.Status
|
||||
}
|
||||
_, err = tx.DailyPlan.UpdateOneID(exist.ID).
|
||||
SetOrderNo(req.OrderNo).SetPlanDate(req.PlanDate).SetPlanQty(req.PlanQty).
|
||||
SetStatus(status).SetStationPlanQty(spq).Save(ctx)
|
||||
} else {
|
||||
_, err = tx.DailyPlan.Create().
|
||||
SetOrderNo(req.OrderNo).SetPlanDate(req.PlanDate).SetPlanQty(req.PlanQty).
|
||||
SetStatus(status).SetStationPlanQty(spq).SetOperator(operator).Save(ctx)
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "daily.plan.save", req.OrderNo, operator, "daily_plan", req.OrderNo, "保存日排产", map[string]any{"planDate": req.PlanDate, "planQty": req.PlanQty})
|
||||
// 叫料池重算(B5:排产是池子唯一来源,任何变更立即对齐)
|
||||
@@ -163,14 +303,21 @@ func (s *Service) saveDailyPlanBatch(ctx context.Context, req DailyPlanReq, oper
|
||||
if len(dates) == 0 {
|
||||
ve.Add("planRange", "请选择排产日期范围", "选好起止日期后再批量排产")
|
||||
}
|
||||
remain, err := s.unscheduledQty(ctx, wo)
|
||||
past := false
|
||||
for _, ds := range dates {
|
||||
if !planDateNotPast(ds) {
|
||||
past = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if past {
|
||||
ve.Add("planRange", "批量排产日期包含过去日期", "补排只能选今天或以后,请调整日期范围")
|
||||
}
|
||||
allPlans, err := s.ctx.EntClient.DailyPlan.Query().Where(dailyplan.OrderNo(req.OrderNo)).All(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
quota := remain // 剩余可排 = 工单总数量 − 累计已排产量
|
||||
if quota < 0 {
|
||||
quota = 0
|
||||
}
|
||||
quota := dailyPlanQuotaExcludingDates(wo, allPlans, dates)
|
||||
days := len(dates)
|
||||
switch req.Mode {
|
||||
case "FIXED":
|
||||
@@ -254,29 +401,43 @@ func (s *Service) saveDailyPlanBatch(ctx context.Context, req DailyPlanReq, oper
|
||||
ve.Add("planQty", "该工单剩余可排为 0,无需排产(已排满)", "如需增产请先调整工单总量,或检查已有排产数量")
|
||||
return ve.OrNil()
|
||||
}
|
||||
for i, ds := range dates {
|
||||
q := planQty[i]
|
||||
if q <= 0 {
|
||||
continue
|
||||
}
|
||||
existOne, _ := s.ctx.EntClient.DailyPlan.Query().
|
||||
Where(dailyplan.OrderNo(req.OrderNo), dailyplan.PlanDate(ds)).First(ctx)
|
||||
if existOne != nil {
|
||||
// 已有完工数量的排产不被覆盖(数量已计入 existTotal,跳过不会超排);
|
||||
// 状态不再承载执行进度,保护改看实绩而不是旧状态值。
|
||||
if existOne.CompletedQty > 0 {
|
||||
continue
|
||||
}
|
||||
_, err = s.ctx.EntClient.DailyPlan.UpdateOneID(existOne.ID).
|
||||
SetPlanQty(q).SetStatus("PENDING").SetStationPlanQty(map[string]int{}).SetOperator(operator).Save(ctx)
|
||||
} else {
|
||||
_, err = s.ctx.EntClient.DailyPlan.Create().
|
||||
SetOrderNo(req.OrderNo).SetPlanDate(ds).SetPlanQty(q).
|
||||
SetStatus("PENDING").SetStationPlanQty(map[string]int{}).SetOperator(operator).Save(ctx)
|
||||
}
|
||||
err = s.withLockedWorkOrder(ctx, req.OrderNo, func(tx *ent.Tx, locked *ent.WorkOrder) error {
|
||||
cur, err := tx.DailyPlan.Query().Where(dailyplan.OrderNo(req.OrderNo)).All(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
realQuota := dailyPlanQuotaExcludingDates(locked, cur, dates)
|
||||
if sum > realQuota {
|
||||
return fmt.Errorf("各日合计 %d 件超过当前可排额度 %d 件,请调减或先停用其它占用额度的排产", sum, realQuota)
|
||||
}
|
||||
for i, ds := range dates {
|
||||
q := planQty[i]
|
||||
if q <= 0 {
|
||||
continue
|
||||
}
|
||||
existOne, _ := tx.DailyPlan.Query().
|
||||
Where(dailyplan.OrderNo(req.OrderNo), dailyplan.PlanDate(ds)).First(ctx)
|
||||
if existOne != nil {
|
||||
// 已有完工数量的排产不被覆盖(数量已计入实绩,跳过不会超排);
|
||||
// 不碰废弃标记:旧废弃行被同日覆盖后仍保持废弃,需人工启用才重新算料。
|
||||
if existOne.CompletedQty > 0 {
|
||||
continue
|
||||
}
|
||||
_, err = tx.DailyPlan.UpdateOneID(existOne.ID).
|
||||
SetPlanQty(q).SetStatus("PENDING").SetStationPlanQty(map[string]int{}).SetOperator(operator).Save(ctx)
|
||||
} else {
|
||||
_, err = tx.DailyPlan.Create().
|
||||
SetOrderNo(req.OrderNo).SetPlanDate(ds).SetPlanQty(q).
|
||||
SetStatus("PENDING").SetStationPlanQty(map[string]int{}).SetOperator(operator).Save(ctx)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
modeLabel := req.Mode
|
||||
if len(req.Items) > 0 {
|
||||
@@ -471,16 +632,20 @@ func (s *Service) ListWorkOrdersForSchedule(ctx context.Context, keyword string)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
schedByOrder := map[string]int{}
|
||||
plansByOrder := map[string][]*ent.DailyPlan{}
|
||||
for _, p := range plans {
|
||||
schedByOrder[p.OrderNo] += p.PlanQty
|
||||
plansByOrder[p.OrderNo] = append(plansByOrder[p.OrderNo], p)
|
||||
}
|
||||
out := make([]*ForScheduleWorkOrder, 0, len(wos))
|
||||
for _, wo := range wos {
|
||||
sched := schedByOrder[wo.WorkOrderNo]
|
||||
remain := wo.Quantity - sched
|
||||
// ScheduledQty=历史排产合计(含废弃行,看「排过多少料」);RemainQty=当前可排额度(欠多少台)
|
||||
sched := 0
|
||||
for _, p := range plansByOrder[wo.WorkOrderNo] {
|
||||
sched += p.PlanQty
|
||||
}
|
||||
remain := dailyPlanQuota(wo, plansByOrder[wo.WorkOrderNo], "")
|
||||
if remain <= 0 {
|
||||
continue // 已排满,排除
|
||||
continue // 没有可排额度,排除
|
||||
}
|
||||
out = append(out, &ForScheduleWorkOrder{
|
||||
WorkOrderNo: wo.WorkOrderNo,
|
||||
@@ -495,6 +660,44 @@ func (s *Service) ListWorkOrdersForSchedule(ctx context.Context, keyword string)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// StationSentInfo 工位维度已发料净额。叫料池按「工位|物料」聚合,不按排产日期拆,
|
||||
// 拿不到某一条排产的精确已发料数,只能给该工单路线工位的净额,文案须注明「工位维度」。
|
||||
type StationSentInfo struct {
|
||||
StationNo int `json:"stationNo"`
|
||||
SentQty int `json:"sentQty"`
|
||||
}
|
||||
|
||||
// DailyPlanDisableInfo 停用排产前的提示数据:该工单路线各工位的已发到货净额。
|
||||
func (s *Service) DailyPlanDisableInfo(ctx context.Context, orderNo string) ([]StationSentInfo, error) {
|
||||
wo, err := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNo(orderNo)).Only(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.New("工单不存在")
|
||||
}
|
||||
items, err := RouteStationsFromWO(ctx, s.ctx.EntClient, wo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nos := RouteStationsOfWO(items)
|
||||
out := []StationSentInfo{}
|
||||
if len(nos) == 0 {
|
||||
return out, nil
|
||||
}
|
||||
rows, err := s.ctx.EntClient.CallPool.Query().Where(callpool.StationNoIn(nos...)).All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sent := map[int]int{}
|
||||
for _, r := range rows {
|
||||
sent[r.StationNo] += r.SentQty
|
||||
}
|
||||
for _, n := range nos {
|
||||
if v := sent[n]; v != 0 {
|
||||
out = append(out, StationSentInfo{StationNo: n, SentQty: v})
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// DeleteDailyPlan 删除一条日排产记录(删除前先取记录用于留痕,删除写 event_log)
|
||||
func (s *Service) DeleteDailyPlan(ctx context.Context, id int, operator string) error {
|
||||
if id <= 0 {
|
||||
@@ -529,7 +732,17 @@ func (s *Service) SetDailyPlanStatus(ctx context.Context, id int, target, operat
|
||||
if target != "PENDING" && target != "CONFIRMED" {
|
||||
return errors.New("仅支持 启用/停用 操作")
|
||||
}
|
||||
p, err := s.ctx.EntClient.DailyPlan.Get(ctx, id)
|
||||
tx, err := s.ctx.EntClient.Tx(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}()
|
||||
p, err := tx.DailyPlan.Get(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New("日排产不存在")
|
||||
}
|
||||
@@ -538,18 +751,41 @@ func (s *Service) SetDailyPlanStatus(ctx context.Context, id int, target, operat
|
||||
return errors.New("该排产为历史状态,不能启用或停用")
|
||||
}
|
||||
if p.Status == target {
|
||||
committed = true
|
||||
return nil // 幂等:状态相同直接成功
|
||||
}
|
||||
discarded := p.Discarded
|
||||
if target == "CONFIRMED" {
|
||||
wo, err := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNo(p.OrderNo)).First(ctx)
|
||||
wo, err := tx.WorkOrder.Query().Where(workorder.WorkOrderNo(p.OrderNo)).ForUpdate().Only(ctx)
|
||||
if err != nil {
|
||||
return errors.New("工单不存在")
|
||||
}
|
||||
if wo.Status != "IN_PROGRESS" {
|
||||
return errors.New("工单未开工,不能启用排产")
|
||||
}
|
||||
// 重新启用:清废弃标记前先校验额度(已完工 + 本行剩余 + 其它有效行剩余 ≤ 工单总量)
|
||||
plans, err := tx.DailyPlan.Query().Where(dailyplan.OrderNo(p.OrderNo)).All(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rem := p.PlanQty - p.CompletedQty
|
||||
if rem < 0 {
|
||||
rem = 0
|
||||
}
|
||||
quota := dailyPlanQuota(wo, plans, p.PlanDate)
|
||||
if rem > quota {
|
||||
return fmt.Errorf("启用后占用将达 %d 件,超过工单总量 %d 件;请先停用或调减其它占用额度的排产", wo.Quantity-quota+rem, wo.Quantity)
|
||||
}
|
||||
discarded = false
|
||||
} else {
|
||||
// 停用 = 废弃这行(不再占额度);已发/已占料不回收,实物留工位
|
||||
discarded = true
|
||||
}
|
||||
if err := s.ctx.EntClient.DailyPlan.UpdateOneID(id).SetStatus(target).Exec(ctx); err != nil {
|
||||
if err := tx.DailyPlan.UpdateOneID(id).SetStatus(target).SetDiscarded(discarded).Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
committed = true
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "daily.plan.status", p.OrderNo, operator, "daily_plan", p.OrderNo,
|
||||
@@ -578,19 +814,14 @@ type AutoScheduleReq struct {
|
||||
Segments []DateSegment `json:"segments"`
|
||||
}
|
||||
|
||||
// unscheduledQty 剩余可排数量 = 工单总数量 − 累计已排产量(ΣplanQty)。
|
||||
// 2026-10-20 口径统一:排产护栏只看"累计已排是否超过工单总数",不再扣减已完工/在产
|
||||
// (已完工/在产工件由生产实绩独立追踪,排产是计划口径,二者解耦,避免误拦合法排产)。
|
||||
// unscheduledQty 剩余可排数量 = 工单量 − 已完工数 − Σ有效行剩余量(2026-11-27 新口径)。
|
||||
// 废弃的停用行不再占额度,欠的量能补排;ScheduledQtyOf 保持原语义,供工单详情展示历史排产合计。
|
||||
func (s *Service) unscheduledQty(ctx context.Context, wo *ent.WorkOrder) (int, error) {
|
||||
sched, err := s.ScheduledQtyOf(ctx, wo.WorkOrderNo)
|
||||
plans, err := s.ctx.EntClient.DailyPlan.Query().Where(dailyplan.OrderNo(wo.WorkOrderNo)).All(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
remain := wo.Quantity - sched
|
||||
if remain < 0 {
|
||||
remain = 0
|
||||
}
|
||||
return remain, nil
|
||||
return dailyPlanQuota(wo, plans, ""), nil
|
||||
}
|
||||
|
||||
// ScheduledQtyOf 累计已排产量:该工单在所有日排产中的 planQty 合计(导出,供 handler 复用)。
|
||||
@@ -732,6 +963,15 @@ func (s *Service) AutoSchedulePreview(ctx context.Context, req AutoScheduleReq)
|
||||
ve.Add("segments", "请至少选择一个日期区间", "选择排产的起止日期后再预览")
|
||||
return nil, ve.OrNil()
|
||||
}
|
||||
for _, ds := range dates {
|
||||
if !planDateNotPast(ds) {
|
||||
ve.Add("segments", "排产日期包含过去日期("+ds+"),不能补排", "把日期区间调整为今天或以后")
|
||||
break
|
||||
}
|
||||
}
|
||||
if ve.Len() > 0 {
|
||||
return nil, ve.OrNil()
|
||||
}
|
||||
_, quota, byDate, err := s.autoScheduleGuard(ctx, req.OrderNo)
|
||||
if err != nil {
|
||||
// guard 内多为单点校验(工单不存在/状态/组合),包装进收集式保持响应形态统一
|
||||
@@ -819,6 +1059,10 @@ func (s *Service) AutoScheduleApply(ctx context.Context, req AutoScheduleApplyRe
|
||||
ve.Add("items", "日期格式错误(应为 yyyy-MM-dd):"+it.Date, "逐行检查明细中的日期格式")
|
||||
continue
|
||||
}
|
||||
if !planDateNotPast(it.Date) {
|
||||
ve.Add("items", "日期 "+it.Date+" 是过去日期,不能补排", "把该日改为今天或以后")
|
||||
continue
|
||||
}
|
||||
if seen[it.Date] {
|
||||
ve.Add("items", "日期重复:"+it.Date, "同一日期只保留一行明细")
|
||||
continue
|
||||
@@ -841,28 +1085,48 @@ func (s *Service) AutoScheduleApply(ctx context.Context, req AutoScheduleApplyRe
|
||||
if err := ve.OrNil(); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, it := range req.Items {
|
||||
existOne := byDate[it.Date]
|
||||
switch {
|
||||
case it.Qty == 0:
|
||||
if existOne != nil {
|
||||
if err := s.ctx.EntClient.DailyPlan.DeleteOneID(existOne.ID).Exec(ctx); err != nil {
|
||||
err = s.withLockedWorkOrder(ctx, req.OrderNo, func(tx *ent.Tx, locked *ent.WorkOrder) error {
|
||||
cur, err := tx.DailyPlan.Query().Where(dailyplan.OrderNo(req.OrderNo)).All(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 本次将覆盖/删除的日期(无完工数量)旧值由新值替代,不再重复占额度(与批量排产同口径)
|
||||
targetDates := make([]string, 0, len(req.Items))
|
||||
for _, it := range req.Items {
|
||||
targetDates = append(targetDates, it.Date)
|
||||
}
|
||||
realQuota := dailyPlanQuotaExcludingDates(locked, cur, targetDates)
|
||||
if sum > realQuota {
|
||||
return fmt.Errorf("各日合计 %d 件超过当前可排额度 %d 件,请回到预览重新调节", sum, realQuota)
|
||||
}
|
||||
for _, it := range req.Items {
|
||||
existOne := byDate[it.Date]
|
||||
switch {
|
||||
case it.Qty == 0:
|
||||
// 已有完工数量的行不删(与批量排产一致):完工记录是排产账,删除只影响展示不删流水
|
||||
if existOne != nil && existOne.CompletedQty == 0 {
|
||||
if err := tx.DailyPlan.DeleteOneID(existOne.ID).Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case existOne != nil:
|
||||
// 覆盖式更新;原 CONFIRMED 回退 PENDING,需重新人工启用;不碰废弃标记
|
||||
if _, err := tx.DailyPlan.UpdateOneID(existOne.ID).
|
||||
SetPlanQty(it.Qty).SetStatus("PENDING").SetOperator(operator).Save(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
if _, err := tx.DailyPlan.Create().
|
||||
SetOrderNo(req.OrderNo).SetPlanDate(it.Date).SetPlanQty(it.Qty).
|
||||
SetStatus("PENDING").SetOperator(operator).Save(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case existOne != nil:
|
||||
// 覆盖式更新;原 CONFIRMED 回退 PENDING,需重新人工启用(与原自动排产口径一致)
|
||||
if _, err := s.ctx.EntClient.DailyPlan.UpdateOneID(existOne.ID).
|
||||
SetPlanQty(it.Qty).SetStatus("PENDING").SetOperator(operator).Save(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
if _, err := s.ctx.EntClient.DailyPlan.Create().
|
||||
SetOrderNo(req.OrderNo).SetPlanDate(it.Date).SetPlanQty(it.Qty).
|
||||
SetStatus("PENDING").SetOperator(operator).Save(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "daily.plan.apply", req.OrderNo, operator, "daily_plan", req.OrderNo,
|
||||
"确认排产(预览逐日调节)", map[string]any{"days": len(req.Items), "total": sum, "quota": quota})
|
||||
|
||||
Reference in New Issue
Block a user