157 lines
3.7 KiB
Go
157 lines
3.7 KiB
Go
package system
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"management/internal/erpserver/handler"
|
|
"management/internal/erpserver/model/dto"
|
|
systemModel "management/internal/erpserver/model/system"
|
|
"management/internal/erpserver/model/view"
|
|
systemService "management/internal/erpserver/service/v1"
|
|
"management/internal/erpserver/templ/system/config"
|
|
"management/internal/pkg/convertor"
|
|
"management/internal/pkg/database"
|
|
)
|
|
|
|
type ConfigHandler struct {
|
|
*handler.Handler
|
|
configService systemService.ConfigService
|
|
}
|
|
|
|
func NewConfigHandler(handler *handler.Handler, configService systemService.ConfigService) *ConfigHandler {
|
|
return &ConfigHandler{
|
|
Handler: handler,
|
|
configService: configService,
|
|
}
|
|
}
|
|
|
|
func (h *ConfigHandler) List(w http.ResponseWriter, r *http.Request) {
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
ctx := r.Context()
|
|
h.Render(ctx, w, config.List(ctx))
|
|
case http.MethodPost:
|
|
var q dto.SearchDto
|
|
q.SearchTimeBegin, q.SearchTimeEnd = convertor.DefaultStartTimeAndEndTime(r.PostFormValue("timeBegin"), r.PostFormValue("timeEnd"))
|
|
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.configService.List(ctx, q)
|
|
if err != nil {
|
|
h.JSONErr(w, err.Error())
|
|
return
|
|
}
|
|
|
|
data := handler.ResponseList{
|
|
Code: 0,
|
|
Message: "ok",
|
|
Count: count,
|
|
Data: res,
|
|
}
|
|
h.JSON(w, data)
|
|
default:
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
}
|
|
}
|
|
|
|
func (h *ConfigHandler) Add(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
h.Render(ctx, w, config.Edit(ctx, &view.EditSysConfig{Config: &systemModel.Config{}}))
|
|
}
|
|
|
|
func (h *ConfigHandler) Edit(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
vars := r.URL.Query()
|
|
id := convertor.QueryInt[int32](vars, "id", 0)
|
|
vm := &view.EditSysConfig{}
|
|
if id > 0 {
|
|
if conf, err := h.configService.Get(r.Context(), id); err == nil {
|
|
vm.Config = conf
|
|
b, _ := json.Marshal(&conf.Value)
|
|
vm.Result = string(b)
|
|
}
|
|
}
|
|
h.Render(ctx, w, config.Edit(ctx, vm))
|
|
}
|
|
|
|
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.JSONErr(w, "Key不能为空")
|
|
return
|
|
}
|
|
if len(value) == 0 {
|
|
h.JSONErr(w, "Value不能为空")
|
|
return
|
|
}
|
|
|
|
ctx := r.Context()
|
|
if id == 0 {
|
|
arg := &systemModel.Config{
|
|
Key: key,
|
|
Value: []byte(value),
|
|
}
|
|
err := h.configService.Create(ctx, arg)
|
|
if err != nil {
|
|
if database.IsUniqueViolation(err) {
|
|
h.JSONErr(w, "数据已存在")
|
|
return
|
|
}
|
|
h.JSONErr(w, err.Error())
|
|
return
|
|
}
|
|
|
|
h.JSONOk(w, "添加成功")
|
|
} else {
|
|
res, err := h.configService.Get(ctx, id)
|
|
if err != nil {
|
|
h.JSONErr(w, err.Error())
|
|
return
|
|
}
|
|
|
|
res.Value = []byte(value)
|
|
err = h.configService.Update(ctx, res)
|
|
if err != nil {
|
|
h.JSONErr(w, err.Error())
|
|
return
|
|
}
|
|
|
|
h.JSONOk(w, "更新成功")
|
|
}
|
|
}
|
|
|
|
func (h *ConfigHandler) RefreshCache(w http.ResponseWriter, r *http.Request) {
|
|
key := r.FormValue("key")
|
|
err := h.configService.RefreshCache(r.Context(), strings.ToLower(key))
|
|
if err != nil {
|
|
h.JSONErr(w, err.Error())
|
|
return
|
|
}
|
|
|
|
h.JSONOk(w, "刷新成功")
|
|
}
|
|
|
|
func (h *ConfigHandler) ResetPear(w http.ResponseWriter, r *http.Request) {
|
|
err := h.configService.ResetPear(r.Context())
|
|
if err != nil {
|
|
h.JSONErr(w, err.Error())
|
|
return
|
|
}
|
|
h.JSONOk(w, "重置成功")
|
|
}
|
|
|
|
func (h *ConfigHandler) Pear(w http.ResponseWriter, r *http.Request) {
|
|
pear, err := h.configService.Pear(r.Context())
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
h.JSON(w, pear)
|
|
}
|