full
This commit is contained in:
parent
a5caa734c3
commit
b5ef982645
@ -5,12 +5,12 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"management/internal/config"
|
||||
db "management/internal/db/sqlc"
|
||||
"management/internal/erpserver"
|
||||
"management/internal/erpserver/biz"
|
||||
"management/internal/erpserver/handler"
|
||||
"management/internal/pkg/binding"
|
||||
"management/internal/pkg/config"
|
||||
"management/internal/pkg/logger"
|
||||
"management/internal/pkg/middleware"
|
||||
"management/internal/pkg/redis"
|
||||
|
||||
@ -5,7 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"management/internal/config"
|
||||
"management/internal/pkg/config"
|
||||
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
@ -4,20 +4,27 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"management/internal/db/model/dto"
|
||||
db "management/internal/db/sqlc"
|
||||
"management/internal/erpserver/model/form"
|
||||
"management/internal/erpserver/model/view"
|
||||
"management/internal/pkg/convertor"
|
||||
"management/internal/pkg/know"
|
||||
"management/internal/pkg/redis"
|
||||
"management/internal/pkg/tpl/html"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type CategoryBiz interface {
|
||||
Create(ctx context.Context, arg *db.CreateCategoryParams) (*db.Category, error)
|
||||
Update(ctx context.Context, arg *db.UpdateCategoryParams) (*db.Category, error)
|
||||
Create(ctx context.Context, req *form.Category) error
|
||||
Update(ctx context.Context, req *form.Category) error
|
||||
All(ctx context.Context) ([]*db.Category, error)
|
||||
List(ctx context.Context, q dto.SearchDto) ([]*db.Category, int64, error)
|
||||
Get(ctx context.Context, id int32) (*db.Category, error)
|
||||
@ -97,12 +104,117 @@ func (b *categoryBiz) Get(ctx context.Context, id int32) (*db.Category, error) {
|
||||
return b.store.GetCategory(ctx, id)
|
||||
}
|
||||
|
||||
func (b *categoryBiz) Create(ctx context.Context, arg *db.CreateCategoryParams) (*db.Category, error) {
|
||||
return b.store.CreateCategory(ctx, arg)
|
||||
func (b *categoryBiz) Create(ctx context.Context, req *form.Category) error {
|
||||
if len(req.Icon) > 0 && !strings.HasPrefix(req.Icon, "/") {
|
||||
req.Icon = "/" + req.Icon
|
||||
}
|
||||
|
||||
if len(req.Letter) == 0 {
|
||||
req.Letter = uuid.New().String()
|
||||
}
|
||||
|
||||
parent := &db.Category{
|
||||
ID: 0,
|
||||
ParentID: 0,
|
||||
ParentPath: ",0,",
|
||||
}
|
||||
if *req.ParentID > 0 {
|
||||
var err error
|
||||
parent, err = b.store.GetCategory(ctx, *req.ParentID)
|
||||
if err != nil {
|
||||
return errors.New("父级节点错误")
|
||||
}
|
||||
}
|
||||
|
||||
var order int32 = 6666
|
||||
if *req.Sort > 0 {
|
||||
order = *req.Sort
|
||||
}
|
||||
|
||||
arg := &db.CreateCategoryParams{
|
||||
Name: req.Name,
|
||||
Icon: req.Icon,
|
||||
Description: req.Description,
|
||||
Letter: req.Letter,
|
||||
ParentID: parent.ID,
|
||||
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||
Status: *req.Status,
|
||||
Sort: order,
|
||||
}
|
||||
_, err := b.store.CreateCategory(ctx, arg)
|
||||
if err != nil {
|
||||
if db.IsUniqueViolation(err) {
|
||||
return errors.New("类别已存在")
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *categoryBiz) Update(ctx context.Context, arg *db.UpdateCategoryParams) (*db.Category, error) {
|
||||
return b.store.UpdateCategory(ctx, arg)
|
||||
func (b *categoryBiz) Update(ctx context.Context, req *form.Category) error {
|
||||
if len(req.Icon) > 0 && !strings.HasPrefix(req.Icon, "/") {
|
||||
req.Icon = "/" + req.Icon
|
||||
}
|
||||
|
||||
if len(req.Letter) == 0 {
|
||||
req.Letter = uuid.New().String()
|
||||
}
|
||||
|
||||
parent := &db.Category{
|
||||
ID: 0,
|
||||
ParentID: 0,
|
||||
ParentPath: ",0,",
|
||||
}
|
||||
if *req.ParentID > 0 {
|
||||
var err error
|
||||
parent, err = b.store.GetCategory(ctx, *req.ParentID)
|
||||
if err != nil {
|
||||
return errors.New("父级节点错误")
|
||||
}
|
||||
}
|
||||
|
||||
var order int32 = 6666
|
||||
if *req.Sort > 0 {
|
||||
order = *req.Sort
|
||||
}
|
||||
|
||||
arg := &db.UpdateCategoryParams{
|
||||
ID: *req.ID,
|
||||
Name: pgtype.Text{
|
||||
String: req.Name,
|
||||
Valid: true,
|
||||
},
|
||||
Icon: pgtype.Text{
|
||||
String: req.Icon,
|
||||
Valid: len(req.Icon) > 0,
|
||||
},
|
||||
Description: pgtype.Text{
|
||||
String: req.Description,
|
||||
Valid: len(req.Description) > 0,
|
||||
},
|
||||
Letter: pgtype.Text{
|
||||
String: req.Letter,
|
||||
Valid: len(req.Letter) > 0,
|
||||
},
|
||||
ParentID: pgtype.Int4{
|
||||
Int32: *req.ParentID,
|
||||
Valid: true,
|
||||
},
|
||||
ParentPath: pgtype.Text{
|
||||
String: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||
Valid: true,
|
||||
},
|
||||
Sort: pgtype.Int4{
|
||||
Int32: order,
|
||||
Valid: true,
|
||||
},
|
||||
Status: pgtype.Int2{
|
||||
Int16: *req.Status,
|
||||
Valid: true,
|
||||
},
|
||||
}
|
||||
_, err := b.store.UpdateCategory(ctx, arg)
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *categoryBiz) Refresh(ctx context.Context) ([]*db.Category, error) {
|
||||
|
||||
@ -3,8 +3,8 @@ package common
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"management/internal/config"
|
||||
commonv1 "management/internal/erpserver/biz/v1/common"
|
||||
"management/internal/pkg/config"
|
||||
"management/internal/pkg/tpl"
|
||||
)
|
||||
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"management/internal/config"
|
||||
commonv1 "management/internal/erpserver/biz/v1/common"
|
||||
"management/internal/pkg/config"
|
||||
"management/internal/pkg/tpl"
|
||||
)
|
||||
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"management/internal/config"
|
||||
"management/internal/erpserver/biz"
|
||||
"management/internal/erpserver/handler/budget"
|
||||
"management/internal/erpserver/handler/common"
|
||||
@ -10,6 +9,7 @@ import (
|
||||
"management/internal/erpserver/handler/income"
|
||||
"management/internal/erpserver/handler/project"
|
||||
"management/internal/erpserver/handler/system"
|
||||
"management/internal/pkg/config"
|
||||
"management/internal/pkg/middleware"
|
||||
"management/internal/pkg/redis"
|
||||
"management/internal/pkg/session"
|
||||
|
||||
@ -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, "更新成功")
|
||||
}
|
||||
}
|
||||
|
||||
12
internal/erpserver/model/form/category.go
Normal file
12
internal/erpserver/model/form/category.go
Normal file
@ -0,0 +1,12 @@
|
||||
package form
|
||||
|
||||
type Category struct {
|
||||
ID *int32 `form:"id" binding:"required"`
|
||||
Name string `form:"name" binding:"required"`
|
||||
ParentID *int32 `form:"parent_id" binding:"required"`
|
||||
Icon string `form:"File"`
|
||||
Description string `form:"description"`
|
||||
Letter string `form:"letter"`
|
||||
Sort *int32 `form:"sort"`
|
||||
Status *int16 `form:"status" binding:"required"`
|
||||
}
|
||||
@ -4,7 +4,7 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"management/internal/config"
|
||||
"management/internal/pkg/config"
|
||||
|
||||
"github.com/natefinch/lumberjack"
|
||||
"github.com/rs/zerolog"
|
||||
|
||||
@ -44,7 +44,7 @@ func RandomString(n int) string {
|
||||
var sb strings.Builder
|
||||
k := len(alphabet)
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
for range n {
|
||||
c := alphabet[randv2.IntN(k)]
|
||||
sb.WriteByte(c)
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"management/internal/config"
|
||||
"management/internal/pkg/config"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
@ -4,7 +4,7 @@ import (
|
||||
"io/fs"
|
||||
"net"
|
||||
|
||||
"management/internal/config"
|
||||
"management/internal/pkg/config"
|
||||
|
||||
"github.com/hirochachacha/go-smb2"
|
||||
)
|
||||
|
||||
@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"management/internal/config"
|
||||
"management/internal/pkg/config"
|
||||
|
||||
"github.com/aead/chacha20poly1305"
|
||||
"github.com/o1egl/paseto"
|
||||
|
||||
BIN
management
BIN
management
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user