report.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package models
  2. import (
  3. "time"
  4. "github.com/beego/beego/v2/client/orm"
  5. )
  6. type ReportStatus string
  7. type ReportSource string
  8. const (
  9. SourceETA ReportSource = "ETA"
  10. SourceHT ReportSource = "HT"
  11. StatusInit ReportStatus = "INIT"
  12. StatusPending ReportStatus = "PENDING"
  13. StatusDone ReportStatus = "DONE"
  14. MaxBatchNum = 1000
  15. CommonColumns = "id,org_id,author,abstract,title,source,cover_src,published_time"
  16. taskColumns = "id,author,published_time"
  17. )
  18. type Report struct {
  19. ID int
  20. OrgID int
  21. Source ReportSource
  22. Title string
  23. Abstract string
  24. Author string
  25. CoverSrc string
  26. Status ReportStatus
  27. SendStatus SendStatus
  28. PublishedTime string
  29. CreatedTime time.Time
  30. UpdatedTime time.Time
  31. }
  32. type ReportView struct {
  33. ID int
  34. OrgID int
  35. Source ReportSource
  36. Title string
  37. Abstract string
  38. Author string
  39. CoverSrc string
  40. SendStatus SendStatus
  41. PublishedTime string
  42. }
  43. func (r *Report) ToView() (item *ReportView) {
  44. item = &ReportView{
  45. ID: r.ID,
  46. OrgID: r.OrgID,
  47. Source: r.Source,
  48. Title: r.Title,
  49. Abstract: r.Abstract,
  50. Author: r.Author,
  51. CoverSrc: r.CoverSrc,
  52. SendStatus: r.SendStatus,
  53. PublishedTime: r.PublishedTime,
  54. }
  55. return
  56. }
  57. func (r *Report) UpdateSendStatus() (err error) {
  58. o := orm.NewOrm()
  59. r.SendStatus = SEND
  60. _, err = o.Update(r, "send_status")
  61. return
  62. }
  63. func (r *Report) tableName() string {
  64. return "reports"
  65. }
  66. func GetReportCountByCondition(condition string, pars []interface{}) (count int, err error) {
  67. o := orm.NewOrm()
  68. sql := `SELECT COUNT(*) AS count FROM reports WHERE 1=1 `
  69. if condition != "" {
  70. sql += condition
  71. }
  72. err = o.Raw(sql, pars).QueryRow(&count)
  73. return
  74. }
  75. func GetReportById(id int) (items *Report, err error) {
  76. o := orm.NewOrm()
  77. sql := `SELECT * FROM reports WHERE id =?`
  78. err = o.Raw(sql, id).QueryRow(&items)
  79. return
  80. }
  81. func GetReportByCondition(condition, sortCondition string, pars []interface{}, startPage, pageSize int) (items []*Report, err error) {
  82. o := orm.NewOrm()
  83. sql := `SELECT * FROM reports WHERE 1=1 `
  84. if condition != "" {
  85. sql += condition
  86. }
  87. if sortCondition != "" {
  88. sql += sortCondition // 排序
  89. }
  90. sql += ` LIMIT ?,?`
  91. _, err = o.Raw(sql, pars, startPage, pageSize).QueryRows(&items)
  92. return
  93. }
  94. func GetETAReportIdsByCondition(condition string, pars []interface{}) (ids []int, err error) {
  95. o := orm.NewOrmUsingDB("rddp")
  96. sql := `SELECT distinct id FROM report WHERE 1=1 `
  97. if condition != "" {
  98. sql += condition
  99. }
  100. _, err = o.Raw(sql, pars).QueryRows(&ids)
  101. return
  102. }