user_source_click_flow.go 2.8 KB

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