getviewstatuslogic.go 2.9 KB

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