package system import ( "encoding/json" "net/http" "strings" "management/internal/db/model/dto" db "management/internal/db/sqlc" "management/internal/erpserver/biz" "management/internal/global/pearadmin" "management/internal/pkg/convertor" "management/internal/pkg/redis" "management/internal/pkg/tpl" ) type ConfigHandler interface { Add(w http.ResponseWriter, r *http.Request) Edit(w http.ResponseWriter, r *http.Request) Save(w http.ResponseWriter, r *http.Request) List(w http.ResponseWriter, r *http.Request) Refresh(w http.ResponseWriter, r *http.Request) ResetPear(w http.ResponseWriter, r *http.Request) ConfigExpansion } type ConfigExpansion interface { Pear(w http.ResponseWriter, r *http.Request) } // configHandler 是 ConfigHandler 接口的实现. type configHandler struct { render tpl.Renderer redis redis.IRedis biz biz.IBiz } // 确保 userHandler 实现了 ConfigHandler 接口. var _ ConfigHandler = (*configHandler)(nil) func NewConfigHandler(render tpl.Renderer, redis redis.IRedis, biz biz.IBiz) *configHandler { return &configHandler{ render: render, redis: redis, biz: biz, } } func (h *configHandler) Add(w http.ResponseWriter, r *http.Request) { h.render.HTML(w, r, "config/edit.tmpl", map[string]any{ "Item": &db.SysConfig{}, "Result": "", }) } type EditSysConfig struct { *db.SysConfig Result string } func (h *configHandler) Edit(w http.ResponseWriter, r *http.Request) { vars := r.URL.Query() id := convertor.QueryInt[int32](vars, "id", 0) vm := &EditSysConfig{} if id > 0 { if conf, err := h.biz.SystemV1().ConfigBiz().Get(r.Context(), id); err == nil { vm.SysConfig = conf vm.Result = string(conf.Value) } } h.render.HTML(w, r, "config/edit.tmpl", map[string]any{ "Item": vm.SysConfig, "Result": vm.Result, }) } func (h *configHandler) Save(w http.ResponseWriter, r *http.Request) { id := convertor.ConvertInt[int32](r.PostFormValue("ID"), 0) key := r.PostFormValue("Key") value := r.PostFormValue("Value") if len(key) == 0 { h.render.JSONERR(w, "Key不能为空") return } if len(value) == 0 { h.render.JSONERR(w, "Value不能为空") return } ctx := r.Context() if id == 0 { arg := &db.CreateSysConfigParams{ Key: key, Value: []byte(value), } err := h.biz.SystemV1().ConfigBiz().Create(ctx, arg) if err != nil { if db.IsUniqueViolation(err) { h.render.JSONERR(w, "数据已存在") return } h.render.JSONERR(w, err.Error()) return } h.render.JSONOK(w, "添加成功") } else { res, err := h.biz.SystemV1().ConfigBiz().Get(ctx, id) if err != nil { h.render.JSONERR(w, err.Error()) return } arg := &db.UpdateSysConfigByKeyParams{ Key: res.Key, Value: []byte(value), } err = h.biz.SystemV1().ConfigBiz().Update(ctx, arg) if err != nil { h.render.JSONERR(w, err.Error()) return } h.render.JSONOK(w, "更新成功") } } func (h *configHandler) List(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: h.render.HTML(w, r, "config/list.tmpl", nil) case http.MethodPost: var q dto.SearchDto q.SearchName = r.PostFormValue("name") q.Page = convertor.ConvertInt(r.PostFormValue("page"), 1) q.Rows = convertor.ConvertInt(r.PostFormValue("rows"), 10) ctx := r.Context() res, count, err := h.biz.SystemV1().ConfigBiz().List(ctx, q) if err != nil { h.render.JSONERR(w, err.Error()) return } data := tpl.ResponseList{ Code: 0, Message: "ok", Count: count, Data: res, } h.render.JSON(w, data) default: http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) } } func (h *configHandler) Refresh(w http.ResponseWriter, r *http.Request) { key := r.FormValue("key") err := h.redis.Del(r.Context(), strings.ToLower(key)) if err != nil { h.render.JSONERR(w, err.Error()) return } h.render.JSONOK(w, "刷新成功") } func (h *configHandler) ResetPear(w http.ResponseWriter, r *http.Request) { b, err := json.Marshal(pearadmin.PearJson) if err != nil { h.render.JSONERR(w, err.Error()) return } err = h.biz.SystemV1().ConfigBiz().Update(r.Context(), &db.UpdateSysConfigByKeyParams{ Key: pearadmin.PearKey, Value: b, }) if err != nil { h.render.JSONERR(w, err.Error()) return } h.render.JSONOK(w, "重置成功") } func (h *configHandler) Pear(w http.ResponseWriter, r *http.Request) { pear, err := h.biz.SystemV1().ConfigBiz().Pear(r.Context()) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } h.render.JSON(w, pear) }