初始化
This commit is contained in:
@@ -1,163 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"bj_power_wms/ent"
|
||||
"bj_power_wms/ent/inspectionrecord"
|
||||
"bj_power_wms/ent/inventorybatch"
|
||||
"bj_power_wms/ent/serialnumber"
|
||||
"bj_power_wms/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
// createInspectionHandler 创建检验记录
|
||||
// body: { targetType: BATCH/SN, targetId, materialCode, inspectionType, status, resultValue, inspector }
|
||||
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
|
||||
}
|
||||
|
||||
rec, err := ctx.EntClient.InspectionRecord.Create().
|
||||
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
|
||||
}
|
||||
|
||||
// 同步库存检验状态
|
||||
if req.TargetType == "BATCH" {
|
||||
ctx.EntClient.InventoryBatch.Update().
|
||||
Where(inventorybatch.BatchNoEQ(req.TargetId)).
|
||||
SetQualityStatus(req.Status).
|
||||
ExecX(ctx0())
|
||||
} else if req.TargetType == "SN" {
|
||||
ctx.EntClient.SerialNumber.Update().
|
||||
Where(serialnumber.SnCodeEQ(req.TargetId)).
|
||||
SetQualityStatus(req.Status).
|
||||
ExecX(ctx0())
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
flipped := 0
|
||||
for _, id := range req.TargetIds {
|
||||
ctx.EntClient.InspectionRecord.Create().
|
||||
SetTargetType(req.TargetType).
|
||||
SetTargetID(id).
|
||||
SetStatus(req.Status).
|
||||
SetNillableInspector(strPtr(req.Inspector)).
|
||||
SaveX(ctx0())
|
||||
if req.TargetType == "BATCH" {
|
||||
ctx.EntClient.InventoryBatch.Update().
|
||||
Where(inventorybatch.BatchNoEQ(id)).
|
||||
SetQualityStatus(req.Status).
|
||||
ExecX(ctx0())
|
||||
} else if req.TargetType == "SN" {
|
||||
ctx.EntClient.SerialNumber.Update().
|
||||
Where(serialnumber.SnCodeEQ(id)).
|
||||
SetQualityStatus(req.Status).
|
||||
ExecX(ctx0())
|
||||
}
|
||||
flipped++
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user