article_history_record_newpv.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_cygx/utils"
  6. "strconv"
  7. "time"
  8. )
  9. type CygxArticleHistoryRecordNewpv struct {
  10. Id int `orm:"column(id);pk"`
  11. ArticleId int
  12. UserId int
  13. CreateTime time.Time
  14. ModifyTime time.Time
  15. Mobile string `description:"手机号"`
  16. Email string `description:"邮箱"`
  17. CompanyId int `description:"公司id"`
  18. CompanyName string `description:"公司名称"`
  19. StopTime int `description:"停留时间"`
  20. OutType int `description:"退出方式,1正常退出,2强制关闭"`
  21. Source string `description:"来源,MOBILE:手机端,PC:电脑端"`
  22. }
  23. //添加阅读记录信息
  24. func AddCygxArticleViewRecordNewpv(item *CygxArticleHistoryRecordNewpv) (lastId int64, err error) {
  25. o, err := orm.NewOrm().Begin()
  26. if err != nil {
  27. return
  28. }
  29. defer func() {
  30. fmt.Println(err)
  31. if err == nil {
  32. o.Commit()
  33. } else {
  34. o.Rollback()
  35. }
  36. }()
  37. lastId, err = o.Insert(item)
  38. //写入记录到总的统计表
  39. record := new(CygxArticleHistoryRecordAll)
  40. record.UserId = item.UserId
  41. record.ArticleId = item.ArticleId
  42. record.CreateTime = time.Now().Format(utils.FormatDateTime)
  43. record.ModifyTime = item.ModifyTime
  44. record.Mobile = item.Mobile
  45. record.Email = item.Email
  46. record.CompanyId = item.CompanyId
  47. record.CompanyName = item.CompanyName
  48. record.StopTime = item.StopTime
  49. record.OutType = item.OutType
  50. record.Source = item.Source
  51. record.Platfor = 1
  52. lastId, err = o.Insert(record)
  53. // 软删除当天策略平台的文章阅读记录
  54. if item.Mobile != "" {
  55. sql := `UPDATE cygx_article_history_record_all
  56. SET is_del = 1
  57. WHERE
  58. article_id = ?
  59. AND mobile = ?
  60. AND platfor = 2
  61. AND create_time >= date(NOW()) `
  62. _, err = o.Raw(sql, record.ArticleId, record.Mobile).Exec()
  63. }
  64. return
  65. }
  66. //获取最新的一条阅读记录
  67. func GetNewArticleHistoryRecordNewpv(uid, articleId int, modifytime string) (item *AddStopTimeNewRep, err error) {
  68. o := orm.NewOrm()
  69. sql := `SELECT * FROM cygx_article_history_record_newpv WHERE user_id = ? AND article_id = ? AND modify_time <='` + modifytime + `' ORDER BY id DESC LIMIT 1;`
  70. err = o.Raw(sql, uid, articleId).QueryRow(&item)
  71. return
  72. }
  73. //把十分钟之内的阅读记录进行累加
  74. func UpdateCygxArticleViewRecordNewpv(itemRep *CygxArticleHistoryRecordNewpv, stopTime int) (err error) {
  75. o, err := orm.NewOrm().Begin()
  76. if err != nil {
  77. return
  78. }
  79. defer func() {
  80. fmt.Println(err)
  81. if err == nil {
  82. o.Commit()
  83. } else {
  84. o.Rollback()
  85. }
  86. }()
  87. sql := `UPDATE cygx_article_history_record_newpv
  88. SET modify_time = NOW(), stop_time = stop_time + ` + strconv.Itoa(stopTime) + `
  89. WHERE
  90. article_id = ?
  91. AND user_id = ?
  92. AND out_type = 2
  93. AND timestampdiff(MINUTE,modify_time,NOW()) < 10`
  94. _, err = o.Raw(sql, itemRep.ArticleId, itemRep.UserId).Exec()
  95. // 修改总表的停留时间
  96. sql = `UPDATE cygx_article_history_record_all
  97. SET modify_time = NOW(), stop_time = stop_time + ` + strconv.Itoa(stopTime) + `
  98. WHERE
  99. article_id = ?
  100. AND user_id = ?
  101. AND out_type = 2
  102. AND timestampdiff(MINUTE,modify_time,NOW()) < 10`
  103. _, err = o.Raw(sql, itemRep.ArticleId, itemRep.UserId).Exec()
  104. return
  105. }
  106. //把十分钟之内的阅读记录进行累加
  107. func UpdateCygxArticleViewRecordNewpvList(itemRep *CygxArticleHistoryRecordNewpv, stopTime int) (err error) {
  108. o := orm.NewOrm()
  109. sql := `UPDATE cygx_article_history_record_newpv
  110. SET stop_time = stop_time + ` + strconv.Itoa(stopTime) + `
  111. WHERE
  112. article_id = ?
  113. AND user_id = ?
  114. AND modify_time = ?
  115. AND id = ?`
  116. _, err = o.Raw(sql, itemRep.ArticleId, itemRep.UserId, itemRep.ModifyTime, itemRep.Id).Exec()
  117. return
  118. }
  119. type CygxArticleHistoryRecordAll struct {
  120. Id int `orm:"column(id);pk"`
  121. ArticleId int
  122. UserId int
  123. CreateTime string
  124. ModifyTime time.Time
  125. Mobile string `description:"手机号"`
  126. Email string `description:"邮箱"`
  127. CompanyId int `description:"公司id"`
  128. CompanyName string `description:"公司名称"`
  129. StopTime int `description:"停留时间"`
  130. OutType int `description:"退出方式,1正常退出,2强制关闭"`
  131. Source string `description:"来源,MOBILE:手机端,PC:电脑端"`
  132. RealName string `description:"用户实际名称"`
  133. CreateDateApi time.Time `description:"同步创建时间"`
  134. CelueHistoryId int `description:"策略平台记录的ID"`
  135. Platfor int `description:"PV阅读记录来源,1:查研观向,2:策略平台"`
  136. IsDel int `description:"是否删除"`
  137. }
  138. //获取当天总表的阅读记录
  139. func GetArticleHistoryRecordAllList() (items []*CygxArticleHistoryRecordNewpv, err error) {
  140. o := orm.NewOrm()
  141. sql := ` SELECT * FROM cygx_article_history_record_all WHERE create_time >= date(NOW())
  142. AND mobile <> ''
  143. AND platfor = 1
  144. GROUP BY mobile,article_id `
  145. _, err = o.Raw(sql).QueryRows(&items)
  146. return
  147. }