chart_info_elastic.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package data
  2. import (
  3. "eta_gn/eta_api/models/data_manage"
  4. "eta_gn/eta_api/services/elastic"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. )
  10. // AddAllChartInfo
  11. // @Description: 更新es中的所有的图表数据
  12. // @author: Roc
  13. // @datetime 2024-11-20 13:11:35
  14. func AddAllChartInfo() {
  15. allList, err := data_manage.GetChartInfoAllList()
  16. if err != nil {
  17. fmt.Println("GetArticleAll Err:", err.Error())
  18. return
  19. }
  20. total := len(allList)
  21. for k, v := range allList {
  22. EsAddOrEditChartInfo(v.ChartInfoId)
  23. fmt.Println("剩余", total-k-1, "条图表数据,当前图表id:", v.ChartInfoId)
  24. }
  25. }
  26. // EsAddOrEditChartInfo 新增和修改ES中的图表数据
  27. func EsAddOrEditChartInfo(chartInfoId int) {
  28. var err error
  29. defer func() {
  30. if err != nil {
  31. fmt.Println("新增和修改ES中的图表数据失败:", err.Error())
  32. }
  33. }()
  34. itemInfo, _ := data_manage.GetChartInfoById(chartInfoId)
  35. //添加es
  36. err = elastic.EsAddOrEditChartInfoData(utils.CHART_INDEX_NAME, strconv.Itoa(itemInfo.ChartInfoId), itemInfo)
  37. return
  38. }
  39. // EsDeleteChartInfo 删除ES中的图表数据
  40. func EsDeleteChartInfo(chartInfoId int) {
  41. var err error
  42. defer func() {
  43. if err != nil {
  44. fmt.Println("删除ES中的图表数据失败:", err.Error())
  45. }
  46. }()
  47. //添加es
  48. err = elastic.EsDeleteDataV2(utils.CHART_INDEX_NAME, strconv.Itoa(chartInfoId))
  49. return
  50. }
  51. // EsSearchChartInfo 搜索图表信息
  52. func EsSearchChartInfo(keyword string, showSysId int, sourceList []int, noPermissionChartIdList []int, startSize, pageSize int) (list []*data_manage.ChartInfo, total int64, err error) {
  53. list, total, err = elastic.SearchChartInfoData(utils.CHART_INDEX_NAME, keyword, showSysId, sourceList, noPermissionChartIdList, startSize, pageSize)
  54. return
  55. }
  56. // EsAddOrEditMyChartInfoByChartInfoId 根据图表id 新增和修改ES中的图表数据
  57. func EsAddOrEditMyChartInfoByChartInfoId(chartInfoId int) {
  58. errMsg := make([]string, 0)
  59. defer func() {
  60. if len(errMsg) > 0 {
  61. fmt.Println("新增和修改ES中的图表数据失败:", strings.Join(errMsg, "\n"))
  62. }
  63. }()
  64. var condition string
  65. var pars []interface{}
  66. condition += " and a.chart_info_id = ? "
  67. pars = append(pars, chartInfoId)
  68. allList, err := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(condition, pars)
  69. if err != nil {
  70. errMsg = append(errMsg, fmt.Sprintf("EsAddOrEditMyChartInfoByChartInfoId Err:%s", err.Error()))
  71. return
  72. }
  73. for _, v := range allList {
  74. //添加es
  75. err = elastic.EsAddOrEditDataInterface(utils.MY_CHART_INDEX_NAME, strconv.Itoa(v.MyChartId), v)
  76. if err != nil {
  77. errMsg = append(errMsg, fmt.Sprintf("myChartId:%d,添加Es失败,err:%s", v.MyChartId, err.Error()))
  78. }
  79. }
  80. return
  81. }
  82. // EsAddOrEditMyChartInfoByMyChartId 根据我的图库id 新增和修改ES中的图表数据
  83. func EsAddOrEditMyChartInfoByMyChartId(myChartId int) {
  84. errMsg := make([]string, 0)
  85. defer func() {
  86. if len(errMsg) > 0 {
  87. fmt.Println("新增和修改ES中的图表数据失败:", strings.Join(errMsg, "\n"))
  88. }
  89. }()
  90. var condition string
  91. var pars []interface{}
  92. condition += " AND a.my_chart_id = ? "
  93. pars = append(pars, myChartId)
  94. allList, err := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(condition, pars)
  95. if err != nil {
  96. errMsg = append(errMsg, fmt.Sprintf("EsAddOrEditMyChartInfoByMyChartId Err:%s", err.Error()))
  97. return
  98. }
  99. for _, v := range allList {
  100. //添加es
  101. err = elastic.EsAddOrEditDataInterface(utils.MY_CHART_INDEX_NAME, strconv.Itoa(v.MyChartId), v)
  102. if err != nil {
  103. errMsg = append(errMsg, fmt.Sprintf("myChartId:%d,添加Es失败,err:%s", v.MyChartId, err.Error()))
  104. }
  105. }
  106. return
  107. }
  108. // EsDeleteMyChartInfoByMyChartId 根据我的图库id 删除ES中的图表数据
  109. func EsDeleteMyChartInfoByMyChartId(myChartId int) {
  110. var err error
  111. defer func() {
  112. if err != nil {
  113. fmt.Println("删除ES中的图表数据失败:", err.Error())
  114. }
  115. }()
  116. //添加es
  117. err = elastic.EsDeleteDataV2(utils.MY_CHART_INDEX_NAME, strconv.Itoa(myChartId))
  118. return
  119. }
  120. // EsSearchMyChartInfo 搜索图表信息
  121. func EsSearchMyChartInfo(keyword string, adminId int, noPermissionChartIdList []int, startSize, pageSize int) (list []*data_manage.MyChartList, total int64, err error) {
  122. list, total, err = elastic.SearchMyChartInfoData(utils.MY_CHART_INDEX_NAME, keyword, adminId, noPermissionChartIdList, startSize, pageSize)
  123. return
  124. }
  125. // EsDeleteMyChartInfoByMyChartIds 根据我的图表IDs删除ES中的我的图表
  126. func EsDeleteMyChartInfoByMyChartIds(ids []int) {
  127. if len(ids) == 0 {
  128. return
  129. }
  130. errMsg := make([]string, 0)
  131. defer func() {
  132. if len(errMsg) > 0 {
  133. fmt.Println("EsDeleteMyChartInfoByMyChartIds, 删除ES中的我的图表数据失败:", strings.Join(errMsg, "\n"))
  134. }
  135. }()
  136. for _, v := range ids {
  137. id := strconv.Itoa(v)
  138. if e := elastic.EsDeleteDataV2(utils.MY_CHART_INDEX_NAME, id); e != nil {
  139. errMsg = append(errMsg, fmt.Sprintf("myChartId:%d,删除Es失败,err:%s", v, e.Error()))
  140. }
  141. }
  142. return
  143. }