getviewstatuslogic.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package logic
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "context"
  5. "fmt"
  6. IC "jyBXSubscribe/rpc/init"
  7. "jyBXSubscribe/rpc/model"
  8. "jyBXSubscribe/rpc/util"
  9. "log"
  10. "strings"
  11. "time"
  12. "jyBXSubscribe/rpc/internal/svc"
  13. "jyBXSubscribe/rpc/type/bxsubscribe"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type GetViewStatusLogic struct {
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. logx.Logger
  20. }
  21. func NewGetViewStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetViewStatusLogic {
  22. return &GetViewStatusLogic{
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. Logger: logx.WithContext(ctx),
  26. }
  27. }
  28. // 查看状态
  29. func (l *GetViewStatusLogic) GetViewStatus(in *bxsubscribe.GetViewStatusReq) (*bxsubscribe.ViewStatusResp, error) {
  30. // todo: add your logic here and delete this line
  31. user := model.EntInfo(common.IntAll(in.EntId), common.IntAll(in.EntUserId))
  32. var (
  33. users *[]*model.User
  34. ss []string
  35. finsql string
  36. )
  37. in.InfoId = util.DecodeId(in.InfoId)
  38. log.Println("查看状态参数", in)
  39. res := new(bxsubscribe.ViewStatusResp)
  40. finsql = `SELECT userid,isvisit,visittime,date FROM pushentniche where infoid =? and source in (2,3) order By date asc`
  41. if user.Role_admin_department {
  42. users = model.GetDisUsers(common.IntAll(in.EntId), user.Dept.Id)
  43. if users != nil && len(*users) > 0 {
  44. //获取所有部门和子部门员工
  45. for _, v := range *users {
  46. ss = append(ss, common.InterfaceToStr(v.Id))
  47. }
  48. } else {
  49. log.Println("部门管理员下为获取到员工", in.EntUserId)
  50. return &bxsubscribe.ViewStatusResp{}, nil
  51. }
  52. } else if user.Role_admin_system {
  53. //获取企业所有用户
  54. users = model.GetEntUsers(common.IntAll(in.EntId))
  55. }
  56. if len(ss) > 0 {
  57. finsql = fmt.Sprintf(`SELECT userid,isvisit,visittime,date FROM pushentniche where infoid =? and source in (2,3) %s order By date asc`, fmt.Sprintf(`and userid in (%s)`, strings.Join(ss, ",")))
  58. }
  59. data := IC.PushMysql.SelectBySql(finsql, in.InfoId)
  60. userMap := make(map[int]string)
  61. for _, u := range *users {
  62. userMap[u.Id] = u.Name
  63. }
  64. if data != nil && len(*data) > 0 {
  65. //d := make(map[string]int)
  66. for _, v := range *data {
  67. userid := common.IntAll(v["userid"])
  68. if name, ok := userMap[userid]; ok && name != "" {
  69. var da bxsubscribe.UserStatus
  70. da.Name = name
  71. da.Isvisit = common.Int64All(v["isvisit"])
  72. visittime := common.InterfaceToStr(v["visittime"])
  73. date := common.Int64All(v["date"])
  74. if date > 0 {
  75. da.Date = time.Unix(date, 0).Format("2006-01-02 15:04:05")
  76. }
  77. da.Visittime = visittime
  78. da.Id = common.Int64All(userid)
  79. res.Items = append(res.Items, &da)
  80. //if _, ok1 := d[common.InterfaceToStr(userid)]; !ok1 {
  81. // res.Items = append(res.Items, &da)
  82. // d[common.InterfaceToStr(userid)] = len(res.Items) - 1
  83. //}
  84. }
  85. }
  86. }
  87. //Reverse(&res.Items)
  88. return res, nil
  89. }
  90. // 数组倒序函数
  91. func Reverse(arr *[]*bxsubscribe.UserStatus) {
  92. var temp *bxsubscribe.UserStatus
  93. length := len(*arr)
  94. for i := 0; i < length/2; i++ {
  95. temp = (*arr)[i]
  96. (*arr)[i] = (*arr)[length-1-i]
  97. (*arr)[length-1-i] = temp
  98. }
  99. }