user_read_record.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package models
  2. import (
  3. "eta/eta_mini_crm/utils"
  4. "fmt"
  5. "time"
  6. "github.com/beego/beego/v2/client/orm"
  7. )
  8. type UserReadRecord struct {
  9. UserReadRecordId int `orm:"pk" description:"id"`
  10. UserId int `description:"用户id"`
  11. ReportId int `description:"报告id"`
  12. ReportTitle string `description:"报告标题"`
  13. // ChartPermissionId1 string `description:"一级品种id"`
  14. // ChartPermissionId2 string `description:"二级品种id"`
  15. ChartPermissionName string `description:"二级品种名称"`
  16. ClassifyId1 int `description:"一级级分类id"`
  17. ClassifyName1 string `description:"一级分类名称"`
  18. ClassifyId2 int `description:"二级分类id"`
  19. ClassifyName2 string `description:"二级分类名称"`
  20. ClassifyId3 int `description:"三级分类id"`
  21. ClassifyName3 string `description:"三级分类名称"`
  22. Timestamp int `description:"阅读开始时间戳"`
  23. EndTimestamp int `description:"阅读结束时间戳"`
  24. CreateTime time.Time `description:"创建时间"`
  25. CreateDate string `description:"创建日期"`
  26. StayTime string `description:"停留时间"`
  27. StayTimestamp string `description:"停留时间戳"`
  28. ReportType int `description:"报告类型:1-普通研报;2-pdf研报"`
  29. }
  30. type UserReadRecordView struct {
  31. UserReadRecordId int `orm:"pk" description:"id"`
  32. UserId int `description:"用户id"`
  33. ReportId int `description:"报告id"`
  34. ReportTitle string `description:"报告标题"`
  35. // ChartPermissionId1 string `description:"一级品种id"`
  36. // ChartPermissionId2 string `description:"二级品种id"`
  37. ChartPermissionName string `description:"二级品种名称"`
  38. ClassifyId1 int `description:"一级级分类id"`
  39. ClassifyName1 string `description:"一级分类名称"`
  40. ClassifyId2 int `description:"二级分类id"`
  41. ClassifyName2 string `description:"二级分类名称"`
  42. ClassifyId3 int `description:"三级分类id"`
  43. ClassifyName3 string `description:"三级分类名称"`
  44. Timestamp int `description:"阅读开始时间戳"`
  45. EndTimestamp int `description:"阅读结束时间戳"`
  46. CreateTime string `description:"创建时间"`
  47. CreateDate string `description:"创建日期"`
  48. StayTime string `description:"停留时间"`
  49. StayTimestamp string `description:"停留时间戳"`
  50. ReportType int `description:"报告类型:1-普通研报;2-pdf研报"`
  51. }
  52. type ReadCntStaitc struct {
  53. CreateDate string
  54. Count int
  55. }
  56. type PermissionCntStaitc struct {
  57. ChartPermissionId int
  58. PermissionName string
  59. Count int
  60. Percent float64
  61. }
  62. func GetUserReadRecordByUserId(firstClassifyIds, secondClassifyIds, thirdClassifyIds []int, userId int, condition string, pars []interface{}, startSize, pageSize int) (items []*UserReadRecordView, err error) {
  63. o := orm.NewOrm()
  64. sql := `SELECT DISTINCT ur.user_read_record_id, ur.report_id, ur.report_title, ur.chart_permission_name, ur.classify_name2,
  65. ur.create_time, ur.stay_time, ur.classify_id2, ur.classify_id3, ur.classify_name3, ur.classify_id1, ur.classify_name1
  66. FROM user_read_record AS ur
  67. LEFT JOIN user_read_permission2 AS urp2
  68. ON ur.user_read_record_id = urp2.user_read_record_id
  69. WHERE user_id = ? `
  70. if condition != "" {
  71. sql += condition
  72. }
  73. if len(firstClassifyIds) != 0 || len(secondClassifyIds) != 0 || len(thirdClassifyIds) != 0 {
  74. sql += ` AND (1=2 `
  75. if len(firstClassifyIds) > 0 {
  76. sql += fmt.Sprintf(" OR ur.classify_id1 IN (%s) ", utils.GetOrmReplaceHolder(len(firstClassifyIds)))
  77. }
  78. if len(secondClassifyIds) > 0 {
  79. sql += fmt.Sprintf(" OR ur.classify_id2 IN (%s) ", utils.GetOrmReplaceHolder(len(secondClassifyIds)))
  80. }
  81. if len(thirdClassifyIds) > 0 {
  82. sql += fmt.Sprintf(" OR ur.classify_id3 IN (%s) ", utils.GetOrmReplaceHolder(len(thirdClassifyIds)))
  83. }
  84. sql += ` ) `
  85. }
  86. sql += ` ORDER BY create_time DESC LIMIT ?, ?`
  87. _, err = o.Raw(sql, userId, pars, firstClassifyIds, secondClassifyIds, thirdClassifyIds, startSize, pageSize).QueryRows(&items)
  88. return
  89. }
  90. func GetUserReadRecordCountByUserId(firstClassifyIds, secondClassifyIds, thirdClassifyIds []int, userId int, condition string, pars []interface{}) (count int, err error) {
  91. o := orm.NewOrm()
  92. sql := `SELECT COUNT(DISTINCT ur.user_read_record_id) AS count
  93. FROM user_read_record AS ur
  94. LEFT JOIN user_read_permission2 AS urp2
  95. ON ur.user_read_record_id = urp2.user_read_record_id
  96. WHERE user_id = ? `
  97. if condition != "" {
  98. sql += condition
  99. }
  100. if len(firstClassifyIds) != 0 || len(secondClassifyIds) != 0 || len(thirdClassifyIds) != 0 {
  101. sql += ` AND (1=2 `
  102. if len(firstClassifyIds) > 0 {
  103. sql += fmt.Sprintf(" OR ur.classify_id1 IN (%s) ", utils.GetOrmReplaceHolder(len(firstClassifyIds)))
  104. }
  105. if len(secondClassifyIds) > 0 {
  106. sql += fmt.Sprintf(" OR ur.classify_id2 IN (%s) ", utils.GetOrmReplaceHolder(len(secondClassifyIds)))
  107. }
  108. if len(thirdClassifyIds) > 0 {
  109. sql += fmt.Sprintf(" OR ur.classify_id3 IN (%s) ", utils.GetOrmReplaceHolder(len(thirdClassifyIds)))
  110. }
  111. sql += ` ) `
  112. }
  113. err = o.Raw(sql, userId, pars, firstClassifyIds, secondClassifyIds, thirdClassifyIds).QueryRow(&count)
  114. return
  115. }
  116. func GetStaticReadCnt(condition string, pars []interface{}, startDate, endDate string) (items []*ReadCntStaitc, err error) {
  117. o := orm.NewOrm()
  118. sql := `SELECT create_date, COUNT(*) AS count
  119. FROM user_read_record AS ur
  120. LEFT JOIN user_read_permission2 AS urp
  121. ON ur.user_read_record_id = urp.user_read_record_id
  122. WHERE 1=1 `
  123. if condition != "" {
  124. sql += condition
  125. }
  126. sql += ` AND (ur.create_date BETWEEN ? AND ?) GROUP BY ur.create_date`
  127. _, err = o.Raw(sql, pars, startDate, endDate).QueryRows(&items)
  128. return
  129. }
  130. func GetStaticPermissionCnt(condition string, pars []interface{}) (items []*PermissionCntStaitc, err error) {
  131. o := orm.NewOrm()
  132. sql := `SELECT urp.chart_permission_id, urp.permission_name,COUNT(*) AS count
  133. FROM user_read_permission1 AS urp
  134. LEFT JOIN user_read_record AS ur
  135. ON urp.user_read_record_id = ur.user_read_record_id
  136. WHERE 1=1 `
  137. if condition != "" {
  138. sql += condition
  139. }
  140. sql += ` GROUP BY urp.chart_permission_id`
  141. _, err = o.Raw(sql, pars).QueryRows(&items)
  142. return
  143. }