- 新增区域状态字段与增删改查接口,支持区域启用/停用 - 重构实体字段JSON标签为camelCase格式统一接口规范 - 优化入库、盘点、装箱流程的交互与提示文案 - 新增装箱管理功能,支持合同号关联与装箱编辑 - 调整前端菜单与帮助文档适配业务场景变更
137 lines
3.4 KiB
Go
137 lines
3.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"bj_power_wms/ent"
|
|
"bj_power_wms/ent/zone"
|
|
"bj_power_wms/internal/svc"
|
|
)
|
|
|
|
// createZoneHandler 创建区域
|
|
func createZoneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
ZoneCode string `json:"zoneCode"`
|
|
ZoneName string `json:"zoneName"`
|
|
Description string `json:"description"`
|
|
}
|
|
if err := parseJSON(r, &req); err != nil {
|
|
fail(w, http.StatusBadRequest, "参数错误")
|
|
return
|
|
}
|
|
if req.ZoneCode == "" || req.ZoneName == "" {
|
|
fail(w, http.StatusBadRequest, "区域编码和区域名称必填")
|
|
return
|
|
}
|
|
|
|
exists, err := ctx.EntClient.Zone.Query().
|
|
Where(zone.ZoneCodeEQ(req.ZoneCode)).
|
|
Exist(ctx0())
|
|
if err != nil {
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
if exists {
|
|
fail(w, http.StatusConflict, "区域编码已存在")
|
|
return
|
|
}
|
|
|
|
z, err := ctx.EntClient.Zone.Create().
|
|
SetZoneCode(req.ZoneCode).
|
|
SetZoneName(req.ZoneName).
|
|
SetNillableDescription(strPtr(req.Description)).
|
|
SetStatus("启用").
|
|
Save(ctx0())
|
|
if err != nil {
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
ok(w, z)
|
|
}
|
|
}
|
|
|
|
// updateZoneHandler 编辑区域
|
|
func updateZoneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
ID int `json:"id"`
|
|
ZoneCode string `json:"zoneCode"`
|
|
ZoneName string `json:"zoneName"`
|
|
Description string `json:"description"`
|
|
}
|
|
if err := parseJSON(r, &req); err != nil || req.ID <= 0 {
|
|
fail(w, http.StatusBadRequest, "参数错误")
|
|
return
|
|
}
|
|
if req.ZoneName == "" {
|
|
fail(w, http.StatusBadRequest, "区域名称必填")
|
|
return
|
|
}
|
|
upd := ctx.EntClient.Zone.UpdateOneID(req.ID).SetZoneName(req.ZoneName)
|
|
if req.ZoneCode != "" {
|
|
upd.SetZoneCode(req.ZoneCode)
|
|
}
|
|
upd.SetNillableDescription(strPtr(req.Description))
|
|
z, err := upd.Save(ctx0())
|
|
if err != nil {
|
|
fail(w, http.StatusInternalServerError, "更新区域失败: "+err.Error())
|
|
return
|
|
}
|
|
ok(w, z)
|
|
}
|
|
}
|
|
|
|
// listZonesHandler 区域列表(分页 + 关键字)
|
|
func listZonesHandler(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)
|
|
keyword := r.URL.Query().Get("keyword")
|
|
|
|
q := ctx.EntClient.Zone.Query()
|
|
if keyword != "" {
|
|
q = q.Where(
|
|
zone.Or(
|
|
zone.ZoneCodeContains(keyword),
|
|
zone.ZoneNameContains(keyword),
|
|
),
|
|
)
|
|
}
|
|
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,
|
|
})
|
|
}
|
|
}
|
|
|
|
// listZonesForPicker 区域下拉列表(不分页,仅启用)
|
|
func listZonesForPicker(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
list, err := ctx.EntClient.Zone.Query().
|
|
Where(zone.StatusEQ("启用")).
|
|
Order(ent.Asc("id")).
|
|
All(ctx0())
|
|
if err != nil {
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
ok(w, list)
|
|
}
|
|
}
|