slice_tree.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package utils
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/frame/g"
  5. "github.com/gogf/gf/util/gconv"
  6. "strings"
  7. )
  8. //有层级关系的数组,父级-》子级 排序
  9. func ParentSonSort(list g.List, params ...interface{}) g.List {
  10. args := make([]interface{}, 8)
  11. for k, v := range params {
  12. if k == 8 {
  13. break
  14. }
  15. args[k] = v
  16. }
  17. var (
  18. pid int //父级id
  19. level int //层级数
  20. fieldName string //父级id键名
  21. id string //id键名
  22. levelName string //层级名称
  23. title string //标题名称
  24. breaks int //中断层级
  25. prefixStr string //字符串前缀
  26. )
  27. pid = gconv.Int(GetSliceByKey(args, 0, 0))
  28. level = gconv.Int(GetSliceByKey(args, 1, 0))
  29. fieldName = gconv.String(GetSliceByKey(args, 2, "pid"))
  30. id = gconv.String(GetSliceByKey(args, 3, "id"))
  31. levelName = gconv.String(GetSliceByKey(args, 4, "flg"))
  32. title = gconv.String(GetSliceByKey(args, 5, "title"))
  33. breaks = gconv.Int(GetSliceByKey(args, 6, -1))
  34. prefixStr = gconv.String(GetSliceByKey(args, 7, "  "))
  35. //定义一个新slice用于返回
  36. var returnSlice g.List
  37. for _, v := range list {
  38. if pid == gconv.Int(v[fieldName]) {
  39. v[levelName] = level
  40. levelClone := level
  41. titlePrefix := ""
  42. for {
  43. if levelClone < 0 {
  44. break
  45. }
  46. titlePrefix = strings.Repeat(prefixStr, 2)
  47. levelClone--
  48. }
  49. titlePrefix += "├"
  50. if level == 0 {
  51. v["title_prefix"] = ""
  52. } else {
  53. v["title_prefix"] = titlePrefix
  54. }
  55. v["title_show"] = fmt.Sprintf("%s%s", v["title_prefix"], v[title])
  56. returnSlice = append(returnSlice, v)
  57. if breaks != -1 && breaks == level {
  58. continue
  59. }
  60. args[0] = v[id]
  61. args[1] = level + 1
  62. newSlice2 := ParentSonSort(list, args...)
  63. if len(newSlice2) > 0 {
  64. returnSlice = append(returnSlice, newSlice2...)
  65. }
  66. }
  67. }
  68. return returnSlice
  69. }
  70. //有层级关系的数组 ,将子级压入到父级(树形结构)
  71. func PushSonToParent(list g.List, params ...interface{}) g.List {
  72. args := make([]interface{}, 6)
  73. for k, v := range params {
  74. if k == 6 {
  75. break
  76. }
  77. args[k] = v
  78. }
  79. var (
  80. pid int //父级id
  81. fieldName string //父级id键名
  82. id string //id键名
  83. key string //子级数组键名
  84. filter string //过滤键名
  85. filterVal interface{} //过滤的值
  86. )
  87. pid = gconv.Int(GetSliceByKey(args, 0, 0))
  88. fieldName = gconv.String(GetSliceByKey(args, 1, "pid"))
  89. id = gconv.String(GetSliceByKey(args, 2, "id"))
  90. key = gconv.String(GetSliceByKey(args, 3, "children"))
  91. filter = gconv.String(GetSliceByKey(args, 4, ""))
  92. filterVal = GetSliceByKey(args, 5, nil)
  93. var returnList g.List
  94. for _, v := range list {
  95. if gconv.Int(v[fieldName]) == pid {
  96. if filter != "" {
  97. if v[filter] == filterVal {
  98. args[0] = v[id]
  99. v[key] = PushSonToParent(list, args...)
  100. returnList = append(returnList, v)
  101. }
  102. } else {
  103. args[0] = v[id]
  104. v[key] = PushSonToParent(list, args...)
  105. returnList = append(returnList, v)
  106. }
  107. }
  108. }
  109. return returnList
  110. }
  111. //获取切片里的值 若为nil 可设置默认值val
  112. func GetSliceByKey(args []interface{}, key int, val interface{}) interface{} {
  113. var value interface{}
  114. if args[key] != nil {
  115. value = args[key]
  116. } else {
  117. value = val
  118. }
  119. return value
  120. }