341 lines
		
	
	
		
			7.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			341 lines
		
	
	
		
			7.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package category
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"encoding/json"
 | |
| 	"errors"
 | |
| 	"strconv"
 | |
| 	"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),
 | |
| 		IsID:       q.SearchID != 0,
 | |
| 		ID:         int32(q.SearchID),
 | |
| 		IsParentID: q.SearchParentID != 0 && q.SearchParentID != 1,
 | |
| 		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 && q.SearchParentID != 1,
 | |
| 		ParentID:   int32(q.SearchParentID),
 | |
| 		Name:       q.SearchName,
 | |
| 		Skip:       (int32(q.Page) - 1) * int32(q.Rows),
 | |
| 		Size:       int32(q.Rows),
 | |
| 	}
 | |
| 
 | |
| 	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 XmSelectCategory(ctx context.Context, id int32) ([]*dto.XmSelectTreeDto, error) {
 | |
| 	all, err := db.Engine.AllCategories(ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return toXmSelectTree(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)
 | |
| 			if v.ParentID == 0 {
 | |
| 				item.Spread = true
 | |
| 			}
 | |
| 			res = append(res, &item)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return res
 | |
| }
 | |
| 
 | |
| func toXmSelectTree(parentId int32, data []*db.Category) []*dto.XmSelectTreeDto {
 | |
| 	var res []*dto.XmSelectTreeDto
 | |
| 	for _, v := range data {
 | |
| 		if v.ParentID == parentId {
 | |
| 			item := dto.XmSelectTreeDto{
 | |
| 				Name:     v.Name,
 | |
| 				Value:    strconv.FormatInt(int64(v.ID), 10),
 | |
| 				Children: toXmSelectTree(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
 | |
| }
 |