industry_map.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. package services
  2. import (
  3. "fmt"
  4. "hongze/hongze_cygx/models"
  5. "math/rand"
  6. "reflect"
  7. "sort"
  8. "time"
  9. )
  10. func MakeTree(allNode []*models.CygxIndustryMapItems, node *models.CygxIndustryMapItems) {
  11. childs, _ := haveChild(allNode, node) //判断节点是否有子节点并返回
  12. if len(childs) > 0 {
  13. node.Children = append(node.Children, childs[0:]...) //添加子节点
  14. for _, v := range childs { //查询子节点的子节点,并添加到子节点
  15. _, has := haveChild(allNode, v)
  16. if has {
  17. MakeTree(allNode, v) //递归添加节点
  18. }
  19. }
  20. }
  21. }
  22. func haveChild(allNode []*models.CygxIndustryMapItems, node *models.CygxIndustryMapItems) (childs []*models.CygxIndustryMapItems, yes bool) {
  23. for _, v := range allNode {
  24. if v.ParentId == node.IndustryMapId {
  25. childs = append(childs, v)
  26. }
  27. }
  28. if len(childs) > 0 {
  29. yes = true
  30. }
  31. return
  32. }
  33. func GetIndustryMap() {
  34. list, err := models.GetCygxIndustryMapByParentId(0)
  35. if err != nil {
  36. return
  37. }
  38. rootNode := list[0]
  39. allNodes, err := models.GetCygxIndustryMapAll()
  40. if err != nil {
  41. return
  42. }
  43. MakeTree(allNodes, rootNode)
  44. }
  45. func GetIndustryTree()(rootNode *models.CygxIndustryMapItems, err error) {
  46. //fmt.Println("start")
  47. list, err := models.GetCygxIndustryMapByParentId(0)
  48. if err != nil {
  49. return
  50. }
  51. rootNode = list[0]
  52. allNodes, err := models.GetCygxIndustryMapAll()
  53. if err != nil {
  54. return
  55. }
  56. MakeTree(allNodes, rootNode)
  57. //resultJson,err:=json.Marshal(rootNode)
  58. //fmt.Println("Err:",err)
  59. //fmt.Println("allNodes:",string(resultJson))
  60. //utils.FileLog.Info("allNodes:%s",string(resultJson))
  61. //fmt.Println("end")
  62. return
  63. }
  64. //获取对应的层级map
  65. func getMap(tree *models.CygxIndustryMapItems, item *models.CygxIndustryMapItems, hasIdMap map[int]string, otherChildMapSlice map[int][]*models.CygxIndustryMapItems) {
  66. //深度
  67. depth := 1
  68. //获取下级
  69. childTree := getChildTree(tree, item)
  70. depth = childTreeToSlice(childTree, hasIdMap, depth, otherChildMapSlice)
  71. //获取上级
  72. var tmpParentSlice []*models.CygxIndustryMapItems
  73. tmpParentSlice, _ = parentTreeToSlice(tree, item, 0, tmpParentSlice, hasIdMap)
  74. //移除第一个最上级的行业(根节点)
  75. tmpParentSlice = append(tmpParentSlice[:0], tmpParentSlice[1:len(tmpParentSlice)]...)
  76. //切片反转
  77. tmpParentSlice = reverse(tmpParentSlice)
  78. for _, v := range tmpParentSlice {
  79. depth++
  80. if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
  81. otherChildMapSlice[depth] = append(otherChildMapSlice[depth], v)
  82. }
  83. }
  84. //获取同级
  85. //fmt.Println("===============")
  86. depth = siblingTreeToSlice(tree, item, hasIdMap, depth, otherChildMapSlice)
  87. //上级的同级~
  88. /*for _,parentSlice := range tmpParentSlice{
  89. depth = siblingTreeToSlice(tree,parentSlice,hasIdMap,depth,otherChildMapSlice)
  90. }*/
  91. return
  92. }
  93. //获取行业图谱切片
  94. func GetIndustryMapNameSlice(industryName string) (nameSlice []string, err error) {
  95. nameSlice = append(nameSlice, industryName)
  96. tree, err := GetIndustryTree()
  97. if err != nil {
  98. return
  99. }
  100. //已经存在的行业id的map集合
  101. hasIdMap := make(map[int]string)
  102. itemList, err := models.GetFirstCygxIndustryListByName(industryName)
  103. if err != nil {
  104. return
  105. }
  106. //找不到对应的数据
  107. if len(itemList) <= 0 {
  108. return
  109. }
  110. industryMapList := make(map[int][]*models.CygxIndustryMapItems)
  111. for _, item := range itemList {
  112. industryMapList[item.Level] = append(industryMapList[item.Level], item)
  113. }
  114. //将查出来的根节点数据的key取出来,放在切片中,并对该切片做正序排列
  115. var sortIndustryList []int
  116. for k, _ := range industryMapList {
  117. sortIndustryList = append(sortIndustryList, k)
  118. }
  119. sort.Ints(sortIndustryList)
  120. //最底层节点的数据集合
  121. list := industryMapList[sortIndustryList[0]]
  122. otherChildMapSlice := map[int][]*models.CygxIndustryMapItems{}
  123. for _, item := range list {
  124. hasIdMap[item.IndustryMapId] = ""
  125. //将自己的节点给加进去
  126. otherChildMapSlice[0] = append(otherChildMapSlice[0], item)
  127. getMap(tree, item, hasIdMap, otherChildMapSlice)
  128. }
  129. var tmpSlice []*models.CygxIndustryMapItems
  130. //将其他规律数据的key取出来,放在切片中,并对该切片做正序排列
  131. var sortList []int
  132. for k, _ := range otherChildMapSlice {
  133. sortList = append(sortList, k)
  134. }
  135. sort.Ints(sortList)
  136. //遍历该切片,根据下标key获取对应的数据,并插入到主数据中
  137. for _, v := range sortList {
  138. tmpChildSlice := otherChildMapSlice[v]
  139. randSlice(tmpChildSlice)
  140. tmpSlice = append(tmpSlice, tmpChildSlice...)
  141. //fmt.Println(k,"=====")
  142. //for _,tmpV := range otherChildMapSlice[v]{
  143. // fmt.Println(tmpV.IndustryMapName)
  144. //}
  145. }
  146. //名字切片
  147. for _, v := range tmpSlice {
  148. //fmt.Println("k===",k,"=======v=======",v)
  149. nameSlice = append(nameSlice, v.IndustryMapName)
  150. }
  151. //fmt.Println(nameSlice)
  152. //fmt.Println(strings.Join(nameSlice,","))
  153. //utils.FileLog.Info("allNodes:%s",strings.Join(nameSlice,","))
  154. return
  155. }
  156. //切片反转
  157. func reverse(s []*models.CygxIndustryMapItems) []*models.CygxIndustryMapItems {
  158. for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
  159. s[i], s[j] = s[j], s[i]
  160. }
  161. return s
  162. }
  163. //获取当前节点的树
  164. func getChildTree(rootNode *models.CygxIndustryMapItems, nowNode *models.CygxIndustryMapItems) (returnNode *models.CygxIndustryMapItems) {
  165. if rootNode.IndustryMapId == nowNode.IndustryMapId {
  166. returnNode = rootNode
  167. return
  168. }
  169. if rootNode.Children != nil {
  170. for _, v := range rootNode.Children {
  171. if v.IndustryMapId == nowNode.IndustryMapId {
  172. returnNode = v
  173. } else {
  174. returnNode = getChildTree(v, nowNode)
  175. }
  176. if returnNode != nil {
  177. return
  178. }
  179. }
  180. }
  181. return
  182. }
  183. //获取子树V2,与上一个子树规律不一样,因为上级
  184. func childTreeToSlice(rootNode *models.CygxIndustryMapItems, hasIdMap map[int]string, depth int, otherChildMapSlice map[int][]*models.CygxIndustryMapItems) (maxDepth int) {
  185. if rootNode.Children != nil {
  186. depth++
  187. maxDepth = depth
  188. for _, v := range rootNode.Children {
  189. //判断是否已经录入,如果已经录入,那么不处理
  190. if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
  191. otherChildMapSlice[depth] = append(otherChildMapSlice[depth], v)
  192. }
  193. }
  194. for _, v := range rootNode.Children {
  195. //判断是否已经录入,如果已经录入,那么不处理
  196. if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
  197. hasIdMap[v.IndustryMapId] = ""
  198. returnDepth := childTreeToSlice(v, hasIdMap, depth, otherChildMapSlice)
  199. if returnDepth > maxDepth {
  200. maxDepth = returnDepth
  201. }
  202. }
  203. }
  204. }
  205. return maxDepth
  206. }
  207. //获取兄弟级树
  208. func siblingTreeToSlice(rootNode *models.CygxIndustryMapItems, nowNode *models.CygxIndustryMapItems, hasIdMap map[int]string, depth int, otherChildMapSlice map[int][]*models.CygxIndustryMapItems) (maxDepth int) {
  209. if rootNode.Children != nil {
  210. depth++
  211. maxDepth = depth
  212. for _, v := range rootNode.Children {
  213. //如果父级id一致的情况下,那么代表这是兄弟节点或者是自己了
  214. if v.ParentId == nowNode.ParentId {
  215. //判断是否已经录入,如果已经录入,那么不处理
  216. if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
  217. otherChildMapSlice[depth] = append(otherChildMapSlice[depth], v)
  218. returnDepth := childTreeToSlice(v, hasIdMap, depth, otherChildMapSlice)
  219. if returnDepth > maxDepth {
  220. maxDepth = returnDepth
  221. }
  222. }
  223. } else {
  224. returnDepth := siblingTreeToSlice(v, nowNode, hasIdMap, depth, otherChildMapSlice)
  225. if returnDepth > maxDepth {
  226. maxDepth = returnDepth
  227. }
  228. }
  229. }
  230. for _, v := range rootNode.Children {
  231. if v.ParentId == nowNode.ParentId {
  232. if _, ok := hasIdMap[v.IndustryMapId]; ok == false {
  233. hasIdMap[v.IndustryMapId] = ""
  234. }
  235. }
  236. }
  237. }
  238. return
  239. }
  240. //获取上级树
  241. func parentTreeToSlice(rootNode *models.CygxIndustryMapItems, nowNode *models.CygxIndustryMapItems, depth int, tmpReturnSlice []*models.CygxIndustryMapItems, hasIdMap map[int]string) (returnSlice []*models.CygxIndustryMapItems, ok bool) {
  242. if depth != 0 {
  243. returnSlice = tmpReturnSlice
  244. }
  245. depth++
  246. returnSlice = append(returnSlice, rootNode)
  247. //if _,ok := hasIdMap[rootNode.IndustryMapId];ok==false{
  248. // hasIdMap[rootNode.IndustryMapId] = ""
  249. //}
  250. if rootNode.Children != nil {
  251. for _, v := range rootNode.Children {
  252. if v.ParentId == nowNode.ParentId {
  253. ok = true
  254. return
  255. }
  256. returnSlice, ok = parentTreeToSlice(v, nowNode, depth, returnSlice, hasIdMap)
  257. //如果返回的匹配完成,那么不进入下一次循环,直接返回
  258. if ok {
  259. return
  260. }
  261. //一次循环结束后,如果没有匹配上,那么移除临时切片中的数据
  262. returnSlice = append(returnSlice[:0], returnSlice[0:depth]...)
  263. }
  264. }
  265. return
  266. }
  267. //切片乱序
  268. func randSlice(slice []*models.CygxIndustryMapItems) {
  269. rv := reflect.ValueOf(slice)
  270. if rv.Type().Kind() != reflect.Slice {
  271. return
  272. }
  273. length := rv.Len()
  274. if length < 2 {
  275. return
  276. }
  277. swap := reflect.Swapper(slice)
  278. rand.Seed(time.Now().Unix())
  279. for i := length - 1; i >= 0; i-- {
  280. j := rand.Intn(length)
  281. swap(i, j)
  282. }
  283. return
  284. }
  285. //获取行业图谱切片(v2版本,调整时间:2021-03-19 18:02:07)
  286. func GetIndustryMapNameSliceV2(industryName string)(nameSlice []string,err error){
  287. tree,err := GetIndustryTree()
  288. if err != nil{
  289. fmt.Println("获取树失败")
  290. return
  291. }
  292. //fmt.Println(tree)
  293. //已经存在的行业id的map集合
  294. hasIdMap := make(map[int]string)
  295. itemList,err := models.GetFirstCygxIndustryListByName(industryName)
  296. if err != nil{
  297. fmt.Println("获取数据失败,",err)
  298. return
  299. }
  300. //找不到对应的数据
  301. if len(itemList) <= 0{
  302. return
  303. }
  304. industryMapList := make(map[int][]*models.CygxIndustryMapItems)
  305. //TODO 这里好像有问题,如果传入行业的话,上面再没有是否只是找到第一个节点判断,那么就会异常抛出
  306. for _,item := range itemList{
  307. industryMapList[item.Level] = append(industryMapList[item.Level],item)
  308. }
  309. //将查出来的根节点数据的key取出来,放在切片中,并对该切片做正序排列
  310. var sortIndustryList []int
  311. for k, _ := range industryMapList {
  312. sortIndustryList = append(sortIndustryList, k)
  313. }
  314. sort.Ints(sortIndustryList)
  315. //最底层节点的数据集合
  316. list := industryMapList[sortIndustryList[0]]
  317. //如果该数据正好是第一层节点数据,那么需要额外判断下
  318. if list[0].ParentId <= 2 {
  319. //如果存在第二级,那么就使用该层级
  320. if len(sortIndustryList) > 1{
  321. list = industryMapList[sortIndustryList[1]]
  322. }
  323. }
  324. //fmt.Println(list)
  325. //return
  326. otherChildMapSlice := map[int][]*models.CygxIndustryMapItems{}
  327. //多个节点时,额外处理
  328. if len(list) > 1{
  329. for _,item := range list{
  330. hasIdMap[item.IndustryMapId]=""
  331. //将自己的节点给加进去
  332. otherChildMapSlice[0] = append(otherChildMapSlice[0],item)
  333. //获取上级
  334. var tmpParentSlice []*models.CygxIndustryMapItems
  335. tmpParentSlice,_ = parentTreeToSlice(tree,item,0,tmpParentSlice,hasIdMap)
  336. //父节点
  337. parentItem := tmpParentSlice[len(tmpParentSlice)-1]
  338. if _,ok := hasIdMap[parentItem.IndustryMapId];ok==false{
  339. hasIdMap[parentItem.IndustryMapId] = ""
  340. otherChildMapSlice[1] = append(otherChildMapSlice[1],parentItem)
  341. }
  342. }
  343. }else{
  344. //匹配到单个节点
  345. item := list[0]
  346. hasIdMap[item.IndustryMapId]=""
  347. //将自己的节点给加进去
  348. otherChildMapSlice[0] = append(otherChildMapSlice[0],item)
  349. childTree := getChildTree(tree,item)
  350. //如果是命中到最后一层节点
  351. if len(childTree.Children) == 0{
  352. //获取上级
  353. var tmpParentSlice []*models.CygxIndustryMapItems
  354. tmpParentSlice,_ = parentTreeToSlice(tree,item,0,tmpParentSlice,hasIdMap)
  355. //父节点
  356. parentItem := tmpParentSlice[len(tmpParentSlice)-1]
  357. if _,ok := hasIdMap[parentItem.IndustryMapId];ok==false{
  358. hasIdMap[parentItem.IndustryMapId] = ""
  359. otherChildMapSlice[1] = append(otherChildMapSlice[1],parentItem)
  360. }
  361. //兄弟节点
  362. siblingTreeToSliceV2(parentItem,item,hasIdMap,2,otherChildMapSlice)
  363. }else{
  364. //如果不是命中到最后一层节点
  365. otherChildMapSlice[1] = append(otherChildMapSlice[1],childTree.Children...)
  366. }
  367. }
  368. //return
  369. var tmpSlice []*models.CygxIndustryMapItems
  370. //将其他规律数据的key取出来,放在切片中,并对该切片做正序排列
  371. var sortList []int
  372. for k, _ := range otherChildMapSlice {
  373. sortList = append(sortList, k)
  374. }
  375. sort.Ints(sortList)
  376. //遍历该切片,根据下标key获取对应的数据,并插入到主数据中
  377. for _,v := range sortList{
  378. tmpChildSlice := otherChildMapSlice[v]
  379. randSlice(tmpChildSlice)
  380. tmpSlice = append(tmpSlice,tmpChildSlice...)
  381. //fmt.Println(k,"=====")
  382. //for _,tmpV := range otherChildMapSlice[v]{
  383. // fmt.Println(tmpV.IndustryMapName)
  384. //}
  385. }
  386. //名字切片
  387. for _,v := range tmpSlice{
  388. //fmt.Println("k===",k,"=======v=======",v)
  389. nameSlice = append(nameSlice,v.IndustryMapName)
  390. }
  391. //fmt.Println(nameSlice)
  392. //fmt.Println(strings.Join(nameSlice,","))
  393. //utils.FileLog.Info("allNodes:%s",strings.Join(nameSlice,","))
  394. return
  395. }
  396. //获取兄弟级树
  397. func siblingTreeToSliceV2(rootNode *models.CygxIndustryMapItems,nowNode *models.CygxIndustryMapItems,hasIdMap map[int]string,depth int,otherChildMapSlice map[int][]*models.CygxIndustryMapItems)(maxDepth int) {
  398. if rootNode.Children != nil{
  399. depth++
  400. maxDepth = depth
  401. for _,v := range rootNode.Children {
  402. //如果父级id一致的情况下,那么代表这是兄弟节点或者是自己了
  403. if v.ParentId == nowNode.ParentId{
  404. //判断是否已经录入,如果已经录入,那么不处理
  405. if _,ok := hasIdMap[v.IndustryMapId];ok==false{
  406. otherChildMapSlice[depth] = append(otherChildMapSlice[depth],v)
  407. hasIdMap[v.IndustryMapId] = ""
  408. }
  409. }else{
  410. returnDepth := siblingTreeToSliceV2(v,nowNode,hasIdMap,depth,otherChildMapSlice)
  411. if returnDepth > maxDepth{
  412. maxDepth = returnDepth
  413. }
  414. }
  415. }
  416. }
  417. return
  418. }
  419. //region数据修复
  420. func FixData() {
  421. tree, err := GetIndustryTree()
  422. if err != nil {
  423. }
  424. models.FixLevelData(tree.IndustryMapId, 1)
  425. fixData(tree, 1)
  426. return
  427. }
  428. func fixData(item *models.CygxIndustryMapItems, depth int) {
  429. depth++
  430. if item.Children == nil {
  431. return
  432. }
  433. for _, v := range item.Children {
  434. models.FixLevelData(v.IndustryMapId, depth)
  435. fixData(v, depth)
  436. }
  437. }
  438. //endregion