- MES(B): 工单/BOM/备料/工序字典/PLC下发/拧紧/扫码报工/半成品/AGV/追溯逻辑,WMS与海康RCS客户端,看板Redis缓存API(概览/设备/进度/报警/趋势)+SSE - WMS(C): JWT滑动续签、Excel导入、盘点、内部API、种子数据、独立Postgres配置 - WMS客户端(E): Go网关8891反向代理+内嵌Vue3十页 - 工位终端(D): SQLite本地缓存+模拟拧紧源+内嵌Vue3页面 - Dashboard(A): 看板数据改接MES内部缓存API,SSE实时刷新+vite代理 - 清理各项目球形磨遗留代码,新增部署手册.md
164 lines
4.9 KiB
Go
164 lines
4.9 KiB
Go
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,
|
|
})
|
|
}
|
|
}
|