62 lines
2.1 KiB
Go
62 lines
2.1 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"bj_power_mes/ent"
|
|
"bj_power_mes/ent/bomitem"
|
|
)
|
|
|
|
type BomItemReq struct {
|
|
WorkOrderNo string `json:"workOrderNo"`
|
|
MaterialCode string `json:"materialCode"`
|
|
MaterialName string `json:"materialName"`
|
|
Spec string `json:"spec"`
|
|
Unit string `json:"unit"`
|
|
ManageMode string `json:"manageMode"`
|
|
UnitQty float64 `json:"unitQty"`
|
|
LossRate float64 `json:"lossRate"`
|
|
RequiredQty float64 `json:"requiredQty"`
|
|
SerialSns []string `json:"serialSns"`
|
|
}
|
|
|
|
// SaveBom 保存工单 BOM(覆盖式:按 工单号+物料编码 做作 upsert)
|
|
func (s *Service) SaveBom(ctx context.Context, workOrderNo string, items []BomItemReq, operator string) error {
|
|
for _, it := range items {
|
|
if it.ManageMode == "" {
|
|
it.ManageMode = "2"
|
|
}
|
|
exist, err := s.ctx.EntClient.BomItem.Query().
|
|
Where(bomitem.WorkOrderNo(workOrderNo), bomitem.MaterialCode(it.MaterialCode)).First(ctx)
|
|
if err == nil && exist != nil {
|
|
u := s.ctx.EntClient.BomItem.UpdateOneID(exist.ID).
|
|
SetMaterialName(it.MaterialName).SetSpec(it.Spec).SetUnit(it.Unit).
|
|
SetManageMode(it.ManageMode).SetUnitQty(it.UnitQty).SetLossRate(it.LossRate).
|
|
SetRequiredQty(it.RequiredQty)
|
|
if len(it.SerialSns) > 0 {
|
|
u.SetSerialSns(it.SerialSns)
|
|
}
|
|
_ = u.Exec(ctx)
|
|
} else {
|
|
b := s.ctx.EntClient.BomItem.Create().
|
|
SetWorkOrderNo(workOrderNo).SetMaterialCode(it.MaterialCode).
|
|
SetMaterialName(it.MaterialName).SetSpec(it.Spec).SetUnit(it.Unit).
|
|
SetManageMode(it.ManageMode).SetUnitQty(it.UnitQty).SetLossRate(it.LossRate).
|
|
SetRequiredQty(it.RequiredQty)
|
|
if len(it.SerialSns) > 0 {
|
|
b.SetSerialSns(it.SerialSns)
|
|
}
|
|
_ = b.Exec(ctx)
|
|
}
|
|
}
|
|
s.ctx.EventLog.Write(ctx, "bom.save", workOrderNo, operator, "work_order_bom", workOrderNo, "保存BOM", map[string]any{"count": len(items)})
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) ListBom(ctx context.Context, workOrderNo string) ([]*ent.BomItem, error) {
|
|
q := s.ctx.EntClient.BomItem.Query()
|
|
if workOrderNo != "" {
|
|
q = q.Where(bomitem.WorkOrderNo(workOrderNo))
|
|
}
|
|
return q.Order(ent.Asc(bomitem.FieldID)).All(ctx)
|
|
} |