123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- package services
- import (
- "errors"
- "eta/eta_forum_hub/models"
- "eta/eta_forum_hub/models/mgodb"
- "eta/eta_forum_hub/services/elastic"
- "eta/eta_forum_hub/utils"
- "fmt"
- "strconv"
- "time"
- )
- // BatchAddOrUpdateEdbInfo 添加批量添加指标到指标库
- func BatchAddOrUpdateEdbInfo(edbList []*models.EdbInfo, edbMapping []*models.EdbInfoCalculateMapping, sysUserId int, sysUserRealName string) (newList []*models.EdbInfo, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- edbCodeMap := make(map[string]*models.EdbInfo)
- for _, v := range edbList {
- tmp, e, msg, _ := AddOrUpdateEdbInfo(v, sysUserId, sysUserRealName)
- if e != nil {
- err = fmt.Errorf("添加指标失败:%s,%s", e.Error(), msg)
- errMsg = "添加指标失败"
- return
- }
- newList = append(newList, tmp)
- edbCodeMap[tmp.EdbCode] = tmp
- }
- //批量添加指标的mapping信息
- calculateMappingItemList := make([]*models.EdbInfoCalculateMapping, 0)
- for _, v := range edbMapping {
- edbInfo, ok := edbCodeMap[v.EdbCode]
- if !ok {
- continue
- }
- fromEdbInfo, ok := edbCodeMap[v.FromEdbCode]
- if !ok {
- continue
- }
- calculateMappingItem := new(models.EdbInfoCalculateMapping)
- calculateMappingItem.CreateTime = time.Now()
- calculateMappingItem.ModifyTime = time.Now()
- calculateMappingItem.Sort = v.Sort
- calculateMappingItem.EdbCode = edbInfo.EdbCode
- calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
- calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
- calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
- calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
- calculateMappingItem.FromSource = fromEdbInfo.Source
- calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
- calculateMappingItem.FromTag = v.FromTag
- calculateMappingItem.Source = edbInfo.Source
- calculateMappingItem.SourceName = edbInfo.SourceName
- calculateMappingItem.FromSubSource = edbInfo.SubSource
- calculateMappingItemList = append(calculateMappingItemList, calculateMappingItem)
- }
- if len(calculateMappingItemList) > 0 {
- err = models.AddEdbInfoCalculateMappingMulti(calculateMappingItemList)
- if err != nil {
- errMsg = "指标映射关系添加失败"
- err = fmt.Errorf("指标映射关系添加失败, %s", err.Error())
- return
- }
- }
- return
- }
- // AddOrUpdateEdbInfo 添加指标到指标库
- func AddOrUpdateEdbInfo(edbItem *models.EdbInfo, sysUserId int, sysUserRealName string) (edbInfo *models.EdbInfo, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- //判断指标是否存在
- edbInfo, err = models.GetEdbInfoByEdbCode(edbItem.Source, edbItem.EdbCode)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- err = nil
- } else {
- errMsg = "判断指标名称是否存在失败"
- err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
- return
- }
- }
- var edbInfoId int64
- // 指标已存在
- if edbInfo != nil && edbInfo.EdbInfoId > 0 {
- edbInfoId = int64(edbInfo.EdbInfoId)
- // 更新指标信息
- edbInfo.Source = edbItem.Source
- edbInfo.SourceName = edbItem.SourceName
- edbInfo.EdbType = edbItem.EdbType
- edbInfo.EdbCode = edbItem.EdbCode
- edbInfo.EdbName = edbItem.EdbName
- edbInfo.EdbNameSource = edbItem.EdbNameSource
- edbInfo.Frequency = edbItem.Frequency
- edbInfo.Unit = edbItem.Unit
- edbInfo.StartDate = edbItem.StartDate
- edbInfo.EndDate = edbItem.EndDate
- edbInfo.ClassifyId = edbItem.ClassifyId
- edbInfo.SysUserId = sysUserId
- edbInfo.SysUserRealName = sysUserRealName
- edbInfo.ModifyTime = time.Now()
- edbInfo.ServerUrl = edbItem.ServerUrl
- edbInfo.Sort = edbItem.Sort
- edbInfo.DataDateType = edbItem.DataDateType
- edbInfo.TerminalCode = edbItem.TerminalCode
- edbInfo.SourceIndexName = edbItem.SourceIndexName
- edbInfo.MaxValue = edbItem.MaxValue
- edbInfo.MinValue = edbItem.MinValue
- edbInfo.EdbInfoType = edbItem.EdbInfoType
- edbInfo.LatestDate = edbItem.LatestDate
- edbInfo.LatestValue = edbItem.LatestValue
- edbInfo.CalculateFormula = edbItem.CalculateFormula
- edbInfo.ManualSave = edbItem.ManualSave
- edbInfo.MoveType = edbItem.MoveType
- edbInfo.MoveFrequency = edbItem.MoveFrequency
- edbInfo.NoUpdate = edbItem.NoUpdate
- edbInfo.EdbNameEn = edbItem.EdbNameEn
- edbInfo.UnitEn = edbItem.UnitEn
- edbInfo.ChartImage = edbItem.ChartImage
- edbInfo.Calendar = edbItem.Calendar
- edbInfo.EmptyType = edbItem.EmptyType
- edbInfo.MaxEmptyType = edbItem.MaxEmptyType
- edbInfo.DataUpdateTime = edbItem.DataUpdateTime
- edbInfo.ErDataUpdateDate = edbItem.ErDataUpdateDate
- edbInfo.EndValue = edbItem.EndValue
- edbInfo.SubSource = edbItem.SubSource
- edbInfo.SubSourceName = edbItem.SubSourceName
- edbInfo.IndicatorCode = edbItem.IndicatorCode
- edbInfo.StockCode = edbItem.StockCode
- edbInfo.Extra = edbItem.Extra
- err = edbInfo.Update([]string{})
- if err != nil {
- errMsg = "保存失败"
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
- //同时删除指标的映射信息
- err = models.DeleteEdbInfoMapping(edbInfoId)
- if err != nil {
- errMsg = "删除指标映射信息失败"
- err = errors.New("删除指标映射信息失败,Err:" + err.Error())
- return
- }
- } else {
- edbInfo = new(models.EdbInfo)
- edbInfo.Source = edbItem.Source
- edbInfo.SourceName = edbItem.SourceName
- edbInfo.EdbType = edbItem.EdbType
- edbInfo.EdbCode = edbItem.EdbCode
- edbInfo.EdbName = edbItem.EdbName
- edbInfo.EdbNameSource = edbItem.EdbNameSource
- edbInfo.Frequency = edbItem.Frequency
- edbInfo.Unit = edbItem.Unit
- edbInfo.StartDate = edbItem.StartDate
- edbInfo.EndDate = edbItem.EndDate
- edbInfo.ClassifyId = edbItem.ClassifyId
- edbInfo.SysUserId = sysUserId
- edbInfo.SysUserRealName = sysUserRealName
- edbInfo.CreateTime = time.Now()
- edbInfo.ModifyTime = time.Now()
- edbInfo.ServerUrl = edbItem.ServerUrl
- edbInfo.Sort = edbItem.Sort
- edbInfo.DataDateType = edbItem.DataDateType
- edbInfo.TerminalCode = edbItem.TerminalCode
- edbInfo.SourceIndexName = edbItem.SourceIndexName
- //timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- //edbInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
- edbInfo.UniqueCode = edbItem.UniqueCode
- edbInfo.MaxValue = edbItem.MaxValue
- edbInfo.MinValue = edbItem.MinValue
- edbInfo.EdbInfoType = edbItem.EdbInfoType
- edbInfo.LatestDate = edbItem.LatestDate
- edbInfo.LatestValue = edbItem.LatestValue
- edbInfo.CalculateFormula = edbItem.CalculateFormula
- edbInfo.ManualSave = edbItem.ManualSave
- edbInfo.MoveType = edbItem.MoveType
- edbInfo.MoveFrequency = edbItem.MoveFrequency
- edbInfo.NoUpdate = edbItem.NoUpdate
- edbInfo.EdbNameEn = edbItem.EdbNameEn
- edbInfo.UnitEn = edbItem.UnitEn
- edbInfo.ChartImage = edbItem.ChartImage
- edbInfo.Calendar = edbItem.Calendar
- edbInfo.EmptyType = edbItem.EmptyType
- edbInfo.MaxEmptyType = edbItem.MaxEmptyType
- edbInfo.DataUpdateTime = edbItem.DataUpdateTime
- edbInfo.ErDataUpdateDate = edbItem.ErDataUpdateDate
- edbInfo.EndValue = edbItem.EndValue
- edbInfo.SubSource = edbItem.SubSource
- edbInfo.SubSourceName = edbItem.SubSourceName
- edbInfo.IndicatorCode = edbItem.IndicatorCode
- edbInfo.StockCode = edbItem.StockCode
- edbInfo.Extra = edbItem.Extra
- edbInfoId, err = models.AddEdbInfo(edbInfo)
- if err != nil {
- errMsg = "保存失败"
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
- edbInfo.EdbInfoId = int(edbInfoId)
- }
- if edbInfo.EdbType == 1 {
- err = mgodb.ModifyEdbDataEdbInfoId(edbInfoId, edbInfo.EdbCode)
- if err != nil {
- errMsg = "保存失败"
- err = errors.New("更新指标数据失败,Err:" + err.Error())
- return
- }
- } else {
- err = mgodb.ModifyEdbCalculateDataEdbInfoId(edbInfoId, edbInfo.EdbCode)
- if err != nil {
- errMsg = "保存失败"
- err = errors.New("更新指标数据失败,Err:" + err.Error())
- return
- }
- }
- // 更新es
- go AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
- return
- }
- // 批量删除指标,
- func BatchDeleteEdbInfo(edbInfoIds []int) (err error, errMsg string) {
- for _, v := range edbInfoIds {
- //查询单个指标是否允许删除
- err, errMsg = DeleteEdbInfo(v)
- if err != nil {
- errMsg = "删除指标失败"
- return
- }
- }
- return
- }
- // 删除单个指标
- func DeleteEdbInfo(edbInfoId int) (err error, errMsg string) {
- //判断指标是否存在
- info, err := models.GetEdbInfoById(edbInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- err = nil
- return
- } else {
- errMsg = "判断指标名称是否存在失败"
- err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
- return
- }
- }
- //判断指标是否用于作图,如果用于作图,则不可删除
- chartCount, tmpErr := models.GetChartEdbMappingCount(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否被用于作图失败,Err:" + tmpErr.Error())
- return
- }
- if chartCount > 0 {
- errMsg = "当前指标已用作画图,不可删除"
- return
- }
- //判断指标是否用作其他指标的计算
- calculateCount, tmpErr := models.GetEdbInfoCalculateMappingCount(edbInfoId)
- if tmpErr != nil {
- errMsg = "删除失败"
- err = errors.New("判断指标是否被用于计算失败,GetEdbInfoCalculateCount Err:" + tmpErr.Error())
- return
- }
- if calculateCount > 0 {
- errMsg = "当前指标已用作,指标运算,不可删除"
- return
- }
- // 删除指标,删除映射关系,删除指标数据
- err = models.DeleteEdbInfo(edbInfoId)
- if err != nil {
- errMsg = "删除失败"
- err = errors.New("删除指标失败,Err:" + err.Error())
- return
- }
- if info.EdbType == 1 {
- err = mgodb.DeleteEdbInfoDataByEdbInfoId(edbInfoId)
- if err != nil {
- errMsg = "删除失败"
- err = errors.New("删除指标数据失败,Err:" + err.Error())
- return
- }
- } else {
- err = mgodb.DeleteEdbInfoCalculateDataByEdbInfoId(edbInfoId)
- if err != nil {
- errMsg = "删除失败"
- err = errors.New("删除指标数据失败,Err:" + err.Error())
- return
- }
- }
- // 删除es中的数据
- go DeleteEdbInfoToEs(edbInfoId)
- return
- }
- // AddOrEditEdbInfoToEs 添加/修改ES中的指标
- func AddOrEditEdbInfoToEs(edbInfoId int) {
- //添加es
- itemInfo, _ := models.GetEdbInfoByCondition("AND edb_info_id=?", []interface{}{edbInfoId})
- go elastic.EsAddOrEditEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(itemInfo.EdbInfoId), itemInfo)
- }
|