2026-10-28 10:10:17 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"bj_power_wms/ent/sysconfig"
|
|
|
|
|
|
"bj_power_wms/internal/svc"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// WMS 基础设置配置键(sys_config 表,系统管理员「基础设置」页维护,保存即生效)。
|
|
|
|
|
|
const (
|
|
|
|
|
|
CfgArchiveRemindDays = "archive.remind.days" // 数据归档提醒周期(天,0=关闭)
|
|
|
|
|
|
CfgArchiveBackupPath = "archive.backup.path" // 备份默认目标路径(记住上次成功路径)
|
2026-10-29 18:16:53 +08:00
|
|
|
|
CfgArchiveProtectDays = "archive.protect.days" // 数据保护期(天):仅允许删除该天数之前的数据,默认 180
|
2026-10-28 10:10:17 +08:00
|
|
|
|
CfgLocationRule = "location.recommend.rule" // 库位推荐规则:last=上次位置优先 / same_shelf=同货架优先
|
|
|
|
|
|
CfgInboundOrderPrefix = "inbound.order.prefix" // 入库单默认前缀/流水规则
|
|
|
|
|
|
CfgAttachmentMaxSize = "attachment.max.size.hint" // 附件单文件大小上限提示(MB)
|
|
|
|
|
|
// 库存大屏(/display)配置组(2026-10-27:大屏相关配置进基础设置)
|
|
|
|
|
|
CfgDisplayStockTitle = "display.stock.title" // 大屏主标题
|
|
|
|
|
|
CfgDisplayStockCorner = "display.stock.corner" // 大屏左上角角标文案
|
|
|
|
|
|
CfgDisplayStockRefresh = "display.stock.refresh" // 概览刷新间隔(秒)
|
|
|
|
|
|
CfgDisplayCallsRefresh = "display.calls.refresh" // 备料叫料池刷新间隔(秒)
|
|
|
|
|
|
CfgDisplayAgeBuckets = "display.age.buckets" // 库龄分档阈值(天,逗号分隔 3 档,升序)
|
2026-11-02 19:17:05 +08:00
|
|
|
|
// MES 对接(2026-11-02:页面可配,保存即生效;空=回退 yaml 配置)
|
|
|
|
|
|
CfgMesBaseURL = "mes.base.url" // MES 服务地址(如 http://127.0.0.1:8888)
|
2026-10-28 10:10:17 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var wmsSettingsDefaults = map[string]string{
|
|
|
|
|
|
CfgArchiveRemindDays: "7",
|
|
|
|
|
|
CfgArchiveBackupPath: "",
|
2026-10-29 18:16:53 +08:00
|
|
|
|
CfgArchiveProtectDays: "180",
|
2026-10-28 10:10:17 +08:00
|
|
|
|
CfgLocationRule: "last",
|
|
|
|
|
|
CfgInboundOrderPrefix: "",
|
|
|
|
|
|
CfgAttachmentMaxSize: "50",
|
|
|
|
|
|
CfgDisplayStockTitle: "库存动态大屏",
|
|
|
|
|
|
CfgDisplayStockCorner: "库房监控",
|
|
|
|
|
|
CfgDisplayStockRefresh: "5",
|
|
|
|
|
|
CfgDisplayCallsRefresh: "10",
|
|
|
|
|
|
CfgDisplayAgeBuckets: "30,90,180",
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SettingsVO WMS 基础设置视图(保存即生效)。
|
|
|
|
|
|
type SettingsVO struct {
|
|
|
|
|
|
ArchiveRemindDays int `json:"archiveRemindDays"` // 数据归档提醒周期(天,0=关闭)
|
|
|
|
|
|
ArchiveBackupPath string `json:"archiveBackupPath"` // 备份默认目标路径
|
|
|
|
|
|
LocationRule string `json:"locationRule"` // 库位推荐规则
|
|
|
|
|
|
InboundOrderPrefix string `json:"inboundOrderPrefix"` // 入库单默认前缀
|
|
|
|
|
|
AttachmentMaxSize int `json:"attachmentMaxSize"` // 附件单文件大小上限提示(MB)
|
|
|
|
|
|
DisplayStockTitle string `json:"displayStockTitle"` // 库存大屏主标题
|
|
|
|
|
|
DisplayStockCorner string `json:"displayStockCorner"` // 库存大屏左上角角标文案
|
|
|
|
|
|
DisplayStockRefresh int `json:"displayStockRefresh"` // 库存大屏概览刷新间隔(秒)
|
|
|
|
|
|
DisplayCallsRefresh int `json:"displayCallsRefresh"` // 备料叫料池刷新间隔(秒)
|
|
|
|
|
|
DisplayAgeBuckets string `json:"displayAgeBuckets"` // 库龄分档阈值(天,逗号分隔 3 档)
|
2026-11-02 19:17:05 +08:00
|
|
|
|
MesBaseURL string `json:"mesBaseURL"` // MES 服务地址(空=用配置文件默认)
|
2026-10-28 10:10:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func cfgStr(kv map[string]string, key, def string) string {
|
|
|
|
|
|
if v, ok := kv[key]; ok && strings.TrimSpace(v) != "" {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
|
|
|
|
|
return def
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func cfgInt(kv map[string]string, key string, def int) int {
|
|
|
|
|
|
v := cfgStr(kv, key, strconv.Itoa(def))
|
|
|
|
|
|
n, err := strconv.Atoi(v)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return def
|
|
|
|
|
|
}
|
|
|
|
|
|
return n
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func loadSettingsKV(ctx *svc.ServiceContext) map[string]string {
|
|
|
|
|
|
kv := map[string]string{}
|
|
|
|
|
|
if rows, err := ctx.EntClient.SysConfig.Query().All(ctx0()); err == nil {
|
|
|
|
|
|
for _, r := range rows {
|
|
|
|
|
|
kv[r.Key] = r.Value
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return kv
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CfgIntFromSysConfig 读取整型配置(未配置/非法回退 def),供提醒等模块复用。
|
|
|
|
|
|
func CfgIntFromSysConfig(ctx *svc.ServiceContext, key string, def int) int {
|
|
|
|
|
|
kv := loadSettingsKV(ctx)
|
|
|
|
|
|
return cfgInt(kv, key, def)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SettingsHandler GET /api/settings 基础设置(admin-only)
|
|
|
|
|
|
func SettingsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
kv := loadSettingsKV(ctx)
|
|
|
|
|
|
vo := SettingsVO{
|
|
|
|
|
|
ArchiveRemindDays: cfgInt(kv, CfgArchiveRemindDays, 7),
|
|
|
|
|
|
ArchiveBackupPath: cfgStr(kv, CfgArchiveBackupPath, ""),
|
|
|
|
|
|
LocationRule: cfgStr(kv, CfgLocationRule, "last"),
|
|
|
|
|
|
InboundOrderPrefix: cfgStr(kv, CfgInboundOrderPrefix, ""),
|
|
|
|
|
|
AttachmentMaxSize: cfgInt(kv, CfgAttachmentMaxSize, 50),
|
|
|
|
|
|
DisplayStockTitle: cfgStr(kv, CfgDisplayStockTitle, wmsSettingsDefaults[CfgDisplayStockTitle]),
|
|
|
|
|
|
DisplayStockCorner: cfgStr(kv, CfgDisplayStockCorner, wmsSettingsDefaults[CfgDisplayStockCorner]),
|
|
|
|
|
|
DisplayStockRefresh: cfgInt(kv, CfgDisplayStockRefresh, 5),
|
|
|
|
|
|
DisplayCallsRefresh: cfgInt(kv, CfgDisplayCallsRefresh, 10),
|
|
|
|
|
|
DisplayAgeBuckets: cfgStr(kv, CfgDisplayAgeBuckets, wmsSettingsDefaults[CfgDisplayAgeBuckets]),
|
2026-11-02 19:17:05 +08:00
|
|
|
|
MesBaseURL: strings.TrimRight(cfgStr(kv, CfgMesBaseURL, ""), "/"),
|
|
|
|
|
|
}
|
|
|
|
|
|
ok(w, vo)
|
2026-10-28 10:10:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SaveSettingsHandler PUT /api/settings 保存基础设置(admin-only)
|
|
|
|
|
|
func SaveSettingsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req SettingsVO
|
|
|
|
|
|
if err := parseJSON(r, &req); err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "请求体解析失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.ArchiveRemindDays < 0 || req.ArchiveRemindDays > 365 {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "提醒周期需 0~365 天")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.AttachmentMaxSize <= 0 || req.AttachmentMaxSize > 1024 {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "附件单文件大小上限提示需 1~1024 MB")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.LocationRule != "last" && req.LocationRule != "same_shelf" {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "库位推荐规则仅支持 last(上次位置优先)/ same_shelf(同货架优先)")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
title := strings.TrimSpace(req.DisplayStockTitle)
|
|
|
|
|
|
if title == "" || len(title) > 60 {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "库存大屏主标题需 1~60 字")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
corner := strings.TrimSpace(req.DisplayStockCorner)
|
|
|
|
|
|
if len(corner) > 30 {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "库存大屏角标文案需 0~30 字")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.DisplayStockRefresh < 3 || req.DisplayStockRefresh > 120 {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "库存大屏概览刷新间隔需 3~120 秒")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.DisplayCallsRefresh < 5 || req.DisplayCallsRefresh > 300 {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "备料叫料池刷新间隔需 5~300 秒")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
buckets := parseAgeBuckets(req.DisplayAgeBuckets)
|
|
|
|
|
|
if buckets == nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "库龄分档阈值需为逗号分隔的 3 个升序天数(如 30,90,180),各档 1~3650")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-11-02 19:17:05 +08:00
|
|
|
|
// MES 服务地址:留空=回退配置文件默认;填了必须 http(s):// 开头(2026-11-02)
|
|
|
|
|
|
mesURL := strings.TrimRight(strings.TrimSpace(req.MesBaseURL), "/")
|
|
|
|
|
|
if mesURL != "" {
|
|
|
|
|
|
if !strings.HasPrefix(mesURL, "http://") && !strings.HasPrefix(mesURL, "https://") {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "MES 服务地址需以 http:// 或 https:// 开头(如 http://127.0.0.1:8888)")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(mesURL) > 200 {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "MES 服务地址过长(≤200 字)")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-10-28 10:10:17 +08:00
|
|
|
|
kv := map[string]string{
|
|
|
|
|
|
CfgArchiveRemindDays: strconv.Itoa(req.ArchiveRemindDays),
|
|
|
|
|
|
CfgArchiveBackupPath: strings.TrimSpace(req.ArchiveBackupPath),
|
|
|
|
|
|
CfgLocationRule: req.LocationRule,
|
|
|
|
|
|
CfgInboundOrderPrefix: strings.TrimSpace(req.InboundOrderPrefix),
|
|
|
|
|
|
CfgAttachmentMaxSize: strconv.Itoa(req.AttachmentMaxSize),
|
|
|
|
|
|
CfgDisplayStockTitle: title,
|
|
|
|
|
|
CfgDisplayStockCorner: corner,
|
|
|
|
|
|
CfgDisplayStockRefresh: strconv.Itoa(req.DisplayStockRefresh),
|
|
|
|
|
|
CfgDisplayCallsRefresh: strconv.Itoa(req.DisplayCallsRefresh),
|
|
|
|
|
|
CfgDisplayAgeBuckets: strconv.Itoa(buckets[0]) + "," + strconv.Itoa(buckets[1]) + "," + strconv.Itoa(buckets[2]),
|
2026-11-02 19:17:05 +08:00
|
|
|
|
CfgMesBaseURL: mesURL,
|
2026-10-28 10:10:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
for k, v := range kv {
|
|
|
|
|
|
if err := upsertConfig(ctx, k, v, ""); err != nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-02 19:17:05 +08:00
|
|
|
|
// MES 地址热生效(空=回退配置文件默认)
|
|
|
|
|
|
ctx.Mes.SetBaseURL(mesURL)
|
2026-10-28 10:10:17 +08:00
|
|
|
|
ok(w, map[string]any{"ok": true})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// parseAgeBuckets 解析库龄分档阈值 "30,90,180" → [30 90 180];非法返回 nil。
|
|
|
|
|
|
func parseAgeBuckets(s string) []int {
|
|
|
|
|
|
parts := strings.Split(s, ",")
|
|
|
|
|
|
if len(parts) != 3 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
out := make([]int, 3)
|
|
|
|
|
|
for i, p := range parts {
|
|
|
|
|
|
n, err := strconv.Atoi(strings.TrimSpace(p))
|
|
|
|
|
|
if err != nil || n < 1 || n > 3650 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
out[i] = n
|
|
|
|
|
|
}
|
|
|
|
|
|
if out[0] >= out[1] || out[1] >= out[2] {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return out
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AgeBucketsFromConfig 读库龄分档配置(非法回退默认 30/90/180),供大屏聚合接口用。
|
|
|
|
|
|
func AgeBucketsFromConfig(ctx *svc.ServiceContext) []int {
|
|
|
|
|
|
if b := parseAgeBuckets(cfgStr(loadSettingsKV(ctx), CfgDisplayAgeBuckets, "30,90,180")); b != nil {
|
|
|
|
|
|
return b
|
|
|
|
|
|
}
|
|
|
|
|
|
return []int{30, 90, 180}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DisplayConfigVO 库存大屏运行配置(免登录只读,仅展示性配置,不含敏感信息)。
|
|
|
|
|
|
type DisplayConfigVO struct {
|
|
|
|
|
|
Title string `json:"title"` // 大屏主标题
|
|
|
|
|
|
Corner string `json:"corner"` // 左上角角标文案
|
|
|
|
|
|
RefreshSeconds int `json:"refreshSeconds"` // 概览刷新间隔(秒)
|
|
|
|
|
|
CallsRefreshSeconds int `json:"callsRefreshSeconds"` // 备料叫料池刷新间隔(秒)
|
|
|
|
|
|
AgeBuckets []int `json:"ageBuckets"` // 库龄分档阈值(天,3 档升序)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// displayConfigHandler GET /api/display/config 大屏运行配置(免登录,与 overview 同组)。
|
|
|
|
|
|
func displayConfigHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
kv := loadSettingsKV(ctx)
|
|
|
|
|
|
ok(w, DisplayConfigVO{
|
|
|
|
|
|
Title: cfgStr(kv, CfgDisplayStockTitle, wmsSettingsDefaults[CfgDisplayStockTitle]),
|
|
|
|
|
|
Corner: cfgStr(kv, CfgDisplayStockCorner, wmsSettingsDefaults[CfgDisplayStockCorner]),
|
|
|
|
|
|
RefreshSeconds: cfgInt(kv, CfgDisplayStockRefresh, 5),
|
|
|
|
|
|
CallsRefreshSeconds: cfgInt(kv, CfgDisplayCallsRefresh, 10),
|
|
|
|
|
|
AgeBuckets: AgeBucketsFromConfig(ctx),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// upsertConfig 幂等写入配置(sys_config key/value)。
|
|
|
|
|
|
func upsertConfig(ctx *svc.ServiceContext, key, value, remark string) error {
|
|
|
|
|
|
exist, err := ctx.EntClient.SysConfig.Query().Where(sysconfig.Key(key)).Only(ctx0())
|
|
|
|
|
|
if err == nil && exist != nil {
|
|
|
|
|
|
_, e := exist.Update().SetValue(value).SetRemark(remark).Save(ctx0())
|
|
|
|
|
|
return e
|
|
|
|
|
|
}
|
|
|
|
|
|
_, e := ctx.EntClient.SysConfig.Create().SetKey(key).SetValue(value).SetRemark(remark).Save(ctx0())
|
|
|
|
|
|
return e
|
|
|
|
|
|
}
|