175 lines
4.4 KiB
Go
175 lines
4.4 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strconv"
|
|
"time"
|
|
|
|
"management/internal/db/model/dto"
|
|
db "management/internal/db/sqlc"
|
|
"management/internal/erpserver/model/view"
|
|
"management/internal/global/keys"
|
|
"management/internal/pkg/redis"
|
|
)
|
|
|
|
type CategoryBiz interface {
|
|
Create(ctx context.Context, arg *db.CreateCategoryParams) (*db.Category, error)
|
|
Update(ctx context.Context, arg *db.UpdateCategoryParams) (*db.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)
|
|
Refresh(ctx context.Context) ([]*db.Category, error)
|
|
RebuildParentPath(ctx context.Context) error
|
|
|
|
Tree(ctx context.Context, id int32) ([]*view.LayuiTree, error)
|
|
XmSelect(ctx context.Context, id int32) ([]*view.XmSelectTree, error)
|
|
}
|
|
|
|
type categoryBiz struct {
|
|
store db.Store
|
|
redis redis.IRedis
|
|
}
|
|
|
|
var _ CategoryBiz = (*categoryBiz)(nil)
|
|
|
|
func NewCategory(store db.Store, redis redis.IRedis) *categoryBiz {
|
|
return &categoryBiz{
|
|
store: store,
|
|
redis: redis,
|
|
}
|
|
}
|
|
|
|
func (b *categoryBiz) All(ctx context.Context) ([]*db.Category, error) {
|
|
key := keys.GetManageKey(ctx, keys.AllCategories)
|
|
bs, err := redis.GetBytes(ctx, key)
|
|
if err == nil {
|
|
var res []*db.Category
|
|
if err := json.Unmarshal(bs, &res); err == nil {
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
return b.Refresh(ctx)
|
|
}
|
|
|
|
func (b *categoryBiz) List(ctx context.Context, q dto.SearchDto) ([]*db.Category, int64, error) {
|
|
countArg := &db.CountCategoriesConditionParams{
|
|
IsStatus: q.SearchStatus != 9999,
|
|
Status: int16(q.SearchStatus),
|
|
IsID: q.SearchID != 0,
|
|
ID: int32(q.SearchID),
|
|
IsParentID: q.SearchParentID != 0,
|
|
ParentID: int32(q.SearchParentID),
|
|
Name: q.SearchName,
|
|
}
|
|
|
|
dataArg := &db.ListCategoriesConditionParams{
|
|
IsStatus: q.SearchStatus != 9999,
|
|
Status: int16(q.SearchStatus),
|
|
IsID: q.SearchID != 0,
|
|
ID: int32(q.SearchID),
|
|
IsParentID: q.SearchParentID != 0,
|
|
ParentID: int32(q.SearchParentID),
|
|
Name: q.SearchName,
|
|
Skip: (int32(q.Page) - 1) * int32(q.Rows),
|
|
Size: int32(q.Rows),
|
|
}
|
|
count, err := b.store.CountCategoriesCondition(ctx, countArg)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
departs, err := b.store.ListCategoriesCondition(ctx, dataArg)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return departs, count, nil
|
|
}
|
|
|
|
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) Update(ctx context.Context, arg *db.UpdateCategoryParams) (*db.Category, error) {
|
|
return b.store.UpdateCategory(ctx, arg)
|
|
}
|
|
|
|
func (b *categoryBiz) Refresh(ctx context.Context) ([]*db.Category, error) {
|
|
all, err := b.store.AllCategories(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
bs, err := json.Marshal(all)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
redis.Del(ctx, keys.GetManageKey(ctx, keys.AllCategorySimple))
|
|
key := keys.GetManageKey(ctx, keys.AllCategories)
|
|
err = redis.Set(ctx, key, bs, time.Hour*6)
|
|
return all, err
|
|
}
|
|
|
|
func (b *categoryBiz) RebuildParentPath(ctx context.Context) error {
|
|
return b.store.CategoryRebuildPath(ctx)
|
|
}
|
|
|
|
func (b *categoryBiz) Tree(ctx context.Context, id int32) ([]*view.LayuiTree, error) {
|
|
all, err := b.All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return b.toTree(id, all), nil
|
|
}
|
|
|
|
func (b *categoryBiz) XmSelect(ctx context.Context, id int32) ([]*view.XmSelectTree, error) {
|
|
all, err := b.All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return b.toXmSelectTree(id, all), nil
|
|
}
|
|
|
|
func (b *categoryBiz) toTree(parentId int32, data []*db.Category) []*view.LayuiTree {
|
|
var res []*view.LayuiTree
|
|
for _, v := range data {
|
|
if v.ParentID == parentId {
|
|
item := view.LayuiTree{}
|
|
item.ID = strconv.FormatInt(int64(v.ID), 10)
|
|
item.Title = v.Name
|
|
item.Children = b.toTree(v.ID, data)
|
|
if v.ParentID == 0 {
|
|
item.Spread = true
|
|
}
|
|
res = append(res, &item)
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func (b *categoryBiz) toXmSelectTree(parentId int32, data []*db.Category) []*view.XmSelectTree {
|
|
var res []*view.XmSelectTree
|
|
for _, v := range data {
|
|
if v.ParentID == parentId {
|
|
item := view.XmSelectTree{
|
|
Name: v.Name,
|
|
Value: strconv.FormatInt(int64(v.ID), 10),
|
|
Children: b.toXmSelectTree(v.ID, data),
|
|
}
|
|
res = append(res, &item)
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|