chore: 批量完成项目多模块迭代优化
1. 废弃半成品库存表并统一库存主表 2. 修复列表排序与搜索大小写不敏感问题 3. 新增用户最近登录时间、工单BOM名称字段 4. 完善角色管理、工位选择器功能 5. 增加拧紧数据补录、成品自动回流WMS能力 6. 统一导出时间范围校验规则 7. 丰富物料/备料单筛选条件 8. 调整菜单与权限命名适配产品型号业务
This commit is contained in:
@@ -2,14 +2,20 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/bomitem"
|
||||
"bj_power_mes/ent/workorder"
|
||||
)
|
||||
|
||||
// 默认BOMName 同一型号多份 BOM 并存时的缺省名称(历史数据/未指定时统一回填此值)
|
||||
const DefaultBOMName = "默认"
|
||||
|
||||
type BomItemReq struct {
|
||||
ProductCode string `json:"productCode"`
|
||||
BomName string `json:"bomName"`
|
||||
MaterialCode string `json:"materialCode"`
|
||||
MaterialName string `json:"materialName"`
|
||||
Spec string `json:"spec"`
|
||||
@@ -22,14 +28,25 @@ type BomItemReq struct {
|
||||
SerialSns []string `json:"serialSns"`
|
||||
}
|
||||
|
||||
// SaveBom 保存产品 BOM(覆盖式:按 产品编码+物料编码 做 upsert)
|
||||
func (s *Service) SaveBom(ctx context.Context, productCode string, items []BomItemReq, operator string) error {
|
||||
// bomNameOrDefault 空 BOM 名回退为「默认」(兼容旧前端/旧数据)
|
||||
func bomNameOrDefault(name string) string {
|
||||
if name == "" {
|
||||
return DefaultBOMName
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// SaveBom 保存产品 BOM(覆盖式 upsert:唯一键 = 产品编码+BOM名称+物料编码)。
|
||||
// 同一产品型号可并存多份 BOM(不同厂家供货/含半成品等不同料单结构),按 bomName 区分,
|
||||
// 工单建单时选定一份;备料/装机绑定均按工单锁定的 bomName 计算。
|
||||
func (s *Service) SaveBom(ctx context.Context, productCode, bomName string, items []BomItemReq, operator string) error {
|
||||
bomName = bomNameOrDefault(bomName)
|
||||
for _, it := range items {
|
||||
if it.ManageMode == "" {
|
||||
it.ManageMode = "2"
|
||||
}
|
||||
exist, err := s.ctx.EntClient.BomItem.Query().
|
||||
Where(bomitem.ProductCode(productCode), bomitem.MaterialCode(it.MaterialCode)).First(ctx)
|
||||
Where(bomitem.ProductCode(productCode), bomitem.BomName(bomName), 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).
|
||||
@@ -49,7 +66,7 @@ func (s *Service) SaveBom(ctx context.Context, productCode string, items []BomIt
|
||||
pc = *it.ProcessCode
|
||||
}
|
||||
b := s.ctx.EntClient.BomItem.Create().
|
||||
SetProductCode(productCode).SetMaterialCode(it.MaterialCode).
|
||||
SetProductCode(productCode).SetBomName(bomName).SetMaterialCode(it.MaterialCode).
|
||||
SetMaterialName(it.MaterialName).SetSpec(it.Spec).SetUnit(it.Unit).
|
||||
SetManageMode(it.ManageMode).SetProcessCode(pc).
|
||||
SetUnitQty(it.UnitQty).SetLossRate(it.LossRate)
|
||||
@@ -61,14 +78,70 @@ func (s *Service) SaveBom(ctx context.Context, productCode string, items []BomIt
|
||||
}
|
||||
}
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "bom.save", "", operator, "product_bom", productCode, "保存产品BOM", map[string]any{"count": len(items)})
|
||||
s.ctx.EventLog.Write(ctx, "bom.save", "", operator, "product_bom", productCode, "保存产品BOM", map[string]any{"bomName": bomName, "count": len(items)})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) ListBom(ctx context.Context, productCode string) ([]*ent.BomItem, error) {
|
||||
// DeleteBomItem 移除 BOM 中的一条物料(按行主键)。
|
||||
// 安全校验:该 (产品编码+BOM名称) 下任一工单已生成备料单时禁止移除,避免已锁定料单被改动。
|
||||
func (s *Service) DeleteBomItem(ctx context.Context, id int, operator string) error {
|
||||
item, err := s.ctx.EntClient.BomItem.Get(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New("BOM 物料不存在")
|
||||
}
|
||||
used, err := s.ctx.EntClient.WorkOrder.Query().
|
||||
Where(workorder.ProductCode(item.ProductCode), workorder.BomName(item.BomName)).Exist(ctx)
|
||||
if err == nil && used {
|
||||
// 该型号+BOM 已被工单选用过(有工单绑定),谨慎起见禁止直接移除
|
||||
return fmt.Errorf("该 BOM 已被「%s」型号的工单选用,移除物料会影响备料/追溯,请先评估相关工单", item.ProductCode)
|
||||
}
|
||||
if err := s.ctx.EntClient.BomItem.DeleteOneID(id).Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "bom.item.delete", "", operator, "product_bom", item.ProductCode,
|
||||
"移除BOM物料 "+item.MaterialCode, map[string]any{"bomName": item.BomName})
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListBom 查询 BOM(可按产品编码 + BOM 名称过滤;bomName 传 "ALL" 或空且指定了产品时返回全部)
|
||||
func (s *Service) ListBom(ctx context.Context, productCode, bomName string) ([]*ent.BomItem, error) {
|
||||
q := s.ctx.EntClient.BomItem.Query()
|
||||
if productCode != "" {
|
||||
q = q.Where(bomitem.ProductCode(productCode))
|
||||
}
|
||||
return q.Order(ent.Asc(bomitem.FieldID)).All(ctx)
|
||||
}
|
||||
if bomName != "" && bomName != "ALL" {
|
||||
q = q.Where(bomitem.BomName(bomName))
|
||||
}
|
||||
return q.Order(ent.Asc(bomitem.FieldProductCode), ent.Asc(bomitem.FieldBomName), ent.Asc(bomitem.FieldID)).All(ctx)
|
||||
}
|
||||
|
||||
// BomNameInfo 型号下的一份 BOM 概要(名称 + 物料数)
|
||||
type BomNameInfo struct {
|
||||
BomName string `json:"bomName"`
|
||||
ItemCount int `json:"itemCount"`
|
||||
}
|
||||
|
||||
// ListBomNames 列出某产品型号下并存的所有 BOM 名称(工单建单选 BOM 下拉数据源)
|
||||
func (s *Service) ListBomNames(ctx context.Context, productCode string) ([]*BomNameInfo, error) {
|
||||
if productCode == "" {
|
||||
return []*BomNameInfo{}, nil
|
||||
}
|
||||
items, err := s.ctx.EntClient.BomItem.Query().
|
||||
Where(bomitem.ProductCode(productCode)).All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
order := []string{}
|
||||
count := map[string]int{}
|
||||
for _, it := range items {
|
||||
if _, ok := count[it.BomName]; !ok {
|
||||
order = append(order, it.BomName)
|
||||
}
|
||||
count[it.BomName]++
|
||||
}
|
||||
out := make([]*BomNameInfo, 0, len(order))
|
||||
for _, name := range order {
|
||||
out = append(out, &BomNameInfo{BomName: name, ItemCount: count[name]})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user