base_from_fenwei_index_service.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Package data
  2. // @Author gmy 2024/8/22 9:44:00
  3. package data
  4. import (
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. )
  9. // FenWeiIndexAddValidate 指标添加校验
  10. func FenWeiIndexAddValidate(indexCodes []string) (*[]data_manage.FenWeiIndexCheckData, error) {
  11. // 根据指标编码获取指标库 指标信息
  12. edbInfos, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_FENWEI, indexCodes)
  13. if err != nil {
  14. return nil, err
  15. }
  16. var respList []data_manage.FenWeiIndexCheckData
  17. if len(edbInfos) > 0 {
  18. for _, ebdInfo := range edbInfos {
  19. respList = append(respList, data_manage.FenWeiIndexCheckData{
  20. IndexCode: ebdInfo.EdbCode,
  21. IndexName: ebdInfo.EdbName,
  22. Unit: ebdInfo.Unit,
  23. Frequency: ebdInfo.Frequency,
  24. EdbInfoId: ebdInfo.EdbInfoId,
  25. ClassifyId: ebdInfo.ClassifyId,
  26. UniqueCode: ebdInfo.UniqueCode,
  27. })
  28. }
  29. }
  30. return &respList, nil
  31. }
  32. // FenWeiIndexNameCheck 指标名称校验
  33. func FenWeiIndexNameCheck(indexNames []string, resp []*data_manage.FenWeiNameCheckResult) ([]*data_manage.FenWeiNameCheckResult, error) {
  34. // 重名校验
  35. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  36. if e != nil {
  37. return nil, e
  38. }
  39. nameExists := make(map[string]bool)
  40. for _, edbInfo := range edbList {
  41. nameExists[edbInfo.EdbName] = true
  42. }
  43. if len(nameExists) > 0 {
  44. for _, v := range resp {
  45. v.Exist = nameExists[v.IndexName]
  46. }
  47. }
  48. return resp, nil
  49. }
  50. // FenWeiIndexAdd 批量添加指标
  51. func FenWeiIndexAdd(req data_manage.FenWeiIndexAddReq, lang string) (edb *data_manage.EdbInfo, err error, errMsg string, skip bool) {
  52. if req.EdbCode == "" {
  53. err = fmt.Errorf("指标ID为空")
  54. return
  55. }
  56. defer func() {
  57. if err != nil {
  58. tips := fmt.Sprintf("BloombergIndexSource2Edb新增失败, Err: %s", err.Error())
  59. utils.FileLog.Info(tips)
  60. }
  61. }()
  62. source := utils.DATA_SOURCE_FENWEI
  63. // 是否已有指标数据
  64. dataList, e := data_manage.GetEdbDataAllByEdbCode(req.EdbCode, source, utils.DATA_SUB_SOURCE_EDB, utils.EDB_DATA_LIMIT)
  65. if e != nil {
  66. err = fmt.Errorf("获取指标数据失败, Err: %s", e.Error())
  67. return
  68. }
  69. // 新增指标数据
  70. if len(dataList) == 0 {
  71. res, e := AddEdbData(source, req.EdbCode, req.Frequency)
  72. if e != nil {
  73. err = fmt.Errorf("index_lib: 新增指标数据失败, Err: %s", e.Error())
  74. return
  75. }
  76. if res == nil {
  77. err = fmt.Errorf("index_lib: 新增指标数据失败, res nil")
  78. return
  79. }
  80. if res.Ret != 200 {
  81. err = fmt.Errorf("index_lib: 新增指标数据失败, Ret: %d", res.Ret)
  82. return
  83. }
  84. }
  85. // 是否新增过指标
  86. exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
  87. if e != nil && e.Error() != utils.ErrNoRow() {
  88. err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
  89. return
  90. }
  91. if exist != nil {
  92. skip = true
  93. return
  94. }
  95. // 开始结束时间
  96. var startDate, endDate string
  97. minMax, e := data_manage.GetEdbInfoMaxAndMinInfo(source, utils.DATA_SUB_SOURCE_EDB, req.EdbCode)
  98. if e != nil && e.Error() != utils.ErrNoRow() {
  99. err = fmt.Errorf("MinMax: 获取指标极值失败, err: %s", e.Error())
  100. return
  101. }
  102. if minMax != nil {
  103. startDate = minMax.MinDate
  104. endDate = minMax.MaxDate
  105. }
  106. // 新增指标到指标库
  107. edbInfo, e, msg, _ := EdbInfoAdd(source, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, startDate, endDate, req.AdminId, req.AdminRealName, lang)
  108. if e != nil {
  109. errMsg = msg
  110. err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
  111. return
  112. }
  113. edb = edbInfo
  114. // EdbInfoAdd方法已经新增es,这里不需要再新增???
  115. // 新增es
  116. // go AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  117. return
  118. }
  119. // GetFenWeiIndexInfo 获取指标信息-分页
  120. func GetFenWeiIndexInfo(keyWord string, classifyIdList []string, frequencyList []string) (fenWeiIndexInfoList []*data_manage.BaseFromFenweiIndex, err error) {
  121. // 获取指标
  122. var condition string
  123. var pars []interface{}
  124. if keyWord != "" {
  125. condition += ` AND CONCAT(index_name,index_code) LIKE '%` + keyWord + `%'`
  126. }
  127. if len(classifyIdList) > 0 {
  128. condition += ` AND classify_id IN (`
  129. for _, v := range classifyIdList {
  130. condition += `?,`
  131. pars = append(pars, v)
  132. }
  133. condition = condition[:len(condition)-1] + `)`
  134. }
  135. if len(frequencyList) > 0 {
  136. condition += ` AND frequency IN (`
  137. for _, v := range frequencyList {
  138. condition += `?,`
  139. pars = append(pars, v)
  140. }
  141. condition = condition[:len(condition)-1] + `)`
  142. }
  143. fenWeiIndexInfoList, err = data_manage.GetFenWeiIndexInfoPage(condition, pars)
  144. if err != nil {
  145. return
  146. }
  147. return
  148. }