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

@@ -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) {