Browse Source

feat(新增行业图谱获取方法)

Roc 4 years ago
parent
commit
32fd36aeb8
2 changed files with 272 additions and 1 deletions
  1. 8 0
      models/industry_map.go
  2. 264 1
      services/industry_map.go

+ 8 - 0
models/industry_map.go

@@ -38,4 +38,12 @@ func GetCygxIndustryMapByParentId(parentId int) (items []*CygxIndustryMapItems,
 	sql := ` SELECT * FROM cygx_industry_map WHERE parent_id=?`
 	_, err = o.Raw(sql, parentId).QueryRows(&items)
 	return
+}
+
+//根据名称模糊匹配获取一条数据
+func GetFirstCygxIndustryItemByName(industryName string) (items *CygxIndustryMapItems, err error) {
+	o := orm.NewOrm()
+	sql := ` SELECT * FROM cygx_industry_map WHERE industry_map_name like '%` + industryName + `%' order by industry_map_id asc`
+	err = o.Raw(sql).QueryRow(&items)
+	return
 }

+ 264 - 1
services/industry_map.go

@@ -5,6 +5,8 @@ import (
 	"fmt"
 	"hongze/hongze_cygx/models"
 	"hongze/hongze_cygx/utils"
+	"sort"
+	"strings"
 )
 
 
@@ -52,4 +54,265 @@ func GetIndustryMap() {
 	fmt.Println("Err:",err)
 	utils.FileLog.Info("allNodes:%s",string(resultJson))
 	fmt.Println("end")
-}
+}
+
+
+func GetIndustryTree()(rootNode *models.CygxIndustryMapItems, err error) {
+	fmt.Println("start")
+	list, err := models.GetCygxIndustryMapByParentId(0)
+	if err != nil {
+		fmt.Println("GetCygxIndustryMapByParentId Err:",err.Error())
+		return
+	}
+	rootNode = list[0]
+	allNodes, err := models.GetCygxIndustryMapAll()
+	if err != nil {
+		fmt.Println("GetCygxIndustryMapAll Err:"+err.Error())
+		return
+	}
+	MakeTree(allNodes, rootNode)
+	resultJson,err:=json.Marshal(rootNode)
+	fmt.Println("Err:",err)
+	fmt.Println("allNodes:",string(resultJson))
+	//utils.FileLog.Info("allNodes:%s",string(resultJson))
+	fmt.Println("end")
+	return
+}
+
+//获取行业图谱切片
+func GetIndustryMapNameSlice(industryName string)(nameSlice []string,err error){
+	tree,err := GetIndustryTree()
+	if err != nil{
+		fmt.Println("获取树失败")
+		return
+	}
+	//fmt.Println(tree)
+
+	item,err := models.GetFirstCygxIndustryItemByName(industryName)
+	if err != nil{
+		fmt.Println("获取数据失败,",err)
+		return
+	}
+	var tmpSlice,tmpChildSlice,tmpParentSlice,tmpSiblingSlice,tmpSiblingChildSlice []*models.CygxIndustryMapItems
+
+	//已经存在的行业id的map集合
+	hasIdMap := make(map[int]string)
+	hasIdMap[item.IndustryMapId]=""
+
+	//获取下级
+	tmpChildSlice = childTreeToSlice(tree,item,tmpChildSlice,hasIdMap)
+	tmpSlice = append(tmpSlice,tmpChildSlice...)
+
+
+	//获取上级
+	tmpParentSlice,_ = parentTreeToSlice(tree,item,0,tmpParentSlice,hasIdMap)
+	//移除第一个最上级的行业
+	tmpParentSlice = append(tmpParentSlice[:0],tmpParentSlice[1:len(tmpParentSlice)]...)
+	tmpParentSlice = reverse(tmpParentSlice)
+	tmpSlice = append(tmpSlice,tmpParentSlice...)
+
+	//获取同级
+	tmpSiblingSlice = siblingTreeToSlice(tree,item,tmpSiblingSlice,hasIdMap)
+	tmpSlice = append(tmpSlice,tmpSiblingSlice...)
+
+
+	//获取同级的子级
+	for _,v := range tmpSiblingSlice{
+		tmpSiblingChildSlice = childTreeToSlice(v,v,tmpSiblingChildSlice,hasIdMap)
+	}
+	tmpSlice = append(tmpSlice,tmpSiblingChildSlice...)
+
+	//名字切片
+	for _,v := range tmpSlice{
+		hasIdMap[v.IndustryMapId] = ""
+		nameSlice = append(nameSlice,v.IndustryMapName)
+	}
+
+	return
+}
+
+func TestGetStr(industryName string){
+	tree,err := GetIndustryTree()
+	if err != nil{
+		fmt.Println("获取树失败")
+	}
+	//fmt.Println(tree)
+
+	item,err := models.GetFirstCygxIndustryItemByName(industryName)
+	if err != nil{
+		fmt.Println("获取数据失败,",err)
+	}
+	fmt.Println(item)
+	var tmpSlice,tmpChildSlice,tmpParentSlice,tmpSiblingSlice,tmpSiblingChildSlice []*models.CygxIndustryMapItems
+
+	//已经存在的行业id的map集合
+	hasIdMap := make(map[int]string)
+	hasIdMap[item.IndustryMapId]=""
+
+	//获取下级
+	tmpChildSlice = childTreeToSlice(tree,item,tmpChildSlice,hasIdMap)
+	tmpSlice = append(tmpSlice,tmpChildSlice...)
+	fmt.Println("下级获取结束")
+
+
+	//获取上级
+	tmpParentSlice,_ = parentTreeToSlice(tree,item,0,tmpParentSlice,hasIdMap)
+	//移除第一个最上级的行业
+	tmpParentSlice = append(tmpParentSlice[:0],tmpParentSlice[1:len(tmpParentSlice)]...)
+	tmpParentSlice = reverse(tmpParentSlice)
+	tmpSlice = append(tmpSlice,tmpParentSlice...)
+
+	//获取同级
+	tmpSiblingSlice = siblingTreeToSlice(tree,item,tmpSiblingSlice,hasIdMap)
+	tmpSlice = append(tmpSlice,tmpSiblingSlice...)
+
+
+	//获取同级的子级
+	for _,v := range tmpSiblingSlice{
+		tmpSiblingChildSlice = childTreeToSlice(v,v,tmpSiblingChildSlice,hasIdMap)
+	}
+	tmpSlice = append(tmpSlice,tmpSiblingChildSlice...)
+
+	//获取其他的子级
+	//找到最上级
+
+	//遍历父级菜单,然后再去遍历对应的数据
+	depth := 0
+	otherChildMapSlice := map[int][]*models.CygxIndustryMapItems{}
+	for _,parentSlice := range tmpParentSlice{
+		depth++
+		childTreeToSliceTwo(parentSlice,hasIdMap,&depth,otherChildMapSlice)
+	}
+
+	//将其他规律数据的key取出来,放在切片中,并对该切片做正序排列
+	var sortList []int
+	for k, _ := range otherChildMapSlice {
+		sortList = append(sortList, k)
+	}
+	sort.Ints(sortList)
+
+	//遍历该切片,根据下标key获取对应的数据,并插入到主数据中
+	for _,v := range sortList{
+		tmpSlice = append(tmpSlice,otherChildMapSlice[v]...)
+	}
+	//tmpSlice = append(tmpSlice,tmpOtherChildSlice...)
+	fmt.Println("获取其他子级结束")
+
+	//名字切片
+	var nameSlice []string
+	for k,v := range tmpSlice{
+		hasIdMap[v.IndustryMapId] = ""
+		fmt.Println("k===",k,"=======v=======",v)
+		nameSlice = append(nameSlice,v.IndustryMapName)
+	}
+	fmt.Println("单次任务结束")
+
+	fmt.Println(nameSlice)
+	fmt.Println(strings.Join(nameSlice,","))
+	utils.FileLog.Info("allNodes:%s",strings.Join(nameSlice,","))
+}
+
+//切片反转
+func reverse(s []*models.CygxIndustryMapItems) []*models.CygxIndustryMapItems {
+	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
+		s[i], s[j] = s[j], s[i]
+	}
+	return s
+}
+
+//查找某个节点的下级树
+func childTreeToSlice(rootNode *models.CygxIndustryMapItems,nowNode *models.CygxIndustryMapItems,tmpSlice []*models.CygxIndustryMapItems,hasIdMap map[int]string) (returnSlice []*models.CygxIndustryMapItems)  {
+	returnSlice = tmpSlice
+	//var parentSlice []string
+	if rootNode.Children != nil{
+		for _,v := range rootNode.Children {
+			if v.ParentId == nowNode.IndustryMapId{
+				if _,ok := hasIdMap[v.IndustryMapId];ok==false{
+					returnSlice = append(returnSlice,v)
+					hasIdMap[v.IndustryMapId] = ""
+				}
+				returnSlice = childTreeToSlice(v,v,returnSlice,hasIdMap)
+
+			}else {
+				returnSlice = childTreeToSlice(v,nowNode,returnSlice,hasIdMap)
+			}
+		}
+	}
+	return
+}
+
+//获取子树V2,与上一个子树规律不一样,因为上级
+func childTreeToSliceTwo(rootNode *models.CygxIndustryMapItems,hasIdMap map[int]string,depth *int,otherChildMapSlice map[int][]*models.CygxIndustryMapItems) {
+	*depth++
+	if rootNode.Children != nil{
+		for _,v := range rootNode.Children {
+			//判断是否已经录入,如果已经录入,那么不处理
+			if _,ok := hasIdMap[v.IndustryMapId];ok==false{
+				otherChildMapSlice[*depth] = append(otherChildMapSlice[*depth],v)
+			}
+		}
+
+		for _,v := range rootNode.Children {
+			//判断是否已经录入,如果已经录入,那么不处理
+			if _,ok := hasIdMap[v.IndustryMapId];ok==false{
+				hasIdMap[v.IndustryMapId] = ""
+				childTreeToSliceTwo(v,hasIdMap,depth,otherChildMapSlice)
+			}
+		}
+	}
+	return
+}
+
+//获取兄弟级树
+func siblingTreeToSlice(rootNode *models.CygxIndustryMapItems,nowNode *models.CygxIndustryMapItems,tmpReturnSlice []*models.CygxIndustryMapItems,hasIdMap map[int]string) (returnSlice []*models.CygxIndustryMapItems)  {
+	//var parentSlice []string
+	returnSlice = tmpReturnSlice
+	if rootNode.Children != nil{
+		for _,v := range rootNode.Children {
+			//如果父级id一致的情况下,那么代表这是兄弟节点或者是自己了
+			if v.ParentId == nowNode.ParentId{
+				if v.IndustryMapId != nowNode.IndustryMapId{
+					if _,ok := hasIdMap[v.IndustryMapId];ok==false{
+						returnSlice = append(returnSlice,v)
+						hasIdMap[v.IndustryMapId] = ""
+					}
+
+				}
+			}else{
+				returnSlice = siblingTreeToSlice(v,nowNode,returnSlice,hasIdMap)
+			}
+		}
+	}
+	return
+}
+
+//获取上级树
+func parentTreeToSlice(rootNode *models.CygxIndustryMapItems,nowNode *models.CygxIndustryMapItems,depth int,tmpReturnSlice []*models.CygxIndustryMapItems,hasIdMap map[int]string) (returnSlice []*models.CygxIndustryMapItems,ok bool)  {
+	if depth != 0 {
+		returnSlice = tmpReturnSlice
+	}
+	depth++
+	if _,ok := hasIdMap[rootNode.IndustryMapId];ok==false{
+		returnSlice = append(returnSlice,rootNode)
+		hasIdMap[rootNode.IndustryMapId] = ""
+	}
+	if rootNode.Children != nil{
+		for _,v := range rootNode.Children {
+			if v.ParentId == nowNode.ParentId{
+				ok = true
+				return
+			}
+			returnSlice,ok = parentTreeToSlice(v,nowNode,depth,returnSlice,hasIdMap)
+			//如果返回的匹配完成,那么不进入下一次循环,直接返回
+			if ok {
+				return
+			}
+
+			//一次循环结束后,如果没有匹配上,那么移除临时切片中的数据
+			returnSlice = append(returnSlice[:0], returnSlice[0:depth]...)
+		}
+	}
+
+	return
+}
+