data_collect_handler.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models/data_manage"
  6. "eta_gn/eta_api/services/alarm_msg"
  7. "eta_gn/eta_api/utils"
  8. "fmt"
  9. "time"
  10. )
  11. // HandleDataCollectCache
  12. // @Description: 资产收藏处理
  13. // @author: Roc
  14. // @datetime 2024-12-27 16:56:36
  15. func HandleDataCollectCache() {
  16. var err error
  17. for {
  18. utils.Rc.Brpop(utils.CACHE_KEY_DATA_COLLECT_HANDLER, func(b []byte) {
  19. defer func() {
  20. if err != nil {
  21. utils.FileLog.Info("HandleDataCollectCache err:" + err.Error())
  22. go alarm_msg.SendAlarmMsg("资产收藏处理失败提醒,errMsg:"+err.Error(), 3)
  23. }
  24. }()
  25. record := new(data_manage.DataCollectHandleItem)
  26. if err = json.Unmarshal(b, &record); err != nil {
  27. fmt.Println("json unmarshal wrong!")
  28. return
  29. }
  30. dataId := record.DataId
  31. dataType := record.DataType
  32. utils.FileLog.Info(fmt.Sprintf("资产收藏处理失败提醒 DealReplaceEdbCache: 旧指标ID:%d,新指标ID:%d", dataId, dataType))
  33. deleteCache := true
  34. setNxKey := fmt.Sprintf("EDB_INFO_REPLACE:%d-%d", dataId, dataType)
  35. defer func() {
  36. if deleteCache {
  37. utils.Rc.Delete(setNxKey)
  38. }
  39. }()
  40. if !utils.Rc.SetNX(setNxKey, 1, 30*time.Minute) {
  41. deleteCache = false
  42. err = fmt.Errorf("资产收藏处理:%d为新指标%d:正在处理中", dataId, dataType)
  43. return
  44. }
  45. switch dataType {
  46. case 1: // 指标处理
  47. handleEdbCollectCache(dataId)
  48. case 2: // 图表处理
  49. handleChartCollectCache(dataId)
  50. }
  51. })
  52. }
  53. }
  54. // handleEdbCollectCache
  55. // @Description: 指标收藏关系处理
  56. // @author: Roc
  57. // @datetime 2024-12-27 17:15:19
  58. // @param edbInfoId int
  59. func handleEdbCollectCache(edbInfoId int) {
  60. var err error
  61. defer func() {
  62. if err != nil {
  63. utils.FileLog.Info("handleEdbCollectCache err:" + err.Error())
  64. go alarm_msg.SendAlarmMsg("指标收藏处理失败提醒,errMsg:"+err.Error(), 3)
  65. }
  66. }()
  67. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  68. if err != nil {
  69. return
  70. }
  71. // 公开那就直接返回
  72. if edbInfo.PublicStatus == utils.DataPublicSuccess {
  73. return
  74. }
  75. edbCollectObj := data_manage.EdbCollect{}
  76. // 获取所有已经收藏的人
  77. allCollectUserIdList, err := edbCollectObj.GetEdbAllCollectUserIdList(edbInfoId)
  78. if err != nil {
  79. err = errors.New("查找指标的所有收藏人失败,Err:" + err.Error())
  80. return
  81. }
  82. // 没有人收藏,那么就直接返回
  83. if len(allCollectUserIdList) <= 0 {
  84. return
  85. }
  86. userIdMap := make(map[int]bool)
  87. for _, v := range allCollectUserIdList {
  88. userIdMap[v] = true
  89. }
  90. // 自己的指标,肯定可以收藏的
  91. delete(userIdMap, edbInfo.SysUserId)
  92. // 获取该指标的被分享人
  93. obj := data_manage.EdbInfoShare{}
  94. shareUserIdList, err := obj.GetAllUserIdListByEdbInfoId(edbInfoId)
  95. if err != nil {
  96. err = errors.New("查找指标的所有收藏人失败,Err:" + err.Error())
  97. return
  98. }
  99. for _, v := range shareUserIdList {
  100. delete(userIdMap, v)
  101. }
  102. // 没有待移除的用户,那么就直接返回
  103. if len(userIdMap) < 0 {
  104. return
  105. }
  106. // 删除收藏关系
  107. deleteUserIdList := make([]int, 0)
  108. for userId, _ := range userIdMap {
  109. deleteUserIdList = append(deleteUserIdList, userId)
  110. }
  111. // 删除收藏关系
  112. err = edbCollectObj.DeleteByEdbInfoIdAndUserIdList(edbInfoId, deleteUserIdList)
  113. return
  114. }
  115. // handleChartCollectCache
  116. // @Description: 图表收藏关系处理
  117. // @author: Roc
  118. // @datetime 2024-12-27 17:15:19
  119. // @param chartInfoId int
  120. func handleChartCollectCache(chartInfoId int) {
  121. var err error
  122. defer func() {
  123. if err != nil {
  124. utils.FileLog.Info("handleChartCollectCache err:" + err.Error())
  125. go alarm_msg.SendAlarmMsg("图表收藏处理失败提醒,errMsg:"+err.Error(), 3)
  126. }
  127. }()
  128. chartInfo, err := data_manage.GetChartInfoById(chartInfoId)
  129. if err != nil {
  130. return
  131. }
  132. // 公开那就直接返回
  133. if chartInfo.PublicStatus == utils.DataPublicSuccess {
  134. return
  135. }
  136. chartCollectObj := data_manage.ChartCollect{}
  137. // 获取所有已经收藏的人
  138. allCollectUserIdList, err := chartCollectObj.GetChartAllCollectUserIdList(chartInfoId)
  139. if err != nil {
  140. err = errors.New("查找图表的所有收藏人失败,Err:" + err.Error())
  141. return
  142. }
  143. // 没有人收藏,那么就直接返回
  144. if len(allCollectUserIdList) <= 0 {
  145. return
  146. }
  147. userIdMap := make(map[int]bool)
  148. for _, v := range allCollectUserIdList {
  149. userIdMap[v] = true
  150. }
  151. // 自己的图表,肯定可以收藏的
  152. delete(userIdMap, chartInfo.SysUserId)
  153. // 获取该图表的被分享人
  154. obj := data_manage.ChartInfoShare{}
  155. shareUserIdList, err := obj.GetAllUserIdListByChartInfoId(chartInfoId)
  156. if err != nil {
  157. err = errors.New("查找图表的所有收藏人失败,Err:" + err.Error())
  158. return
  159. }
  160. for _, v := range shareUserIdList {
  161. delete(userIdMap, v)
  162. }
  163. // 没有待移除的用户,那么就直接返回
  164. if len(userIdMap) < 0 {
  165. return
  166. }
  167. // 删除收藏关系
  168. deleteUserIdList := make([]int, 0)
  169. for userId, _ := range userIdMap {
  170. deleteUserIdList = append(deleteUserIdList, userId)
  171. }
  172. // 删除收藏关系
  173. err = chartCollectObj.DeleteByChartInfoIdAndUserIdList(chartInfoId, deleteUserIdList)
  174. return
  175. }