This commit is contained in:
2025-04-02 10:16:07 +08:00
parent a5caa734c3
commit b5ef982645
24 changed files with 149 additions and 101 deletions

View File

@@ -1,18 +1,15 @@
package system
import (
"fmt"
"net/http"
"strings"
"management/internal/db/model/dto"
db "management/internal/db/sqlc"
"management/internal/erpserver/biz"
"management/internal/erpserver/model/form"
"management/internal/pkg/binding"
"management/internal/pkg/convertor"
"management/internal/pkg/tpl"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
type CategoryHandler interface {
@@ -98,98 +95,25 @@ func (h *categoryHandler) Edit(w http.ResponseWriter, r *http.Request) {
}
func (h *categoryHandler) Save(w http.ResponseWriter, r *http.Request) {
id := convertor.ConvertInt(r.PostFormValue("ID"), 0)
parentID := convertor.ConvertInt[int32](r.PostFormValue("ParentID"), 0)
name := r.PostFormValue("Name")
icon := r.PostFormValue("File")
if len(icon) > 0 && !strings.HasPrefix(icon, "/") {
icon = "/" + icon
}
description := r.PostFormValue("Description")
letter := r.PostFormValue("Letter")
if len(letter) == 0 {
letter = uuid.New().String()
}
sort := convertor.ConvertInt[int32](r.PostFormValue("Sort"), 6666)
status := convertor.ConvertInt[int16](r.PostFormValue("Status"), 9999)
ctx := r.Context()
var parent *db.Category
if parentID > 0 {
var err error
parent, err = h.biz.SystemV1().CategoryBiz().Get(ctx, parentID)
if err != nil {
h.render.JSONERR(w, "父级节点错误")
return
}
var req form.Category
if err := binding.Form.Bind(r, &req); err != nil {
h.render.JSONERR(w, binding.ValidatorErrors(err))
return
}
path := fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)
path = strings.ReplaceAll(path, ",,", ",")
if id == 0 {
arg := &db.CreateCategoryParams{
Name: name,
Icon: icon,
Description: description,
Letter: letter,
ParentID: parentID,
ParentPath: path,
Status: status,
Sort: sort,
}
_, err := h.biz.SystemV1().CategoryBiz().Create(ctx, arg)
if *req.ID == 0 {
err := h.biz.SystemV1().CategoryBiz().Create(r.Context(), &req)
if err != nil {
if db.IsUniqueViolation(err) {
h.render.JSONERR(w, "名称已存在")
return
}
h.render.JSONERR(w, err.Error())
return
}
h.render.JSONOK(w, "添加成功")
} else {
arg := &db.UpdateCategoryParams{
ID: int32(id),
Name: pgtype.Text{
String: name,
Valid: true,
},
Icon: pgtype.Text{
String: icon,
Valid: len(icon) > 0,
},
Description: pgtype.Text{
String: description,
Valid: len(description) > 0,
},
Letter: pgtype.Text{
String: letter,
Valid: len(letter) > 0,
},
ParentID: pgtype.Int4{
Int32: parentID,
Valid: true,
},
ParentPath: pgtype.Text{
String: path,
Valid: true,
},
Sort: pgtype.Int4{
Int32: sort,
Valid: true,
},
Status: pgtype.Int2{
Int16: status,
Valid: true,
},
}
_, err := h.biz.SystemV1().CategoryBiz().Update(ctx, arg)
err := h.biz.SystemV1().CategoryBiz().Update(r.Context(), &req)
if err != nil {
h.render.JSONERR(w, err.Error())
return
}
h.render.JSONOK(w, "更新成功")
}
}