初始化2
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/dailyplan"
|
||||
)
|
||||
|
||||
// parseDate 解析 yyyy-MM-dd 或标准时间
|
||||
func parseDate(v string) (time.Time, error) {
|
||||
if t, err := time.ParseInLocation("2006-01-02", v, time.Local); err == nil {
|
||||
return t, nil
|
||||
}
|
||||
return time.ParseInLocation(time.RFC3339, v, time.Local)
|
||||
}
|
||||
|
||||
type DailyPlanReq struct {
|
||||
Id int `json:"id"`
|
||||
OrderNo string `json:"orderNo"`
|
||||
PlanDate string `json:"planDate"`
|
||||
PlanQty int `json:"planQty"`
|
||||
CompletedQty int `json:"completedQty"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// SaveDailyPlan 创建/更新日排产(同一 工单+日期 唯一)
|
||||
func (s *Service) SaveDailyPlan(ctx context.Context, req DailyPlanReq, operator string) error {
|
||||
if req.OrderNo == "" || req.PlanDate == "" {
|
||||
return errors.New("工单号和排产日期必填")
|
||||
}
|
||||
exist, err := s.ctx.EntClient.DailyPlan.Query().
|
||||
Where(dailyplan.OrderNo(req.OrderNo), dailyplan.PlanDate(req.PlanDate)).First(ctx)
|
||||
status := req.Status
|
||||
if status == "" {
|
||||
status = "PENDING"
|
||||
}
|
||||
if err == nil && exist != nil {
|
||||
_, err = s.ctx.EntClient.DailyPlan.UpdateOneID(exist.ID).
|
||||
SetOrderNo(req.OrderNo).SetPlanDate(req.PlanDate).SetPlanQty(req.PlanQty).SetStatus(status).Save(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
_, err = s.ctx.EntClient.DailyPlan.Create().
|
||||
SetOrderNo(req.OrderNo).SetPlanDate(req.PlanDate).SetPlanQty(req.PlanQty).
|
||||
SetStatus(status).SetOperator(operator).Save(ctx)
|
||||
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})
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListDailyPlans 按工单/日期查询日排产
|
||||
func (s *Service) ListDailyPlans(ctx context.Context, orderNo, planDate string) ([]*ent.DailyPlan, error) {
|
||||
q := s.ctx.EntClient.DailyPlan.Query()
|
||||
if orderNo != "" {
|
||||
q = q.Where(dailyplan.OrderNo(orderNo))
|
||||
}
|
||||
if planDate != "" {
|
||||
q = q.Where(dailyplan.PlanDate(planDate))
|
||||
}
|
||||
return q.Order(ent.Desc(dailyplan.FieldPlanDate)).All(ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user