107 lines
3.0 KiB
Go
107 lines
3.0 KiB
Go
package logic
|
||||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"errors"
|
|||
|
|
"sort"
|
|||
|
|
"strings"
|
|||
|
|
|
|||
|
|
"bj_power_mes/ent/dailyplan"
|
|||
|
|
"bj_power_mes/ent/workorder"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// DailyPlanCompareItem 日排产历史对比行(按工单汇集窗口内排产)
|
|||
|
|
type DailyPlanCompareItem struct {
|
|||
|
|
WorkOrderNo string `json:"workOrderNo"`
|
|||
|
|
ProductCode string `json:"productCode"`
|
|||
|
|
PlanQty int `json:"planQty"`
|
|||
|
|
CompletedQty int `json:"completedQty"`
|
|||
|
|
Diff int `json:"diff"`
|
|||
|
|
PlanDate string `json:"planDate"`
|
|||
|
|
Status string `json:"status"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// DailyPlanCompareSummary 汇总
|
|||
|
|
type DailyPlanCompareSummary struct {
|
|||
|
|
PlanTotal int `json:"planTotal"`
|
|||
|
|
DoneTotal int `json:"doneTotal"`
|
|||
|
|
DiffTotal int `json:"diffTotal"`
|
|||
|
|
OrderCount int `json:"orderCount"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// DailyPlanCompareResult 对比结果
|
|||
|
|
type DailyPlanCompareResult struct {
|
|||
|
|
Items []DailyPlanCompareItem `json:"items"`
|
|||
|
|
Summary DailyPlanCompareSummary `json:"summary"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// DailyPlanCompare 按 [start,end] 查 daily_plan,按工单汇集历史排产:
|
|||
|
|
// planQty=Σ计划数量、completedQty=Σ已完成、diff=planQty-completedQty;
|
|||
|
|
// productCode/status 关联 work_order;planDate 取窗口内该工单最早排产日。
|
|||
|
|
func (s *Service) DailyPlanCompare(ctx context.Context, start, end string) (*DailyPlanCompareResult, error) {
|
|||
|
|
start = strings.TrimSpace(start)
|
|||
|
|
end = strings.TrimSpace(end)
|
|||
|
|
if start == "" || end == "" {
|
|||
|
|
return nil, errors.New("请指定开始日期与结束日期")
|
|||
|
|
}
|
|||
|
|
q := s.ctx.EntClient.DailyPlan.Query().
|
|||
|
|
Where(dailyplan.PlanDateGTE(start), dailyplan.PlanDateLTE(end))
|
|||
|
|
plans, err := q.All(ctx)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
// 按工单汇集
|
|||
|
|
type agg struct {
|
|||
|
|
planQty int
|
|||
|
|
doneQty int
|
|||
|
|
date string
|
|||
|
|
}
|
|||
|
|
byOrder := map[string]*agg{}
|
|||
|
|
for _, p := range plans {
|
|||
|
|
a := byOrder[p.OrderNo]
|
|||
|
|
if a == nil {
|
|||
|
|
a = &agg{}
|
|||
|
|
byOrder[p.OrderNo] = a
|
|||
|
|
}
|
|||
|
|
a.planQty += p.PlanQty
|
|||
|
|
a.doneQty += p.CompletedQty
|
|||
|
|
if a.date == "" || p.PlanDate < a.date {
|
|||
|
|
a.date = p.PlanDate
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
orderNos := make([]string, 0, len(byOrder))
|
|||
|
|
for no := range byOrder {
|
|||
|
|
orderNos = append(orderNos, no)
|
|||
|
|
}
|
|||
|
|
sort.Strings(orderNos)
|
|||
|
|
// 关联工单:成品编码 + 工单状态
|
|||
|
|
productCodes := map[string]string{}
|
|||
|
|
statuses := map[string]string{}
|
|||
|
|
if len(orderNos) > 0 {
|
|||
|
|
wos, e := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNoIn(orderNos...)).All(ctx)
|
|||
|
|
if e == nil {
|
|||
|
|
for _, wo := range wos {
|
|||
|
|
productCodes[wo.WorkOrderNo] = wo.ProductCode
|
|||
|
|
statuses[wo.WorkOrderNo] = wo.Status
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
res := &DailyPlanCompareResult{Items: []DailyPlanCompareItem{}}
|
|||
|
|
summary := DailyPlanCompareSummary{}
|
|||
|
|
for _, no := range orderNos {
|
|||
|
|
a := byOrder[no]
|
|||
|
|
item := DailyPlanCompareItem{
|
|||
|
|
WorkOrderNo: no, ProductCode: productCodes[no],
|
|||
|
|
PlanQty: a.planQty, CompletedQty: a.doneQty, Diff: a.planQty - a.doneQty,
|
|||
|
|
PlanDate: a.date, Status: statuses[no],
|
|||
|
|
}
|
|||
|
|
res.Items = append(res.Items, item)
|
|||
|
|
summary.PlanTotal += item.PlanQty
|
|||
|
|
summary.DoneTotal += item.CompletedQty
|
|||
|
|
}
|
|||
|
|
summary.DiffTotal = summary.PlanTotal - summary.DoneTotal
|
|||
|
|
summary.OrderCount = len(res.Items)
|
|||
|
|
res.Summary = summary
|
|||
|
|
return res, nil
|
|||
|
|
}
|