report.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package models
  2. import (
  3. "eta/eta_mini_crm_ht/utils"
  4. "time"
  5. "github.com/beego/beego/v2/client/orm"
  6. )
  7. type ReportStatus string
  8. type ReportSource string
  9. const (
  10. SourceETA ReportSource = "ETA"
  11. SourceHT ReportSource = "HT"
  12. StatusInit ReportStatus = "INIT"
  13. StatusPending ReportStatus = "PENDING"
  14. StatusDone ReportStatus = "DONE"
  15. MaxBatchNum = 1000
  16. CommonColumns = "id,org_id,author,abstract,title,source,cover_src,published_time,plate_name"
  17. taskColumns = "id,author,published_time"
  18. )
  19. type Report struct {
  20. Id int `description:"id"`
  21. OrgId int `description:"组织id"`
  22. Source ReportSource `description:"来源"`
  23. Title string `description:"标题"`
  24. Abstract string `description:"摘要"`
  25. Author string `description:"作者"`
  26. CoverSrc int `description:"封面"`
  27. ClassifyId int `description:"分类Id"`
  28. PlateName string `description:"板块名称"`
  29. Status ReportStatus `description:"状态"`
  30. SendStatus SendStatus `description:"发送状态"`
  31. PublishedTime string `description:"发布时间"`
  32. CreatedTime time.Time `description:"创建时间"`
  33. UpdatedTime time.Time `description:"更新时间"`
  34. }
  35. type ReportView struct {
  36. Id int
  37. OrgId int
  38. Source ReportSource
  39. Title string
  40. Abstract string
  41. Author string
  42. CoverSrc string
  43. PlateName string
  44. PermissionNames string
  45. SendStatus SendStatus
  46. PublishedTime string
  47. }
  48. func (r *Report) ToView() (item *ReportView) {
  49. item = &ReportView{
  50. Id: r.Id,
  51. OrgId: r.OrgId,
  52. Source: r.Source,
  53. Title: r.Title,
  54. Abstract: r.Abstract,
  55. Author: r.Author,
  56. PlateName: r.PlateName,
  57. SendStatus: r.SendStatus,
  58. PublishedTime: r.PublishedTime,
  59. }
  60. return
  61. }
  62. func (r *Report) TableName() string {
  63. return "reports"
  64. }
  65. func (r *Report) UpdateSendStatus(status SendStatus) (err error) {
  66. o := orm.NewOrm()
  67. r.SendStatus = status
  68. _, err = o.Update(r, "send_status")
  69. return
  70. }
  71. func GetReportCountByCondition(condition string, pars []interface{}) (count int, err error) {
  72. o := orm.NewOrm()
  73. sql := `SELECT COUNT(*) AS count FROM reports WHERE 1=1 `
  74. if condition != "" {
  75. sql += condition
  76. }
  77. err = o.Raw(sql, pars).QueryRow(&count)
  78. return
  79. }
  80. func GetReportById(id int) (items *Report, err error) {
  81. o := orm.NewOrm()
  82. sql := `SELECT distinct id ,title,abstract,author,published_time,send_status,source,classify_id,plate_name FROM reports WHERE id =?`
  83. err = o.Raw(sql, id).QueryRow(&items)
  84. return
  85. }
  86. func GetReportIdByPermissionIds(id int) (items *Report, err error) {
  87. o := orm.NewOrm()
  88. sql := `SELECT distinct id ,title,abstract,author,published_time,send_status,source,classify_id,plate_name FROM reports WHERE id =?`
  89. err = o.Raw(sql, id).QueryRow(&items)
  90. return
  91. }
  92. func GetReportByCondition(condition, sortCondition string, pars []interface{}, startPage, pageSize int) (items []*Report, err error) {
  93. o := orm.NewOrm()
  94. sql := `SELECT * FROM reports WHERE 1=1 `
  95. if condition != "" {
  96. sql += condition
  97. }
  98. if sortCondition != "" {
  99. sql += sortCondition // 排序
  100. }
  101. sql += ` LIMIT ?,?`
  102. _, err = o.Raw(sql, pars, startPage, pageSize).QueryRows(&items)
  103. return
  104. }
  105. func GetETAReportIdsByCondition(condition string, pars []interface{}) (ids []int, err error) {
  106. o := orm.NewOrmUsingDB("rddp")
  107. sql := `SELECT distinct id FROM report WHERE 1=1 `
  108. if condition != "" {
  109. sql += condition
  110. }
  111. _, err = o.Raw(sql, pars).QueryRows(&ids)
  112. return
  113. }
  114. func GetETAReportById(id int) (classifyId int, err error) {
  115. o := orm.NewOrmUsingDB("rddp")
  116. sql := `SELECT COALESCE(NULLIF(classify_id_third,0),NULLIF(classify_id_second,0),classify_id_first) classify_id FROM report WHERE id =?`
  117. err = o.Raw(sql, id).QueryRow(&classifyId)
  118. return
  119. }
  120. func GetETAReportIdByClassifyId(id int) (classifyId int, err error) {
  121. o := orm.NewOrmUsingDB("rddp")
  122. sql := `SELECT COALESCE(NULLIF(classify_id_third,0),NULLIF(classify_id_second,0),classify_id_first) classify_id FROM report WHERE id=? `
  123. err = o.Raw(sql, id).QueryRow(&classifyId)
  124. return
  125. }
  126. func GetReportIdByClassifyId(ids []int) (reportIds []int, err error) {
  127. o := orm.NewOrm()
  128. condition := "source='ETA' and classify_id in (" + utils.GetOrmReplaceHolder(len(ids)) + ") "
  129. sql := `SELECT distinct id FROM reports WHERE ` + condition
  130. _, err = o.Raw(sql, ids).QueryRows(&reportIds)
  131. return
  132. }
  133. func GetReportIdByPlateName(names []string) (reportIds []int, err error) {
  134. o := orm.NewOrm()
  135. condition := "source='HT' and plate_name in (" + utils.GetOrmReplaceHolder(len(names)) + ") "
  136. sql := `SELECT distinct id FROM reports WHERE ` + condition
  137. _, err = o.Raw(sql, names).QueryRows(&reportIds)
  138. return
  139. }
  140. func GetReportByIds(sortCondition string, ids []int, startSize int, pageSize int) (reports []Report, err error) {
  141. o := orm.NewOrm()
  142. condition := "id in (" + utils.GetOrmReplaceHolder(len(ids)) + ") " + sortCondition + " limit ?,?"
  143. sql := `SELECT * FROM reports WHERE ` + condition
  144. _, err = o.Raw(sql, ids, startSize, pageSize).QueryRows(&reports)
  145. return
  146. }
  147. func GetReportIdsByCondition(condition string, pars []interface{}) (ids []int, err error) {
  148. o := orm.NewOrm()
  149. sql := `SELECT id FROM reports WHERE 1=1 ` + condition
  150. _, err = o.Raw(sql, pars).QueryRows(&ids)
  151. return
  152. }