cygx_yanxuan_special_company.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_mfyx/models"
  6. "hongze/hongze_mfyx/utils"
  7. "time"
  8. )
  9. // 记录用户阅读时长
  10. func AddSpecialRecord(user *models.WxUserItem, specialId, stopTime int) (err error) {
  11. defer func() {
  12. if err != nil {
  13. go utils.SendAlarmMsg(fmt.Sprint("记录用户阅读时长 失败 AddSpecialRecord Err:"+err.Error(), "userId:", user.UserId, "specialId:", specialId), 2)
  14. }
  15. }()
  16. if user.CompanyId == 0 {
  17. return
  18. }
  19. //校验研选专栏权限,以及无权限的时候的对应状态码
  20. havePower, e := GetYanxuanSpecialDetailUserPower(user)
  21. if e != nil {
  22. err = errors.New("GetYanxuanSpecialDetailUserPower, Err: " + e.Error())
  23. return
  24. }
  25. var permissionCode int
  26. if havePower {
  27. permissionCode = 1
  28. }
  29. var sellerName string
  30. //获取销售信息
  31. sellerName, _ = GetSellerName(user)
  32. if stopTime >= 3 {
  33. //判断一个用户是否阅读过 某一篇研选专栏
  34. totalRecord, e := models.GetCygxYanxuanSpecialRecordCountByUser(user.UserId, specialId)
  35. if e != nil {
  36. err = errors.New("GetCygxYanxuanSpecialRecordCountByUser, Err: " + e.Error())
  37. return
  38. }
  39. item := new(models.CygxYanxuanSpecialRecord)
  40. item.UserId = user.UserId
  41. item.Mobile = user.Mobile
  42. item.Email = user.Email
  43. item.CompanyId = user.CompanyId
  44. item.CompanyName = user.CompanyName
  45. item.RealName = user.RealName
  46. item.SellerName = sellerName
  47. item.CreateTime = time.Now().Add(-time.Duration(stopTime) * time.Second) // 往前推迟的时间就是他的阅读时间
  48. item.ModifyTime = time.Now()
  49. item.RegisterPlatform = utils.REGISTER_PLATFORM
  50. item.YanxuanSpecialId = specialId
  51. item.StopTime = stopTime
  52. item.PermissionCode = permissionCode
  53. _, e = models.AddCygxYanxuanSpecialRecord(item) // 添加历史记录
  54. if e != nil {
  55. err = errors.New("AddCygxYanxuanSpecialRecord, Err: " + e.Error())
  56. return
  57. }
  58. //如果不是弘则研究的人员阅读的,就修改Pv、Uv数量
  59. if user.CompanyId != utils.HZ_COMPANY_ID {
  60. //专栏Pv数量进行加一
  61. e = models.UpdateYanxuanSpecialPv(specialId)
  62. if e != nil {
  63. err = errors.New("UpdateYanxuanSpecialPv, Err: " + e.Error())
  64. return
  65. }
  66. //专栏作者Pv数量进行加一
  67. e = models.UpdateCygxYanxuanSpecialAuthorPv(user.UserId)
  68. if e != nil {
  69. err = errors.New("UpdateCygxYanxuanSpecialAuthorPv, Err: " + e.Error())
  70. return
  71. }
  72. //如果没有阅读过,那么就给专栏文章的UV、作者的UV进行加一
  73. if totalRecord == 0 {
  74. e = models.UpdateYanxuanSpecialUv(specialId)
  75. if e != nil {
  76. err = errors.New("UpdateYanxuanSpecialUv, Err: " + e.Error())
  77. return
  78. }
  79. //专栏作者Uv数量进行加一
  80. e = models.UpdateCygxYanxuanSpecialAuthorUv(user.UserId)
  81. if e != nil {
  82. err = errors.New("UpdateCygxYanxuanSpecialAuthorUv, Err: " + e.Error())
  83. return
  84. }
  85. }
  86. } else {
  87. //专栏Pv数量进行加一
  88. e = models.UpdateYanxuanSpecialHzPv(specialId)
  89. if e != nil {
  90. err = errors.New("UpdateYanxuanSpecialHzPv, Err: " + e.Error())
  91. return
  92. }
  93. //如果没有阅读过,那么就给专栏文章的UV、作者的UV进行加一
  94. if totalRecord == 0 {
  95. e = models.UpdateYanxuanSpecialHzUv(specialId)
  96. if e != nil {
  97. err = errors.New("UpdateYanxuanSpecialHzUv, Err: " + e.Error())
  98. return
  99. }
  100. }
  101. }
  102. }
  103. itemLog := new(models.CygxYanxuanSpecialRecordLog)
  104. itemLog.UserId = user.UserId
  105. itemLog.Mobile = user.Mobile
  106. itemLog.Email = user.Email
  107. itemLog.CompanyId = user.CompanyId
  108. itemLog.CompanyName = user.CompanyName
  109. itemLog.RealName = user.RealName
  110. itemLog.SellerName = sellerName
  111. itemLog.CreateTime = time.Now().Add(-time.Duration(stopTime) * time.Second) // 往前推迟的时间就是他的阅读时间
  112. itemLog.ModifyTime = time.Now()
  113. itemLog.RegisterPlatform = utils.REGISTER_PLATFORM
  114. itemLog.YanxuanSpecialId = specialId
  115. itemLog.StopTime = stopTime
  116. itemLog.PermissionCode = permissionCode
  117. _, e = models.AddCygxYanxuanSpecialRecordLog(itemLog) // 添加历史记录
  118. if e != nil {
  119. err = errors.New("AddCygxYanxuanSpecialRecordLog, Err: " + e.Error())
  120. return
  121. }
  122. return
  123. }
  124. // GetYanxuanSpecialRecordByYanxuanSpecialId 获取研选专栏阅读 pv map
  125. func GetYanxuanSpecialRecordByYanxuanSpecialId(articleIds []int) (mapResp map[int]int) {
  126. var err error
  127. defer func() {
  128. if err != nil {
  129. fmt.Println(err)
  130. go utils.SendAlarmMsg("获取研选专栏阅读,信息失败,GetYanxuanSpecialRecordByYanxuanSpecialId Err:"+err.Error(), 3)
  131. }
  132. }()
  133. lenIds := len(articleIds)
  134. if lenIds == 0 {
  135. return
  136. }
  137. var condition string
  138. var pars []interface{}
  139. //condition = ` AND yanxuan_special_id IN (` + utils.GetOrmInReplace(lenIds) + `) `
  140. //pars = append(pars, articleIds)
  141. //listPv, e := models.GetCygxYanxuanSpecialRecordListPv(condition, pars)
  142. //if e != nil {
  143. // err = errors.New("GetCygxArticleHistoryRecordNewpvListPvCy, Err: " + e.Error())
  144. // return
  145. //}
  146. condition += ` AND id IN (` + utils.GetOrmInReplace(lenIds) + `) `
  147. pars = append(pars, articleIds)
  148. listPv, e := models.GetYanxuanSpecialListBycondition(condition, pars, 0, lenIds)
  149. if e != nil && e.Error() != utils.ErrNoRow() {
  150. err = errors.New("GetYanxuanSpecialListBycondition, Err: " + e.Error())
  151. return
  152. }
  153. mapResp = make(map[int]int, 0)
  154. for _, v := range listPv {
  155. mapResp[v.Id] = v.Pv + v.HzPv
  156. }
  157. return
  158. }