Przeglądaj źródła

Merge branch 'feature/eta1.9.1_edb_refresh' into debug

xyxie 9 miesięcy temu
rodzic
commit
3790727d80
2 zmienionych plików z 226 dodań i 61 usunięć
  1. 85 3
      models/data_manage/edb_info_relation.go
  2. 141 58
      services/edb_info_replace.go

+ 85 - 3
models/data_manage/edb_info_relation.go

@@ -327,7 +327,7 @@ func GetReplaceChildEdbInfoRelationTotal(edbInfoId int) (total int, err error) {
 func GetReplaceChildEdbInfoRelationList(edbInfoId int, startSize, pageSize int) (items []*EdbInfoRelation, err error) {
 	o := orm.NewOrmUsingDB("data")
 	// 列表数据
-	sql := ` SELECT * FROM edb_info_relation where  child_edb_info_id=? and root_edb_info_id !=? ORDER BY edb_info_relation_id ASC group by parent_relation_id  LIMIT ?,? `
+	sql := ` SELECT * FROM edb_info_relation where  child_edb_info_id=? and root_edb_info_id !=?  group by parent_relation_id ORDER BY edb_info_relation_id ASC  LIMIT ?,? `
 	_, err = o.Raw(sql, edbInfoId, edbInfoId, startSize, pageSize).QueryRows(&items)
 	return
 }
@@ -353,7 +353,7 @@ func GetReplaceEdbInfoRelationList(edbInfoId int, startSize, pageSize int) (item
 	return
 }
 
-// 替换指标引用表中的指标
+// 替换指标引用表中直接引用的指标
 func ReplaceRelationEdbInfoId(oldEdbInfo, newEdbInfo *EdbInfo, edbRelationIds []int, relationList []*EdbInfoRelation, refreshEdbInfoIds []int, indexCodeList []string) (err error) {
 	o, err := orm.NewOrmUsingDB("data").Begin()
 	if err != nil {
@@ -375,12 +375,94 @@ func ReplaceRelationEdbInfoId(oldEdbInfo, newEdbInfo *EdbInfo, edbRelationIds []
 		return
 	}
 
+	sourceWhere := ` and (refer_object_type in (1,2 ) or (refer_object_type=4 and refer_object_sub_type !=5) )` //平衡表和事件日历中的直接引用无需替换,
 	// 替换edb_info_id
-	sql = ` UPDATE edb_info_relation SET edb_info_id=?, source=?, edb_name=?, edb_code=?, modify_time=?, relation_time=?  WHERE edb_info_id=? and edb_info_relation_id in (` + utils.GetOrmInReplace(len(edbRelationIds)) + `)`
+	sql = ` UPDATE edb_info_relation SET edb_info_id=?, source=?, edb_name=?, edb_code=?, modify_time=?, relation_time=?  WHERE edb_info_id=? ` + sourceWhere + ` and relation_type=0 and edb_info_relation_id in (` + utils.GetOrmInReplace(len(edbRelationIds)) + `)`
 	_, err = o.Raw(sql, newEdbInfo.EdbInfoId, newEdbInfo.Source, newEdbInfo.EdbName, newEdbInfo.EdbCode, now, now, oldEdbInfo.EdbInfoId, edbRelationIds).Exec()
 	if err != nil {
 		return
 	}
+
+	// 更新code值
+	sql = ` UPDATE edb_info_relation SET relation_code=CONCAT_WS("_", edb_info_id,refer_object_id,refer_object_type,refer_object_sub_type)  WHERE relation_type=0 ` + sourceWhere + `  and edb_info_relation_id in (` + utils.GetOrmInReplace(len(edbRelationIds)) + `)`
+	_, err = o.Raw(sql, edbRelationIds).Exec()
+	if err != nil {
+		return
+	}
+	// 新增间接引用
+	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(refreshEdbInfoIds) > 0 {
+		// todo 更新指标的刷新状态
+		sql := ` UPDATE edb_info SET no_update = 0 WHERE  edb_info_id IN (` + utils.GetOrmInReplace(len(refreshEdbInfoIds)) + `) AND no_update = 1`
+		_, err = o.Raw(sql, refreshEdbInfoIds).Exec()
+		if err != nil {
+			return
+		}
+	}
+
+	//更新数据源钢联化工指标
+	if len(indexCodeList) > 0 {
+		// 更改数据源的更新状态
+		sql := ` UPDATE base_from_mysteel_chemical_index SET is_stop = 0 WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodeList)) + `) and is_stop=1`
+		_, err = o.Raw(sql, indexCodeList).Exec()
+		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
+}
+
+// UpdateSecondRelationEdbInfoId 更新指标替换后的间接引用记录
+func UpdateSecondRelationEdbInfoId(edbRelationIds []int, relationList []*EdbInfoRelation, refreshEdbInfoIds []int, indexCodeList []string) (err error) {
+	o, err := orm.NewOrmUsingDB("data").Begin()
+	if err != nil {
+		return
+	}
+	defer func() {
+		if err != nil {
+			_ = o.Rollback()
+			return
+		}
+		_ = o.Commit()
+	}()
+	// 删除相关的间接引用
+	sql := ` DELETE FROM edb_info_relation WHERE relation_type=1 and parent_relation_id in (` + utils.GetOrmInReplace(len(edbRelationIds)) + `)`
+	_, err = o.Raw(sql, edbRelationIds).Exec()
+	if err != nil {
+		return
+	}
+
 	// 新增间接引用
 	relationCodesMap := make(map[string]struct{}, 0)
 	if len(relationList) > 0 {

+ 141 - 58
services/edb_info_replace.go

@@ -280,9 +280,11 @@ func ReplaceEdbInRelation(oldEdbInfo, newEdbInfo *data_manage.EdbInfo) {
 	//indexCodeList := make([]string, 0)
 	//refreshIds := make([]int, 0)
 
-	if newEdbInfo.EdbType == 2 || newEdbInfo.EdbInfoType == 1 {
-		//需要添加间接引用
-		//查询出所有关联的指标id
+	//分页查询,每次处理500条记录
+	pageSize := 500
+
+	// 替换直接引用中的指标
+	if newEdbInfo.EdbType == 2 {
 		edbInfoList := make([]*data_manage.EdbInfo, 0)
 		edbInfoList = append(edbInfoList, newEdbInfo)
 		calculateEdbMappingListMap, calculateEdbMappingIdsMap, err = data.GetEdbListByEdbInfoId(edbInfoList)
@@ -296,16 +298,39 @@ func ReplaceEdbInRelation(oldEdbInfo, newEdbInfo *data_manage.EdbInfo) {
 			err = fmt.Errorf("查询%d指标关联指标列表为空", newEdbInfo.EdbInfoId)
 			return
 		}
-	} /*else if newEdbInfo.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
-		indexCodeList = append(indexCodeList, newEdbInfo.EdbCode)
 	}
-	// todo 新指标本身没有设置成启用
-	refreshIds = append(refreshIds, newEdbInfo.EdbInfoId)*/
-	//分页查询,每次处理500条记录
-	pageSize := 500
-	// 替换间接引用中的指标
+	total, err := data_manage.GetReplaceEdbInfoRelationTotal(oldEdbInfo.EdbInfoId)
+	if err != nil {
+		err = fmt.Errorf("查询引用表中关联的指标总数失败 err: %v", err)
+		return
+	}
+	totalPage := 0
+	if total > 0 {
+		totalPage = (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
+		//查询图表列表
+		for i := 0; i < totalPage; i += 1 {
+			startSize := i * pageSize
+			list, e := data_manage.GetReplaceEdbInfoRelationList(oldEdbInfo.EdbInfoId, startSize, pageSize)
+			if e != nil {
+				err = fmt.Errorf("查询图表关联指标列表失败 Err:%s", e)
+				return
+			}
+			if len(list) == 0 {
+				break
+			}
+			replaceTotal1, logMsg1, e := replaceEdbInRelation(oldEdbInfo, newEdbInfo, list, childEdbMappingIds, calculateEdbMappingListMap)
+			if e != nil {
+				err = e
+				return
+			}
+			replaceTotal += replaceTotal1
+			logMsg += logMsg1
+		}
+	}
+
+	// 更新间接引用中的指标
 	//查询相关的记录总数
-	total, err := data_manage.GetReplaceChildEdbInfoRelationTotal(oldEdbInfo.EdbInfoId)
+	total, err = data_manage.GetReplaceChildEdbInfoRelationTotal(oldEdbInfo.EdbInfoId)
 	if err != nil {
 		if err.Error() == utils.ErrNoRow() {
 			err = nil
@@ -315,7 +340,7 @@ func ReplaceEdbInRelation(oldEdbInfo, newEdbInfo *data_manage.EdbInfo) {
 		}
 	}
 	if total > 0 {
-		totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
+		totalPage = (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
 		//查询列表
 		for i := 0; i < totalPage; i += 1 {
 			startSize := i * pageSize
@@ -327,7 +352,7 @@ func ReplaceEdbInRelation(oldEdbInfo, newEdbInfo *data_manage.EdbInfo) {
 			// 查询直接引用
 			relationIds := make([]int, 0)
 			for _, v := range tmpList {
-				relationIds = append(relationIds, v.EdbInfoRelationId)
+				relationIds = append(relationIds, v.ParentRelationId)
 			}
 			if len(relationIds) > 0 {
 				list, e := data_manage.GetEdbInfoRelationByRelationIds(relationIds)
@@ -335,8 +360,32 @@ func ReplaceEdbInRelation(oldEdbInfo, newEdbInfo *data_manage.EdbInfo) {
 					err = fmt.Errorf("查询图表关联指标列表失败 Err:%s", e)
 					return
 				}
+				//查询直接引用指标关联关系
+				edbInfoListMap := make(map[int]struct{})
+				edbInfoIds := make([]int, 0)
+				for _, v := range list {
+					if _, ok := edbInfoListMap[v.EdbInfoId]; !ok {
+						edbInfoListMap[v.EdbInfoId] = struct{}{}
+						edbInfoIds = append(edbInfoIds, v.EdbInfoId)
+					}
+				}
+				edbInfoList := make([]*data_manage.EdbInfo, 0)
+				if len(edbInfoIds) > 0 {
+					// 查询指标信息
+					edbInfoList, err = data_manage.GetEdbInfoByIdList(edbInfoIds)
+					if err != nil {
+						err = fmt.Errorf("查询指标信息失败 Err:%s", err)
+						return
+					}
+				}
+				calculateEdbMappingListMap, calculateEdbMappingIdsMap, err = data.GetEdbListByEdbInfoId(edbInfoList)
+				if err != nil {
+					err = fmt.Errorf("查询指标关联指标列表失败 Err:%s", err)
+					return
+				}
+
 				//如何过滤掉只有间接引用,没有直接引用的
-				replaceTotal1, logMsg1, e := replaceEdbInRelation(oldEdbInfo, newEdbInfo, list, childEdbMappingIds, calculateEdbMappingListMap)
+				replaceTotal1, logMsg1, e := UpdateSecondEdbInRelation(list, calculateEdbMappingListMap, calculateEdbMappingIdsMap, edbInfoList)
 				if e != nil {
 					err = e
 					return
@@ -346,41 +395,12 @@ func ReplaceEdbInRelation(oldEdbInfo, newEdbInfo *data_manage.EdbInfo) {
 			}
 		}
 	}
-	// 替换直接引用中的指标
-	total, err = data_manage.GetReplaceEdbInfoRelationTotal(oldEdbInfo.EdbInfoId)
-	if err != nil {
-		err = fmt.Errorf("查询引用表中关联的指标总数失败 err: %v", err)
-		return
-	}
-	if total == 0 {
-		return
-	}
 
-	totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
-	//查询图表列表
-	for i := 0; i < totalPage; i += 1 {
-		startSize := i * pageSize
-		list, e := data_manage.GetReplaceEdbInfoRelationList(oldEdbInfo.EdbInfoId, startSize, pageSize)
-		if e != nil {
-			err = fmt.Errorf("查询图表关联指标列表失败 Err:%s", e)
-			return
-		}
-		if len(list) == 0 {
-			break
-		}
-		replaceTotal1, logMsg1, e := replaceEdbInRelation(oldEdbInfo, newEdbInfo, list, childEdbMappingIds, calculateEdbMappingListMap)
-		if e != nil {
-			err = e
-			return
-		}
-		replaceTotal += replaceTotal1
-		logMsg += logMsg1
-	}
 	return
 }
 
+// 完成直接引用中的指标替换工程
 func replaceEdbInRelation(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, list []*data_manage.EdbInfoRelation, childEdbMappingIds []int, calculateEdbMappingListMap map[int]*data_manage.EdbInfoCalculateMapping) (replaceTotal int, logMsg string, err error) {
-	nowTime := time.Now()
 	replaceEdbIds := make([]int, 0)
 	//calculateEdbMappingListMap := make(map[int]*data_manage.EdbInfoCalculateMapping)
 	//calculateEdbMappingIdsMap := make(map[int][]int)
@@ -388,14 +408,79 @@ func replaceEdbInRelation(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, list []*d
 	indexCodeList := make([]string, 0)
 	addList := make([]*data_manage.EdbInfoRelation, 0)
 	refreshIds := make([]int, 0)
+	nowTime := time.Now()
 	for _, v := range list {
-		if v.RelationType == 0 && (v.ReferObjectType == utils.EDB_RELATION_CALENDAR ||
-			(v.ReferObjectType == utils.EDB_RELATION_TABLE && v.ReferObjectSubType == utils.BALANCE_TABLE)) {
-			//平衡表和事件日历中的直接引用无需替换,
-		} else {
-			replaceEdbIds = append(replaceEdbIds, v.EdbInfoRelationId)
-			// 添加间接引用数据
-			if newEdbInfo.EdbType == 2 || newEdbInfo.EdbInfoType == 1 {
+		replaceEdbIds = append(replaceEdbIds, v.EdbInfoRelationId)
+		if newEdbInfo.EdbType == 2 {
+			for _, childEdbMappingId := range childEdbMappingIds {
+				childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
+				if !ok2 {
+					continue
+				}
+
+				if childEdbMapping.FromSource == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
+					indexCodeList = append(indexCodeList, childEdbMapping.FromEdbCode)
+				}
+				tmp1 := &data_manage.EdbInfoRelation{
+					ReferObjectId:      v.ReferObjectId,
+					ReferObjectType:    v.ReferObjectType,
+					ReferObjectSubType: v.ReferObjectSubType,
+					EdbInfoId:          childEdbMapping.FromEdbInfoId,
+					EdbName:            childEdbMapping.FromEdbName,
+					Source:             childEdbMapping.FromSource,
+					EdbCode:            childEdbMapping.FromEdbCode,
+					CreateTime:         nowTime,
+					ModifyTime:         nowTime,
+					RelationTime:       nowTime,
+					RelationType:       1,
+					RootEdbInfoId:      newEdbInfo.EdbInfoId,
+					ChildEdbInfoId:     childEdbMapping.EdbInfoId,
+				}
+				tmp1.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp1.RootEdbInfoId, tmp1.ReferObjectId, tmp1.ReferObjectType, tmp1.ReferObjectSubType)
+				addList = append(addList, tmp1)
+				refreshIds = append(refreshIds, childEdbMapping.FromEdbInfoId)
+				// todo 防止重复
+			}
+		}
+		logMsg += strconv.Itoa(v.EdbInfoRelationId) + ";"
+	}
+	if len(replaceEdbIds) > 0 {
+		err = data_manage.ReplaceRelationEdbInfoId(oldEdbInfo, newEdbInfo, replaceEdbIds, addList, refreshIds, indexCodeList)
+		if err != nil {
+			logMsg = ""
+			err = fmt.Errorf("替换指标引用表中的指标ID失败 Err:%s", err)
+			return
+		}
+		replaceTotal = len(replaceEdbIds)
+	}
+	return
+}
+
+func UpdateSecondEdbInRelation(list []*data_manage.EdbInfoRelation, calculateEdbMappingListMap map[int]*data_manage.EdbInfoCalculateMapping, calculateEdbMappingIdsMap map[int][]int, edbInfoList []*data_manage.EdbInfo) (replaceTotal int, logMsg string, err error) {
+	nowTime := time.Now()
+	edbInfoRelationIds := make([]int, 0)
+	indexCodeList := make([]string, 0)
+	addList := make([]*data_manage.EdbInfoRelation, 0)
+	refreshIds := make([]int, 0)
+	edbInfoMap := make(map[int]*data_manage.EdbInfo)
+	for _, v := range edbInfoList {
+		edbInfoMap[v.EdbInfoId] = v
+	}
+	// 查询所有的直接引用,删除所有的间接引用,添加所有直接引用的间接引用
+	for _, v := range list {
+		if v.RelationType == 0 {
+			edbInfoRelationIds = append(edbInfoRelationIds, v.EdbInfoRelationId)
+			edbInfo, ok := edbInfoMap[v.EdbInfoId]
+			if !ok {
+				err = fmt.Errorf("查询指标信息失败 EdbInfoId:%d", v.EdbInfoId)
+				return
+			}
+			if edbInfo.EdbType == 2 { //计算指标
+				childEdbMappingIds, ok := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
+				if !ok {
+					err = fmt.Errorf("查询%d指标关联指标列表为空", edbInfo.EdbInfoId)
+					return
+				}
 				for _, childEdbMappingId := range childEdbMappingIds {
 					childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
 					if !ok2 {
@@ -417,27 +502,25 @@ func replaceEdbInRelation(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, list []*d
 						ModifyTime:         nowTime,
 						RelationTime:       nowTime,
 						RelationType:       1,
-						RootEdbInfoId:      newEdbInfo.EdbInfoId,
+						RootEdbInfoId:      edbInfo.EdbInfoId,
 						ChildEdbInfoId:     childEdbMapping.EdbInfoId,
 					}
 					tmp1.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp1.RootEdbInfoId, tmp1.ReferObjectId, tmp1.ReferObjectType, tmp1.ReferObjectSubType)
 					addList = append(addList, tmp1)
 					refreshIds = append(refreshIds, childEdbMapping.FromEdbInfoId)
-					// todo 防止重复
 				}
 			}
-			logMsg += strconv.Itoa(v.EdbInfoRelationId) + ";"
 		}
-
 	}
-	if len(replaceEdbIds) > 0 {
-		err = data_manage.ReplaceRelationEdbInfoId(oldEdbInfo, newEdbInfo, replaceEdbIds, addList, refreshIds, indexCodeList)
+
+	if len(edbInfoRelationIds) > 0 {
+		err = data_manage.UpdateSecondRelationEdbInfoId(edbInfoRelationIds, addList, refreshIds, indexCodeList)
 		if err != nil {
 			logMsg = ""
 			err = fmt.Errorf("替换指标引用表中的指标ID失败 Err:%s", err)
 			return
 		}
-		replaceTotal = len(replaceEdbIds)
+		replaceTotal = len(edbInfoRelationIds)
 	}
 	return
 }