consumer.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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-task/model"
  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.dao.TaskUserCheckIn(c, uid)
  18. if err != nil {
  19. log.Error("s.dao.TaskUserCheckIn(%d) error(%v)", uid, err)
  20. return
  21. }
  22. s.sendConsumerLog(c, &model.ConsumerLog{
  23. UID: uid,
  24. Uname: uname,
  25. Action: model.ActionHandsUP,
  26. Ctime: time.Now().Format(model.TimeFormatSec),
  27. Desc: "checkin",
  28. })
  29. mapParas := map[string]interface{}{
  30. "action": model.ActionHandsUP,
  31. "uid": uid,
  32. }
  33. if _, err = s.dao.AddTaskHis(c, 0, model.ActionHandsUP, 0, 0, uid, 0, 0, "checkin"); err != nil {
  34. log.Error("s.dao.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.Free(c, uid)
  55. return
  56. }
  57. // Online 用户列表
  58. func (s *Service) Online(c context.Context) (cms []*model.Consumers, err error) {
  59. cms, err = s.dao.Consumers(c)
  60. if err != nil {
  61. log.Error("s.dao.Consumers error(%v)", err)
  62. return
  63. }
  64. if len(cms) > 0 {
  65. var wg errgroup.Group
  66. wg.Go(func() error {
  67. if err := s.mulIDtoName(c, cms, s.dao.GetNameByUID, "UID", "UserName"); err != nil {
  68. log.Error("mulIDtoName s.dao.GetNameByUID error(%v)", err)
  69. }
  70. return nil
  71. })
  72. wg.Go(func() error {
  73. if err := s.mulIDtoName(c, cms, s.dao.OutTime, "UID", "LastOut"); err != nil {
  74. log.Error("mulIDtoName s.dao.OutTime error(%v)", err)
  75. }
  76. return nil
  77. })
  78. wg.Wait()
  79. }
  80. return
  81. }
  82. // InOutList 用户登入登出历史
  83. func (s *Service) InOutList(c context.Context, unames string, bt, et string) (l []*model.InQuit, err error) {
  84. uids := []int64{}
  85. if len(unames) > 0 {
  86. if res, err := s.dao.Uids(c, strings.Split(unames, ",")); err == nil {
  87. for _, uid := range res {
  88. uids = append(uids, uid)
  89. }
  90. }
  91. }
  92. // 前端参数是日期,搜索参数必须到秒
  93. if len(bt) > 0 && len(et) > 0 {
  94. bt = bt + " 00:00:00"
  95. et = et + " 23:59:59"
  96. }
  97. return s.dao.InQuitList(c, uids, bt, et)
  98. }
  99. // CheckOnline 检查在线状态
  100. func (s *Service) CheckOnline(c context.Context, uid int64) (on bool) {
  101. if s.dao.IsConsumerOn(c, uid) == 1 {
  102. on = true
  103. }
  104. return
  105. }
  106. // CheckGroup 检查用户组权限
  107. func (s *Service) CheckGroup(c context.Context, uid int64) (role int8, err error) {
  108. role, err = s.dao.GetUserRole(c, uid)
  109. if err != nil || role == 0 {
  110. log.Error("非法用户(%d) error(%v)", uid, err)
  111. return
  112. }
  113. return
  114. }
  115. func (s *Service) checkOut(c context.Context, uid int64) (err error) {
  116. if s.dao.IsConsumerOn(c, uid) == 0 {
  117. log.Info("已经签出(%d)", uid)
  118. return
  119. }
  120. _, err = s.dao.TaskUserCheckOff(c, uid)
  121. if err != nil {
  122. log.Error("s.dao.TaskUserCheckOff(%d) error(%v)", uid, err)
  123. return
  124. }
  125. s.sendConsumerLog(c, &model.ConsumerLog{
  126. UID: uid,
  127. Uname: "",
  128. Action: model.ActionHandsOFF,
  129. Ctime: time.Now().Format(model.TimeFormatSec),
  130. Desc: "checkout",
  131. })
  132. mapParas := map[string]interface{}{
  133. "action": model.ActionHandsOFF,
  134. "uid": uid,
  135. }
  136. if _, err = s.dao.AddTaskHis(c, 0, model.ActionHandsOFF, 0, 0, uid, 0, 0, "checkOut"); err != nil {
  137. log.Error("s.dao.AddTaskLog(%v) error(%v)", mapParas, uid)
  138. }
  139. return
  140. }
  141. func (s *Service) isLeader(c context.Context, uid int64) bool {
  142. role, e := s.dao.GetUserRole(c, uid)
  143. if e != nil {
  144. log.Error("s.dao.GetUserRole(%d) error(%v)", uid, e)
  145. return false
  146. }
  147. if role == model.TaskLeader {
  148. return true
  149. }
  150. return false
  151. }