64 lines
1.5 KiB
Go
64 lines
1.5 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, "zoneCode 和 zoneName 必填")
|
|
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)).
|
|
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) {
|
|
list, err := ctx.EntClient.Zone.Query().Order(ent.Desc("id")).All(ctx0())
|
|
if err != nil {
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
ok(w, list)
|
|
}
|
|
}
|