feat: 完成附件存储重构与磁盘监控功能,同步物料档案与入库单规则更新
1. 新增跨平台磁盘空间监控能力,每日7点自动检测附件目录剩余空间,触发阈值告警 2. 重构附件存储方案为年/月/日/文件类型分层结构,统一MES与WMS的附件管理逻辑 3. 对齐物料档案与入库单的质量状态校验规则,仅合格品计入库存与出库 4. 实现入库单作废功能与区域库位的多级父子结构管理 5. 删除物料简称字段,补充规格型号与单位必填项,统一系统数据口径 6. 新增附件中心与磁盘状态查询接口,完善权限控制与操作日志
This commit is contained in:
@@ -4,261 +4,68 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bj_power_wms/ent"
|
||||
"bj_power_wms/ent/zone"
|
||||
"bj_power_wms/internal/svc"
|
||||
)
|
||||
|
||||
// zoneLocationReq 区域下的一个货位(货架号 / 第几层 / 位置号,均可手填)。
|
||||
// 区域维护按「区域编码」分组展示:一个区域编码 + 名称只登记一次,货位可以有多条。
|
||||
type zoneLocationReq struct {
|
||||
ID int `json:"id"`
|
||||
ShelfNo string `json:"shelfNo"`
|
||||
LayerNo string `json:"layerNo"`
|
||||
PositionNo string `json:"positionNo"`
|
||||
// 层级常量:区域(level1) → 货架(level2) → 层(level3) → 位置号(level4)
|
||||
const (
|
||||
zoneLevelRegion = 1
|
||||
zoneLevelShelf = 2
|
||||
zoneLevelLayer = 3
|
||||
zoneLevelPos = 4
|
||||
)
|
||||
|
||||
func zoneLevelName(l int) string {
|
||||
switch l {
|
||||
case 1:
|
||||
return "区域"
|
||||
case 2:
|
||||
return "货架"
|
||||
case 3:
|
||||
return "层"
|
||||
case 4:
|
||||
return "位置号"
|
||||
}
|
||||
return "节点"
|
||||
}
|
||||
|
||||
// normLoc 归一货位三级输入(去首尾空格),与 DB 组合唯一索引 COALESCE 归一空值的口径保持一致
|
||||
func normLoc(loc *zoneLocationReq) {
|
||||
loc.ShelfNo = strings.TrimSpace(loc.ShelfNo)
|
||||
loc.LayerNo = strings.TrimSpace(loc.LayerNo)
|
||||
loc.PositionNo = strings.TrimSpace(loc.PositionNo)
|
||||
// zoneNodeVO 层级节点视图对象(列表/下拉共用)
|
||||
type zoneNodeVO struct {
|
||||
ID int `json:"id"`
|
||||
Level int `json:"level"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
ParentCode string `json:"parentCode"`
|
||||
ZoneCode string `json:"zoneCode"`
|
||||
ZoneName string `json:"zoneName"`
|
||||
ShelfNo string `json:"shelfNo"`
|
||||
LayerNo string `json:"layerNo"`
|
||||
PositionNo string `json:"positionNo"`
|
||||
Status string `json:"status"`
|
||||
Description string `json:"description"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
}
|
||||
|
||||
// locComboKey 货位三级组合的比较键(同区域内判重用)
|
||||
func locComboKey(shelfNo, layerNo, positionNo string) string {
|
||||
return shelfNo + "\x00" + layerNo + "\x00" + positionNo
|
||||
}
|
||||
|
||||
// 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"`
|
||||
Locations []zoneLocationReq `json:"locations"`
|
||||
// 兼容旧版单货位入参
|
||||
ShelfNo string `json:"shelfNo"`
|
||||
LayerNo string `json:"layerNo"`
|
||||
PositionNo string `json:"positionNo"`
|
||||
}
|
||||
if err := parseJSON(r, &req); err != nil {
|
||||
fail(w, http.StatusBadRequest, "参数错误")
|
||||
return
|
||||
}
|
||||
req.ZoneCode = strings.ToUpper(strings.TrimSpace(req.ZoneCode))
|
||||
req.ZoneName = strings.TrimSpace(req.ZoneName)
|
||||
if req.ZoneCode == "" || req.ZoneName == "" {
|
||||
fail(w, http.StatusBadRequest, "区域编码和区域名称必填")
|
||||
return
|
||||
}
|
||||
if req.Locations == nil {
|
||||
req.Locations = []zoneLocationReq{{ShelfNo: req.ShelfNo, LayerNo: req.LayerNo, PositionNo: req.PositionNo}}
|
||||
}
|
||||
if len(req.Locations) == 0 {
|
||||
fail(w, http.StatusBadRequest, "至少填写一个货位")
|
||||
return
|
||||
}
|
||||
|
||||
// 区域编码已存在:区域维护按区域编码分组,同一区域应通过「编辑」继续添加货位
|
||||
sameZone, err := ctx.EntClient.Zone.Query().
|
||||
Where(zone.ZoneCodeEQ(req.ZoneCode)).
|
||||
All(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if len(sameZone) > 0 {
|
||||
fail(w, http.StatusConflict, "区域编码已存在:"+req.ZoneCode+",请在列表中编辑该区域添加货位")
|
||||
return
|
||||
}
|
||||
|
||||
// 货位唯一性(U18):请求内按四级组合判重,与 DB 组合唯一索引 ux_zones_location 一致
|
||||
seen := map[string]bool{}
|
||||
for i := range req.Locations {
|
||||
normLoc(&req.Locations[i])
|
||||
key := locComboKey(req.Locations[i].ShelfNo, req.Locations[i].LayerNo, req.Locations[i].PositionNo)
|
||||
if seen[key] {
|
||||
fail(w, http.StatusConflict, locDupMsg(req.ZoneCode, req.Locations[i].ShelfNo, req.Locations[i].LayerNo, req.Locations[i].PositionNo))
|
||||
return
|
||||
}
|
||||
seen[key] = true
|
||||
}
|
||||
|
||||
created := make([]*ent.Zone, 0, len(req.Locations))
|
||||
for _, loc := range req.Locations {
|
||||
z, err := ctx.EntClient.Zone.Create().
|
||||
SetZoneCode(req.ZoneCode).
|
||||
SetZoneName(req.ZoneName).
|
||||
SetNillableDescription(strPtr(strings.TrimSpace(req.Description))).
|
||||
SetNillableShelfNo(strPtr(loc.ShelfNo)).
|
||||
SetNillableLayerNo(strPtr(loc.LayerNo)).
|
||||
SetNillablePositionNo(strPtr(loc.PositionNo)).
|
||||
SetStatus("启用").
|
||||
Save(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
created = append(created, z)
|
||||
}
|
||||
ctx.EventLog.Write(ctx0(), "zone.create", r.Header.Get("X-Username"), "zone", req.ZoneCode,
|
||||
"新建区域 "+req.ZoneName+"("+strconv.Itoa(len(created))+" 个货位)", nil)
|
||||
ok(w, created)
|
||||
func toZoneNodeVO(z *ent.Zone) zoneNodeVO {
|
||||
return zoneNodeVO{
|
||||
ID: z.ID, Level: z.Level, Code: z.Code, Name: z.Name, ParentCode: z.ParentCode,
|
||||
ZoneCode: z.ZoneCode, ZoneName: z.ZoneName, ShelfNo: z.ShelfNo, LayerNo: z.LayerNo,
|
||||
PositionNo: z.PositionNo, Status: z.Status, Description: z.Description, CreatedAt: z.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// updateZoneHandler 编辑区域(区域信息 + 该区域下的货位一次性同步)
|
||||
func updateZoneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
// listZonesHandler 按层级列出节点(4 个 Tab 各自独立分页 + 按上级筛选)
|
||||
// 参数:level(必填1~4)、parentCode、zoneCode、shelfNo、layerNo、keyword、status、page、pageSize
|
||||
func listZonesHandler(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"`
|
||||
Status string `json:"status"`
|
||||
Locations []zoneLocationReq `json:"locations"`
|
||||
}
|
||||
if err := parseJSON(r, &req); err != nil {
|
||||
fail(w, http.StatusBadRequest, "参数错误")
|
||||
level := atoi(r.URL.Query().Get("level"), 0)
|
||||
if level < 1 || level > 4 {
|
||||
fail(w, http.StatusBadRequest, "level 必须为 1~4")
|
||||
return
|
||||
}
|
||||
req.ZoneName = strings.TrimSpace(req.ZoneName)
|
||||
if req.ZoneName == "" {
|
||||
fail(w, http.StatusBadRequest, "区域名称必填")
|
||||
return
|
||||
}
|
||||
// 兼容旧版:仅传 id 时先反查区域编码
|
||||
if req.ZoneCode == "" && req.ID > 0 {
|
||||
cur, err := ctx.EntClient.Zone.Get(ctx0(), req.ID)
|
||||
if err != nil {
|
||||
fail(w, http.StatusNotFound, "区域不存在")
|
||||
return
|
||||
}
|
||||
req.ZoneCode = cur.ZoneCode
|
||||
}
|
||||
req.ZoneCode = strings.ToUpper(strings.TrimSpace(req.ZoneCode))
|
||||
if req.ZoneCode == "" {
|
||||
fail(w, http.StatusBadRequest, "区域编码必填")
|
||||
return
|
||||
}
|
||||
status := req.Status
|
||||
if status == "" {
|
||||
status = "启用"
|
||||
}
|
||||
if status != "启用" && status != "停用" {
|
||||
fail(w, http.StatusBadRequest, "状态取值非法")
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := ctx.EntClient.Zone.Query().Where(zone.ZoneCodeEQ(req.ZoneCode)).All(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
fail(w, http.StatusNotFound, "区域不存在:"+req.ZoneCode)
|
||||
return
|
||||
}
|
||||
|
||||
// locations 为 nil(未传)时只改区域信息,不动货位;传了就按提交结果整体同步
|
||||
var toCreate []zoneLocationReq
|
||||
keepIDs := make([]int, 0, len(req.Locations))
|
||||
if req.Locations != nil {
|
||||
if len(req.Locations) == 0 {
|
||||
fail(w, http.StatusBadRequest, "至少保留一个货位")
|
||||
return
|
||||
}
|
||||
exist := make(map[int]bool, len(rows))
|
||||
for _, z := range rows {
|
||||
exist[z.ID] = true
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for i := range req.Locations {
|
||||
normLoc(&req.Locations[i])
|
||||
loc := req.Locations[i]
|
||||
key := locComboKey(loc.ShelfNo, loc.LayerNo, loc.PositionNo)
|
||||
if seen[key] {
|
||||
fail(w, http.StatusConflict, locDupMsg(req.ZoneCode, loc.ShelfNo, loc.LayerNo, loc.PositionNo))
|
||||
return
|
||||
}
|
||||
seen[key] = true
|
||||
if loc.ID > 0 {
|
||||
if !exist[loc.ID] {
|
||||
fail(w, http.StatusBadRequest, "货位不存在或不属于该区域")
|
||||
return
|
||||
}
|
||||
keepIDs = append(keepIDs, loc.ID)
|
||||
} else {
|
||||
toCreate = append(toCreate, loc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 区域信息(名称/说明/状态)在该区域所有货位行上统一更新
|
||||
if _, err := ctx.EntClient.Zone.Update().Where(zone.ZoneCodeEQ(req.ZoneCode)).
|
||||
SetZoneName(req.ZoneName).
|
||||
SetNillableDescription(strPtr(strings.TrimSpace(req.Description))).
|
||||
SetStatus(status).
|
||||
Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "更新区域失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if req.Locations != nil {
|
||||
// 先删后增:避免与 DB 组合唯一索引 ux_zones_location 冲突
|
||||
del := ctx.EntClient.Zone.Delete().Where(zone.ZoneCodeEQ(req.ZoneCode))
|
||||
if len(keepIDs) > 0 {
|
||||
del = del.Where(zone.IDNotIn(keepIDs...))
|
||||
}
|
||||
if _, err := del.Exec(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "删除货位失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
for _, loc := range req.Locations {
|
||||
if loc.ID <= 0 {
|
||||
continue
|
||||
}
|
||||
if _, err := ctx.EntClient.Zone.UpdateOneID(loc.ID).
|
||||
SetZoneName(req.ZoneName).
|
||||
SetNillableShelfNo(strPtr(loc.ShelfNo)).
|
||||
SetNillableLayerNo(strPtr(loc.LayerNo)).
|
||||
SetNillablePositionNo(strPtr(loc.PositionNo)).
|
||||
SetStatus(status).
|
||||
Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "更新货位失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
for _, loc := range toCreate {
|
||||
if _, err := ctx.EntClient.Zone.Create().
|
||||
SetZoneCode(req.ZoneCode).
|
||||
SetZoneName(req.ZoneName).
|
||||
SetNillableDescription(strPtr(strings.TrimSpace(req.Description))).
|
||||
SetNillableShelfNo(strPtr(loc.ShelfNo)).
|
||||
SetNillableLayerNo(strPtr(loc.LayerNo)).
|
||||
SetNillablePositionNo(strPtr(loc.PositionNo)).
|
||||
SetStatus(status).
|
||||
Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "新增货位失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
ctx.EventLog.Write(ctx0(), "zone.update", r.Header.Get("X-Username"), "zone", req.ZoneCode,
|
||||
"编辑区域 "+req.ZoneName, nil)
|
||||
ok(w, map[string]any{"zoneCode": req.ZoneCode, "zoneName": req.ZoneName})
|
||||
}
|
||||
}
|
||||
|
||||
// listZonesGroupedHandler 区域列表(按区域编码分组:一个区域一行,货位作为子项返回)
|
||||
// 优化点(小周问题记录 L6):原先每行 = 一个货位,同一区域编码/名称在列表里大量重复;
|
||||
// 现按区域编码聚合,货位放在 locations 子数组里,分页按「区域」计数。
|
||||
func listZonesGroupedHandler(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)
|
||||
if page <= 0 {
|
||||
@@ -267,105 +74,396 @@ func listZonesGroupedHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
if pageSize <= 0 {
|
||||
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")
|
||||
if pageSize > 200 {
|
||||
pageSize = 200
|
||||
}
|
||||
|
||||
q := ctx.EntClient.Zone.Query()
|
||||
if zoneCode != "" {
|
||||
// 区域编码搜索大小写不敏感
|
||||
q = q.Where(zone.ZoneCodeContainsFold(zoneCode))
|
||||
q := ctx.EntClient.Zone.Query().Where(zone.LevelEQ(level))
|
||||
if pc := strings.TrimSpace(r.URL.Query().Get("parentCode")); pc != "" {
|
||||
q = q.Where(zone.ParentCodeEQ(pc))
|
||||
}
|
||||
if zoneName != "" {
|
||||
q = q.Where(zone.ZoneNameContainsFold(zoneName))
|
||||
if zc := strings.TrimSpace(r.URL.Query().Get("zoneCode")); zc != "" {
|
||||
q = q.Where(zone.ZoneCodeEQ(strings.ToUpper(zc)))
|
||||
}
|
||||
if status != "" {
|
||||
q = q.Where(zone.StatusEQ(status))
|
||||
if sh := strings.TrimSpace(r.URL.Query().Get("shelfNo")); sh != "" {
|
||||
q = q.Where(zone.ShelfNoEQ(sh))
|
||||
}
|
||||
if startDate != "" {
|
||||
if t, err := time.ParseInLocation("2006-01-02", startDate, time.Local); err == nil {
|
||||
q = q.Where(zone.CreatedAtGTE(t.Unix()))
|
||||
}
|
||||
if ly := strings.TrimSpace(r.URL.Query().Get("layerNo")); ly != "" {
|
||||
q = q.Where(zone.LayerNoEQ(ly))
|
||||
}
|
||||
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()))
|
||||
}
|
||||
if kw := strings.TrimSpace(r.URL.Query().Get("keyword")); kw != "" {
|
||||
q = q.Where(zone.Or(zone.CodeContainsFold(kw), zone.NameContainsFold(kw)))
|
||||
}
|
||||
rows, err := q.Order(ent.Asc("zone_code"), ent.Asc("id")).All(ctx0())
|
||||
if st := strings.TrimSpace(r.URL.Query().Get("status")); st != "" {
|
||||
q = q.Where(zone.StatusEQ(st))
|
||||
}
|
||||
|
||||
total, err := q.Count(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
type zoneGroup struct {
|
||||
ZoneCode string `json:"zoneCode"`
|
||||
ZoneName string `json:"zoneName"`
|
||||
Description string `json:"description"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
LocationCount int `json:"locationCount"`
|
||||
Locations []map[string]any `json:"locations"`
|
||||
rows, err := q.Order(ent.Asc("code"), ent.Asc("id")).
|
||||
Offset((page - 1) * pageSize).Limit(pageSize).All(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
order := make([]string, 0)
|
||||
groups := map[string]*zoneGroup{}
|
||||
list := make([]zoneNodeVO, 0, len(rows))
|
||||
for _, z := range rows {
|
||||
g, found := groups[z.ZoneCode]
|
||||
if !found {
|
||||
g = &zoneGroup{
|
||||
ZoneCode: z.ZoneCode, ZoneName: z.ZoneName, Description: z.Description,
|
||||
Status: z.Status, CreatedAt: z.CreatedAt,
|
||||
Locations: make([]map[string]any, 0, 4),
|
||||
}
|
||||
groups[z.ZoneCode] = g
|
||||
order = append(order, z.ZoneCode)
|
||||
}
|
||||
g.Locations = append(g.Locations, map[string]any{
|
||||
"id": z.ID, "shelfNo": z.ShelfNo, "layerNo": z.LayerNo, "positionNo": z.PositionNo,
|
||||
})
|
||||
g.LocationCount++
|
||||
list = append(list, toZoneNodeVO(z))
|
||||
}
|
||||
|
||||
total := len(order)
|
||||
start := (page - 1) * pageSize
|
||||
if start > total {
|
||||
start = total
|
||||
}
|
||||
end := start + pageSize
|
||||
if end > total {
|
||||
end = total
|
||||
}
|
||||
list := make([]*zoneGroup, 0, end-start)
|
||||
for _, code := range order[start:end] {
|
||||
list = append(list, groups[code])
|
||||
}
|
||||
ok(w, map[string]any{
|
||||
"total": total,
|
||||
"list": list,
|
||||
"page": page,
|
||||
"pageSize": pageSize,
|
||||
})
|
||||
ok(w, map[string]any{"total": total, "list": list, "page": page, "pageSize": pageSize})
|
||||
}
|
||||
}
|
||||
|
||||
// listZonesForPicker 区域下拉列表(不分页,仅启用)
|
||||
func listZonesForPicker(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
// zoneNodeReq 新建层级节点请求
|
||||
type zoneNodeReq struct {
|
||||
Level int `json:"level"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
ParentCode string `json:"parentCode"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
// createZoneNodeHandler 新建层级节点(按 level 解析上级并写冗余路径;建货架自动建默认位置)
|
||||
func createZoneNodeHandler(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())
|
||||
var req zoneNodeReq
|
||||
if err := parseJSON(r, &req); err != nil {
|
||||
fail(w, http.StatusBadRequest, "参数错误")
|
||||
return
|
||||
}
|
||||
req.Code = strings.TrimSpace(req.Code)
|
||||
req.Name = strings.TrimSpace(req.Name)
|
||||
req.ParentCode = strings.TrimSpace(req.ParentCode)
|
||||
req.Remark = strings.TrimSpace(req.Remark)
|
||||
if req.Level < 1 || req.Level > 4 {
|
||||
fail(w, http.StatusBadRequest, "level 必须为 1~4")
|
||||
return
|
||||
}
|
||||
if req.Code == "" {
|
||||
fail(w, http.StatusBadRequest, "编码必填")
|
||||
return
|
||||
}
|
||||
if req.Level == 1 {
|
||||
req.Code = strings.ToUpper(req.Code)
|
||||
if req.Name == "" {
|
||||
fail(w, http.StatusBadRequest, "区域名称必填")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var zoneCode, shelfNo, layerNo, parentCode string
|
||||
parentCode = req.ParentCode
|
||||
switch req.Level {
|
||||
case 1:
|
||||
zoneCode = req.Code
|
||||
parentCode = ""
|
||||
case 2:
|
||||
if req.ParentCode == "" {
|
||||
fail(w, http.StatusBadRequest, "请选择所属区域")
|
||||
return
|
||||
}
|
||||
reg, err := ctx.EntClient.Zone.Query().Where(zone.LevelEQ(1), zone.CodeEQ(req.ParentCode)).Only(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "所属区域不存在:"+req.ParentCode)
|
||||
return
|
||||
}
|
||||
zoneCode = reg.ZoneCode
|
||||
shelfNo = req.Code
|
||||
case 3:
|
||||
if req.ParentCode == "" {
|
||||
fail(w, http.StatusBadRequest, "请选择所属货架")
|
||||
return
|
||||
}
|
||||
sh, err := ctx.EntClient.Zone.Query().Where(zone.LevelEQ(2), zone.CodeEQ(req.ParentCode)).Only(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "所属货架不存在:"+req.ParentCode)
|
||||
return
|
||||
}
|
||||
zoneCode = sh.ZoneCode
|
||||
shelfNo = sh.ShelfNo
|
||||
layerNo = req.Code
|
||||
case 4:
|
||||
if req.ParentCode == "" {
|
||||
fail(w, http.StatusBadRequest, "请选择所属层或货架(默认位置)")
|
||||
return
|
||||
}
|
||||
parent, err := ctx.EntClient.Zone.Query().
|
||||
Where(zone.CodeEQ(req.ParentCode)).Where(zone.Or(zone.LevelEQ(3), zone.LevelEQ(2))).Only(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "所属层/货架不存在:"+req.ParentCode)
|
||||
return
|
||||
}
|
||||
zoneCode = parent.ZoneCode
|
||||
shelfNo = parent.ShelfNo
|
||||
if parent.Level == 3 {
|
||||
layerNo = parent.Code
|
||||
} else {
|
||||
layerNo = "" // 默认位置:无层
|
||||
}
|
||||
}
|
||||
|
||||
// 唯一性:同层级 + 同区域 + 同货架 + 同层 + 同编码 不可重复
|
||||
dup, _ := ctx.EntClient.Zone.Query().
|
||||
Where(zone.LevelEQ(req.Level), zone.ZoneCodeEQ(zoneCode),
|
||||
zone.ShelfNoEQ(shelfNo), zone.LayerNoEQ(layerNo), zone.CodeEQ(req.Code)).
|
||||
Exist(ctx0())
|
||||
if dup {
|
||||
fail(w, http.StatusConflict, "该编码已存在:"+req.Code)
|
||||
return
|
||||
}
|
||||
|
||||
create := ctx.EntClient.Zone.Create().
|
||||
SetLevel(req.Level).SetCode(req.Code).SetParentCode(parentCode).
|
||||
SetZoneCode(zoneCode).SetNillableShelfNo(strPtr(shelfNo)).
|
||||
SetNillableLayerNo(strPtr(layerNo)).SetNillablePositionNo(strPtr(req.Code)).
|
||||
SetStatus("启用").SetNillableDescription(strPtr(req.Remark))
|
||||
if req.Level == 1 {
|
||||
create.SetZoneName(req.Name) // 区域名称
|
||||
} else if req.Level == 4 {
|
||||
create.SetName(req.Name) // 位置名称
|
||||
}
|
||||
z, err := create.Save(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 建货架(level2)时自动创建一个「默认位置」(level4,挂在货架下、无层),物料可不细分时挂这里
|
||||
if req.Level == 2 {
|
||||
_, _ = ctx.EntClient.Zone.Create().
|
||||
SetLevel(4).SetCode("默认").SetParentCode(req.Code).
|
||||
SetZoneCode(zoneCode).SetShelfNo(shelfNo).SetNillableLayerNo(strPtr("")).
|
||||
SetPositionNo("默认").SetName("默认位置").SetStatus("启用").
|
||||
Save(ctx0())
|
||||
}
|
||||
|
||||
ctx.EventLog.Write(ctx0(), "zone.create", r.Header.Get("X-Username"), "zone", req.Code,
|
||||
"新建"+zoneLevelName(req.Level)+" "+req.Code, nil)
|
||||
ok(w, toZoneNodeVO(z))
|
||||
}
|
||||
}
|
||||
|
||||
type zoneNodeUpdateReq struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Remark string `json:"remark"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// updateZoneNodeHandler 编辑节点(区域改名称/说明/停用;位置改位置名称/停用;其余层级仅改停用)
|
||||
func updateZoneNodeHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req zoneNodeUpdateReq
|
||||
if err := parseJSON(r, &req); err != nil {
|
||||
fail(w, http.StatusBadRequest, "参数错误")
|
||||
return
|
||||
}
|
||||
if req.ID <= 0 {
|
||||
fail(w, http.StatusBadRequest, "id 必填")
|
||||
return
|
||||
}
|
||||
z, err := ctx.EntClient.Zone.Get(ctx0(), req.ID)
|
||||
if err != nil {
|
||||
fail(w, http.StatusNotFound, "节点不存在")
|
||||
return
|
||||
}
|
||||
status := req.Status
|
||||
if status == "" {
|
||||
status = z.Status
|
||||
}
|
||||
if status != "启用" && status != "停用" {
|
||||
fail(w, http.StatusBadRequest, "状态取值非法")
|
||||
return
|
||||
}
|
||||
upd := ctx.EntClient.Zone.UpdateOneID(req.ID).SetStatus(status)
|
||||
if z.Level == 1 {
|
||||
upd.SetZoneName(strings.TrimSpace(req.Name))
|
||||
upd.SetNillableDescription(strPtr(strings.TrimSpace(req.Remark)))
|
||||
}
|
||||
if z.Level == 4 {
|
||||
upd.SetName(strings.TrimSpace(req.Name))
|
||||
}
|
||||
if _, err := upd.Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
ctx.EventLog.Write(ctx0(), "zone.update", r.Header.Get("X-Username"), "zone", z.Code,
|
||||
"编辑"+zoneLevelName(z.Level)+" "+z.Code, nil)
|
||||
ok(w, map[string]any{"id": req.ID})
|
||||
}
|
||||
}
|
||||
|
||||
// listZonesForPicker 库位下拉(兼容 + 层级两用):
|
||||
// - 不带 level:返回所有启用位置号(level4)扁平结构(兼容 Outbound/Stocktake/Inventory 旧调用)
|
||||
// - 带 level:返回该层级节点(用于入库页区域→货架→层→位置 级联下拉)
|
||||
func listZonesForPicker(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
level := atoi(r.URL.Query().Get("level"), 0)
|
||||
status := strings.TrimSpace(r.URL.Query().Get("status"))
|
||||
if level == 0 {
|
||||
q := ctx.EntClient.Zone.Query().Where(zone.LevelEQ(4))
|
||||
if status == "" {
|
||||
q = q.Where(zone.StatusEQ("启用"))
|
||||
} else {
|
||||
q = q.Where(zone.StatusEQ(status))
|
||||
}
|
||||
rows, err := q.Order(ent.Asc("code")).All(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
list := make([]map[string]any, 0, len(rows))
|
||||
for _, z := range rows {
|
||||
list = append(list, map[string]any{
|
||||
"zoneCode": z.ZoneCode, "zoneName": z.ZoneName, "shelfNo": z.ShelfNo,
|
||||
"layerNo": z.LayerNo, "positionNo": z.PositionNo, "name": z.Name, "code": z.Code,
|
||||
})
|
||||
}
|
||||
ok(w, list)
|
||||
return
|
||||
}
|
||||
q := ctx.EntClient.Zone.Query().Where(zone.LevelEQ(level))
|
||||
if pc := strings.TrimSpace(r.URL.Query().Get("parentCode")); pc != "" {
|
||||
q = q.Where(zone.ParentCodeEQ(pc))
|
||||
}
|
||||
if zc := strings.TrimSpace(r.URL.Query().Get("zoneCode")); zc != "" {
|
||||
q = q.Where(zone.ZoneCodeEQ(strings.ToUpper(zc)))
|
||||
}
|
||||
if sh := strings.TrimSpace(r.URL.Query().Get("shelfNo")); sh != "" {
|
||||
q = q.Where(zone.ShelfNoEQ(sh))
|
||||
}
|
||||
if ly := strings.TrimSpace(r.URL.Query().Get("layerNo")); ly != "" {
|
||||
q = q.Where(zone.LayerNoEQ(ly))
|
||||
}
|
||||
if status == "" {
|
||||
q = q.Where(zone.StatusEQ("启用"))
|
||||
} else {
|
||||
q = q.Where(zone.StatusEQ(status))
|
||||
}
|
||||
rows, err := q.Order(ent.Asc("code")).All(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
list := make([]map[string]any, 0, len(rows))
|
||||
for _, z := range rows {
|
||||
list = append(list, map[string]any{
|
||||
"id": z.ID, "level": z.Level, "code": z.Code, "name": z.Name,
|
||||
"parentCode": z.ParentCode, "zoneCode": z.ZoneCode, "shelfNo": z.ShelfNo, "layerNo": z.LayerNo,
|
||||
})
|
||||
}
|
||||
ok(w, list)
|
||||
}
|
||||
}
|
||||
|
||||
// locDupMsg 货位重复登记的统一提示(复用 locLabel 拼四级货位串,U18)。
|
||||
func locDupMsg(zoneCode, shelfNo, layerNo, positionNo string) string {
|
||||
return "该货位已存在:" + locLabel(zoneCode, shelfNo, layerNo, positionNo)
|
||||
type zoneBatchGenReq struct {
|
||||
ZoneCode string `json:"zoneCode"`
|
||||
ShelfNo string `json:"shelfNo"`
|
||||
LayerStart int `json:"layerStart"`
|
||||
LayerEnd int `json:"layerEnd"`
|
||||
PosStart int `json:"posStart"`
|
||||
PosEnd int `json:"posEnd"`
|
||||
Preview bool `json:"preview"`
|
||||
}
|
||||
|
||||
// batchGenerateHandler 批量生成位置号:选区域→货架→层范围→位置范围,先预览后提交。
|
||||
// 自动确保层节点(level3)存在;已存在的位置号跳过不重复建。
|
||||
func batchGenerateHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req zoneBatchGenReq
|
||||
if err := parseJSON(r, &req); err != nil {
|
||||
fail(w, http.StatusBadRequest, "参数错误")
|
||||
return
|
||||
}
|
||||
req.ZoneCode = strings.ToUpper(strings.TrimSpace(req.ZoneCode))
|
||||
req.ShelfNo = strings.TrimSpace(req.ShelfNo)
|
||||
if req.ZoneCode == "" || req.ShelfNo == "" {
|
||||
fail(w, http.StatusBadRequest, "请选择区域与货架")
|
||||
return
|
||||
}
|
||||
if req.LayerStart < 0 || req.LayerEnd < req.LayerStart || req.PosStart < 0 || req.PosEnd < req.PosStart {
|
||||
fail(w, http.StatusBadRequest, "层/位置起止不合法(起始需 ≤ 结束,且 ≥ 0)")
|
||||
return
|
||||
}
|
||||
sh, err := ctx.EntClient.Zone.Query().Where(zone.LevelEQ(2), zone.ZoneCodeEQ(req.ZoneCode), zone.CodeEQ(req.ShelfNo)).Only(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "货架不存在:"+req.ZoneCode+"/"+req.ShelfNo)
|
||||
return
|
||||
}
|
||||
zoneCode := sh.ZoneCode
|
||||
|
||||
layers := make([]int, 0)
|
||||
for l := req.LayerStart; l <= req.LayerEnd; l++ {
|
||||
layers = append(layers, l)
|
||||
}
|
||||
poss := make([]int, 0)
|
||||
for p := req.PosStart; p <= req.PosEnd; p++ {
|
||||
poss = append(poss, p)
|
||||
}
|
||||
if len(layers)*len(poss) > 2000 {
|
||||
fail(w, http.StatusBadRequest, "一次最多生成 2000 个位置,请缩小范围")
|
||||
return
|
||||
}
|
||||
|
||||
// 预览:返回将生成的位置清单(含是否已存在)
|
||||
type genItem struct {
|
||||
LayerNo string `json:"layerNo"`
|
||||
PositionNo string `json:"positionNo"`
|
||||
PositionName string `json:"positionName"`
|
||||
Exists bool `json:"exists"`
|
||||
}
|
||||
preview := make([]genItem, 0, len(layers)*len(poss))
|
||||
for _, l := range layers {
|
||||
ls := strconv.Itoa(l)
|
||||
for _, p := range poss {
|
||||
ps := strconv.Itoa(p)
|
||||
exists, _ := ctx.EntClient.Zone.Query().
|
||||
Where(zone.LevelEQ(4), zone.ZoneCodeEQ(zoneCode), zone.ShelfNoEQ(req.ShelfNo),
|
||||
zone.LayerNoEQ(ls), zone.CodeEQ(ps)).Exist(ctx0())
|
||||
preview = append(preview, genItem{LayerNo: ls, PositionNo: ps, PositionName: req.ShelfNo + "架" + ls + "层" + ps + "位", Exists: exists})
|
||||
}
|
||||
}
|
||||
if req.Preview {
|
||||
ok(w, map[string]any{"preview": true, "list": preview, "total": len(preview)})
|
||||
return
|
||||
}
|
||||
|
||||
// 提交:确保层节点存在 + 创建不存在的位置号
|
||||
created, skipped := 0, 0
|
||||
for _, l := range layers {
|
||||
ls := strconv.Itoa(l)
|
||||
if exist, _ := ctx.EntClient.Zone.Query().Where(zone.LevelEQ(3), zone.ZoneCodeEQ(zoneCode), zone.ShelfNoEQ(req.ShelfNo), zone.CodeEQ(ls)).Exist(ctx0()); !exist {
|
||||
if _, err := ctx.EntClient.Zone.Create().
|
||||
SetLevel(3).SetCode(ls).SetParentCode(req.ShelfNo).
|
||||
SetZoneCode(zoneCode).SetShelfNo(req.ShelfNo).SetLayerNo(ls).SetStatus("启用").Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "创建层失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
for _, p := range poss {
|
||||
ps := strconv.Itoa(p)
|
||||
if exist, _ := ctx.EntClient.Zone.Query().
|
||||
Where(zone.LevelEQ(4), zone.ZoneCodeEQ(zoneCode), zone.ShelfNoEQ(req.ShelfNo),
|
||||
zone.LayerNoEQ(ls), zone.CodeEQ(ps)).Exist(ctx0()); exist {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
if _, err := ctx.EntClient.Zone.Create().
|
||||
SetLevel(4).SetCode(ps).SetParentCode(ls).
|
||||
SetZoneCode(zoneCode).SetShelfNo(req.ShelfNo).SetLayerNo(ls).SetPositionNo(ps).
|
||||
SetName(req.ShelfNo + "架" + ls + "层" + ps + "位").SetStatus("启用").Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "创建位置失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
created++
|
||||
}
|
||||
}
|
||||
ctx.EventLog.Write(ctx0(), "zone.batch", r.Header.Get("X-Username"), "zone", req.ShelfNo,
|
||||
"批量生成位置 "+req.ZoneCode+"/"+req.ShelfNo+" 层"+strconv.Itoa(req.LayerStart)+"-"+strconv.Itoa(req.LayerEnd)+
|
||||
" 位"+strconv.Itoa(req.PosStart)+"-"+strconv.Itoa(req.PosEnd)+":新建"+strconv.Itoa(created)+" 跳过"+strconv.Itoa(skipped), nil)
|
||||
ok(w, map[string]any{"preview": false, "created": created, "skipped": skipped, "total": len(layers) * len(poss)})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user