user_source_click_flow.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package user
  2. import (
  3. "eta/eta_mini_ht_api/models"
  4. "eta/eta_mini_ht_api/models/message"
  5. "gorm.io/gorm"
  6. "time"
  7. )
  8. const (
  9. userSourceClickFlows = "user_source_click_flows"
  10. )
  11. // UserReportClickFlow 用户点击研报流水记录
  12. type UserSourceClickFlow struct {
  13. ID int `gorm:"column:id;primaryKey;autoIncrement:'id'"`
  14. TraceId string `gorm:"column:trace_id"`
  15. UserID int `gorm:"column:user_id"` // 用户ID\
  16. Mobile string `gorm:"column:mobile"`
  17. SourceId int `gorm:"column:source_id"` // 研报ID
  18. SourceTitle string `gorm:"column:source_title"` // 研报ID
  19. SourceType message.SourceType `gorm:"column:source_type;type:enum('REPORT','VIDEO','AUDIO')"`
  20. ClickTime time.Time `gorm:"column:click_time"` // 点击时间
  21. ReadDurationSeconds int64 `gorm:"column:read_duration_seconds"`
  22. IPAddress string `gorm:"column:ip_address"` // IP地址
  23. Location string `gorm:"column:location"` // 地理位置
  24. Hidden bool `gorm:"column:hidden;type:tinyint(1);default:0"`
  25. Referer string `gorm:"column:referer"` // 来源页面
  26. AdditionalData string `gorm:"column:additional_data"` // 额外数据
  27. }
  28. func (v *UserSourceClickFlow) BeforeCreate(tx *gorm.DB) (err error) {
  29. v.ClickTime = time.Now()
  30. return
  31. }
  32. func UpdateSourceClicks(count UserSourceClickFlow) (err error) {
  33. db := models.Main()
  34. err = db.Table(userSourceClickFlows).Where("trace_id =?", count.TraceId).UpdateColumn("read_duration_seconds", count.ReadDurationSeconds).Error
  35. return
  36. }
  37. func CountSourceClicks(count UserSourceClickFlow) (err error) {
  38. db := models.Main()
  39. err = db.Create(&count).Error
  40. return
  41. }
  42. func GetClickRecordByTraceId(traceId string) (record UserSourceClickFlow, err error) {
  43. db := models.Main()
  44. err = db.Select("*").Where("trace_id=?", traceId).First(&record).Error
  45. return
  46. }
  47. type CountClickFlowById struct {
  48. SourceId int `gorm:"column:source_id"`
  49. Count int `gorm:"column:count"`
  50. }
  51. func GetTimeDurationReportCountsById(begin string, end string, limit int, sourceType message.SourceType) (ids []CountClickFlowById, err error) {
  52. db := models.Main()
  53. err = db.Table(userSourceClickFlows).Select("source_id,count(*) count").Where("source_id>0 and source_type=? and DATE(click_time) BETWEEN ? AND ? AND hidden =0", sourceType, begin, end).Group("source_id").Order("count desc").Limit(limit).Scan(&ids).Error
  54. return
  55. }
  56. func HiddenFlows(id int, sourceType message.SourceType) (err error) {
  57. db := models.Main()
  58. err = db.Table(userSourceClickFlows).Where("source_type=? and source_id =?", sourceType, id).Update("hidden", 1).Error
  59. return
  60. }
  61. func ShowFlows(id int, sourceType message.SourceType) (err error) {
  62. db := models.Main()
  63. err = db.Table(userSourceClickFlows).Where("source_type=? and source_id =?", sourceType, id).Update("hidden", 0).Error
  64. return
  65. }