edb_collect.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package data
  2. import (
  3. "eta_gn/eta_api/models/data_manage"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. )
  9. // GetEdbCollectClassifyTreeRecursive 递归获取分类树形结构
  10. func GetEdbCollectClassifyTreeRecursive(list []*data_manage.EdbCollectClassifyItem, parentId int) []*data_manage.EdbCollectClassifyItem {
  11. res := make([]*data_manage.EdbCollectClassifyItem, 0)
  12. for _, v := range list {
  13. if v.ParentId == parentId {
  14. t := GetEdbCollectClassifyTreeRecursive(list, v.ClassifyId)
  15. if len(t) > 0 {
  16. v.Children = t
  17. }
  18. res = append(res, v)
  19. }
  20. }
  21. return res
  22. }
  23. // GetEdbCollectClassifyChildIds 获取分类的子分类IDs
  24. func GetEdbCollectClassifyChildIds(classifyId, adminId int, selfInclude bool) (classifyIds []int, err error) {
  25. // DM就不用FIND_IN_SET去查了, 从分类的LevelPath中遍历出来
  26. exists := make(map[int]bool)
  27. if selfInclude {
  28. exists[classifyId] = true
  29. classifyIds = append(classifyIds, classifyId)
  30. }
  31. classifyOb := new(data_manage.EdbCollectClassify)
  32. cond := fmt.Sprintf(` AND %s = ?`, classifyOb.Cols().SysUserId)
  33. pars := make([]interface{}, 0)
  34. pars = append(pars, adminId)
  35. list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf(`%s ASC`, classifyOb.Cols().Sort))
  36. if e != nil {
  37. err = fmt.Errorf("获取用户收藏分类失败, %v", e)
  38. return
  39. }
  40. strClassifyId := strconv.Itoa(classifyId)
  41. for _, v := range list {
  42. if v.LevelPath == "" {
  43. continue
  44. }
  45. if exists[v.EdbCollectClassifyId] {
  46. continue
  47. }
  48. pathArr := strings.Split(v.LevelPath, ",")
  49. if len(pathArr) > 0 && utils.InArrayByStr(pathArr, strClassifyId) {
  50. classifyIds = append(classifyIds, v.EdbCollectClassifyId)
  51. exists[v.EdbCollectClassifyId] = true
  52. }
  53. }
  54. return
  55. }