article_history_record.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_mfyx/utils"
  6. "time"
  7. )
  8. type CygxArticleHistoryRecord struct {
  9. Id int `orm:"column(id);pk"`
  10. ArticleId int
  11. UserId int
  12. CreateTime time.Time
  13. Mobile string `description:"手机号"`
  14. Email string `description:"邮箱"`
  15. CompanyId int `description:"公司id"`
  16. CompanyName string `description:"公司名称"`
  17. ModifyTime time.Time `description:"修改时间"`
  18. StopTime int `description:"停留时间"`
  19. OutType int `description:"退出方式,1正常退出,2强制关闭"`
  20. }
  21. type CygxArticleHistoryResp struct {
  22. Pv int `description:"阅读PV"`
  23. ArticleId int `description:"文章id"`
  24. Num int `description:"数量"`
  25. }
  26. // 添加历史信息
  27. func AddCygxArticleHistoryRecord(item *CygxArticleHistoryRecord) (lastId int64, err error) {
  28. o, err := orm.NewOrm().Begin()
  29. if err != nil {
  30. return
  31. }
  32. defer func() {
  33. fmt.Println(err)
  34. if err == nil {
  35. o.Commit()
  36. } else {
  37. o.Rollback()
  38. }
  39. }()
  40. //var count int
  41. //sql := `SELECT COUNT(1) AS count FROM cygx_article_history_record WHERE user_id=? AND article_id=? `
  42. //err = o.Raw(sql, item.UserId, item.ArticleId).QueryRow(&count)
  43. sql := `UPDATE wx_user SET report_last_view_time=NOW() WHERE user_id=?`
  44. _, err = o.Raw(sql, item.UserId).Exec()
  45. if err != nil {
  46. return
  47. }
  48. //if count > 0 {
  49. // sql := `UPDATE cygx_article_history_record SET modify_time=NOW() WHERE user_id=? AND article_id=? `
  50. // _, err = o.Raw(sql, item.UserId, item.ArticleId).Exec()
  51. //} else {
  52. // item.ModifyTime = time.Now()
  53. // lastId, err = o.Insert(item)
  54. //}
  55. item.ModifyTime = time.Now()
  56. lastId, err = o.Insert(item)
  57. return
  58. }
  59. // 获取用户阅读记录
  60. func GetUserToArticleCount(uid, articleId int) (count int, err error) {
  61. sqlCount := `SELECT COUNT(1) AS count FROM cygx_article_history_record WHERE user_id=? AND article_id=? `
  62. o := orm.NewOrm()
  63. err = o.Raw(sqlCount, uid, articleId).QueryRow(&count)
  64. return
  65. }
  66. type AddStopTimeRep struct {
  67. ArticleId int `description:"文章ID"`
  68. StopTime int `description:"停留时间"`
  69. OutType int `description:"退出方式,1正常退出,2强制关闭"`
  70. Source string `description:"来源,MOBILE:手机端,PC:电脑端"`
  71. }
  72. type AddStopTimeHtgjRep struct {
  73. ArticleId int `description:"文章ID"`
  74. StopTime int `description:"停留时间"`
  75. OutType int `description:"退出方式,1正常退出,2强制关闭"`
  76. Source string `description:"来源,MOBILE:手机端,PC:电脑端"`
  77. CompanyCode string `description:"机构编码"`
  78. CompanyName string `description:"机构名称"`
  79. Email string `description:"机构邮箱"`
  80. Sign string `description:"签名"`
  81. }
  82. type AddStopTimeNewRep struct {
  83. Id int `description:"ID"`
  84. ArticleId int `description:"文章ID"`
  85. StopTime int `description:"停留时间"`
  86. OutType int `description:"退出方式,1正常退出,2强制关闭"`
  87. }
  88. type ArticleDetailAddStopTimeRep struct {
  89. HasPermission int `description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,已提交过申请,4:无该行业权限,未提交过申请,5:潜在客户,未提交过申请,6:潜在客户,已提交过申请"`
  90. HasFree int `description:"1:已付费(至少包含一个品类的权限),2:未付费(没有任何品类权限)"`
  91. }
  92. func UpdateArticleStopTime(item *AddStopTimeNewRep) (err error) {
  93. o := orm.NewOrm()
  94. sql := `UPDATE cygx_article_history_record SET stop_time = ?,out_type = ? WHERE id =?`
  95. _, err = o.Raw(sql, item.StopTime, item.OutType, item.Id).Exec()
  96. return
  97. }
  98. // 获取最新的一条阅读记录
  99. func GetNewArticleHistoryRecord(uid, articleId int) (item *AddStopTimeNewRep, err error) {
  100. o := orm.NewOrm()
  101. sql := `SELECT * FROM cygx_article_history_record WHERE user_id = ? AND article_id = ? ORDER BY id DESC LIMIT 1;`
  102. err = o.Raw(sql, uid, articleId).QueryRow(&item)
  103. return
  104. }
  105. // 获取用户阅读记录
  106. func GetNoAddStoptimeArticleCount(uid, articleId int) (count int, err error) {
  107. sqlCount := `SELECT COUNT(1) AS count FROM cygx_article_history_record WHERE user_id=? AND article_id=? AND create_time > '` + utils.OnlineTime + `' AND stop_time = 0 `
  108. o := orm.NewOrm()
  109. err = o.Raw(sqlCount, uid, articleId).QueryRow(&count)
  110. return
  111. }
  112. // 最新标的列表
  113. func GetArticleHistoryList() (items []*CygxArticleHistoryRecordNewpv, err error) {
  114. o := orm.NewOrm()
  115. sql := `SELECT * FROM cygx_article_history_record WHERE company_id != 16 `
  116. _, err = o.Raw(sql).QueryRows(&items)
  117. return
  118. }
  119. // 获取用户阅读记录
  120. func GetUserToArticleHistory(uid int, articleIdArr []int) (items []*CygxArticleHistoryResp, err error) {
  121. arrLen := len(articleIdArr)
  122. if arrLen == 0 {
  123. return
  124. }
  125. sql := `SELECT
  126. article_id
  127. FROM
  128. cygx_article_history_record
  129. WHERE
  130. 1 = 1
  131. AND user_id = ?
  132. AND article_id IN (` + utils.GetOrmInReplace(len(articleIdArr)) + `)
  133. GROUP BY
  134. article_id `
  135. o := orm.NewOrm()
  136. _, err = o.Raw(sql, uid, articleIdArr).QueryRows(&items)
  137. return
  138. }