110 lines
3.1 KiB
Go
110 lines
3.1 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"bj_power_mes/ent"
|
|
"bj_power_mes/ent/workorder"
|
|
)
|
|
|
|
type WorkOrderReq struct {
|
|
Id int `json:"id"`
|
|
WorkOrderNo string `json:"workOrderNo"`
|
|
ProductTypeId int `json:"productTypeId"`
|
|
ProductCode string `json:"productCode"`
|
|
ProductName string `json:"productName"`
|
|
Quantity int `json:"quantity"`
|
|
ProcessSeq string `json:"processSeq"`
|
|
Status string `json:"status"`
|
|
PlanStart string `json:"planStart"`
|
|
PlanEnd string `json:"planEnd"`
|
|
}
|
|
|
|
// CreateWorkOrder 创建工单
|
|
func (s *Service) CreateWorkOrder(ctx context.Context, req WorkOrderReq, operator string) error {
|
|
if req.WorkOrderNo == "" {
|
|
return errors.New("工单号不能为空")
|
|
}
|
|
if req.ProductTypeId == 0 {
|
|
return errors.New("请选择产品类型")
|
|
}
|
|
if req.Quantity <= 0 {
|
|
req.Quantity = 1
|
|
}
|
|
status := req.Status
|
|
if status == "" {
|
|
status = "CREATED"
|
|
}
|
|
if req.ProcessSeq == "" {
|
|
req.ProcessSeq = "123456789101112"
|
|
}
|
|
b := s.ctx.EntClient.WorkOrder.Create().
|
|
SetWorkOrderNo(req.WorkOrderNo).
|
|
SetProductTypeId(req.ProductTypeId).
|
|
SetProductCode(req.ProductCode).
|
|
SetProductName(req.ProductName).
|
|
SetQuantity(req.Quantity).
|
|
SetProcessSeq(req.ProcessSeq).
|
|
SetStatus(status)
|
|
if req.PlanStart != "" {
|
|
if t, err := parseDate(req.PlanStart); err == nil {
|
|
b.SetPlanStart(t)
|
|
}
|
|
}
|
|
if req.PlanEnd != "" {
|
|
if t, err := parseDate(req.PlanEnd); err == nil {
|
|
b.SetPlanEnd(t)
|
|
}
|
|
}
|
|
if _, err := b.Save(ctx); err != nil {
|
|
return err
|
|
}
|
|
s.ctx.EventLog.Write(ctx, "work.order.create", req.WorkOrderNo, operator, "work_order", req.WorkOrderNo, "创建工单", map[string]any{"quantity": req.Quantity})
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) UpdateWorkOrder(ctx context.Context, req WorkOrderReq, operator string) error {
|
|
if req.Id == 0 {
|
|
return errors.New("缺少 id")
|
|
}
|
|
u := s.ctx.EntClient.WorkOrder.UpdateOneID(req.Id).
|
|
SetStatus(req.Status).SetQuantity(req.Quantity).SetProcessSeq(req.ProcessSeq)
|
|
if req.PlanStart != "" {
|
|
if t, err := parseDate(req.PlanStart); err == nil {
|
|
u.SetPlanStart(t)
|
|
}
|
|
}
|
|
if req.PlanEnd != "" {
|
|
if t, err := parseDate(req.PlanEnd); err == nil {
|
|
u.SetPlanEnd(t)
|
|
}
|
|
}
|
|
if err := u.Exec(ctx); err != nil {
|
|
return err
|
|
}
|
|
wo, err := s.ctx.EntClient.WorkOrder.Get(ctx, req.Id)
|
|
if err == nil {
|
|
s.ctx.EventLog.Write(ctx, "work.order.update", wo.WorkOrderNo, operator, "work_order", wo.WorkOrderNo, "更新工单", map[string]any{"status": req.Status})
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListWorkOrders 查询工单(支持工单号/状态过滤)
|
|
func (s *Service) ListWorkOrders(ctx context.Context, orderNo, status string) ([]*ent.WorkOrder, error) {
|
|
q := s.ctx.EntClient.WorkOrder.Query()
|
|
if orderNo != "" {
|
|
q = q.Where(workorder.WorkOrderNoContains(orderNo))
|
|
}
|
|
if status != "" {
|
|
q = q.Where(workorder.Status(status))
|
|
}
|
|
return q.Order(ent.Desc(workorder.FieldID)).All(ctx)
|
|
}
|
|
|
|
func (s *Service) GetWorkOrder(ctx context.Context, id int) (*ent.WorkOrder, error) {
|
|
if id > 0 {
|
|
return s.ctx.EntClient.WorkOrder.Get(ctx, id)
|
|
}
|
|
return s.ctx.EntClient.WorkOrder.Query().Order(ent.Desc(workorder.FieldID)).First(ctx)
|
|
} |