industry_map.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package services
  2. import (
  3. "hongze/hongze_cygx/models"
  4. "math/rand"
  5. "reflect"
  6. "sort"
  7. "time"
  8. )
  9. func MakeTree(allNode []*models.CygxIndustryMapItems, node *models.CygxIndustryMapItems) {
  10. childs, _ := haveChild(allNode, node) //判断节点是否有子节点并返回
  11. if len(childs) > 0 {
  12. node.Children = append(node.Children, childs[0:]...) //添加子节点
  13. for _, v := range childs { //查询子节点的子节点,并添加到子节点
  14. _, has := haveChild(allNode, v)
  15. if has {
  16. MakeTree(allNode, v) //递归添加节点
  17. }
  18. }
  19. }
  20. }
  21. func haveChild(allNode []*models.CygxIndustryMapItems, node *models.CygxIndustryMapItems) (childs []*models.CygxIndustryMapItems, yes bool) {
  22. for _, v := range allNode {
  23. if v.ParentId == node.IndustryMapId {
  24. childs = append(childs, v)
  25. }
  26. }
  27. if len(childs) > 0 {
  28. yes = true
  29. }
  30. return
  31. }
  32. func GetIndustryMap() {
  33. list, err := models.GetCygxIndustryMapByParentId(0)
  34. if err != nil {
  35. return
  36. }
  37. rootNode := list[0]
  38. allNodes, err := models.GetCygxIndustryMapAll()
  39. if err != nil {
  40. return
  41. }
  42. MakeTree(allNodes, rootNode)
  43. }
  44. func GetIndustryTree() (rootNode *models.CygxIndustryMapItems, err error) {
  45. list, err := models.GetCygxIndustryMapByParentId(0)
  46. if err != nil {
  47. return
  48. }
  49. rootNode = list[0]
  50. allNodes, err := models.GetCygxIndustryMapAll()
  51. if err != nil {
  52. return
  53. }
  54. MakeTree(allNodes, rootNode)
  55. return
  56. }
  57. //获取对应的层级map
  58. func getMap(tree *models.CygxIndustryMapItems, item *models.CygxIndustryMapItems, hasIdMap map[int]string, otherChildMapSlice map[int][]*models.CygxIndustryMapItems) {
  59. //深度
  60. depth := 1
  61. //获取下级
  62. childTree := getChildTree(tree, item)
  63. depth = childTreeToSlice(childTree, hasIdMap, depth, otherChildMapSlice)
  64. //获取上级
  65. var tmpParentSlice []*models.CygxIndustryMapItems
  66. tmpParentSlice, _ = parentTreeToSlice(tree, item, 0, tmpParentSlice, hasIdMap)
  67. //移除第一个最上级的行业(根节点)
  68. tmpParentSlice = append(tmpParentSlice[:0], tmpParentSlice[1:len(tmpParentSlice)]...)
  69. //切片反转
  70. tmpParentSlice = reverse(tmpParentSlice)
  71. for _, v := range tmpParentSlice {
  72. depth++
  73. if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
  74. otherChildMapSlice[depth] = append(otherChildMapSlice[depth], v)
  75. }
  76. }
  77. //获取同级
  78. //fmt.Println("===============")
  79. depth = siblingTreeToSlice(tree, item, hasIdMap, depth, otherChildMapSlice)
  80. //上级的同级~
  81. /*for _,parentSlice := range tmpParentSlice{
  82. depth = siblingTreeToSlice(tree,parentSlice,hasIdMap,depth,otherChildMapSlice)
  83. }*/
  84. return
  85. }
  86. //获取行业图谱切片
  87. func GetIndustryMapNameSlice(industryName string) (nameSlice []string, err error) {
  88. nameSlice = append(nameSlice, industryName)
  89. tree, err := GetIndustryTree()
  90. if err != nil {
  91. return
  92. }
  93. //已经存在的行业id的map集合
  94. hasIdMap := make(map[int]string)
  95. itemList, err := models.GetFirstCygxIndustryListByName(industryName)
  96. if err != nil {
  97. return
  98. }
  99. //找不到对应的数据
  100. if len(itemList) <= 0 {
  101. return
  102. }
  103. industryMapList := make(map[int][]*models.CygxIndustryMapItems)
  104. for _, item := range itemList {
  105. industryMapList[item.Level] = append(industryMapList[item.Level], item)
  106. }
  107. //将查出来的根节点数据的key取出来,放在切片中,并对该切片做正序排列
  108. var sortIndustryList []int
  109. for k, _ := range industryMapList {
  110. sortIndustryList = append(sortIndustryList, k)
  111. }
  112. sort.Ints(sortIndustryList)
  113. //最底层节点的数据集合
  114. list := industryMapList[sortIndustryList[0]]
  115. otherChildMapSlice := map[int][]*models.CygxIndustryMapItems{}
  116. for _, item := range list {
  117. hasIdMap[item.IndustryMapId] = ""
  118. //将自己的节点给加进去
  119. otherChildMapSlice[0] = append(otherChildMapSlice[0], item)
  120. getMap(tree, item, hasIdMap, otherChildMapSlice)
  121. }
  122. var tmpSlice []*models.CygxIndustryMapItems
  123. //将其他规律数据的key取出来,放在切片中,并对该切片做正序排列
  124. var sortList []int
  125. for k, _ := range otherChildMapSlice {
  126. sortList = append(sortList, k)
  127. }
  128. sort.Ints(sortList)
  129. //遍历该切片,根据下标key获取对应的数据,并插入到主数据中
  130. for _, v := range sortList {
  131. tmpChildSlice := otherChildMapSlice[v]
  132. randSlice(tmpChildSlice)
  133. tmpSlice = append(tmpSlice, tmpChildSlice...)
  134. //fmt.Println(k,"=====")
  135. //for _,tmpV := range otherChildMapSlice[v]{
  136. // fmt.Println(tmpV.IndustryMapName)
  137. //}
  138. }
  139. //名字切片
  140. for _, v := range tmpSlice {
  141. //fmt.Println("k===",k,"=======v=======",v)
  142. nameSlice = append(nameSlice, v.IndustryMapName)
  143. }
  144. //fmt.Println(nameSlice)
  145. //fmt.Println(strings.Join(nameSlice,","))
  146. //utils.FileLog.Info("allNodes:%s",strings.Join(nameSlice,","))
  147. return
  148. }
  149. //切片反转
  150. func reverse(s []*models.CygxIndustryMapItems) []*models.CygxIndustryMapItems {
  151. for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
  152. s[i], s[j] = s[j], s[i]
  153. }
  154. return s
  155. }
  156. //获取当前节点的树
  157. func getChildTree(rootNode *models.CygxIndustryMapItems, nowNode *models.CygxIndustryMapItems) (returnNode *models.CygxIndustryMapItems) {
  158. if rootNode.IndustryMapId == nowNode.IndustryMapId {
  159. returnNode = rootNode
  160. return
  161. }
  162. if rootNode.Children != nil {
  163. for _, v := range rootNode.Children {
  164. if v.IndustryMapId == nowNode.IndustryMapId {
  165. returnNode = v
  166. } else {
  167. returnNode = getChildTree(v, nowNode)
  168. }
  169. if returnNode != nil {
  170. return
  171. }
  172. }
  173. }
  174. return
  175. }
  176. //获取子树V2,与上一个子树规律不一样,因为上级
  177. func childTreeToSlice(rootNode *models.CygxIndustryMapItems, hasIdMap map[int]string, depth int, otherChildMapSlice map[int][]*models.CygxIndustryMapItems) (maxDepth int) {
  178. if rootNode.Children != nil {
  179. depth++
  180. maxDepth = depth
  181. for _, v := range rootNode.Children {
  182. //判断是否已经录入,如果已经录入,那么不处理
  183. if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
  184. otherChildMapSlice[depth] = append(otherChildMapSlice[depth], v)
  185. }
  186. }
  187. for _, v := range rootNode.Children {
  188. //判断是否已经录入,如果已经录入,那么不处理
  189. if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
  190. hasIdMap[v.IndustryMapId] = ""
  191. returnDepth := childTreeToSlice(v, hasIdMap, depth, otherChildMapSlice)
  192. if returnDepth > maxDepth {
  193. maxDepth = returnDepth
  194. }
  195. }
  196. }
  197. }
  198. return maxDepth
  199. }
  200. //获取兄弟级树
  201. func siblingTreeToSlice(rootNode *models.CygxIndustryMapItems, nowNode *models.CygxIndustryMapItems, hasIdMap map[int]string, depth int, otherChildMapSlice map[int][]*models.CygxIndustryMapItems) (maxDepth int) {
  202. if rootNode.Children != nil {
  203. depth++
  204. maxDepth = depth
  205. for _, v := range rootNode.Children {
  206. //如果父级id一致的情况下,那么代表这是兄弟节点或者是自己了
  207. if v.ParentId == nowNode.ParentId {
  208. //判断是否已经录入,如果已经录入,那么不处理
  209. if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
  210. otherChildMapSlice[depth] = append(otherChildMapSlice[depth], v)
  211. returnDepth := childTreeToSlice(v, hasIdMap, depth, otherChildMapSlice)
  212. if returnDepth > maxDepth {
  213. maxDepth = returnDepth
  214. }
  215. }
  216. } else {
  217. returnDepth := siblingTreeToSlice(v, nowNode, hasIdMap, depth, otherChildMapSlice)
  218. if returnDepth > maxDepth {
  219. maxDepth = returnDepth
  220. }
  221. }
  222. }
  223. for _, v := range rootNode.Children {
  224. if v.ParentId == nowNode.ParentId {
  225. if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
  226. hasIdMap[v.IndustryMapId] = ""
  227. }
  228. }
  229. }
  230. }
  231. return
  232. }
  233. //获取上级树
  234. func parentTreeToSlice(rootNode *models.CygxIndustryMapItems, nowNode *models.CygxIndustryMapItems, depth int, tmpReturnSlice []*models.CygxIndustryMapItems, hasIdMap map[int]string) (returnSlice []*models.CygxIndustryMapItems, ok bool) {
  235. if depth != 0 {
  236. returnSlice = tmpReturnSlice
  237. }
  238. depth++
  239. returnSlice = append(returnSlice, rootNode)
  240. //if _,ok := hasIdMap[rootNode.IndustryMapId];ok==false{
  241. // hasIdMap[rootNode.IndustryMapId] = ""
  242. //}
  243. if rootNode.Children != nil {
  244. for _, v := range rootNode.Children {
  245. if v.ParentId == nowNode.ParentId {
  246. ok = true
  247. return
  248. }
  249. returnSlice, ok = parentTreeToSlice(v, nowNode, depth, returnSlice, hasIdMap)
  250. //如果返回的匹配完成,那么不进入下一次循环,直接返回
  251. if ok {
  252. return
  253. }
  254. //一次循环结束后,如果没有匹配上,那么移除临时切片中的数据
  255. returnSlice = append(returnSlice[:0], returnSlice[0:depth]...)
  256. }
  257. }
  258. return
  259. }
  260. //切片乱序
  261. func randSlice(slice []*models.CygxIndustryMapItems) {
  262. rv := reflect.ValueOf(slice)
  263. if rv.Type().Kind() != reflect.Slice {
  264. return
  265. }
  266. length := rv.Len()
  267. if length < 2 {
  268. return
  269. }
  270. swap := reflect.Swapper(slice)
  271. rand.Seed(time.Now().Unix())
  272. for i := length - 1; i >= 0; i-- {
  273. j := rand.Intn(length)
  274. swap(i, j)
  275. }
  276. return
  277. }
  278. //region数据修复
  279. func FixData() {
  280. tree, err := GetIndustryTree()
  281. if err != nil {
  282. }
  283. models.FixLevelData(tree.IndustryMapId, 1)
  284. fixData(tree, 1)
  285. return
  286. }
  287. func fixData(item *models.CygxIndustryMapItems, depth int) {
  288. depth++
  289. if item.Children == nil {
  290. return
  291. }
  292. for _, v := range item.Children {
  293. models.FixLevelData(v.IndustryMapId, depth)
  294. fixData(v, depth)
  295. }
  296. }
  297. //endregion