Browse Source

初始化添加指标引用

xyxie 8 months ago
parent
commit
6e8f200c33
3 changed files with 95 additions and 71 deletions
  1. 1 0
      models/data_manage/edb_info.go
  2. 46 3
      models/data_manage/edb_info_relation.go
  3. 48 68
      services/edb_relation.go

+ 1 - 0
models/data_manage/edb_info.go

@@ -30,6 +30,7 @@ type EdbInfo struct {
 	MaxValue         float64 `description:"指标最大值"`
 	CalculateFormula string  `description:"计算公式"`
 	NoUpdate         int8    `description:"是否停止更新,0:继续更新;1:停止更新"`
+	EdbInfoType      int     `description:"指标类型,0:普通指标,1:预测指标"`
 	EdbType          int     `description:"指标类型:1:基础指标,2:计算指标"`
 }
 

+ 46 - 3
models/data_manage/edb_info_relation.go

@@ -21,15 +21,58 @@ type EdbInfoRelation struct {
 	RelationType       int       `description:"引用类型,0:直接饮用,1间接引用"`
 	RootEdbInfoId      int       `description:"间接引用时,关联的直接引用的指标ID"`
 	ChildEdbInfoId     int       `description:"间接引用时,计算指标直接关联的指标ID"`
+	RelationCode       string    `description:"引用标识"`
+	ParentRelationId   int       `description:"间接引用关联的直接引用的ID"`
 }
 
 func (e *EdbInfoRelation) TableName() string {
 	return "edb_info_relation"
 }
 
-func AddEdbInfoRelationMulti(items []*EdbInfoRelation) (err error) {
-	o := orm.NewOrmUsingDB("data")
-	_, err = o.InsertMulti(len(items), items)
+func AddEdbInfoRelationMulti(relationList []*EdbInfoRelation) (err error) {
+	o, err := orm.NewOrmUsingDB("data").Begin()
+	if err != nil {
+		return
+	}
+	defer func() {
+		if err != nil {
+			_ = o.Rollback()
+			return
+		}
+		_ = o.Commit()
+	}()
+
+	relationCodesMap := make(map[string]struct{}, 0)
+	if len(relationList) > 0 {
+		for _, relation := range relationList {
+			if relation.RelationType == 1 {
+				relationCodesMap[relation.RelationCode] = struct{}{}
+			}
+		}
+		_, err = o.InsertMulti(len(relationList), relationList)
+		if err != nil {
+			return
+		}
+	}
+	if len(relationList) > 0 {
+		// 更新间接引用指标的关联ID
+		relationCodes := make([]string, 0)
+		for relationCode := range relationCodesMap {
+			relationCodes = append(relationCodes, relationCode)
+		}
+		if len(relationCodes) > 0 {
+			sql := ` UPDATE edb_info_relation e1  
+JOIN edb_info_relation e2 ON e1.relation_code = e2.relation_code   
+SET e1.parent_relation_id = e2.edb_info_relation_id  
+WHERE  
+    e1.relation_type = 1   
+    AND e2.relation_type = 0 AND e1.parent_relation_id !=e2.edb_info_relation_id AND e1.relation_code in (` + utils.GetOrmInReplace(len(relationCodes)) + `)`
+			_, err = o.Raw(sql, relationCodes).Exec()
+			if err != nil {
+				return
+			}
+		}
+	}
 	return
 }
 

+ 48 - 68
services/edb_relation.go

@@ -14,10 +14,10 @@ import (
 )
 
 func InitEdbRelation() {
-	//InitChartEdbRelation()
-	//InitChartCrossVariety()
-	//initCalendarIndicatorRelation()
-	//initSandBoxEdbRelation()
+	InitChartEdbRelation()
+	InitChartCrossVariety()
+	initCalendarIndicatorRelation()
+	initSandBoxEdbRelation()
 	InitExcelEdbRelation()
 }
 func InitChartEdbRelation() {
@@ -72,7 +72,7 @@ func InitChartEdbRelation() {
 		}
 		// 查询计算指标信息,并且建立关联关系
 		// 查询间接引用的指标信息
-		calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
+		calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
 		if e != nil {
 			err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
 			return
@@ -144,10 +144,11 @@ func InitChartEdbRelation() {
 					ModifyTime:         nowTime,
 					RelationTime:       v.CreateTime,
 				}
+				tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
 				addList = append(addList, tmp)
 				existRelationMap[name] = struct{}{}
 				// 添加间接引用记录
-				if edbInfo.EdbType == 2 {
+				if edbInfo.EdbType == 2 || edbInfo.EdbInfoType == 1 {
 					childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
 					if !ok1 {
 						continue
@@ -161,24 +162,21 @@ func InitChartEdbRelation() {
 						if _, ok2 := existRelationMap[name1]; !ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
 							continue
 						}
-						childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId]
-						if !ok2 {
-							continue
-						}
 						tmp1 := &data_manage.EdbInfoRelation{
 							ReferObjectId:      v.ChartInfoId,
 							ReferObjectType:    utils.EDB_RELATION_CHART,
 							ReferObjectSubType: chartInfo.Source,
-							EdbInfoId:          childEdb.EdbInfoId,
-							EdbName:            childEdb.EdbName,
-							Source:             childEdb.Source,
-							EdbCode:            childEdb.EdbCode,
+							EdbInfoId:          childEdbMapping.FromEdbInfoId,
+							EdbName:            childEdbMapping.FromEdbName,
+							Source:             childEdbMapping.FromSource,
+							EdbCode:            childEdbMapping.FromEdbCode,
 							CreateTime:         nowTime,
 							ModifyTime:         nowTime,
 							RelationTime:       v.CreateTime,
 							RelationType:       1,
 							RootEdbInfoId:      edbInfo.EdbInfoId,
 							ChildEdbInfoId:     childEdbMapping.EdbInfoId,
+							RelationCode:       tmp.RelationCode,
 						}
 						addList = append(addList, tmp1)
 						// todo 防止重复
@@ -307,7 +305,7 @@ func InitChartCrossVariety() {
 		}
 		// 查询计算指标信息,并且建立关联关系
 		// 查询间接引用的指标信息
-		calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
+		calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
 		if e != nil {
 			err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
 			return
@@ -380,10 +378,11 @@ func InitChartCrossVariety() {
 						ModifyTime:         nowTime,
 						RelationTime:       item.CreateTime,
 					}
+					tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
 					addList = append(addList, tmp)
 					existRelationMap[name] = struct{}{}
 					// 添加间接引用记录
-					if edbInfo.EdbType == 2 {
+					if edbInfo.EdbType == 2 || edbInfo.EdbInfoType == 1 {
 						childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
 						if !ok1 {
 							continue
@@ -397,24 +396,22 @@ func InitChartCrossVariety() {
 							if _, ok2 := existRelationMap[name1]; !ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
 								continue
 							}
-							childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId]
-							if !ok2 {
-								continue
-							}
+
 							tmp1 := &data_manage.EdbInfoRelation{
 								ReferObjectId:      item.ChartInfoId,
 								ReferObjectType:    utils.EDB_RELATION_CHART,
 								ReferObjectSubType: utils.CHART_SOURCE_CROSS_HEDGING,
-								EdbInfoId:          childEdb.EdbInfoId,
-								EdbName:            childEdb.EdbName,
-								Source:             childEdb.Source,
-								EdbCode:            childEdb.EdbCode,
+								EdbInfoId:          childEdbMapping.FromEdbInfoId,
+								EdbName:            childEdbMapping.FromEdbName,
+								Source:             childEdbMapping.FromSource,
+								EdbCode:            childEdbMapping.FromEdbCode,
 								CreateTime:         nowTime,
 								ModifyTime:         nowTime,
 								RelationTime:       item.CreateTime,
 								RelationType:       1,
 								RootEdbInfoId:      edbInfo.EdbInfoId,
 								ChildEdbInfoId:     childEdbMapping.EdbInfoId,
+								RelationCode:       tmp.RelationCode,
 							}
 							addList = append(addList, tmp1)
 							// todo 防止重复
@@ -512,7 +509,7 @@ func initCalendarIndicatorRelation() {
 
 		// 查询计算指标信息,并且建立关联关系
 		// 查询间接引用的指标信息
-		calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
+		calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
 		if e != nil {
 			err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
 			return
@@ -559,10 +556,11 @@ func initCalendarIndicatorRelation() {
 							ModifyTime:      nowTime,
 							RelationTime:    v.CreateTime,
 						}
+						tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
 						addList = append(addList, tmp)
 						existRelationMap[name] = struct{}{}
 						// 添加间接引用记录
-						if edbInfo.EdbType == 2 {
+						if edbInfo.EdbType == 2 || edbInfo.EdbInfoType == 1 {
 							childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
 							if !ok1 {
 								continue
@@ -576,23 +574,20 @@ func initCalendarIndicatorRelation() {
 								if _, ok2 := existRelationMap[name1]; !ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
 									continue
 								}
-								childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId]
-								if !ok2 {
-									continue
-								}
 								tmp1 := &data_manage.EdbInfoRelation{
 									ReferObjectId:   v.FeCalendarMatterId,
 									ReferObjectType: utils.EDB_RELATION_CALENDAR,
-									EdbInfoId:       childEdb.EdbInfoId,
-									EdbName:         childEdb.EdbName,
-									Source:          childEdb.Source,
-									EdbCode:         childEdb.EdbCode,
+									EdbInfoId:       childEdbMapping.FromEdbInfoId,
+									EdbName:         childEdbMapping.FromEdbName,
+									Source:          childEdbMapping.FromSource,
+									EdbCode:         childEdbMapping.FromEdbCode,
 									CreateTime:      nowTime,
 									ModifyTime:      nowTime,
 									RelationTime:    v.CreateTime,
 									RelationType:    1,
 									RootEdbInfoId:   edbInfo.EdbInfoId,
 									ChildEdbInfoId:  childEdbMapping.EdbInfoId,
+									RelationCode:    tmp.RelationCode,
 								}
 								addList = append(addList, tmp1)
 								// todo 防止重复
@@ -680,7 +675,7 @@ func InitExcelEdbRelation() {
 		}
 		// 查询计算指标信息,并且建立关联关系
 		// 查询间接引用的指标信息
-		calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
+		calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
 		if e != nil {
 			err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
 			return
@@ -729,10 +724,11 @@ func InitExcelEdbRelation() {
 					ModifyTime:         nowTime,
 					RelationTime:       v.CreateTime,
 				}
+				tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
 				addList = append(addList, tmp)
 				existRelationMap[name] = struct{}{}
 				// 添加间接引用记录
-				if edbInfo.EdbType == 2 {
+				if edbInfo.EdbType == 2 || edbInfo.EdbInfoType == 1 {
 					childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
 					if !ok1 {
 						continue
@@ -746,24 +742,21 @@ func InitExcelEdbRelation() {
 						if _, ok2 := existRelationMap[name1]; !ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
 							continue
 						}
-						childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId]
-						if !ok2 {
-							continue
-						}
 						tmp1 := &data_manage.EdbInfoRelation{
 							ReferObjectId:      v.ExcelInfoId,
 							ReferObjectType:    utils.EDB_RELATION_TABLE,
 							ReferObjectSubType: v.Source,
-							EdbInfoId:          childEdb.EdbInfoId,
-							EdbName:            childEdb.EdbName,
-							Source:             childEdb.Source,
-							EdbCode:            childEdb.EdbCode,
+							EdbInfoId:          childEdbMapping.FromEdbInfoId,
+							EdbName:            childEdbMapping.FromEdbName,
+							Source:             childEdbMapping.FromSource,
+							EdbCode:            childEdbMapping.FromEdbCode,
 							CreateTime:         nowTime,
 							ModifyTime:         nowTime,
 							RelationTime:       v.CreateTime,
 							RelationType:       1,
 							RootEdbInfoId:      edbInfo.EdbInfoId,
 							ChildEdbInfoId:     childEdbMapping.EdbInfoId,
+							RelationCode:       tmp.RelationCode,
 						}
 						addList = append(addList, tmp1)
 						// todo 防止重复
@@ -864,7 +857,7 @@ func initSandBoxEdbRelation() {
 		}
 		// 查询计算指标信息,并且建立关联关系
 		// 查询间接引用的指标信息
-		calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
+		calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
 		if e != nil {
 			err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
 			return
@@ -912,10 +905,11 @@ func initSandBoxEdbRelation() {
 						ModifyTime:      nowTime,
 						RelationTime:    v.CreateTime,
 					}
+					tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
 					addList = append(addList, tmp)
 					existRelationMap[name] = struct{}{}
 					// 添加间接引用记录
-					if edbInfo.EdbType == 2 {
+					if edbInfo.EdbType == 2 || edbInfo.EdbInfoType == 1 {
 						childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
 						if !ok1 {
 							continue
@@ -929,23 +923,20 @@ func initSandBoxEdbRelation() {
 							if _, ok2 := existRelationMap[name1]; !ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
 								continue
 							}
-							childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId]
-							if !ok2 {
-								continue
-							}
 							tmp1 := &data_manage.EdbInfoRelation{
 								ReferObjectId:   v.SandboxId,
 								ReferObjectType: utils.EDB_RELATION_SANDBOX,
-								EdbInfoId:       childEdb.EdbInfoId,
-								EdbName:         childEdb.EdbName,
-								Source:          childEdb.Source,
-								EdbCode:         childEdb.EdbCode,
+								EdbInfoId:       childEdbMapping.FromEdbInfoId,
+								EdbName:         childEdbMapping.FromEdbName,
+								Source:          childEdbMapping.FromSource,
+								EdbCode:         childEdbMapping.FromEdbCode,
 								CreateTime:      nowTime,
 								ModifyTime:      nowTime,
 								RelationTime:    v.CreateTime,
 								RelationType:    1,
 								RootEdbInfoId:   edbInfo.EdbInfoId,
 								ChildEdbInfoId:  childEdbMapping.EdbInfoId,
+								RelationCode:    tmp.RelationCode,
 							}
 							addList = append(addList, tmp1)
 							// todo 防止重复
@@ -998,13 +989,13 @@ func getSandBoxEdbIdsByContent(content string) (edbInfoIds []int, err error) {
 	return
 }
 
-func GetEdbListByEdbInfoId(edbInfoList []*data_manage.EdbInfo) (edbInfoMap map[int]*data_manage.EdbInfo, edbMappingListMap map[int]*data_manage.EdbInfoCalculateMapping, edbInfoMappingRootIdsMap map[int][]int, err error) {
+func GetEdbListByEdbInfoId(edbInfoList []*data_manage.EdbInfo) (edbMappingListMap map[int]*data_manage.EdbInfoCalculateMapping, edbInfoMappingRootIdsMap map[int][]int, err error) {
 	if len(edbInfoList) == 0 {
 		return
 	}
 	edbInfoIds := make([]int, 0)
 	for _, v := range edbInfoList {
-		if v.EdbType == 2 {
+		if v.EdbType == 2 || v.EdbInfoType == 1 {
 			edbInfoIds = append(edbInfoIds, v.EdbInfoId)
 		}
 	}
@@ -1034,7 +1025,7 @@ func GetEdbListByEdbInfoId(edbInfoList []*data_manage.EdbInfo) (edbInfoMap map[i
 	edbInfoMappingRootIdsMap = make(map[int][]int, 0)
 	edbMappingMap := make(map[int]struct{})
 	for _, edbInfo := range edbInfoList {
-		if edbInfo.EdbType == 2 {
+		if edbInfo.EdbType == 2 || edbInfo.EdbInfoType == 1 {
 			edbInfoId := edbInfo.EdbInfoId
 			edbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, edbInfoId)
 			if err != nil {
@@ -1052,18 +1043,7 @@ func GetEdbListByEdbInfoId(edbInfoList []*data_manage.EdbInfo) (edbInfoMap map[i
 	for k, _ := range edbInfoIdMap {
 		edbInfoIdList = append(edbInfoIdList, k)
 	}
-	edbInfoMap = make(map[int]*data_manage.EdbInfo)
 	edbMappingListMap = make(map[int]*data_manage.EdbInfoCalculateMapping)
-	if len(edbInfoIdList) > 0 {
-		edbInfoList, err = data_manage.GetEdbInfoByIdList(edbInfoIdList)
-		if err != nil {
-			err = fmt.Errorf(" GetEdbInfoByIdList err: %s", err.Error())
-			return
-		}
-		for _, v := range edbInfoList {
-			edbInfoMap[v.EdbInfoId] = v
-		}
-	}
 
 	if len(edbMappingList) > 0 {
 		for _, v := range edbMappingList {