123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- 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
- }
- // 只统计钢联化工和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)
- deleteRelationIds := 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,
- }
- addList = append(addList, tmp)
- }
- }
- // 删除不再需要的引用关系
- for deleteId, _ := range deleteMap {
- deleteRelationIds = append(deleteRelationIds, deleteId)
- }
- //更新指标刷新状态为启用
- err = data_manage.AddOrUpdateEdbInfoRelation(addList, deleteRelationIds, 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,先删除
- deleteRelationIds := make([]int, 0)
- if len(matterIds) > 0 {
- relationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(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 {
- deleteRelationIds = append(deleteRelationIds, relation.EdbInfoRelationId)
- }
- if newMatter, ok := updateMatterMap[relation.ReferObjectId]; ok {
- if relation.EdbInfoId != newMatter.EdbInfoId {
- deleteRelationIds = append(deleteRelationIds, relation.EdbInfoRelationId)
- }
- }
- }
- }
- if len(deleteRelationIds) > 0 {
- err = data_manage.DeleteEdbRelationByObjectIds(deleteRelationIds, utils.EDB_RELATION_CALENDAR)
- if err != nil {
- err = fmt.Errorf("删除事件日历指标引用失败,%s", err.Error())
- return
- }
- deleteRelationIds = make([]int, 0)
- }
- // 获取已有事项
- 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
- }
- // 只统计钢联化工和wind来源的指标
- 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)
- }
- }
- //更新指标刷新状态为启用
- err = data_manage.AddOrUpdateEdbInfoRelation(addList, deleteRelationIds, 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
- }
|