2026-08-28 15:06:01 +08:00
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-11-19 08:31:05 +08:00
|
|
|
"bj_power_mes/ent"
|
2026-09-04 14:18:53 +08:00
|
|
|
"bj_power_mes/ent/dailyplan"
|
2026-08-28 15:06:01 +08:00
|
|
|
"bj_power_mes/ent/workorder"
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-19 08:31:05 +08:00
|
|
|
// bumpWorkOrderProgress 增加工单已完成数量(由完工动作驱动)。
|
|
|
|
|
// client 传调用方事务 client(自动完工与报工同事务)时,完工计数随事务一起提交/回滚。
|
|
|
|
|
func (s *Service) bumpWorkOrderProgress(ctx context.Context, client *ent.Client, orderNo, sn string) {
|
|
|
|
|
wo, err := client.WorkOrder.Query().Where(workorder.WorkOrderNo(orderNo)).First(ctx)
|
2026-08-28 15:06:01 +08:00
|
|
|
if err != nil || wo == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if wo.FinishedNum >= wo.Quantity {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-19 08:31:05 +08:00
|
|
|
_, _ = client.WorkOrder.UpdateOneID(wo.ID).
|
2026-08-28 15:06:01 +08:00
|
|
|
SetFinishedNum(wo.FinishedNum + 1).
|
|
|
|
|
SetStatus("IN_PROGRESS").Save(ctx)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 14:18:53 +08:00
|
|
|
// bumpDailyPlanCompleted 工单完工联动当日排产:completedQty+1,达到 planQty 置 DONE。
|
|
|
|
|
// 当天没有匹配排产则跳过(不做自动建单)。仅由真正的完工事件调用。
|
2026-11-16 16:42:44 +08:00
|
|
|
// 2026-11-16 修复:状态过滤补入 CONFIRMED——已启用排产此前完工不累计实绩(恒 0、diff 恒等于计划量)。
|
|
|
|
|
// CONFIRMED 未足额时保持 CONFIRMED(算料开关语义不变,不转 PROCESSING),足额仍置 DONE。
|
2026-11-19 08:31:05 +08:00
|
|
|
func (s *Service) bumpDailyPlanCompleted(ctx context.Context, client *ent.Client, orderNo string, t time.Time) {
|
2026-09-04 14:18:53 +08:00
|
|
|
date := t.Format("2006-01-02")
|
2026-11-19 08:31:05 +08:00
|
|
|
plans, err := client.DailyPlan.Query().
|
2026-09-04 14:18:53 +08:00
|
|
|
Where(dailyplan.OrderNo(orderNo), dailyplan.PlanDate(date)).
|
2026-11-16 16:42:44 +08:00
|
|
|
Where(dailyplan.StatusIn("PENDING", "CONFIRMED", "PROCESSING", "DONE")).All(ctx)
|
2026-09-04 14:18:53 +08:00
|
|
|
if err != nil || len(plans) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for _, p := range plans {
|
|
|
|
|
if p.CompletedQty >= p.PlanQty {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
newQty := p.CompletedQty + 1
|
|
|
|
|
st := p.Status
|
|
|
|
|
if newQty >= p.PlanQty {
|
|
|
|
|
st = "DONE"
|
|
|
|
|
} else if st == "PENDING" {
|
|
|
|
|
st = "PROCESSING"
|
|
|
|
|
}
|
2026-11-19 08:31:05 +08:00
|
|
|
_, _ = client.DailyPlan.UpdateOneID(p.ID).
|
2026-09-04 14:18:53 +08:00
|
|
|
SetCompletedQty(newQty).SetStatus(st).Save(ctx)
|
|
|
|
|
}
|
|
|
|
|
}
|