rai_serve_count.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_cygx/models"
  7. "hongze/hongze_cygx/models/company"
  8. "hongze/hongze_cygx/models/rai_serve"
  9. "hongze/hongze_cygx/utils"
  10. "time"
  11. )
  12. //权益服务统计
  13. // Redis对列消息中的结构体
  14. type RaiServeBillRedis struct {
  15. Content string `comment:"服务内容说明"`
  16. UserId int `comment:"用户ID"`
  17. ComapnyId int `comment:"公司ID"`
  18. SourceId int `comment:"来源ID"`
  19. Source string `comment:"来源"`
  20. RegisterPlatform int `comment:"来源 1小程序,2:网页"`
  21. ViewTime time.Time `comment:"浏览时间"`
  22. }
  23. // 权益服务统计添加到Redis队列中
  24. func CygxRaiServeBillRedisAdd(content, source string, userId, comapnyId, sourceId, registerPlatform int, viewTime time.Time) (err error) {
  25. defer func() {
  26. if err != nil {
  27. fmt.Println(err)
  28. msg := fmt.Sprint("source:", source, "userId:", userId, "sourceId", sourceId)
  29. go utils.SendAlarmMsg("权益服务统计添加到Redis队列中,写入Redis队列消息失败 CygxRaiServeBillRedisAdd:"+err.Error()+msg, 2)
  30. }
  31. }()
  32. log := &rai_serve.RaiServeBillRedis{Content: content, Source: source, UserId: userId, ComapnyId: comapnyId, SourceId: sourceId, RegisterPlatform: registerPlatform, ViewTime: viewTime}
  33. if utils.Re == nil {
  34. err := utils.Rc.LPush(utils.CYGX_RAI_SERVE_BILL_KEY, log)
  35. if err != nil {
  36. fmt.Println("RaiServeBillRedis LPush Err:" + err.Error())
  37. }
  38. }
  39. return
  40. }
  41. //func init() {
  42. // CygxRaiServeBillRedisAddReduce()
  43. //}
  44. // CygxRaiServeBillRedisAddReduce 处理权益服务统计
  45. func CygxRaiServeBillRedisAddReduce() (err error) {
  46. for {
  47. //SourceType int `description:"1:报名、 2:取消报名、3:活动编辑、4:活动发布,取消发布、5:活动到会。"`
  48. utils.Rc.Brpop(utils.CYGX_RAI_SERVE_BILL_KEY, func(b []byte) {
  49. var log rai_serve.RaiServeBillRedis
  50. if err := json.Unmarshal(b, &log); err != nil {
  51. fmt.Println("json unmarshal wrong!")
  52. go utils.SendAlarmMsg("处理权益服务统计Redis队列消息失败:"+err.Error()+string(b), 2)
  53. }
  54. //如果不是共享给 权益服务组的,则不处理
  55. taotalRaiServe, err := company.GetCompanyProductAaiServeCount(log.ComapnyId, utils.RAI_SERVE_GROUP_ID)
  56. if err != nil {
  57. go utils.SendAlarmMsg("处理权益服务统计Redis队列消息失败:GetCompanyProductAaiServeCount"+err.Error()+string(b), 2)
  58. }
  59. fmt.Println("taotaRaiServe", taotalRaiServe)
  60. if taotalRaiServe == 0 {
  61. return
  62. }
  63. //如果已经有记录了就不处理
  64. taotalSource, err := rai_serve.GetCygxRaiServeBillCountByUserAndSource(log.UserId, log.SourceId, log.Source)
  65. if err != nil {
  66. go utils.SendAlarmMsg("处理权益服务统计Redis队列消息失败:GetCygxRaiServeBillCountByUserAndSource"+err.Error()+string(b), 2)
  67. }
  68. if taotalSource > 0 {
  69. return
  70. }
  71. switch log.Source {
  72. case utils.CYGX_OBJ_YANXUANSPECIAL: //研选专栏阅读记录处理
  73. go RaiServeBillRedisAddReduceByYanxuanspecial(log)
  74. fmt.Println("研选专栏阅读记录处理")
  75. break
  76. default:
  77. fmt.Println(string(b))
  78. go utils.SendAlarmMsg("处理研选活动扣点处理Redis队列消息失败:"+string(b), 2)
  79. }
  80. })
  81. }
  82. }
  83. // 处理权益服务统计->研选阅读记录
  84. func RaiServeBillRedisAddReduceByYanxuanspecial(log rai_serve.RaiServeBillRedis) (err error) {
  85. source := log.Source
  86. userId := log.UserId
  87. sourceId := log.SourceId
  88. defer func() {
  89. if err != nil {
  90. go utils.SendAlarmMsg("用户报名活动扣点,处理Redis队列消息失败:"+err.Error()+fmt.Sprint("source:", source, "userId:", userId, "sourceId", sourceId), 2)
  91. }
  92. }()
  93. wxUser, e := models.GetWxUserItemByUserId(userId)
  94. if e != nil {
  95. err = errors.New("GetWxUserItemByUserId, Err: " + e.Error())
  96. return
  97. }
  98. item := new(rai_serve.CygxRaiServeBill)
  99. item.Content = log.Content
  100. item.ServeTypeId = 5
  101. item.ServeTypeName = "阅读uv"
  102. item.UserId = wxUser.UserId
  103. item.Mobile = wxUser.Mobile
  104. item.Email = wxUser.Email
  105. item.CompanyId = wxUser.CompanyId
  106. item.CompanyName = wxUser.CompanyName
  107. item.RealName = wxUser.RealName
  108. item.RegisterPlatform = log.RegisterPlatform
  109. item.ServeCount = 0.5
  110. if wxUser.IsMaker == 1 {
  111. item.IsKp = wxUser.IsMaker
  112. item.ServeCount = item.ServeCount * 3
  113. }
  114. item.SourceId = log.SourceId
  115. item.Source = log.Source
  116. item.CreateTime = time.Now()
  117. item.ViewTime = log.ViewTime
  118. err = rai_serve.AddCygxRaiServeBill(item)
  119. return
  120. }