task.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_api/cache"
  6. "hongze/hongze_api/models"
  7. "hongze/hongze_api/services/alarm_msg"
  8. "hongze/hongze_api/services/go_redis"
  9. "hongze/hongze_api/utils"
  10. "time"
  11. )
  12. func Task() {
  13. fmt.Println("start")
  14. //InitSetUnionId()
  15. //FixUnionId()
  16. fmt.Println("end")
  17. go AutoUpdateUserView()
  18. }
  19. // AutoUpdateUserView 自动添加阅读记录
  20. func AutoUpdateUserView() {
  21. defer func() {
  22. if err := recover(); err != nil {
  23. fmt.Println("[AutoUpdateUserView]", err)
  24. }
  25. }()
  26. for {
  27. go_redis.BRPop2Func(utils.CACHE_KEY_USER_VIEW, func(b []byte) {
  28. var userViewRedisData cache.UserViewRedisData
  29. utils.FileLog.Info("redis:" + string(b))
  30. if err := json.Unmarshal(b, &userViewRedisData); err != nil {
  31. go alarm_msg.SendAlarmMsg(utils.APPNAME+" "+utils.RunMode+" 失败提醒:"+fmt.Sprint("AutoUpdateUserView ERR:", err, ";response data:", string(b)), 2)
  32. //}
  33. //if _, err := models.AddLogs(&log); err != nil {
  34. // fmt.Println("AddLogs:", err.Error(), log)
  35. } else {
  36. // 客户的阅读时间
  37. err = models.UpdateCompanyProductViewData(userViewRedisData.CompanyId, userViewRedisData.ProductId, userViewRedisData.ViewTime)
  38. if err != nil {
  39. go alarm_msg.SendAlarmMsg(utils.APPNAME+" "+utils.RunMode+" 失败提醒:"+fmt.Sprint("AutoUpdateUserView Update UpdateCompanyProductViewData ERR:", err, ";response data:", string(b)), 2)
  40. }
  41. // 添加报告阅读汇总记录
  42. {
  43. source := userViewRedisData.Source
  44. if source == 0 {
  45. if userViewRedisData.ProductId == 2 {
  46. source = 4
  47. } else {
  48. source = 1
  49. }
  50. }
  51. item := &models.UserReportViewRecord{
  52. Id: 1,
  53. Source: source,
  54. UserId: userViewRedisData.UserId,
  55. ReportId: userViewRedisData.ReportId,
  56. ReportChapterId: userViewRedisData.ReportChapterId,
  57. Mobile: userViewRedisData.Mobile,
  58. Email: userViewRedisData.Email,
  59. RealName: userViewRedisData.RealName,
  60. CompanyName: userViewRedisData.CompanyName,
  61. StopTime: userViewRedisData.StopTime,
  62. OutId: fmt.Sprint(userViewRedisData.OutId),
  63. CreateTime: userViewRedisData.ViewTime,
  64. }
  65. viewTime, tmpErr := time.ParseInLocation(utils.FormatDateTime, userViewRedisData.ViewTime, time.Local)
  66. if tmpErr != nil {
  67. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+" 失败提醒", fmt.Sprint("AutoUpdateUserView ADD UserReportViewRecord ERR:", err, ";response data:", string(b)), utils.EmailSendToUsers)
  68. } else {
  69. err = item.Insert(viewTime.Year(), item)
  70. if err != nil {
  71. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+" 失败提醒", fmt.Sprint("AutoUpdateUserView ADD UserReportViewRecord ERR:", err, ";response data:", string(b)), utils.EmailSendToUsers)
  72. }
  73. }
  74. }
  75. // 联系人的阅读时间
  76. err = models.UpdateWxUserViewData(userViewRedisData.UserId, userViewRedisData.ProductId, userViewRedisData.ViewTime)
  77. if err != nil {
  78. go alarm_msg.SendAlarmMsg(utils.APPNAME+" "+utils.RunMode+" 失败提醒:"+fmt.Sprint("AutoUpdateUserView Update UpdateWxUserViewData ERR:", err, ";response data:", string(b)), 2)
  79. }
  80. }
  81. })
  82. }
  83. }