1. 移除所有多字段关键字混搜,改为各筛选字段独立查询 2. 调整excel导出相关逻辑,优化文件名处理与导入模板 3. 修复物料导入的类型解析与token获取逻辑 4. 更新依赖与前端页面文案、筛选参数
159 lines
4.3 KiB
Go
159 lines
4.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"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
|
|
}
|
|
ctx.EventLog.Write(ctx0(), "zone.create", r.Header.Get("X-Username"), "zone", z.ZoneCode,
|
|
"新建区域 "+req.ZoneName, nil)
|
|
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
|
|
}
|
|
ctx.EventLog.Write(ctx0(), "zone.update", r.Header.Get("X-Username"), "zone", req.ZoneCode,
|
|
"编辑区域 #"+strconv.Itoa(req.ID)+" "+req.ZoneName, nil)
|
|
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)
|
|
zoneCode := r.URL.Query().Get("zoneCode")
|
|
zoneName := r.URL.Query().Get("zoneName")
|
|
status := r.URL.Query().Get("status")
|
|
startDate := r.URL.Query().Get("startDate")
|
|
endDate := r.URL.Query().Get("endDate")
|
|
|
|
q := ctx.EntClient.Zone.Query()
|
|
if zoneCode != "" {
|
|
// 区域编码搜索大小写不敏感
|
|
q = q.Where(zone.ZoneCodeContainsFold(zoneCode))
|
|
}
|
|
if zoneName != "" {
|
|
q = q.Where(zone.ZoneNameContainsFold(zoneName))
|
|
}
|
|
if status != "" {
|
|
q = q.Where(zone.StatusEQ(status))
|
|
}
|
|
if startDate != "" {
|
|
if t, err := time.ParseInLocation("2006-01-02", startDate, time.Local); err == nil {
|
|
q = q.Where(zone.CreatedAtGTE(t.Unix()))
|
|
}
|
|
}
|
|
if endDate != "" {
|
|
if t, err := time.ParseInLocation("2006-01-02", endDate, time.Local); err == nil {
|
|
q = q.Where(zone.CreatedAtLTE(t.Add(24 * time.Hour).Unix()))
|
|
}
|
|
}
|
|
total, err := q.Count(ctx0())
|
|
if err != nil {
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
list, err := q.Order(ent.Asc("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)
|
|
}
|
|
}
|