user_report_view_record.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "strings"
  6. "sync"
  7. )
  8. // UserReportViewRecord 用户报告阅读记录表(汇总表)
  9. type UserReportViewRecord struct {
  10. Id int `orm:"column(id);pk"`
  11. Source int8 `description:"来源,1:rddp的报告;2:weekly_report的PHP报告;3:weekly_report商品的报告(应该是作废了);4:察研观向的报告"`
  12. UserId int `description:"用户id"`
  13. ReportId int `description:"报告id"`
  14. ReportChapterId int `description:"报告章节id"`
  15. Mobile string `description:"手机号"`
  16. Email string `description:"邮箱"`
  17. RealName string `description:"用户实际姓名"`
  18. CompanyName string `description:"公司名称"`
  19. StopTime int `description:"停留时间"`
  20. OutId string `description:"外部关联id"`
  21. CreateTime string `description:"创建时间"`
  22. }
  23. // TableName 表名
  24. func (obj UserReportViewRecord) TableName() string {
  25. return `user_report_view_record`
  26. }
  27. // GetViewReportList 单条记录插入
  28. func (obj UserReportViewRecord) GetViewReportList(year int, condition string, pars []interface{}, startSize, pageSize int) (total int64, items []*UserReportViewRecord, err error) {
  29. tableName := obj.GetTableName(year)
  30. o := orm.NewOrm()
  31. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1 = 1 `, tableName)
  32. if condition != "" {
  33. sql += condition
  34. }
  35. sql += ` ORDER BY create_time DESC,id DESC LIMIT ?,? `
  36. total, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  37. return
  38. }
  39. // Insert 单条记录插入
  40. func (obj UserReportViewRecord) Insert(year int, item *UserReportViewRecord) (err error) {
  41. tableName := obj.GetTableName(year)
  42. sql := "INSERT INTO " + tableName + " (`source`, `user_id`, `report_id`, `report_chapter_id`, `mobile`, `email`, `real_name`, `company_name`, `stop_time`, `out_id`, `create_time`) VALUES "
  43. valStr := fmt.Sprintf(`( %d, %d, %d, %d, "%s", "%s","%s", "%s", %d, "%s", "%s")`, item.Source, item.UserId, item.ReportId, item.ReportChapterId, item.Mobile, item.Email, item.RealName, item.CompanyName, item.StopTime, item.OutId, item.CreateTime)
  44. sql = sql + valStr + ";"
  45. o := orm.NewOrm()
  46. // 先校验表是否存在,不存在的话,需要创建表
  47. err = obj.CheckAndCreateTable(o, tableName)
  48. if err != nil {
  49. return
  50. }
  51. // 插入数据
  52. _, err = o.Raw(sql).Exec()
  53. return
  54. }
  55. // InsertMul 单条记录插入
  56. func (obj UserReportViewRecord) InsertMul(year int, items []*UserReportViewRecord) (err error) {
  57. tableName := obj.GetTableName(year)
  58. sql := "INSERT INTO " + tableName + " (`source`, `user_id`, `report_id`, `report_chapter_id`, `mobile`, `email`, `real_name`, `company_name`, `stop_time`, `out_id`, `create_time`) VALUES "
  59. valStrList := make([]string, 0)
  60. for _, item := range items {
  61. valStrList = append(valStrList, fmt.Sprintf(`( %d, %d, %d, %d, "%s", "%s","%s", "%s", %d, "%s", "%s")`, item.Source, item.UserId, item.ReportId, item.ReportChapterId, item.Mobile, item.Email, item.RealName, item.CompanyName, item.StopTime, item.OutId, item.CreateTime))
  62. }
  63. if len(valStrList) <= 0 {
  64. return
  65. }
  66. valStr := strings.Join(valStrList, ",")
  67. sql = sql + valStr + ";"
  68. o := orm.NewOrm()
  69. // 先校验表是否存在,不存在的话,需要创建表
  70. err = obj.CheckAndCreateTable(o, tableName)
  71. if err != nil {
  72. return
  73. }
  74. // 插入数据
  75. _, err = o.Raw(sql).Exec()
  76. return
  77. }
  78. // GetTableName 获取表名
  79. func (obj UserReportViewRecord) GetTableName(year int) string {
  80. return fmt.Sprintf(`user_report_view_record_%d`, year)
  81. }
  82. // tableNameMap 已创建表的map
  83. var tableNameMap = map[string]string{}
  84. // createTableMutex 创建表的时候的锁
  85. var createTableMutex = &sync.Mutex{}
  86. // CheckAndCreateTable 通过表名校验表是否存在,不存在的话,需要创建表
  87. func (obj UserReportViewRecord) CheckAndCreateTable(o orm.Ormer, tableName string) (err error) {
  88. if _, ok := tableNameMap[tableName]; ok {
  89. return
  90. }
  91. // 表判断的时候需要加锁,避免多个协程调用时,表还没有创建成功,又被另一个协程发现表不存在,然后再次创建表
  92. createTableMutex.Lock()
  93. defer func() {
  94. createTableMutex.Unlock()
  95. }()
  96. db := o.Raw(fmt.Sprintf(`SHOW TABLES LIKE "%s"`, tableName))
  97. var result []orm.Params
  98. _, err = db.Values(&result)
  99. if err != nil {
  100. fmt.Println("查询表失败:", err)
  101. return
  102. }
  103. // 不存在该表,那么就去创建表
  104. if len(result) <= 0 {
  105. baseSql := fmt.Sprintf("CREATE TABLE `%s` (\n `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id',\n `source` tinyint(20) NOT NULL DEFAULT '0' COMMENT '来源,1:rddp的报告;2:weekly_report的PHP报告;3:weekly_report商品的报告(应该是作废了);4:察研观向的报告',\n `user_id` int(11) DEFAULT '0' COMMENT '用户id',\n `report_id` int(11) DEFAULT '0' COMMENT '报告id',\n `report_chapter_id` int(10) DEFAULT NULL COMMENT '报告章节id',\n `mobile` varchar(20) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '手机号',\n `email` varchar(100) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '邮箱',\n `real_name` varchar(30) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '用户时间名称',\n `company_name` varchar(100) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '公司名称',\n `stop_time` int(5) NOT NULL DEFAULT '0' COMMENT '停留时间',\n `out_id` varchar(20) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '外部关联id',\n `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '阅读时间',\n PRIMARY KEY (`id`),\n KEY `idx_user_id` (`user_id`),\n KEY `idx_report_id` (`report_id`),\n KEY `idx_mobile` (`mobile`),\n KEY `idx_email` (`email`),\n KEY `idx_ctime` (`create_time`)\n) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;", tableName)
  106. _, err = o.Raw(baseSql).Exec()
  107. if err != nil {
  108. return
  109. }
  110. }
  111. tableNameMap[tableName] = tableName
  112. return
  113. }