chart_collect_elastic.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package services
  2. import (
  3. "eta/eta_forum_admin/models/chart_collect"
  4. "eta/eta_forum_admin/services/elastic"
  5. "eta/eta_forum_admin/utils"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. )
  10. // EsAddOrEditChartCollectByChartInfoId 新增和修改ES中的收藏图表数据
  11. func EsAddOrEditChartCollectByChartInfoId(chartInfoId int) {
  12. errMsg := make([]string, 0)
  13. defer func() {
  14. if len(errMsg) > 0 {
  15. fmt.Println("新增和修改ES中的图表数据失败:", strings.Join(errMsg, "\n"))
  16. }
  17. }()
  18. var condition string
  19. var pars []interface{}
  20. condition += " and a.chart_info_id = ? "
  21. pars = append(pars, chartInfoId)
  22. allList, err := chart_collect.GetChartCollectByChartInfoIdAndUserIdByCondition(condition, pars)
  23. if err != nil {
  24. errMsg = append(errMsg, fmt.Sprintf("EsAddOrEditChartCollectByChartInfoId Err:%s", err.Error()))
  25. return
  26. }
  27. for _, v := range allList {
  28. //添加es
  29. v.CollectClassifyIds += ","
  30. err = elastic.EsAddOrEditDataInterface(utils.CHART_COLLECT_INDEX_NAME, v.UserChartInfoId, v)
  31. if err != nil {
  32. errMsg = append(errMsg, fmt.Sprintf("图表收藏:%s,添加Es失败,err:%s", v.UserChartInfoId, err.Error()))
  33. }
  34. }
  35. return
  36. }
  37. // EsAddOrEditChartCollectByChartInfoIdUserId 新增和修改ES中的收藏图表数据
  38. func EsAddOrEditChartCollectByChartInfoIdUserId(chartInfoId, userId int) {
  39. errMsg := make([]string, 0)
  40. defer func() {
  41. if len(errMsg) > 0 {
  42. fmt.Println("新增和修改ES中的图表数据失败:", strings.Join(errMsg, "\n"))
  43. }
  44. }()
  45. var condition string
  46. var pars []interface{}
  47. condition += " and a.chart_info_id = ? and a.user_id = ? "
  48. pars = append(pars, chartInfoId, userId)
  49. allList, err := chart_collect.GetChartCollectByChartInfoIdAndUserIdByCondition(condition, pars)
  50. if err != nil {
  51. errMsg = append(errMsg, fmt.Sprintf("EsAddOrEditChartCollectByChartInfoId Err:%s", err.Error()))
  52. return
  53. }
  54. for _, v := range allList {
  55. //添加es
  56. v.CollectClassifyIds += ","
  57. fmt.Println(v)
  58. err = elastic.EsAddOrEditDataInterface(utils.CHART_COLLECT_INDEX_NAME, v.UserChartInfoId, v)
  59. if err != nil {
  60. errMsg = append(errMsg, fmt.Sprintf("图表收藏:%s,添加Es失败,err:%s", v.UserChartInfoId, err.Error()))
  61. }
  62. }
  63. return
  64. }
  65. // EsDeleteChartCollectByChartInfoIdUserId 取消收藏
  66. func EsDeleteChartCollectByChartInfoIdUserId(chartInfoId, userId int) {
  67. var err error
  68. defer func() {
  69. if err != nil {
  70. fmt.Println("删除ES中的图表数据失败:", err.Error())
  71. }
  72. }()
  73. //添加es
  74. // 先删除,再新增
  75. userChartInfoId := strconv.Itoa(userId) + "_" + strconv.Itoa(chartInfoId)
  76. err = elastic.EsDeleteDataV2(utils.CHART_COLLECT_INDEX_NAME, userChartInfoId)
  77. if err != nil {
  78. err = fmt.Errorf(fmt.Sprintf("图表收藏:%s,删除Es失败,err:%s", userChartInfoId, err.Error()))
  79. return
  80. }
  81. var condition string
  82. var pars []interface{}
  83. condition += " and a.chart_info_id = ? and a.user_id = ? "
  84. pars = append(pars, chartInfoId, userId)
  85. allList, err := chart_collect.GetChartCollectByChartInfoIdAndUserIdByCondition(condition, pars)
  86. if err != nil {
  87. return
  88. }
  89. for _, v := range allList {
  90. //添加es
  91. v.CollectClassifyIds += ","
  92. err = elastic.EsAddOrEditDataInterface(utils.CHART_COLLECT_INDEX_NAME, v.UserChartInfoId, v)
  93. if err != nil {
  94. err = fmt.Errorf("图表收藏:%s,添加Es失败,err:%s", v.UserChartInfoId, err.Error())
  95. return
  96. }
  97. }
  98. return
  99. }
  100. // EsDeleteChartCollectByChartInfoIdUserIds 删除图表时,删除图表的收藏记录
  101. func EsDeleteChartCollectByChartInfoIdUserIds(userChartInfoIds []string) {
  102. if len(userChartInfoIds) == 0 {
  103. return
  104. }
  105. errMsg := make([]string, 0)
  106. defer func() {
  107. if len(errMsg) > 0 {
  108. fmt.Println("EsDeleteMyChartInfoByMyChartIds, 删除ES中的我的图表数据失败:", strings.Join(errMsg, "\n"))
  109. }
  110. }()
  111. for _, v := range userChartInfoIds {
  112. id := v
  113. if e := elastic.EsDeleteDataV2(utils.CHART_COLLECT_INDEX_NAME, id); e != nil {
  114. errMsg = append(errMsg, fmt.Sprintf("myChartId:%d,删除Es失败,err:%s", v, e.Error()))
  115. }
  116. }
  117. return
  118. }
  119. func AddAllChartCollectData() {
  120. var condition string
  121. var pars []interface{}
  122. allList, err := chart_collect.GetChartCollectByChartInfoIdAndUserIdByCondition(condition, pars)
  123. if err != nil {
  124. return
  125. }
  126. for _, v := range allList {
  127. //添加es
  128. v.CollectClassifyIds += ","
  129. err = elastic.EsAddOrEditDataInterface(utils.CHART_COLLECT_INDEX_NAME, v.UserChartInfoId, v)
  130. if err != nil {
  131. err = fmt.Errorf("图表收藏:%s,添加Es失败,err:%s", v.UserChartInfoId, err.Error())
  132. return
  133. }
  134. }
  135. }