bi_dashboard.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models/bi_dashboard"
  5. "eta_gn/eta_api/models/knowledge"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "strconv"
  9. "time"
  10. "github.com/go-redis/redis/v8"
  11. )
  12. func SaveBiDashboardKnowledgeResource(dashboardId, adminId int) (msg string, err error) {
  13. ids, err := bi_dashboard.GetBiDashboardDetailIdByBidashboardIdAndType(dashboardId, 3)
  14. if err != nil {
  15. return
  16. }
  17. if len(ids) == 0 {
  18. return
  19. }
  20. data, er := utils.Rc.RedisBytes(utils.CACHE_KEY_BI_DASHBOARD_PREPARE_RESOURCE + strconv.Itoa(adminId))
  21. req := new(knowledge.BiDashboardResourceAddReq)
  22. if er != nil {
  23. if er == redis.Nil {
  24. msg = "暂无知识库"
  25. err = nil
  26. return
  27. }
  28. msg = "知识资源库添加失败"
  29. return
  30. }
  31. if err = json.Unmarshal(data, &req); err != nil {
  32. msg = "知识资源库添加失败"
  33. }
  34. defer func() {
  35. if err == nil {
  36. utils.Rc.Delete(utils.CACHE_KEY_BI_DASHBOARD_PREPARE_RESOURCE + strconv.Itoa(adminId))
  37. }
  38. }()
  39. insertMapping := make([]*knowledge.KnowledgeResourceBiDashboardDetail, 0, len(req.KnowledgeResourceList))
  40. for _, v := range req.KnowledgeResourceList {
  41. tmp := new(knowledge.KnowledgeResourceBiDashboardDetail)
  42. tmp.BiDashboardDetailId = ids[0]
  43. tmp.KnowledgeResourceId = v.KnowledgeResourceId
  44. tmp.KnowledgeResourceType = 3
  45. tmp.CreateTime = time.Now()
  46. insertMapping = append(insertMapping, tmp)
  47. }
  48. obj := new(knowledge.KnowledgeResourceBiDashboardDetail)
  49. err = obj.BatchAdd(insertMapping)
  50. if err != nil {
  51. msg = "知识资源库添加失败"
  52. }
  53. return
  54. }
  55. // UpdateBiDashboardEditing 更新看板编辑状态
  56. func UpdateBiDashboardEditing(boardId, status, userId int, userName string) (ret bi_dashboard.BiDashboardEditingCache, err error) {
  57. if boardId <= 0 {
  58. return
  59. }
  60. cacheKey := ""
  61. cacheKey = fmt.Sprint(utils.CACHE_BI_DASHBOARD_EDITING, boardId)
  62. // 完成编辑
  63. if status == 2 {
  64. _ = utils.Rc.Delete(cacheKey)
  65. return
  66. }
  67. // 读取缓存中的结果
  68. var editor bi_dashboard.BiDashboardEditingCache
  69. strCache, _ := utils.Rc.RedisString(cacheKey)
  70. fmt.Println(strCache)
  71. if strCache != "" {
  72. e := json.Unmarshal([]byte(strCache), &editor)
  73. if e != nil {
  74. err = fmt.Errorf("解析缓存内容失败: %s", e.Error())
  75. return
  76. }
  77. }
  78. // 标记编辑中
  79. if status == 1 {
  80. // 无人编辑, 写入缓存
  81. if !editor.IsEditing {
  82. ret.IsEditing = true
  83. ret.AdminId = userId
  84. ret.Editor = userName
  85. ret.Tips = fmt.Sprintf("当前%s正在编辑看板", userName)
  86. b, _ := json.Marshal(ret)
  87. utils.Rc.SetNX(cacheKey, string(b), 3*time.Minute)
  88. return
  89. }
  90. // 有人编辑
  91. if editor.IsEditing {
  92. // 编辑用户与当前用户不一致, 返回编辑用户, 一致则更新缓存
  93. if userId == editor.AdminId {
  94. b, _ := json.Marshal(editor)
  95. utils.Rc.Do("SETEX", cacheKey, int64(180), string(b))
  96. }
  97. ret = editor
  98. return
  99. }
  100. } else {
  101. // 默认查询
  102. ret = editor
  103. }
  104. return
  105. }