|
- package dictionary
-
- import (
- "context"
- "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
- "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
- "sort"
-
- "github.com/zeromicro/go-zero/core/logx"
- )
-
- type ListDictItemLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
-
- func NewListDictItemLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListDictItemLogic {
- return &ListDictItemLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx,
- }
- }
-
- func (l *ListDictItemLogic) ListDictItem(req *types.DictItemReq) (resp *types.PageResult, err error) {
- resp = &types.PageResult{}
- var dictList []types.DictItemInfo
- db := l.svcCtx.DbEngin.Model(&types.DictItemInfo{}).Table("t_dict_item")
-
- if req.ItemText != "" {
- db = db.Where("item_text LIKE ?", "%"+req.ItemText+"%")
- }
- if req.ItemValue != "" {
- db = db.Where("item_value LIKE ?", "%"+req.ItemValue+"%")
- }
- if req.Type != "" {
- db = db.Where("type = ?", req.Type)
- }
- if req.ParentId != "" {
- db = db.Where("parent_id = ?", req.ParentId)
- }
- if req.Status != "" {
- db = db.Where("status = ?", req.Status)
- }
- if req.DictId != "" {
- db = db.Where("dict_id = ?", req.DictId)
- }
- err = db.Order("sort_order").Find(&dictList).Error
-
- // 找出第一级字典项,(父字典项id为0)
- dictItemFormat := make([]types.DictItemInfo, 0)
- for _, item := range dictList {
- if item.ParentId == "0" {
- dictItemFormat = append(dictItemFormat, item)
- }
- }
- // 排序
- sort.Slice(dictItemFormat, func(i, j int) bool {
- return dictItemFormat[i].SortOrder < dictItemFormat[j].SortOrder
- })
-
- // 递归找出一级路由下面的子路由
- getTreeMap(dictItemFormat, dictList)
-
- resp.List = dictItemFormat
-
- return resp, nil
- }
-
- func getTreeMap(dictItemFormat []types.DictItemInfo, dictItems []types.DictItemInfo) {
- for index, itemF := range dictItemFormat {
- for _, dictItem := range dictItems {
- if itemF.Id == dictItem.ParentId {
- // itemF 只是个复制值
- //itemF.Children = append(itemF.Children, dictItem)
- dictItemFormat[index].Children = append(dictItemFormat[index].Children, dictItem)
- }
- }
- if len(dictItemFormat[index].Children) > 0 {
- // 排序
- sort.Slice(dictItemFormat[index].Children, func(i, j int) bool {
- return dictItemFormat[index].Children[i].SortOrder < dictItemFormat[index].Children[j].SortOrder
- })
- getTreeMap(dictItemFormat[index].Children, dictItems)
- }
- }
- }
|