article_history_record.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package models
  2. import (
  3. "fmt"
  4. "hongze/hongze_cygx/utils"
  5. "rdluck_tools/orm"
  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. }
  19. //添加历史信息
  20. func AddCygxArticleHistoryRecord(item *CygxArticleHistoryRecord) (lastId int64, err error) {
  21. o := orm.NewOrm()
  22. o.Begin()
  23. defer func() {
  24. fmt.Println(err)
  25. if err == nil {
  26. o.Commit()
  27. } else {
  28. o.Rollback()
  29. }
  30. }()
  31. //var count int
  32. //sql := `SELECT COUNT(1) AS count FROM cygx_article_history_record WHERE user_id=? AND article_id=? `
  33. //err = o.Raw(sql, item.UserId, item.ArticleId).QueryRow(&count)
  34. sql := `UPDATE wx_user SET report_last_view_time=NOW() WHERE user_id=?`
  35. _, err = o.Raw(sql, item.UserId).Exec()
  36. if err != nil {
  37. return
  38. }
  39. //if count > 0 {
  40. // sql := `UPDATE cygx_article_history_record SET modify_time=NOW() WHERE user_id=? AND article_id=? `
  41. // _, err = o.Raw(sql, item.UserId, item.ArticleId).Exec()
  42. //} else {
  43. // item.ModifyTime = time.Now()
  44. // lastId, err = o.Insert(item)
  45. //}
  46. item.ModifyTime = time.Now()
  47. lastId, err = o.Insert(item)
  48. return
  49. }
  50. //获取用户阅读记录
  51. func GetUserToArticleCount(uid, articleId int) (count int, err error) {
  52. sqlCount := `SELECT COUNT(1) AS count FROM cygx_article_history_record WHERE user_id=? AND article_id=? `
  53. o := orm.NewOrm()
  54. err = o.Raw(sqlCount, uid, articleId).QueryRow(&count)
  55. return
  56. }
  57. type AddStopTimeRep struct {
  58. ArticleId int `description:"文章ID"`
  59. StopTime int `description:"停留时间"`
  60. }
  61. type AddStopTimeNewRep struct {
  62. Id int `description:"ID"`
  63. ArticleId int `description:"文章ID"`
  64. StopTime int `description:"停留时间"`
  65. }
  66. type ArticleDetailAddStopTimeRep struct {
  67. HasPermission int `description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,已提交过申请,4:无该行业权限,未提交过申请,5:潜在客户,未提交过申请,6:潜在客户,已提交过申请"`
  68. HasFree int `description:"1:已付费(至少包含一个品类的权限),2:未付费(没有任何品类权限)"`
  69. }
  70. func AddArticleStopTime(item *AddStopTimeNewRep) (err error) {
  71. o := orm.NewOrm()
  72. //o.Begin()
  73. //defer func() {
  74. // if err == nil {
  75. // o.Commit()
  76. // } else {
  77. // o.Rollback()
  78. // }
  79. //}()
  80. sql := `UPDATE cygx_article_history_record SET stop_time = ? WHERE id =?`
  81. _, err = o.Raw(sql, item.StopTime, item.Id).Exec()
  82. if err != nil && err.Error() != utils.ErrNoRow() {
  83. return
  84. }
  85. //sql = ` DELETE FROM cygx_article_history_record WHERE create_time > '2021-06-01 00:00:00' AND stop_time = 0`
  86. //_, err = o.Raw(sql).Exec()
  87. return
  88. }
  89. //获取最新的一条阅读记录
  90. func GetNewArticleHistoryRecord(uid, articleId int) (item *AddStopTimeNewRep, err error) {
  91. o := orm.NewOrm()
  92. sql := `SELECT * FROM cygx_article_history_record WHERE user_id = ? AND article_id = ? ORDER BY id DESC LIMIT 1;`
  93. err = o.Raw(sql, uid, articleId).QueryRow(&item)
  94. return
  95. }
  96. //获取用户阅读记录
  97. func GetNoAddStoptimeArticleCount(uid, articleId int) (count int, err error) {
  98. 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 `
  99. //sqlCount := `SELECT COUNT(1) AS count FROM cygx_article_history_record WHERE id = ? AND stop_time = 0 `
  100. o := orm.NewOrm()
  101. err = o.Raw(sqlCount, uid, articleId).QueryRow(&count)
  102. return
  103. }