package data import ( "eta/eta_api/models/data_manage" "eta/eta_api/models/fe_calendar" "eta/eta_api/services/alarm_msg" "eta/eta_api/services/sandbox" "eta/eta_api/utils" "fmt" "strings" "time" ) // SaveChartEdbInfoRelation 添加/编辑图表指标引用关联记录 func SaveChartEdbInfoRelation(edbInfoIds []int, chartInfo *data_manage.ChartInfo) (err error) { //更新指标刷新状态为启用 err = saveEdbInfoRelation(edbInfoIds, chartInfo.ChartInfoId, utils.EDB_RELATION_CHART, chartInfo.Source) return } // saveEdbInfoRelation 添加/编辑图表指标引用关联记录 func saveEdbInfoRelation(edbInfoIds []int, objectId, objectType, objectSubType int) (err error) { // 实现添加引用记录的逻辑 if len(edbInfoIds) == 0 { return } defer func() { if err != nil { tips := "实现添加引用记录的逻辑-添加/编辑图表指标引用关联记录失败, ErrMsg:\n" + err.Error() utils.FileLog.Info(tips) go alarm_msg.SendAlarmMsg(tips, 3) } }() refreshIds := make([]int, 0) indexCodeList := make([]string, 0) // 查询指标信息 edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds) if e != nil { err = fmt.Errorf("查询指标信息失败,%s", e.Error()) return } // 查询计算指标信息,并且建立关联关系 // 查询间接引用的指标信息 calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList) if e != nil { err = fmt.Errorf("查询计算指标信息失败,%s", e.Error()) return } // 只统计钢联化工和wind来源的指标 for _, edbInfo := range edbInfoList { /*if edbInfo.Source != utils.DATA_SOURCE_WIND && edbInfo.Source != utils.DATA_SOURCE_MYSTEEL_CHEMICAL { continue }*/ refreshIds = append(refreshIds, edbInfo.EdbInfoId) if edbInfo.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL { indexCodeList = append(indexCodeList, edbInfo.EdbCode) } } // 循转组装引用 // 查询已有的引用关系 existList, e := data_manage.GetEdbInfoRelationByReferObjectId(objectId, objectType) if e != nil { err = fmt.Errorf("查询已有的引用关系失败,%s", e.Error()) return } deleteMap := make(map[int]bool) relationMap := make(map[int]bool) for _, exist := range existList { deleteMap[exist.EdbInfoId] = true relationMap[exist.EdbInfoId] = true } // 新增不存在的引用关系 // 删除不再需要的引用关系 nowTime := time.Now() addList := make([]*data_manage.EdbInfoRelation, 0) deleteEdbInfoIds := make([]int, 0) for _, edbInfo := range edbInfoList { /*if edbInfo.Source != utils.DATA_SOURCE_WIND && edbInfo.Source != utils.DATA_SOURCE_MYSTEEL_CHEMICAL { continue }*/ if _, ok := relationMap[edbInfo.EdbInfoId]; ok { delete(deleteMap, edbInfo.EdbInfoId) } else { tmp := &data_manage.EdbInfoRelation{ ReferObjectId: objectId, ReferObjectType: objectType, ReferObjectSubType: objectSubType, EdbInfoId: edbInfo.EdbInfoId, EdbName: edbInfo.EdbName, Source: edbInfo.Source, EdbCode: edbInfo.EdbCode, CreateTime: nowTime, ModifyTime: nowTime, RelationTime: nowTime, } addList = append(addList, tmp) if edbInfo.EdbType == 2 { childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId] if !ok1 { continue } for _, childEdbMappingId := range childEdbMappingIds { childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId] if !ok2 { continue } childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId] if !ok2 { continue } if childEdb.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL { indexCodeList = append(indexCodeList, childEdb.EdbCode) } tmp1 := &data_manage.EdbInfoRelation{ ReferObjectId: objectId, ReferObjectType: objectType, ReferObjectSubType: objectSubType, EdbInfoId: childEdb.EdbInfoId, EdbName: childEdb.EdbName, Source: childEdb.Source, EdbCode: childEdb.EdbCode, CreateTime: nowTime, ModifyTime: nowTime, RelationTime: nowTime, RelationType: 1, RootEdbInfoId: edbInfo.EdbInfoId, ChildEdbInfoId: childEdbMapping.EdbInfoId, } addList = append(addList, tmp1) refreshIds = append(refreshIds, childEdb.EdbInfoId) // todo 防止重复 } } } } // 删除不再需要的引用关系 for deleteId, _ := range deleteMap { deleteEdbInfoIds = append(deleteEdbInfoIds, deleteId) } //更新指标刷新状态为启用 err = data_manage.AddOrUpdateEdbInfoRelation(objectId, objectType, addList, deleteEdbInfoIds, refreshIds, indexCodeList) if err != nil { err = fmt.Errorf("删除不再需要的引用关系失败,%s", err.Error()) return } return } // SaveSandBoxEdbInfoRelation 添加/编辑 eta逻辑图指标引用 func SaveSandBoxEdbInfoRelation(sandBoxId int, sandBoxContent string) (err error) { edbInfoIds, err := sandbox.GetSandBoxEdbIdsByContent(sandBoxContent) if err != nil { return } if len(edbInfoIds) == 0 { return } //更新指标刷新状态为启用 err = saveEdbInfoRelation(edbInfoIds, sandBoxId, utils.EDB_RELATION_SANDBOX, 0) return } // SaveCalendarEdbInfoRelation 添加/编辑 事件日历指标引用 func SaveCalendarEdbInfoRelation(chartPermissionId int, matterDate string, editMatters, removeMatters []*fe_calendar.FeCalendarMatter) (err error) { //整理相关的事件ID matterIds := make([]int, 0) updateMatterMap := make(map[int]*fe_calendar.FeCalendarMatter) deleteMatterMap := make(map[int]struct{}) for _, matter := range removeMatters { deleteMatterMap[matter.FeCalendarMatterId] = struct{}{} matterIds = append(matterIds, matter.FeCalendarMatterId) } for _, matter := range editMatters { updateMatterMap[matter.FeCalendarMatterId] = matter matterIds = append(matterIds, matter.FeCalendarMatterId) } //删除ID,先删除 deleteObjectIds := make([]int, 0) if len(matterIds) > 0 { relationList, e := data_manage.GetEdbInfoRelationAllByReferObjectIds(matterIds, utils.EDB_RELATION_CALENDAR) if e != nil { err = fmt.Errorf("查询事件日历指标引用失败,%s", e.Error()) return } for _, relation := range relationList { if _, ok := deleteMatterMap[relation.ReferObjectId]; ok { deleteObjectIds = append(deleteObjectIds, relation.ReferObjectId) } if newMatter, ok := updateMatterMap[relation.ReferObjectId]; ok { if relation.EdbInfoId != newMatter.EdbInfoId { deleteObjectIds = append(deleteObjectIds, relation.ReferObjectId) } } } } if len(deleteObjectIds) > 0 { err = data_manage.DeleteEdbRelationByObjectIds(deleteObjectIds, utils.EDB_RELATION_CALENDAR) if err != nil { err = fmt.Errorf("删除事件日历指标引用失败,%s", err.Error()) return } } // 获取已有事项 matterOb := new(fe_calendar.FeCalendarMatter) cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, fe_calendar.FeCalendarMatterCols.ChartPermissionId, fe_calendar.FeCalendarMatterCols.MatterDate) pars := make([]interface{}, 0) pars = append(pars, chartPermissionId, matterDate) order := fmt.Sprintf(`%s ASC`, fe_calendar.FeCalendarMatterCols.Sort) matters, e := matterOb.GetItemsByCondition(cond, pars, []string{}, order) if e != nil { err = fmt.Errorf("查询事件日历事项失败,%s", e.Error()) return } // 循环查询matters edbInfoIds := make([]int, 0) refreshIds := make([]int, 0) indexCodeList := make([]string, 0) newMatterIds := make([]int, 0) for _, matter := range matters { newMatterIds = append(newMatterIds, matter.FeCalendarMatterId) edbInfoIds = append(edbInfoIds, matter.EdbInfoId) } // 查询指标信息 edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds) if e != nil { err = fmt.Errorf("查询指标信息失败,%s", e.Error()) return } // 查询计算指标信息,并且建立关联关系 // 查询间接引用的指标信息 calculateEdbListMap, calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList) if e != nil { err = fmt.Errorf("查询计算指标信息失败,%s", e.Error()) return } addEdbInfoIdMap := make(map[int]*data_manage.EdbInfo) for _, edbInfo := range edbInfoList { /*if edbInfo.Source != utils.DATA_SOURCE_WIND && edbInfo.Source != utils.DATA_SOURCE_MYSTEEL_CHEMICAL { continue }*/ refreshIds = append(refreshIds, edbInfo.EdbInfoId) addEdbInfoIdMap[edbInfo.EdbInfoId] = edbInfo if edbInfo.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL { indexCodeList = append(indexCodeList, edbInfo.EdbCode) } } relationMap := make(map[int]struct{}) if len(newMatterIds) > 0 { //查询已有的matters relationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(newMatterIds, utils.EDB_RELATION_CALENDAR) if e != nil { err = fmt.Errorf("查询事件日历指标引用失败,%s", e.Error()) return } for _, relation := range relationList { relationMap[relation.ReferObjectId] = struct{}{} } } addList := make([]*data_manage.EdbInfoRelation, 0) nowTime := time.Now() for _, matter := range matters { _, ok1 := relationMap[matter.FeCalendarMatterId] edbInfo, ok2 := addEdbInfoIdMap[matter.EdbInfoId] if !ok1 && ok2 { tmp := &data_manage.EdbInfoRelation{ ReferObjectId: matter.FeCalendarMatterId, ReferObjectType: utils.EDB_RELATION_CALENDAR, ReferObjectSubType: 0, EdbInfoId: edbInfo.EdbInfoId, EdbName: edbInfo.EdbName, Source: edbInfo.Source, EdbCode: edbInfo.EdbCode, CreateTime: nowTime, ModifyTime: nowTime, } addList = append(addList, tmp) //todo 添加指标间接引用 if edbInfo.EdbType == 2 { childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId] if !ok1 { continue } for _, childEdbMappingId := range childEdbMappingIds { childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId] if !ok2 { continue } childEdb, ok2 := calculateEdbListMap[childEdbMapping.FromEdbInfoId] if !ok2 { continue } if childEdb.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL { indexCodeList = append(indexCodeList, childEdb.EdbCode) } tmp1 := &data_manage.EdbInfoRelation{ ReferObjectId: matter.FeCalendarMatterId, ReferObjectType: utils.EDB_RELATION_CALENDAR, ReferObjectSubType: 0, EdbInfoId: childEdb.EdbInfoId, EdbName: childEdb.EdbName, Source: childEdb.Source, EdbCode: childEdb.EdbCode, CreateTime: nowTime, ModifyTime: nowTime, RelationTime: nowTime, RelationType: 1, RootEdbInfoId: edbInfo.EdbInfoId, ChildEdbInfoId: childEdbMapping.EdbInfoId, } addList = append(addList, tmp1) refreshIds = append(refreshIds, childEdb.EdbInfoId) // todo 防止重复 } } } } //更新指标刷新状态为启用 err = data_manage.AddOrUpdateEdbInfoRelationFeMatter(addList, refreshIds, indexCodeList) if err != nil { err = fmt.Errorf("添加指标引用,%s", err.Error()) return } return } // GetEdbRelationList 获取指标引用列表 func GetEdbRelationList(source int, classifyId, sysUserId, frequency, keyword, status string, startSize, pageSize int, sortParam, sortType string) (total int, list []*data_manage.BaseRelationEdbInfo, err error) { var pars []interface{} var condition string list = make([]*data_manage.BaseRelationEdbInfo, 0) isStop := -1 if status == `暂停` { isStop = 1 } else if status == "启用" { isStop = 0 } switch source { case utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND: condition += ` AND e.source = ? ` pars = append(pars, source) if isStop >= 0 { condition += " AND e.no_update = ? " pars = append(pars, isStop) } if classifyId != `` { classifyIdSlice := strings.Split(classifyId, ",") condition += ` AND e.classify_id IN (` + utils.GetOrmInReplace(len(classifyIdSlice)) + `)` pars = append(pars, classifyIdSlice) } if sysUserId != `` { sysUserIdSlice := strings.Split(sysUserId, ",") condition += ` AND e.sys_user_id IN (` + utils.GetOrmInReplace(len(sysUserIdSlice)) + `)` pars = append(pars, sysUserIdSlice) } if frequency != `` { frequencySlice := strings.Split(frequency, ",") condition += ` AND e.frequency IN (` + utils.GetOrmInReplace(len(frequencySlice)) + `)` pars = append(pars, frequencySlice) } if keyword != `` { keywordSlice := strings.Split(keyword, " ") if len(keywordSlice) > 0 { tmpConditionSlice := make([]string, 0) tmpConditionSlice = append(tmpConditionSlice, ` e.edb_name like ? or e.edb_code like ? `) pars = utils.GetLikeKeywordPars(pars, keyword, 2) for _, v := range keywordSlice { if v == ` ` || v == `` { continue } tmpConditionSlice = append(tmpConditionSlice, ` e.edb_name like ? or e.edb_code like ? `) pars = utils.GetLikeKeywordPars(pars, v, 2) } condition += ` AND (` + strings.Join(tmpConditionSlice, " or ") + `)` } else { condition += ` AND (e.edb_name like ? or e.edb_code like ? )` pars = utils.GetLikeKeywordPars(pars, keyword, 2) } } sortStr := `` if sortParam != `` { sortStr = fmt.Sprintf("%s %s,e.edb_info_id desc ", sortParam, sortType) } total, list, err = data_manage.GetEdbInfoRelationList(condition, pars, sortStr, startSize, pageSize) } 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) { //查询指标信息 //查询指标映射 //查询所有指标数据 //查询这个指标相关的mapping信息放到数组里, //将得到的指标ID信息放到数组里 hasFindMap := make(map[int]struct{}) edbInfoIdMap := make(map[int]struct{}) edbMappingList := make([]*data_manage.EdbInfoCalculateMapping, 0) edbInfoMappingRootIdsMap = make(map[int][]int, 0) edbMappingMap := make(map[int]struct{}) for _, edbInfo := range edbInfoList { if edbInfo.EdbType == 2 { edbInfoId := edbInfo.EdbInfoId edbMappingList, err = GetCalculateEdbInfoByEdbInfoId(edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, edbInfoId) if err != nil { err = fmt.Errorf(" GetCalculateEdbInfoByEdbInfoId err: %s", err.Error()) return } } } if len(edbMappingList) == 0 { return } // 查询指标信息 // 指标信息map edbInfoIdList := make([]int, 0) 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 { edbMappingListMap[v.EdbInfoCalculateMappingId] = v } } 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) { newEdbMappingList = edbMappingList _, ok := hasFindMap[edbInfoId] if ok { return } 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 } if len(edbInfoMappingList) > 0 { fromEdbInfoIdList := make([]int, 0) edbInfoMappingIdList := make([]int, 0) for _, v := range edbInfoMappingList { fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId) edbInfoMappingIdList = append(edbInfoMappingIdList, v.EdbInfoCalculateMappingId) if _, ok1 := edbInfoIdMap[v.FromEdbInfoId]; !ok1 { edbInfoIdMap[v.FromEdbInfoId] = struct{}{} } if _, ok2 := edbMappingMap[v.EdbInfoCalculateMappingId]; !ok2 { edbMappingMap[v.EdbInfoCalculateMappingId] = struct{}{} tmp := &data_manage.EdbInfoCalculateMapping{ EdbInfoCalculateMappingId: v.EdbInfoCalculateMappingId, EdbInfoId: v.EdbInfoId, Source: v.Source, SourceName: v.SourceName, EdbCode: v.EdbCode, FromEdbInfoId: v.FromEdbInfoId, FromEdbCode: v.FromEdbCode, FromEdbName: v.FromEdbName, FromSource: v.FromSource, FromSourceName: v.FromSourceName, FromTag: v.FromTag, Sort: v.Sort, CreateTime: v.CreateTime, ModifyTime: v.ModifyTime, } newEdbMappingList = append(newEdbMappingList, tmp) } 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()) return } } } hasFindMap[v.FromEdbInfoId] = struct{}{} } edbInfoMappingRootIdsMap[rootEdbInfoId] = append(edbInfoMappingRootIdsMap[rootEdbInfoId], edbInfoMappingIdList...) } hasFindMap[edbInfoId] = struct{}{} return }