task_consumer.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/sync/errgroup"
  6. "strings"
  7. "time"
  8. "go-common/app/admin/main/videoup/model/archive"
  9. "go-common/library/log"
  10. )
  11. // HandsUp 签入
  12. func (s *Service) HandsUp(c context.Context, uid int64, uname string) (err error) {
  13. if s.CheckOnline(c, uid) {
  14. log.Info("已经登入(%d)", uid)
  15. return
  16. }
  17. _, err = s.arc.TaskUserCheckIn(c, uid)
  18. if err != nil {
  19. log.Error("s.arc.TaskUserCheckIn(%d) error(%v)", uid, err)
  20. return
  21. }
  22. s.sendConsumerLog(c, &archive.ConsumerLog{
  23. UID: uid,
  24. Uname: uname,
  25. Action: archive.ActionHandsUP,
  26. Ctime: time.Now().Format(archive.TimeFormatSec),
  27. Desc: "checkin",
  28. })
  29. mapParas := map[string]interface{}{
  30. "action": archive.ActionHandsUP,
  31. "uid": uid,
  32. }
  33. if _, err = s.arc.AddTaskHis(c, 0, archive.ActionHandsUP, 0, 0, uid, 0, 0, "checkin"); err != nil {
  34. log.Error("s.arc.AddTaskLog(%v) error(%v)", mapParas, uid)
  35. return
  36. }
  37. log.Info("用户签入(%d)", uid)
  38. return
  39. }
  40. // HandsOff 签出
  41. func (s *Service) HandsOff(c context.Context, uid int64, fuid int64) (err error) {
  42. if fuid != 0 { //管理员强制踢出组员
  43. if !s.isLeader(c, uid) {
  44. return fmt.Errorf("只有组长能强制踢出")
  45. }
  46. log.Info("管理员%d踢出组员%d", uid, fuid)
  47. uid = fuid
  48. }
  49. err = s.checkOut(c, uid)
  50. if err != nil {
  51. log.Error("s.checkOut(%d) error(%v)", uid, err)
  52. return
  53. }
  54. s.sendConsumerLog(c, &archive.ConsumerLog{
  55. UID: uid,
  56. Uname: "",
  57. Action: archive.ActionHandsOFF,
  58. Ctime: time.Now().Format(archive.TimeFormatSec),
  59. Desc: "checkout",
  60. })
  61. s.Free(c, uid)
  62. return
  63. }
  64. // Online 用户列表
  65. func (s *Service) Online(c context.Context) (cms []*archive.Consumers, err error) {
  66. cms, err = s.arc.Consumers(c)
  67. if err != nil {
  68. log.Error("s.arc.Consumers error(%v)", err)
  69. return
  70. }
  71. if len(cms) > 0 {
  72. var wg errgroup.Group
  73. wg.Go(func() error {
  74. if err := s.mulIDtoName(c, cms, s.mng.GetNameByUID, "UID", "UserName"); err != nil {
  75. log.Error("mulIDtoName s.mng.GetNameByUID error(%v)", err)
  76. }
  77. return nil
  78. })
  79. wg.Go(func() error {
  80. if err := s.mulIDtoName(c, cms, s.search.OutTime, "UID", "LastOut"); err != nil {
  81. log.Error("mulIDtoName s.search.OutTime error(%v)", err)
  82. }
  83. return nil
  84. })
  85. wg.Wait()
  86. }
  87. return
  88. }
  89. // InOutList 用户登入登出历史
  90. func (s *Service) InOutList(c context.Context, unames string, bt, et string) (l []*archive.InQuit, err error) {
  91. uids := []int64{}
  92. if len(unames) > 0 {
  93. if res, err := s.mng.Uids(c, strings.Split(unames, ",")); err == nil {
  94. for _, uid := range res {
  95. uids = append(uids, uid)
  96. }
  97. }
  98. }
  99. // 前端参数是日期,搜索参数必须到秒
  100. if len(bt) > 0 && len(et) > 0 {
  101. bt = bt + " 00:00:00"
  102. et = et + " 23:59:59"
  103. }
  104. return s.search.InQuitList(c, uids, bt, et)
  105. }
  106. // CheckOnline 检查在线状态
  107. func (s *Service) CheckOnline(c context.Context, uid int64) (on bool) {
  108. if s.arc.IsConsumerOn(c, uid) == 1 {
  109. on = true
  110. }
  111. return
  112. }
  113. // CheckGroup 检查用户组权限
  114. func (s *Service) CheckGroup(c context.Context, uid int64) (role int8, err error) {
  115. role, err = s.mng.GetUserRole(c, uid)
  116. if err != nil || role == 0 {
  117. log.Error("非法用户(%d) error(%v)", uid, err)
  118. return
  119. }
  120. return
  121. }
  122. func (s *Service) checkOut(c context.Context, uid int64) (err error) {
  123. if s.arc.IsConsumerOn(c, uid) == 0 {
  124. log.Info("已经签出(%d)", uid)
  125. return
  126. }
  127. _, err = s.arc.TaskUserCheckOff(c, uid)
  128. if err != nil {
  129. log.Error("s.arc.TaskUserCheckOff(%d) error(%v)", uid, err)
  130. return
  131. }
  132. mapParas := map[string]interface{}{
  133. "action": archive.ActionHandsOFF,
  134. "uid": uid,
  135. }
  136. if _, err = s.arc.AddTaskHis(c, 0, archive.ActionHandsOFF, 0, 0, uid, 0, 0, "checkOut"); err != nil {
  137. log.Error("s.arc.AddTaskLog(%v) error(%v)", mapParas, uid)
  138. }
  139. return
  140. }