324 lines
6.8 KiB
Go
324 lines
6.8 KiB
Go
package category
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"management/internal/db/model/dto"
|
|
db "management/internal/db/sqlc"
|
|
"management/internal/global"
|
|
"management/internal/global/keys"
|
|
"management/internal/pkg/redis"
|
|
)
|
|
|
|
func ListCategoriesCondition(ctx context.Context, q dto.SearchDto) ([]*db.Category, int64, error) {
|
|
countArg := &db.CountCategoriesConditionParams{
|
|
IsStatus: q.SearchStatus != 9999,
|
|
Status: int16(q.SearchStatus),
|
|
IsParentID: q.SearchParentID != 0,
|
|
ParentID: int32(q.SearchParentID),
|
|
}
|
|
|
|
dataArg := &db.ListCategoriesConditionParams{
|
|
IsStatus: q.SearchStatus != 9999,
|
|
Status: int16(q.SearchStatus),
|
|
IsParentID: q.SearchParentID != 0,
|
|
ParentID: int32(q.SearchParentID),
|
|
Skip: (int32(q.Page) - 1) * int32(q.Rows),
|
|
Size: int32(q.Rows),
|
|
}
|
|
|
|
if len(q.SearchKey) > 0 {
|
|
switch strings.ToLower(q.SearchName) {
|
|
case "id":
|
|
id, err := strconv.Atoi(q.SearchKey)
|
|
if err == nil {
|
|
countArg.IsID = true
|
|
countArg.ID = int32(id)
|
|
|
|
dataArg.IsID = true
|
|
dataArg.ID = int32(id)
|
|
}
|
|
case "name":
|
|
countArg.Name = q.SearchKey
|
|
dataArg.Name = q.SearchKey
|
|
}
|
|
}
|
|
count, err := db.Engine.CountCategoriesCondition(ctx, countArg)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
categories, err := db.Engine.ListCategoriesCondition(ctx, dataArg)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return categories, count, nil
|
|
}
|
|
|
|
func DTreeCategory(ctx context.Context, id int32) ([]*dto.DTreeDto, error) {
|
|
all, err := db.Engine.AllCategories(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return toDtree(id, all), nil
|
|
}
|
|
|
|
func GetParentCategorySelectLetter(ctx context.Context, letter string) ([]*global.DataDict, error) {
|
|
all := AllCategories(ctx)
|
|
if len(all) == 0 {
|
|
return nil, errors.New("请刷新类别缓存")
|
|
}
|
|
|
|
var current *db.Category
|
|
for _, v := range all {
|
|
if v.Letter == letter {
|
|
current = v
|
|
break
|
|
}
|
|
}
|
|
if current == nil {
|
|
return nil, errors.New("未找到当前类别")
|
|
}
|
|
|
|
var res []*global.DataDict
|
|
res = append(res, &global.DataDict{
|
|
Name: "请选择",
|
|
Value: "0",
|
|
})
|
|
for _, v := range all {
|
|
if v.ParentID == current.ID {
|
|
item := global.DataDict{
|
|
Name: v.Name,
|
|
Value: strconv.Itoa(int(v.ID)),
|
|
}
|
|
res = append(res, &item)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func ListByParentID(ctx context.Context, parentID int32) ([]*db.Category, error) {
|
|
all := AllCategories(ctx)
|
|
if len(all) == 0 {
|
|
return nil, errors.New("请刷新类别缓存")
|
|
}
|
|
var res []*db.Category
|
|
for _, v := range all {
|
|
if v.ParentID == parentID {
|
|
res = append(res, v)
|
|
}
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func ListByLetter(ctx context.Context, letter string) ([]*db.Category, error) {
|
|
all := AllCategories(ctx)
|
|
if len(all) == 0 {
|
|
return nil, errors.New("请刷新类别缓存")
|
|
}
|
|
var current *db.Category
|
|
for _, v := range all {
|
|
if v.Letter == letter {
|
|
current = v
|
|
break
|
|
}
|
|
}
|
|
if current == nil {
|
|
return nil, errors.New("未找到当前类别")
|
|
}
|
|
|
|
var res []*db.Category
|
|
for _, v := range all {
|
|
if v.ParentID == current.ID {
|
|
res = append(res, v)
|
|
}
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func GetParentCategorySelect(ctx context.Context, id int32) ([]*global.DataDict, error) {
|
|
all := AllCategories(ctx)
|
|
if len(all) == 0 {
|
|
return nil, errors.New("请刷新类别缓存")
|
|
}
|
|
|
|
var res []*global.DataDict
|
|
res = append(res, &global.DataDict{
|
|
Name: "请选择",
|
|
Value: "0",
|
|
})
|
|
for _, v := range all {
|
|
if v.ParentID == id {
|
|
item := global.DataDict{
|
|
Name: v.Name,
|
|
Value: strconv.Itoa(int(v.ID)),
|
|
}
|
|
res = append(res, &item)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func toDtree(parentId int32, data []*db.Category) []*dto.DTreeDto {
|
|
var res []*dto.DTreeDto
|
|
for _, v := range data {
|
|
if v.ParentID == parentId {
|
|
item := dto.DTreeDto{}
|
|
item.ID = strconv.FormatInt(int64(v.ID), 10)
|
|
item.Title = v.Name
|
|
item.Last = !hasChildren(v.ID, data)
|
|
item.ParentId = strconv.FormatInt(int64(v.ParentID), 10)
|
|
item.Children = toDtree(v.ID, data)
|
|
res = append(res, &item)
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func hasChildren(parentId int32, data []*db.Category) bool {
|
|
if len(data) > 0 {
|
|
for _, v := range data {
|
|
if v.ParentID == parentId {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func RefreshCategory(ctx context.Context) error {
|
|
all, err := db.Engine.AllCategories(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
b, err := json.Marshal(all)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
redis.Del(ctx, keys.GetManageKey(ctx, keys.AllCategorySimple))
|
|
key := keys.GetManageKey(ctx, keys.AllCategories)
|
|
err = redis.Set(ctx, key, b, time.Hour*6)
|
|
return err
|
|
}
|
|
|
|
func AllCategories(ctx context.Context) []*db.Category {
|
|
var res []*db.Category
|
|
key := keys.GetManageKey(ctx, keys.AllCategories)
|
|
b, err := redis.GetBytes(ctx, key)
|
|
if err == nil {
|
|
if err := json.Unmarshal(b, &res); err == nil {
|
|
return res
|
|
}
|
|
}
|
|
|
|
res, err = db.Engine.AllCategories(ctx)
|
|
if err == nil {
|
|
if b, err = json.Marshal(res); err == nil {
|
|
redis.Set(ctx, key, b, time.Hour*6)
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
type CategorySet struct {
|
|
Title string `json:"title"`
|
|
Data []*CategoryItem `json:"data"`
|
|
}
|
|
|
|
type CategoryItem struct {
|
|
ID int32 `json:"id"`
|
|
Name string `json:"name"`
|
|
Icon string `json:"icon"`
|
|
Description string `json:"description"`
|
|
Url string `json:"url"`
|
|
}
|
|
|
|
func AllCategoriesItem(ctx context.Context) []*CategoryItem {
|
|
var res []*CategoryItem
|
|
key := keys.GetManageKey(ctx, keys.AllCategorySimple)
|
|
b, err := redis.GetBytes(ctx, key)
|
|
if err == nil {
|
|
if err = json.Unmarshal(b, &res); err == nil {
|
|
return res
|
|
}
|
|
}
|
|
all, err := db.Engine.AllCategories(ctx)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
for _, v := range all {
|
|
res = append(res, &CategoryItem{
|
|
ID: v.ID,
|
|
Name: v.Name,
|
|
})
|
|
}
|
|
b, err = json.Marshal(res)
|
|
if err == nil {
|
|
redis.Set(ctx, key, b, time.Hour*6)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func ListCategoriesByParentID(ctx context.Context, parentID int) ([]*CategorySet, error) {
|
|
var res []*CategorySet
|
|
key := keys.GetManageKey(ctx, keys.ListCategoriesByParentID, parentID)
|
|
b, err := redis.GetBytes(ctx, key)
|
|
if err == nil {
|
|
if err = json.Unmarshal(b, &res); err == nil {
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
all, err := db.Engine.ListCategoriesByPath(ctx, "%,"+strconv.Itoa(parentID)+",%")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
root := findCategoryItem(parentID, all)
|
|
for _, v := range root {
|
|
children := findCategoryItem(int(v.ID), all)
|
|
if len(children) > 0 {
|
|
res = append(res, &CategorySet{
|
|
Title: v.Name,
|
|
Data: children,
|
|
})
|
|
}
|
|
}
|
|
|
|
b, err = json.Marshal(res)
|
|
if err == nil {
|
|
redis.Set(ctx, key, b, time.Hour*6)
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func findCategoryItem(parentID int, all []*db.Category) []*CategoryItem {
|
|
var res []*CategoryItem
|
|
for _, v := range all {
|
|
if v.ParentID == int32(parentID) {
|
|
item := CategoryItem{}
|
|
item.ID = v.ID
|
|
item.Name = v.Name
|
|
item.Icon = v.Icon
|
|
item.Description = v.Description
|
|
res = append(res, &item)
|
|
}
|
|
}
|
|
return res
|
|
}
|