base_from_ly_index_service.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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(classifyId string, searchParam string, currentIndex, pageSize int) (*data_manage.BaseFromLyIndexPage, error) {
  13. count, err := data_manage.GetLyIndexCount(classifyId, 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(classifyId, searchParam, currentIndex, pageSize)
  24. if err != nil {
  25. return nil, err
  26. }
  27. // 获取指标编码列表
  28. var indexCodes []string
  29. for _, v := range lyIndexList {
  30. indexCodes = append(indexCodes, v.IndexCode)
  31. }
  32. // 获取指标数据
  33. lyIndexData, err := data_manage.GetLyLastUpdateTimeLastByIndexCode(indexCodes)
  34. if err != nil {
  35. return nil, err
  36. }
  37. // 将lyIndexData转换为map
  38. lyIndexDataMap := make(map[string]*data_manage.BaseFromLyData)
  39. for _, v := range lyIndexData {
  40. lyIndexDataMap[v.IndexCode] = v
  41. }
  42. lyData, err := data_manage.GetLyLastDataTimeByIndexCode(indexCodes)
  43. if err != nil {
  44. return nil, err
  45. }
  46. // 将lyData转换为map
  47. lyDataMap := make(map[string]*data_manage.BaseFromLyData)
  48. for _, v := range lyData {
  49. lyDataMap[v.IndexCode] = v
  50. }
  51. // 查询指标库是否已添加 不做另外补偿维护index表中的edb_exist字段,直接去查,因为维护会改变指标库指标添加代码结构
  52. edbInfoList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_LY, indexCodes)
  53. if err != nil {
  54. return nil, err
  55. }
  56. edbInfoMap := make(map[string]*data_manage.EdbInfo)
  57. for _, v := range edbInfoList {
  58. edbInfoMap[v.EdbCode] = v
  59. }
  60. if len(lyIndexData) > 0 {
  61. for _, v := range lyIndexList {
  62. if lyIndexDataMap[v.IndexCode] != nil {
  63. toYmd := utils.TimeFormatToYmd(lyIndexDataMap[v.IndexCode].ModifyTime)
  64. v.ModifyTimeMax = toYmd
  65. }
  66. if lyDataMap[v.IndexCode] != nil {
  67. v.Value = lyDataMap[v.IndexCode].Value
  68. }
  69. if edbInfoMap[v.IndexCode] != nil {
  70. v.EdbExist = utils.IS_YES
  71. }
  72. }
  73. }
  74. lyIndexPage.List = lyIndexList
  75. lyIndexPage.Paging = page
  76. return &lyIndexPage, nil
  77. }
  78. // GetIndexDataPage 获取指标分页数据列表
  79. func GetIndexDataPage(indexId, startSize, pageSize int) (*data_manage.BaseFromLyDataPage, error) {
  80. count, err := data_manage.GetLyDataCountByIndexId(indexId)
  81. if err != nil {
  82. return nil, err
  83. }
  84. lyDataPage := data_manage.BaseFromLyDataPage{}
  85. page := paging.GetPaging(startSize, pageSize, count)
  86. if count <= 0 {
  87. lyDataPage.Paging = page
  88. return &lyDataPage, nil
  89. }
  90. dataList, err := data_manage.GetLyDataPageByIndexId(indexId, startSize, pageSize)
  91. if err != nil {
  92. return nil, err
  93. }
  94. lyDataPage.List = dataList
  95. lyDataPage.Paging = page
  96. return &lyDataPage, nil
  97. }
  98. // LyIndexAddValidate 指标添加校验
  99. func LyIndexAddValidate(indexCodes []string) (*[]data_manage.IndexCheckData, error) {
  100. // 根据指标编码获取指标库 指标信息
  101. edbInfos, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_LY, indexCodes)
  102. if err != nil {
  103. return nil, err
  104. }
  105. var respList []data_manage.IndexCheckData
  106. if len(edbInfos) > 0 {
  107. for _, ebdInfo := range edbInfos {
  108. respList = append(respList, data_manage.IndexCheckData{
  109. IndexCode: ebdInfo.EdbCode,
  110. IndexName: ebdInfo.EdbName,
  111. Unit: ebdInfo.Unit,
  112. Frequency: ebdInfo.Frequency,
  113. EdbInfoId: ebdInfo.EdbInfoId,
  114. ClassifyId: ebdInfo.ClassifyId,
  115. UniqueCode: ebdInfo.UniqueCode,
  116. })
  117. }
  118. }
  119. return &respList, nil
  120. }
  121. // LyIndexNameCheck 指标名称校验
  122. func LyIndexNameCheck(indexNames []string, resp []*data_manage.NameCheckResult) ([]*data_manage.NameCheckResult, error) {
  123. // 重名校验
  124. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  125. if e != nil {
  126. return nil, e
  127. }
  128. nameExists := make(map[string]bool)
  129. for _, edbInfo := range edbList {
  130. nameExists[edbInfo.EdbName] = true
  131. }
  132. if len(nameExists) > 0 {
  133. for _, v := range resp {
  134. v.Exist = nameExists[v.IndexName]
  135. }
  136. }
  137. return resp, nil
  138. }
  139. type LyIndexAddReq struct {
  140. EdbCode string `description:"指标编码"`
  141. EdbName string `description:"指标名称"`
  142. Frequency string `description:"频度"`
  143. Unit string `description:"单位"`
  144. ClassifyId int `description:"分类ID"`
  145. AdminId int `description:"管理员ID"`
  146. AdminRealName string `description:"管理员名称"`
  147. }
  148. // LyIndexAdd 批量添加指标
  149. func LyIndexAdd(req LyIndexAddReq, lang string) (edb *data_manage.EdbInfo, err error, errMsg string, skip bool) {
  150. if req.EdbCode == "" {
  151. err = fmt.Errorf("指标ID为空")
  152. return
  153. }
  154. defer func() {
  155. if err != nil {
  156. tips := fmt.Sprintf("BloombergIndexSource2Edb新增失败, Err: %s", err.Error())
  157. logs.Info(tips)
  158. utils.FileLog.Info(tips)
  159. }
  160. }()
  161. source := utils.DATA_SOURCE_LY
  162. // 是否已有指标数据
  163. dataList, e := data_manage.GetEdbDataAllByEdbCode(req.EdbCode, source, utils.DATA_SUB_SOURCE_EDB, utils.EDB_DATA_LIMIT)
  164. if e != nil {
  165. err = fmt.Errorf("获取指标数据失败, Err: %s", e.Error())
  166. return
  167. }
  168. // 新增指标数据
  169. if len(dataList) == 0 {
  170. res, e := AddEdbData(source, req.EdbCode, req.Frequency)
  171. if e != nil {
  172. err = fmt.Errorf("index_lib: 新增指标数据失败, Err: %s", e.Error())
  173. return
  174. }
  175. if res == nil {
  176. err = fmt.Errorf("index_lib: 新增指标数据失败, res nil")
  177. return
  178. }
  179. if res.Ret != 200 {
  180. err = fmt.Errorf("index_lib: 新增指标数据失败, Ret: %d", res.Ret)
  181. return
  182. }
  183. }
  184. // 是否新增过指标
  185. exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
  186. if e != nil && e.Error() != utils.ErrNoRow() {
  187. err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
  188. return
  189. }
  190. if exist != nil {
  191. skip = true
  192. return
  193. }
  194. // 开始结束时间
  195. var startDate, endDate string
  196. minMax, e := data_manage.GetEdbInfoMaxAndMinInfo(source, utils.DATA_SUB_SOURCE_EDB, req.EdbCode)
  197. if e != nil && e.Error() != utils.ErrNoRow() {
  198. err = fmt.Errorf("MinMax: 获取指标极值失败, err: %s", e.Error())
  199. return
  200. }
  201. if minMax != nil {
  202. startDate = minMax.MinDate
  203. endDate = minMax.MaxDate
  204. }
  205. // 新增指标到指标库
  206. 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)
  207. if e != nil {
  208. errMsg = msg
  209. err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
  210. return
  211. }
  212. edb = edbInfo
  213. // 标记原始指标为已添加
  214. /*err = data_manage.UpdateLyIndexEdbExist(req.EdbCode, utils.IS_YES)
  215. if err != nil {
  216. err = fmt.Errorf("BaseIndex: 标记已添加指标库失败, err: %s", err.Error())
  217. return
  218. }*/
  219. // EdbInfoAdd方法已经新增es,这里不需要再新增???
  220. // 新增es
  221. // go AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  222. return
  223. }