report.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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,plate_name"
  16. taskColumns = "id,author,published_time"
  17. )
  18. type Report struct {
  19. Id int `description:"id"`
  20. OrgId int `description:"组织id"`
  21. Source ReportSource `description:"来源"`
  22. Title string `description:"标题"`
  23. Abstract string `description:"摘要"`
  24. Author string `description:"作者"`
  25. CoverSrc int `description:"封面"`
  26. PlateName string `description:"板块名称"`
  27. Status ReportStatus `description:"状态"`
  28. SendStatus SendStatus `description:"发送状态"`
  29. PublishedTime string `description:"发布时间"`
  30. CreatedTime time.Time `description:"创建时间"`
  31. UpdatedTime time.Time `description:"更新时间"`
  32. }
  33. type ReportView struct {
  34. Id int
  35. OrgId int
  36. Source ReportSource
  37. Title string
  38. Abstract string
  39. Author string
  40. CoverSrc string
  41. PlateName string
  42. PermissionNames string
  43. SendStatus SendStatus
  44. PublishedTime string
  45. }
  46. func (r *Report) ToView() (item *ReportView) {
  47. item = &ReportView{
  48. Id: r.Id,
  49. OrgId: r.OrgId,
  50. Source: r.Source,
  51. Title: r.Title,
  52. Abstract: r.Abstract,
  53. Author: r.Author,
  54. PlateName: r.PlateName,
  55. SendStatus: r.SendStatus,
  56. PublishedTime: r.PublishedTime,
  57. }
  58. return
  59. }
  60. func (r *Report) TableName() string {
  61. return "reports"
  62. }
  63. func (r *Report) UpdateSendStatus(status SendStatus) (err error) {
  64. o := orm.NewOrm()
  65. r.SendStatus = status
  66. _, err = o.Update(r, "send_status")
  67. return
  68. }
  69. func GetReportCountByCondition(condition string, pars []interface{}) (count int, err error) {
  70. o := orm.NewOrm()
  71. sql := `SELECT COUNT(*) AS count FROM reports WHERE 1=1 `
  72. if condition != "" {
  73. sql += condition
  74. }
  75. err = o.Raw(sql, pars).QueryRow(&count)
  76. return
  77. }
  78. func GetReportById(id int) (items *Report, err error) {
  79. o := orm.NewOrm()
  80. sql := `SELECT distinct id ,title,abstract,author,published_time,send_status FROM reports WHERE id =?`
  81. err = o.Raw(sql, id).QueryRow(&items)
  82. return
  83. }
  84. func GetReportByCondition(condition, sortCondition string, pars []interface{}, startPage, pageSize int) (items []*Report, err error) {
  85. o := orm.NewOrm()
  86. sql := `SELECT * FROM reports WHERE 1=1 `
  87. if condition != "" {
  88. sql += condition
  89. }
  90. if sortCondition != "" {
  91. sql += sortCondition // 排序
  92. }
  93. sql += ` LIMIT ?,?`
  94. _, err = o.Raw(sql, pars, startPage, pageSize).QueryRows(&items)
  95. return
  96. }
  97. func GetETAReportIdsByCondition(condition string, pars []interface{}) (ids []int, err error) {
  98. o := orm.NewOrmUsingDB("rddp")
  99. sql := `SELECT distinct id FROM report WHERE 1=1 `
  100. if condition != "" {
  101. sql += condition
  102. }
  103. _, err = o.Raw(sql, pars).QueryRows(&ids)
  104. return
  105. }
  106. func GetETAReportById(id int) (classifyId int, err error) {
  107. o := orm.NewOrmUsingDB("rddp")
  108. sql := `SELECT COALESCE(NULLIF(classify_id_third,0),NULLIF(classify_id_second,0),classify_id_first) classify_id FROM report WHERE id =?`
  109. err = o.Raw(sql, id).QueryRow(&classifyId)
  110. return
  111. }
  112. func GetETAReportIdByClassifyId(id int) (classifyId int, err error) {
  113. o := orm.NewOrmUsingDB("rddp")
  114. sql := `SELECT COALESCE(NULLIF(classify_id_third,0),NULLIF(classify_id_second,0),classify_id_first) classify_id FROM report WHERE id=? `
  115. err = o.Raw(sql, id).QueryRow(&classifyId)
  116. return
  117. }