2025-03-31 15:06:32 +08:00

240 lines
5.9 KiB
Go

package system
import (
"fmt"
"net/http"
"strings"
"management/internal/db/model/dto"
db "management/internal/db/sqlc"
"management/internal/erpserver/biz"
"management/internal/pkg/convertor"
"management/internal/pkg/tpl"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
type CategoryHandler interface {
List(w http.ResponseWriter, r *http.Request)
Add(w http.ResponseWriter, r *http.Request)
AddChildren(w http.ResponseWriter, r *http.Request)
Edit(w http.ResponseWriter, r *http.Request)
Save(w http.ResponseWriter, r *http.Request)
Tree(w http.ResponseWriter, r *http.Request)
Refresh(w http.ResponseWriter, r *http.Request)
RebuildParentPath(w http.ResponseWriter, r *http.Request)
}
type categoryHandler struct {
render tpl.Renderer
biz biz.IBiz
}
var _ CategoryHandler = (*categoryHandler)(nil)
func NewCategoryHandler(render tpl.Renderer, biz biz.IBiz) *categoryHandler {
return &categoryHandler{
render: render,
biz: biz,
}
}
func (h *categoryHandler) List(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
h.render.HTML(w, r, "category/list.tmpl", nil)
case http.MethodPost:
var q dto.SearchDto
q.SearchStatus = convertor.ConvertInt(r.PostFormValue("status"), 9999)
q.SearchParentID = convertor.ConvertInt(r.PostFormValue("parentId"), 0)
q.SearchName = r.PostFormValue("name")
q.SearchID = convertor.ConvertInt[int64](r.PostFormValue("id"), 0)
q.Page = convertor.ConvertInt(r.PostFormValue("page"), 1)
q.Rows = convertor.ConvertInt(r.PostFormValue("rows"), 10)
res, count, err := h.biz.SystemV1().CategoryBiz().List(r.Context(), q)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
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 *categoryHandler) Add(w http.ResponseWriter, r *http.Request) {
h.render.HTML(w, r, "category/edit.tmpl", map[string]any{
"Item": &db.Category{Sort: 6666},
})
}
func (h *categoryHandler) AddChildren(w http.ResponseWriter, r *http.Request) {
vars := r.URL.Query()
parentID := convertor.QueryInt[int32](vars, "parentID", 0)
vm := &db.Category{ParentID: parentID, Sort: 6666}
h.render.HTML(w, r, "category/edit.tmpl", map[string]any{
"Item": vm,
})
}
func (h *categoryHandler) Edit(w http.ResponseWriter, r *http.Request) {
vars := r.URL.Query()
id := convertor.QueryInt[int32](vars, "id", 0)
vm := &db.Category{Sort: 6666}
if id > 0 {
vm, _ = h.biz.SystemV1().CategoryBiz().Get(r.Context(), id)
}
h.render.HTML(w, r, "category/edit.tmpl", map[string]any{
"Item": vm,
})
}
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
}
}
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 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)
if err != nil {
h.render.JSONERR(w, err.Error())
return
}
h.render.JSONOK(w, "更新成功")
}
}
func (h *categoryHandler) Tree(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := r.URL.Query()
if vars.Get("type") == "xmselect" {
res, err := h.biz.SystemV1().CategoryBiz().XmSelect(ctx, 0)
if err != nil {
h.render.JSONERR(w, err.Error())
return
}
h.render.JSON(w, res)
return
} else {
res, err := h.biz.SystemV1().CategoryBiz().Tree(ctx, 0)
if err != nil {
h.render.JSONERR(w, err.Error())
return
}
h.render.JSON(w, res)
return
}
}
func (h *categoryHandler) Refresh(w http.ResponseWriter, r *http.Request) {
_, err := h.biz.SystemV1().CategoryBiz().Refresh(r.Context())
if err != nil {
h.render.JSONERR(w, err.Error())
return
}
h.render.JSONOK(w, "刷新成功")
}
func (h *categoryHandler) RebuildParentPath(w http.ResponseWriter, r *http.Request) {
err := h.biz.SystemV1().CategoryBiz().RebuildParentPath(r.Context())
if err != nil {
h.render.JSONERR(w, err.Error())
return
}
h.render.JSONOK(w, "重建成功")
}