Browse Source

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

# Conflicts:
#	models/data_manage/edb_info.go
#	models/sandbox/sandbox.go
#	services/sandbox/sandbox.go
xyxie 10 months ago
parent
commit
572942bb34

+ 6 - 1
controllers/data_manage/excel/excel_info.go

@@ -308,6 +308,8 @@ func (c *ExcelInfoController) Add() {
 	resp.ExcelInfoId = excelInfo.ExcelInfoId
 	resp.UniqueCode = excelInfo.UniqueCode
 
+	//新增指标引用记录
+	_ = data.SaveExcelEdbInfoRelation(excelInfo.ExcelInfoId, excelInfo.Source, false)
 	//新增操作日志
 	//{
 	//	excelLog := &data_manage.ExcelInfoLog{
@@ -1064,7 +1066,8 @@ func (c *ExcelInfoController) Edit() {
 		ExcelInfoId: excelInfo.ExcelInfoId,
 		UniqueCode:  excelInfo.UniqueCode,
 	}
-
+	//新增指标引用记录
+	_ = data.SaveExcelEdbInfoRelation(excelInfo.ExcelInfoId, excelInfo.Source, false)
 	//删除公共图库那边的缓存
 	_ = utils.Rc.Delete(utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelInfo.UniqueCode)
 
@@ -2559,6 +2562,8 @@ func (c *ExcelInfoController) Copy() {
 		br.IsSendEmail = isSendEmail
 		return
 	}
+	//新增指标引用记录
+	_ = data.SaveExcelEdbInfoRelation(excelInfo.ExcelInfoId, excelInfo.Source, true)
 
 	resp := new(response.AddExcelInfoResp)
 	resp.ExcelInfoId = excelInfo.ExcelInfoId

+ 11 - 0
models/data_manage/edb_info_calculate_mapping.go

@@ -1,6 +1,7 @@
 package data_manage
 
 import (
+	"eta/eta_api/utils"
 	"github.com/beego/beego/v2/client/orm"
 	"time"
 )
@@ -101,3 +102,13 @@ func GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId int) (items []*EdbInfoC
 	_, err = o.Raw(sql, edbInfoId).QueryRows(&items)
 	return
 }
+
+// GetEdbInfoCalculateMappingListByEdbInfoIds 根据生成的指标id获取来源的指标id列表
+func GetEdbInfoCalculateMappingListByEdbInfoIds(edbInfoIds []int) (items []*EdbInfoCalculateMappingInfo, err error) {
+	o := orm.NewOrmUsingDB("data")
+	sql := ` SELECT a.*,b.edb_type as from_edb_type,b.edb_info_type as from_edb_info_type, b.unique_code AS from_unique_code, b.classify_id AS from_classify_id,b.no_update FROM edb_info_calculate_mapping AS a
+			INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
+			WHERE a.edb_info_id in (` + utils.GetOrmInReplace(len(edbInfoIds)) + `) `
+	_, err = o.Raw(sql, edbInfoIds).QueryRows(&items)
+	return
+}

+ 1 - 1
models/data_manage/edb_info_relation.go

@@ -21,7 +21,7 @@ type EdbInfoRelation struct {
 	RelationTime       time.Time `description:"引用时间"`
 	RelationType       int       `description:"引用类型,0:直接饮用,1间接引用"`
 	RootEdbInfoId      int       `description:"间接引用时,关联的直接引用的指标ID"`
-	ChildEdbInfoId     int       `description:"间接引用时,计算指标直接关联的指标ID"`
+	ChildEdbInfoId     int       `description:"间接引用时,关联的计算指标ID"`
 }
 
 func (e *EdbInfoRelation) TableName() string {

+ 9 - 0
models/data_manage/excel/excel_edb_mapping.go

@@ -119,6 +119,15 @@ func GetAllExcelEdbMappingByExcelInfoId(excelInfoId int) (items []*ExcelEdbMappi
 	return
 }
 
+// GetAllExcelEdbMappingByExcelInfoIds 根据excel的id获取所有的指标
+func GetAllExcelEdbMappingByExcelInfoIds(excelInfoIds []int) (items []*ExcelEdbMapping, err error) {
+	o := orm.NewOrmUsingDB("data")
+	sql := `SELECT a.* FROM  excel_edb_mapping a
+         WHERE a.excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `) ORDER BY a.excel_edb_mapping_id ASC `
+	_, err = o.Raw(sql, excelInfoIds).QueryRows(&items)
+	return
+}
+
 // GetExcelEdbMappingByEdbInfoIdAndSource 根据指标id获取配置关系
 func GetExcelEdbMappingByEdbInfoIdAndSource(edbInfoId int, sources []int) (items []*ExcelEdbMapping, err error) {
 	o := orm.NewOrmUsingDB("data")

+ 108 - 17
services/data/edb_info_relation.go

@@ -2,6 +2,7 @@ package data
 
 import (
 	"eta/eta_api/models/data_manage"
+	excelModel "eta/eta_api/models/data_manage/excel"
 	"eta/eta_api/models/fe_calendar"
 	"eta/eta_api/services/alarm_msg"
 	"eta/eta_api/services/sandbox"
@@ -95,7 +96,7 @@ func saveEdbInfoRelation(edbInfoIds []int, objectId, objectType, objectSubType i
 				RelationTime:       nowTime,
 			}
 			addList = append(addList, tmp)
-			if edbInfo.EdbType == 2 {
+			if edbInfo.EdbType == 2 || edbInfo.EdbInfoType == 1 {
 				childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
 				if !ok1 {
 					continue
@@ -284,8 +285,8 @@ func SaveCalendarEdbInfoRelation(chartPermissionId int, matterDate string, editM
 				ModifyTime:         nowTime,
 			}
 			addList = append(addList, tmp)
-			//todo 添加指标间接引用
-			if edbInfo.EdbType == 2 {
+			//添加指标间接引用
+			if edbInfo.EdbType == 2 || edbInfo.EdbInfoType == 1 {
 				childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
 				if !ok1 {
 					continue
@@ -406,7 +407,31 @@ func GetEdbRelationList(source int, classifyId, sysUserId, frequency, keyword, s
 }
 
 func GetEdbListByEdbInfoId(edbInfoList []*data_manage.EdbInfo) (edbInfoMap map[int]*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 || v.EdbInfoType == 1 {
+			edbInfoIds = append(edbInfoIds, v.EdbInfoId)
+		}
+	}
+	if len(edbInfoIds) == 0 {
+		return
+	}
 	//查询指标信息
+	allEdbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo, 0)
+	allMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoIds(edbInfoIds)
+	if e != nil {
+		err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoIds err: %s", e.Error())
+		return
+	}
+	for _, v := range allMappingList {
+		if _, ok := allEdbMappingMap[v.EdbInfoId]; !ok {
+			allEdbMappingMap[v.EdbInfoId] = make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
+		}
+		allEdbMappingMap[v.EdbInfoId] = append(allEdbMappingMap[v.EdbInfoId], v)
+	}
 	//查询指标映射
 	//查询所有指标数据
 	//查询这个指标相关的mapping信息放到数组里,
@@ -417,9 +442,9 @@ 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(edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, edbInfoId)
+			edbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, edbInfoId)
 			if err != nil {
 				err = fmt.Errorf(" GetCalculateEdbInfoByEdbInfoId err: %s", err.Error())
 				return
@@ -456,8 +481,8 @@ func GetEdbListByEdbInfoId(edbInfoList []*data_manage.EdbInfo) (edbInfoMap map[i
 	return
 }
 
-// GetCalculateEdbInfoByEdbInfoId 计算指标追溯
-func GetCalculateEdbInfoByEdbInfoId(edbInfoId int, hasFindMap map[int]struct{}, edbInfoIdMap map[int]struct{}, edbMappingList []*data_manage.EdbInfoCalculateMapping, edbMappingMap map[int]struct{}, edbInfoMappingRootIdsMap map[int][]int, rootEdbInfoId int) (newEdbMappingList []*data_manage.EdbInfoCalculateMapping, err error) {
+// getCalculateEdbInfoByEdbInfoId 计算指标追溯
+func getCalculateEdbInfoByEdbInfoId(allEdbMappingMap map[int][]*data_manage.EdbInfoCalculateMappingInfo, edbInfoId int, hasFindMap map[int]struct{}, edbInfoIdMap map[int]struct{}, edbMappingList []*data_manage.EdbInfoCalculateMapping, edbMappingMap map[int]struct{}, edbInfoMappingRootIdsMap map[int][]int, rootEdbInfoId int) (newEdbMappingList []*data_manage.EdbInfoCalculateMapping, err error) {
 	newEdbMappingList = edbMappingList
 	_, ok := hasFindMap[edbInfoId]
 	if ok {
@@ -467,13 +492,16 @@ func GetCalculateEdbInfoByEdbInfoId(edbInfoId int, hasFindMap map[int]struct{},
 	if _, ok1 := edbInfoIdMap[edbInfoId]; !ok1 {
 		edbInfoIdMap[edbInfoId] = struct{}{}
 	}
-
-	edbInfoMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
-	if e != nil {
-		err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", e.Error())
-		return
+	edbInfoMappingList := make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
+	edbInfoMappingList, ok = allEdbMappingMap[edbInfoId]
+	if !ok {
+		edbInfoMappingList, err = data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
+		if err != nil {
+			err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", err.Error())
+			return
+		}
 	}
-
+	hasFindMap[edbInfoId] = struct{}{}
 	if len(edbInfoMappingList) > 0 {
 		fromEdbInfoIdList := make([]int, 0)
 		edbInfoMappingIdList := make([]int, 0)
@@ -508,9 +536,9 @@ func GetCalculateEdbInfoByEdbInfoId(edbInfoId int, hasFindMap map[int]struct{},
 			if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
 				// 查过了就不查了
 				if _, ok2 := hasFindMap[v.FromEdbInfoId]; !ok2 {
-					newEdbMappingList, e = GetCalculateEdbInfoByEdbInfoId(v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, rootEdbInfoId)
-					if e != nil {
-						err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", e.Error())
+					newEdbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, rootEdbInfoId)
+					if err != nil {
+						err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", err.Error())
 						return
 					}
 				}
@@ -520,6 +548,69 @@ func GetCalculateEdbInfoByEdbInfoId(edbInfoId int, hasFindMap map[int]struct{},
 		edbInfoMappingRootIdsMap[rootEdbInfoId] = append(edbInfoMappingRootIdsMap[rootEdbInfoId], edbInfoMappingIdList...)
 	}
 
-	hasFindMap[edbInfoId] = struct{}{}
+	return
+}
+
+// SaveExcelEdbInfoRelation 添加/编辑表格时同步修改指标引用关联记录
+func SaveExcelEdbInfoRelation(excelInfoId, source int, addChildExcel bool) (err error) {
+	//查询和指标相关的时间序列表格和混合表格
+	if !utils.InArrayByInt([]int{utils.TIME_TABLE, utils.MIXED_TABLE, utils.BALANCE_TABLE}, source) {
+		return
+	}
+	if addChildExcel && source == utils.BALANCE_TABLE {
+		//查询excel信息,
+		excelInfoList, e := excelModel.GetChildExcelInfoByParentId(excelInfoId)
+		if e != nil {
+			err = fmt.Errorf("查询excel信息失败,错误:%s", e.Error())
+			return
+		}
+
+		//查询
+		if len(excelInfoList) > 0 {
+			//汇总表格ID
+			excelIds := make([]int, 0)
+			for _, v := range excelInfoList {
+				if v.BalanceType == 0 {
+					excelIds = append(excelIds, v.ExcelInfoId)
+				}
+			}
+			mappingList, e := excelModel.GetAllExcelEdbMappingByExcelInfoIds(excelIds)
+			if e != nil {
+				err = fmt.Errorf("查询和指标相关的表格失败,错误:%s", e.Error())
+				return
+			}
+			//整理map
+			mappingMap := make(map[int][]int)
+			for _, v := range mappingList {
+				mappingMap[v.ExcelInfoId] = append(mappingMap[v.ExcelInfoId], v.EdbInfoId)
+			}
+
+			// 添加指标引用
+			for _, v := range excelInfoList {
+				edbInfoIds, ok := mappingMap[v.ExcelInfoId]
+				if !ok {
+					continue
+				}
+				err = saveEdbInfoRelation(edbInfoIds, v.ExcelInfoId, utils.EDB_RELATION_TABLE, source)
+			}
+			//更新
+		}
+	}
+	mappingList, err := excelModel.GetAllExcelEdbMappingByExcelInfoId(excelInfoId)
+	if err != nil {
+		err = fmt.Errorf("查询和指标相关的表格失败,错误:%s", err.Error())
+		return
+	}
+	//查询指标ID
+	edbInfoIds := make([]int, 0)
+	for k := range mappingList {
+		edbInfoIds = append(edbInfoIds, k)
+	}
+	//查询指标引用关联记录
+	//更新指标刷新状态为启用
+	if len(edbInfoIds) == 0 {
+		return
+	}
+	err = saveEdbInfoRelation(edbInfoIds, excelInfoId, utils.EDB_RELATION_TABLE, source)
 	return
 }

+ 4 - 1
services/data/excel/excel_op.go

@@ -2,6 +2,7 @@ package excel
 
 import (
 	"errors"
+	"eta/eta_api/models/data_manage"
 	excelModel "eta/eta_api/models/data_manage/excel"
 	"eta/eta_api/models/system"
 	"eta/eta_api/services"
@@ -98,7 +99,7 @@ func Delete(excelInfo *excelModel.ExcelInfo, sysUser *system.Admin) (err error,
 			err = fmt.Errorf("删除图表,指标信息失败 Err:%s", err.Error())
 			return
 		}
-
+		_ = data_manage.DeleteEdbRelationByObjectIds(excelIds, utils.EDB_RELATION_TABLE)
 		return
 	}
 
@@ -107,6 +108,8 @@ func Delete(excelInfo *excelModel.ExcelInfo, sysUser *system.Admin) (err error,
 	excelInfo.ModifyTime = time.Now()
 	err = excelInfo.Update([]string{"IsDelete", "ModifyTime"})
 
+	// 删除指标引用
+	_ = data_manage.DeleteEdbRelationByObjectIds([]int{excelInfo.ExcelInfoId}, utils.EDB_RELATION_TABLE)
 	return
 }