base_from_ly_index_service.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Package data
  2. // @Author gmy 2024/8/12 16:44:00
  3. package data
  4. import (
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "github.com/beego/beego/v2/core/logs"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. )
  11. // GetIndexPage 获取指标分页列表
  12. func GetIndexPage(searchParam string, currentIndex, pageSize int) (*data_manage.BaseFromLyIndexPage, error) {
  13. count, err := data_manage.GetLyIndexCount(searchParam)
  14. if err != nil {
  15. return nil, err
  16. }
  17. lyIndexPage := data_manage.BaseFromLyIndexPage{}
  18. page := paging.GetPaging(currentIndex, pageSize, count)
  19. if count <= 0 {
  20. lyIndexPage.Paging = page
  21. return &lyIndexPage, nil
  22. }
  23. lyIndexList, err := data_manage.GetLyIndexPage(searchParam, currentIndex, pageSize)
  24. if err != nil {
  25. return nil, err
  26. }
  27. lyIndexPage.List = lyIndexList
  28. return &lyIndexPage, nil
  29. }
  30. // GetIndexDataPage 获取指标分页数据列表
  31. func GetIndexDataPage(indexId, startSize, pageSize int) (*data_manage.BaseFromLyDataPage, error) {
  32. count, err := data_manage.GetLyDataCountByIndexId(indexId)
  33. if err != nil {
  34. return nil, err
  35. }
  36. lyDataPage := data_manage.BaseFromLyDataPage{}
  37. page := paging.GetPaging(startSize, pageSize, count)
  38. if count <= 0 {
  39. lyDataPage.Paging = page
  40. return &lyDataPage, nil
  41. }
  42. dataList, err := data_manage.GetLyDataPageByIndexId(indexId, startSize, pageSize)
  43. if err != nil {
  44. return nil, err
  45. }
  46. lyDataPage.List = dataList
  47. return &lyDataPage, nil
  48. }
  49. // LyIndexAddValidate 指标添加校验
  50. func LyIndexAddValidate(indexCodes []string) (*[]data_manage.BaseFromLyIndex, error) {
  51. edbInfos, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_LY, indexCodes)
  52. if err != nil {
  53. return nil, err
  54. }
  55. var respList []data_manage.BaseFromLyIndex
  56. if len(edbInfos) > 0 {
  57. for _, ebdInfo := range edbInfos {
  58. respList = append(respList, data_manage.BaseFromLyIndex{
  59. IndexCode: ebdInfo.EdbCode,
  60. IndexName: ebdInfo.EdbName,
  61. Unit: ebdInfo.Unit,
  62. Frequency: ebdInfo.Frequency,
  63. })
  64. }
  65. }
  66. return &respList, nil
  67. }
  68. // LyIndexNameCheck 指标名称校验
  69. func LyIndexNameCheck(indexNames []string, resp []*data_manage.NameCheckResult) ([]*data_manage.NameCheckResult, error) {
  70. // 重名校验
  71. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  72. if e != nil {
  73. return nil, e
  74. }
  75. nameExists := make(map[string]bool)
  76. for _, edbInfo := range edbList {
  77. nameExists[edbInfo.EdbName] = true
  78. }
  79. if len(nameExists) > 0 {
  80. for _, v := range resp {
  81. v.Exist = nameExists[v.IndexName]
  82. }
  83. }
  84. return resp, nil
  85. }
  86. type LyIndexAddReq struct {
  87. EdbCode string `description:"指标编码"`
  88. EdbName string `description:"指标名称"`
  89. Frequency string `description:"频度"`
  90. Unit string `description:"单位"`
  91. ClassifyId int `description:"分类ID"`
  92. AdminId int `description:"管理员ID"`
  93. AdminRealName string `description:"管理员名称"`
  94. }
  95. // LyIndexAdd 批量添加指标
  96. func LyIndexAdd(req LyIndexAddReq, lang string) (edb *data_manage.EdbInfo, err error, errMsg string, skip bool) {
  97. if req.EdbCode == "" {
  98. err = fmt.Errorf("指标ID为空")
  99. return
  100. }
  101. defer func() {
  102. if err != nil {
  103. tips := fmt.Sprintf("BloombergIndexSource2Edb新增失败, Err: %s", err.Error())
  104. logs.Info(tips)
  105. utils.FileLog.Info(tips)
  106. }
  107. }()
  108. source := utils.DATA_SOURCE_LY
  109. // 是否已有指标数据
  110. dataList, e := data_manage.GetEdbDataAllByEdbCode(req.EdbCode, source, utils.DATA_SUB_SOURCE_EDB, utils.EDB_DATA_LIMIT)
  111. if e != nil {
  112. err = fmt.Errorf("获取指标数据失败, Err: %s", e.Error())
  113. return
  114. }
  115. // 新增指标数据
  116. if len(dataList) == 0 {
  117. res, e := AddEdbData(source, req.EdbCode, req.Frequency)
  118. if e != nil {
  119. err = fmt.Errorf("index_lib: 新增指标数据失败, Err: %s", e.Error())
  120. return
  121. }
  122. if res == nil {
  123. err = fmt.Errorf("index_lib: 新增指标数据失败, res nil")
  124. return
  125. }
  126. if res.Ret != 200 {
  127. err = fmt.Errorf("index_lib: 新增指标数据失败, Ret: %d", res.Ret)
  128. return
  129. }
  130. }
  131. // 是否新增过指标
  132. exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
  133. if e != nil && e.Error() != utils.ErrNoRow() {
  134. err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
  135. return
  136. }
  137. if exist != nil {
  138. skip = true
  139. return
  140. }
  141. // 开始结束时间
  142. var startDate, endDate string
  143. minMax, e := data_manage.GetEdbInfoMaxAndMinInfo(source, utils.DATA_SUB_SOURCE_EDB, req.EdbCode)
  144. if e != nil && e.Error() != utils.ErrNoRow() {
  145. err = fmt.Errorf("MinMax: 获取指标极值失败, err: %s", e.Error())
  146. return
  147. }
  148. if minMax != nil {
  149. startDate = minMax.MinDate
  150. endDate = minMax.MaxDate
  151. }
  152. // 新增指标到指标库
  153. 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)
  154. if e != nil {
  155. errMsg = msg
  156. err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
  157. return
  158. }
  159. edb = edbInfo
  160. // 标记原始指标为已添加
  161. err = data_manage.UpdateLyIndexEdbExist(req.EdbCode)
  162. if err != nil {
  163. err = fmt.Errorf("BaseIndex: 标记已添加指标库失败, err: %s", err.Error())
  164. return
  165. }
  166. // todo EdbInfoAdd方法已经新增es,这里不需要再新增???
  167. // 新增es
  168. // go AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  169. return
  170. }