123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708 |
- package data
- import (
- "eta_gn/eta_api/models/data_manage"
- excelModel "eta_gn/eta_api/models/data_manage/excel"
- "eta_gn/eta_api/models/fe_calendar"
- "eta_gn/eta_api/services/alarm_msg"
- "eta_gn/eta_api/services/sandbox"
- "eta_gn/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)
- // 查询指标信息
- edbInfoListTmp, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
- if e != nil {
- err = fmt.Errorf("查询指标信息失败,%s", e.Error())
- return
- }
- // 过滤预测指标
- edbInfoList := make([]*data_manage.EdbInfo, 0)
- for _, v := range edbInfoListTmp {
- if v.EdbInfoType == 0 {
- edbInfoList = append(edbInfoList, v)
- }
- }
- // 查询计算指标信息,并且建立关联关系
- // 查询间接引用的指标信息
- 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,
- }
- tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
- addList = append(addList, tmp)
- if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
- childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
- if !ok1 {
- continue
- }
- 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: objectId,
- ReferObjectType: objectType,
- ReferObjectSubType: objectSubType,
- EdbInfoId: childEdbMapping.FromEdbInfoId,
- EdbName: childEdbMapping.FromEdbName,
- Source: childEdbMapping.FromSource,
- EdbCode: childEdbMapping.FromEdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: nowTime,
- RelationType: 1,
- RootEdbInfoId: edbInfo.EdbInfoId,
- ChildEdbInfoId: childEdbMapping.EdbInfoId,
- RelationCode: tmp.RelationCode,
- }
- addList = append(addList, tmp1)
- refreshIds = append(refreshIds, childEdbMapping.FromEdbInfoId)
- // 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) {
- defer func() {
- if err != nil {
- err = fmt.Errorf("添加/编辑 事件日历指标引用失败,%s", err.Error())
- go alarm_msg.SendAlarmMsg(err.Error(), 3)
- }
- }()
- //整理相关的事件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)
- }
- // 查询指标信息
- edbInfoListTmp, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
- if e != nil {
- err = fmt.Errorf("查询指标信息失败,%s", e.Error())
- return
- }
- // 过滤预测指标
- edbInfoList := make([]*data_manage.EdbInfo, 0)
- for _, v := range edbInfoListTmp {
- if v.EdbInfoType == 0 {
- edbInfoList = append(edbInfoList, v)
- }
- }
- // 查询计算指标信息,并且建立关联关系
- // 查询间接引用的指标信息
- 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,
- RelationTime: matter.CreateTime,
- }
- tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
- addList = append(addList, tmp)
- //添加指标间接引用
- if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
- childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
- if !ok1 {
- continue
- }
- 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: matter.FeCalendarMatterId,
- ReferObjectType: utils.EDB_RELATION_CALENDAR,
- ReferObjectSubType: 0,
- EdbInfoId: childEdbMapping.FromEdbInfoId,
- EdbName: childEdbMapping.FromEdbName,
- Source: childEdbMapping.FromSource,
- EdbCode: childEdbMapping.FromEdbCode,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- RelationTime: nowTime,
- RelationType: 1,
- RootEdbInfoId: edbInfo.EdbInfoId,
- ChildEdbInfoId: childEdbMapping.EdbInfoId,
- RelationCode: tmp.RelationCode,
- }
- addList = append(addList, tmp1)
- refreshIds = append(refreshIds, childEdbMapping.FromEdbInfoId)
- // todo 防止重复
- }
- }
- }
- }
- //更新指标刷新状态为启用
- err = data_manage.AddOrUpdateEdbInfoRelationMulti(addList, refreshIds, indexCodeList)
- if err != nil {
- err = fmt.Errorf("添加指标引用,%s", err.Error())
- return
- }
- return
- }
- // GetEdbRelationList 获取指标引用列表
- func GetEdbRelationList(source, edbType 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
- switch status {
- case `暂停`:
- isStop = 1
- case `启用`:
- isStop = 0
- case `供应商停用`:
- isStop = 3
- }
- // 关联表语句
- var addFieldStr, joinTableStr string
- switch source {
- case utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND:
- condition += ` AND e.source = ? `
- pars = append(pars, source)
- }
- if edbType == 2 { //计算指标
- condition += ` AND e.edb_type = ? AND e.edb_info_type = 0`
- pars = append(pars, edbType)
- }
- switch isStop {
- case -1:
- // 供应商停用
- if source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
- joinTableStr = ` LEFT JOIN base_from_mysteel_chemical_index z ON e.edb_code = z.index_code `
- addFieldStr = ` ,z.is_supplier_stop `
- }
- case 0, 1:
- condition += " AND e.no_update = ? "
- pars = append(pars, isStop)
- // 供应商停用
- if source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
- condition += " AND z.is_supplier_stop = ? "
- pars = append(pars, 0)
- joinTableStr = ` LEFT JOIN base_from_mysteel_chemical_index z ON e.edb_code = z.index_code `
- addFieldStr = ` ,z.is_supplier_stop `
- }
- case 3:
- // 供应商停用
- if source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
- condition += " AND z.is_supplier_stop = ? "
- pars = append(pars, 1)
- joinTableStr = ` LEFT JOIN base_from_mysteel_chemical_index z ON e.edb_code = z.index_code `
- addFieldStr = ` ,z.is_supplier_stop `
- }
- }
- 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, addFieldStr, joinTableStr, sortStr, startSize, pageSize)
- return
- }
- // 查找当前计算指标的所有溯源指标
- 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 && v.EdbInfoType == 0 { //普通计算指标,排除预算指标
- 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信息放到数组里,
- //将得到的指标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 && edbInfo.EdbInfoType == 0 {
- edbInfoId := edbInfo.EdbInfoId
- edbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, 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)
- }
- edbMappingListMap = make(map[int]*data_manage.EdbInfoCalculateMapping)
- if len(edbMappingList) > 0 {
- for _, v := range edbMappingList {
- edbMappingListMap[v.EdbInfoCalculateMappingId] = v
- }
- }
- return
- }
- // 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 {
- return
- }
- if _, ok1 := edbInfoIdMap[edbInfoId]; !ok1 {
- edbInfoIdMap[edbInfoId] = struct{}{}
- }
- 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)
- 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, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, rootEdbInfoId)
- if err != nil {
- err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", err.Error())
- return
- }
- }
- }
- hasFindMap[v.FromEdbInfoId] = struct{}{}
- }
- edbInfoMappingRootIdsMap[rootEdbInfoId] = append(edbInfoMappingRootIdsMap[rootEdbInfoId], edbInfoMappingIdList...)
- }
- return
- }
- // SaveExcelEdbInfoRelation 添加/编辑表格时同步修改指标引用关联记录
- func SaveExcelEdbInfoRelation(excelInfoId, source int, addChildExcel bool) (err error) {
- defer func() {
- if err != nil {
- err = fmt.Errorf("添加/编辑表格时同步修改指标引用关联记录失败,%s", err.Error())
- go alarm_msg.SendAlarmMsg(err.Error(), 3)
- }
- }()
- //查询和指标相关的时间序列表格和混合表格
- 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 _, v := range mappingList {
- edbInfoIds = append(edbInfoIds, v.EdbInfoId)
- }
- //查询指标引用关联记录
- //更新指标刷新状态为启用
- if len(edbInfoIds) == 0 {
- return
- }
- err = saveEdbInfoRelation(edbInfoIds, excelInfoId, utils.EDB_RELATION_TABLE, source)
- return
- }
- // GetCalculateEdbByFromEdbInfo 找到依赖于该基础指标的所有计算指标
- func GetCalculateEdbByFromEdbInfo(edbInfoIds []int, calculateEdbIds []int, hasFind map[int]struct{}) (newCalculateEdbIds []int, err error) {
- if len(edbInfoIds) == 0 {
- return
- }
- newCalculateEdbIds = calculateEdbIds
- newEdbInfoIds := make([]int, 0)
- for _, v := range edbInfoIds {
- if _, ok := hasFind[v]; ok {
- continue
- }
- newEdbInfoIds = append(newEdbInfoIds, v)
- }
- if len(newEdbInfoIds) == 0 {
- return
- }
- var condition string
- var pars []interface{}
- // 关联指标
- condition += ` AND b.from_edb_info_id in (` + utils.GetOrmInReplace(len(newEdbInfoIds)) + `)`
- pars = append(pars, newEdbInfoIds)
- //获取关联图表列表
- list, err := data_manage.GetRelationEdbInfoListMappingByCondition(condition, pars)
- if err != nil && err.Error() != utils.ErrNoRow() {
- err = fmt.Errorf("获取关联指标信息失败,Err:%s", err.Error())
- return
- }
- calculateEdbIdsTmp := make([]int, 0)
- for _, mapping := range list {
- if mapping.EdbType == 2 && mapping.EdbInfoType == 0 { // 如果指标库里的计算指标,则加入,否则继续找
- newCalculateEdbIds = append(newCalculateEdbIds, mapping.EdbInfoId)
- calculateEdbIdsTmp = append(calculateEdbIdsTmp, mapping.EdbInfoId)
- }
- }
- for _, v := range newEdbInfoIds {
- hasFind[v] = struct{}{}
- }
- if len(calculateEdbIdsTmp) > 0 {
- newCalculateEdbIds, err = GetCalculateEdbByFromEdbInfo(calculateEdbIdsTmp, newCalculateEdbIds, hasFind)
- if err != nil {
- return
- }
- }
- return
- }
|