refactor(wms): 合并库存表为统一inventory,重构业务逻辑
1. 合并inventory_batch与serial_number为统一inventory表,用manage_mode区分结构件批次/精密件SN 2. 移除冗余的material_type字段与相关逻辑 3. 重构库存查询、检验、入库出库等业务逻辑适配新表结构 4. 调整前端路由与菜单,拆分基础数据为区域维护和物料档案 5. 优化入库校验逻辑,避免空单问题 6. 调整Vite构建配置,关闭自动清空输出目录 7. 为各模块添加详细中文注释,统一业务术语 8. 生成并更新Ent自动代码文件
This commit is contained in:
@@ -5,8 +5,7 @@ import (
|
||||
|
||||
"bj_power_wms/ent"
|
||||
"bj_power_wms/ent/inspectionrecord"
|
||||
"bj_power_wms/ent/inventorybatch"
|
||||
"bj_power_wms/ent/serialnumber"
|
||||
"bj_power_wms/ent/inventory"
|
||||
"bj_power_wms/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -14,6 +13,7 @@ import (
|
||||
|
||||
// createInspectionHandler 创建检验记录
|
||||
// body: { targetType: BATCH/SN, targetId, materialCode, inspectionType, status, resultValue, inspector }
|
||||
// 同步库存检验状态:BATCH → inventory(manage_mode=1, batch_no);SN → inventory(manage_mode=2, sn_code)
|
||||
func createInspectionHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
@@ -39,7 +39,19 @@ func createInspectionHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
rec, err := ctx.EntClient.InspectionRecord.Create().
|
||||
tx, err := ctx.EntClient.Tx(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, "开启事务失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
rec, err := tx.InspectionRecord.Create().
|
||||
SetTargetType(req.TargetType).
|
||||
SetTargetID(req.TargetId).
|
||||
SetMaterialCode(req.MaterialCode).
|
||||
@@ -55,17 +67,25 @@ func createInspectionHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
|
||||
// 同步库存检验状态
|
||||
q := tx.Inventory.Update()
|
||||
if req.TargetType == "BATCH" {
|
||||
ctx.EntClient.InventoryBatch.Update().
|
||||
Where(inventorybatch.BatchNoEQ(req.TargetId)).
|
||||
SetQualityStatus(req.Status).
|
||||
ExecX(ctx0())
|
||||
q = q.Where(inventory.ManageModeEQ(1), inventory.BatchNoEQ(req.TargetId))
|
||||
} else if req.TargetType == "SN" {
|
||||
ctx.EntClient.SerialNumber.Update().
|
||||
Where(serialnumber.SnCodeEQ(req.TargetId)).
|
||||
SetQualityStatus(req.Status).
|
||||
ExecX(ctx0())
|
||||
q = q.Where(inventory.ManageModeEQ(2), inventory.SnCodeEQ(req.TargetId))
|
||||
} else {
|
||||
fail(w, http.StatusBadRequest, "targetType 只支持 BATCH/SN")
|
||||
return
|
||||
}
|
||||
if _, err = q.SetQualityStatus(req.Status).Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "同步库存检验状态失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "提交事务失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
committed = true
|
||||
|
||||
logx.Infof("inspection: %s %s -> %s by %s", req.TargetType, req.TargetId, req.Status, req.Inspector)
|
||||
ok(w, rec)
|
||||
@@ -91,28 +111,51 @@ func batchFlipInspectionHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
tx, err := ctx.EntClient.Tx(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, "开启事务失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
flipped := 0
|
||||
for _, id := range req.TargetIds {
|
||||
ctx.EntClient.InspectionRecord.Create().
|
||||
if _, err = tx.InspectionRecord.Create().
|
||||
SetTargetType(req.TargetType).
|
||||
SetTargetID(id).
|
||||
SetStatus(req.Status).
|
||||
SetNillableInspector(strPtr(req.Inspector)).
|
||||
SaveX(ctx0())
|
||||
Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "创建检验记录失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
q := tx.Inventory.Update()
|
||||
if req.TargetType == "BATCH" {
|
||||
ctx.EntClient.InventoryBatch.Update().
|
||||
Where(inventorybatch.BatchNoEQ(id)).
|
||||
SetQualityStatus(req.Status).
|
||||
ExecX(ctx0())
|
||||
q = q.Where(inventory.ManageModeEQ(1), inventory.BatchNoEQ(id))
|
||||
} else if req.TargetType == "SN" {
|
||||
ctx.EntClient.SerialNumber.Update().
|
||||
Where(serialnumber.SnCodeEQ(id)).
|
||||
SetQualityStatus(req.Status).
|
||||
ExecX(ctx0())
|
||||
q = q.Where(inventory.ManageModeEQ(2), inventory.SnCodeEQ(id))
|
||||
} else {
|
||||
fail(w, http.StatusBadRequest, "targetType 只支持 BATCH/SN")
|
||||
return
|
||||
}
|
||||
if _, err = q.SetQualityStatus(req.Status).Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "同步库存检验状态失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
flipped++
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "提交事务失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
committed = true
|
||||
|
||||
ok(w, map[string]any{"flipped": flipped})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user