2026-08-28 15:06:01 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
|
|
"bj_power_wms/ent"
|
|
|
|
|
|
"bj_power_wms/ent/inspectionrecord"
|
2026-09-02 08:21:11 +08:00
|
|
|
|
"bj_power_wms/ent/inventory"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"bj_power_wms/internal/svc"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// createInspectionHandler 创建检验记录
|
|
|
|
|
|
// body: { targetType: BATCH/SN, targetId, materialCode, inspectionType, status, resultValue, inspector }
|
2026-09-02 08:21:11 +08:00
|
|
|
|
// 同步库存检验状态:BATCH → inventory(manage_mode=1, batch_no);SN → inventory(manage_mode=2, sn_code)
|
2026-08-28 15:06:01 +08:00
|
|
|
|
func createInspectionHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req struct {
|
|
|
|
|
|
TargetType string `json:"targetType"`
|
|
|
|
|
|
TargetId string `json:"targetId"`
|
|
|
|
|
|
MaterialCode string `json:"materialCode"`
|
|
|
|
|
|
InspectionType string `json:"inspectionType"`
|
|
|
|
|
|
Status string `json:"status"` // 合格/不合格
|
|
|
|
|
|
ResultValue string `json:"resultValue"`
|
|
|
|
|
|
Inspector string `json:"inspector"`
|
|
|
|
|
|
Remark string `json:"remark"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := parseJSON(r, &req); err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "参数错误")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.TargetId == "" || req.Status == "" {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "targetId 和 status 必填")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.Status != "合格" && req.Status != "不合格" {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "status 只能为 合格/不合格")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-02 08:21:11 +08:00
|
|
|
|
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().
|
2026-08-28 15:06:01 +08:00
|
|
|
|
SetTargetType(req.TargetType).
|
|
|
|
|
|
SetTargetID(req.TargetId).
|
|
|
|
|
|
SetMaterialCode(req.MaterialCode).
|
|
|
|
|
|
SetInspectionType(req.InspectionType).
|
|
|
|
|
|
SetStatus(req.Status).
|
|
|
|
|
|
SetNillableResultValue(strPtr(req.ResultValue)).
|
|
|
|
|
|
SetNillableInspector(strPtr(req.Inspector)).
|
|
|
|
|
|
SetNillableRemark(strPtr(req.Remark)).
|
|
|
|
|
|
Save(ctx0())
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 同步库存检验状态
|
2026-09-02 08:21:11 +08:00
|
|
|
|
q := tx.Inventory.Update()
|
2026-08-28 15:06:01 +08:00
|
|
|
|
if req.TargetType == "BATCH" {
|
2026-09-02 08:21:11 +08:00
|
|
|
|
q = q.Where(inventory.ManageModeEQ(1), inventory.BatchNoEQ(req.TargetId))
|
2026-08-28 15:06:01 +08:00
|
|
|
|
} else if req.TargetType == "SN" {
|
2026-09-02 08:21:11 +08:00
|
|
|
|
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
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
2026-09-02 08:21:11 +08:00
|
|
|
|
committed = true
|
2026-08-28 15:06:01 +08:00
|
|
|
|
|
|
|
|
|
|
logx.Infof("inspection: %s %s -> %s by %s", req.TargetType, req.TargetId, req.Status, req.Inspector)
|
|
|
|
|
|
ok(w, rec)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// batchFlipInspectionHandler 批量翻转检验状态
|
|
|
|
|
|
// body: { targetType: BATCH/SN, targetIds: [], status, inspector }
|
|
|
|
|
|
func batchFlipInspectionHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req struct {
|
|
|
|
|
|
TargetType string `json:"targetType"`
|
|
|
|
|
|
TargetIds []string `json:"targetIds"`
|
|
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
|
Inspector string `json:"inspector"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := parseJSON(r, &req); err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "参数错误")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(req.TargetIds) == 0 || (req.Status != "合格" && req.Status != "不合格") {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "targetIds 和 status(合格/不合格) 必填")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-02 08:21:11 +08:00
|
|
|
|
tx, err := ctx.EntClient.Tx(ctx0())
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, "开启事务失败: "+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
committed := false
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
if !committed {
|
|
|
|
|
|
_ = tx.Rollback()
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
2026-08-28 15:06:01 +08:00
|
|
|
|
flipped := 0
|
|
|
|
|
|
for _, id := range req.TargetIds {
|
2026-09-02 08:21:11 +08:00
|
|
|
|
if _, err = tx.InspectionRecord.Create().
|
2026-08-28 15:06:01 +08:00
|
|
|
|
SetTargetType(req.TargetType).
|
|
|
|
|
|
SetTargetID(id).
|
|
|
|
|
|
SetStatus(req.Status).
|
|
|
|
|
|
SetNillableInspector(strPtr(req.Inspector)).
|
2026-09-02 08:21:11 +08:00
|
|
|
|
Save(ctx0()); err != nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, "创建检验记录失败: "+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
q := tx.Inventory.Update()
|
2026-08-28 15:06:01 +08:00
|
|
|
|
if req.TargetType == "BATCH" {
|
2026-09-02 08:21:11 +08:00
|
|
|
|
q = q.Where(inventory.ManageModeEQ(1), inventory.BatchNoEQ(id))
|
2026-08-28 15:06:01 +08:00
|
|
|
|
} else if req.TargetType == "SN" {
|
2026-09-02 08:21:11 +08:00
|
|
|
|
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
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
flipped++
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-02 08:21:11 +08:00
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, "提交事务失败: "+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
committed = true
|
|
|
|
|
|
|
2026-08-28 15:06:01 +08:00
|
|
|
|
ok(w, map[string]any{"flipped": flipped})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// queryInspectionHandler 检验记录查询
|
|
|
|
|
|
func queryInspectionHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
page := atoi(r.URL.Query().Get("page"), 1)
|
|
|
|
|
|
pageSize := atoi(r.URL.Query().Get("pageSize"), 20)
|
|
|
|
|
|
targetId := r.URL.Query().Get("targetId")
|
|
|
|
|
|
materialCode := r.URL.Query().Get("materialCode")
|
|
|
|
|
|
inspectionType := r.URL.Query().Get("inspectionType")
|
|
|
|
|
|
status := r.URL.Query().Get("status")
|
|
|
|
|
|
|
|
|
|
|
|
q := ctx.EntClient.InspectionRecord.Query()
|
|
|
|
|
|
if targetId != "" {
|
|
|
|
|
|
q = q.Where(inspectionrecord.TargetIDContains(targetId))
|
|
|
|
|
|
}
|
|
|
|
|
|
if materialCode != "" {
|
|
|
|
|
|
q = q.Where(inspectionrecord.MaterialCodeEQ(materialCode))
|
|
|
|
|
|
}
|
|
|
|
|
|
if inspectionType != "" {
|
|
|
|
|
|
q = q.Where(inspectionrecord.InspectionTypeEQ(inspectionType))
|
|
|
|
|
|
}
|
|
|
|
|
|
if status != "" {
|
|
|
|
|
|
q = q.Where(inspectionrecord.StatusEQ(status))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
total, err := q.Count(ctx0())
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
list, err := q.Order(ent.Desc("id")).
|
|
|
|
|
|
Offset((page - 1) * pageSize).Limit(pageSize).All(ctx0())
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ok(w, map[string]any{
|
|
|
|
|
|
"total": total,
|
|
|
|
|
|
"list": list,
|
|
|
|
|
|
"page": page,
|
|
|
|
|
|
"pageSize": pageSize,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|