report_pdf.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package models
  2. import (
  3. "eta/eta_mini_crm/utils"
  4. "time"
  5. "github.com/beego/beego/v2/client/orm"
  6. )
  7. type ReportPdf struct {
  8. ReportPdfId int `orm:"pk" description:"id"`
  9. PdfUrl string `description:"pdf文件URL"`
  10. PdfName string `description:"pdf文件名称"`
  11. Title string `description:"pdf文件标题"`
  12. Author string `description:"作者"`
  13. Abstract string `description:"摘要"`
  14. ClassifyIdFirst int `description:"一级分类id"`
  15. ClassifyNameFirst string `description:"一级分类名称"`
  16. ClassifyIdSecond int `description:"二级分类id"`
  17. ClassifyNameSecond string `description:"二级分类名称"`
  18. ClassifyIdThird int `description:"三级分类id"`
  19. ClassifyNameThird string `description:"三级分类名称"`
  20. Stage int `description:"期数"`
  21. PublishTime time.Time `description:"发布时间"`
  22. ModifyTime time.Time `description:"更新时间"`
  23. Pv int `description:"pv"`
  24. Uv int `description:"uv"`
  25. SysUserId int `description:"创建人id"`
  26. SysRealName string `description:"创建人姓名"`
  27. State int `description:"状态,1:已发布;2:未发布"`
  28. }
  29. type ReportPdfView struct {
  30. ReportPdfId int `orm:"pk" description:"id"`
  31. PdfUrl string `description:"pdf文件URL"`
  32. PdfName string `description:"pdf文件名称"`
  33. Title string `description:"pdf文件标题"`
  34. Author string `description:"作者"`
  35. Abstract string `description:"摘要"`
  36. ClassifyIdFirst int `description:"一级分类id"`
  37. ClassifyNameFirst string `description:"一级分类名称"`
  38. ClassifyIdSecond int `description:"二级分类id"`
  39. ClassifyNameSecond string `description:"二级分类名称"`
  40. ClassifyIdThird int `description:"三级分类id"`
  41. ClassifyNameThird string `description:"三级分类名称"`
  42. Stage int `description:"期数"`
  43. PublishTime string `description:"发布时间"`
  44. ModifyTime time.Time `description:"更新时间"`
  45. Pv int `description:"pv"`
  46. Uv int `description:"uv"`
  47. SysUserId int `description:"创建人id"`
  48. SysRealName string `description:"创建人姓名"`
  49. State int `description:"状态,1:已发布;2:未发布"`
  50. }
  51. func (r *ReportPdf) Insert() (insertId int64, err error) {
  52. tx, err := orm.NewOrm().Begin()
  53. if err != nil {
  54. return
  55. }
  56. defer func() {
  57. if err != nil {
  58. tx.Rollback()
  59. } else {
  60. tx.Commit()
  61. }
  62. }()
  63. // 计算研报期数
  64. sql := `SELECT MAX(stage) + 1 AS count FROM report_pdf WHERE classify_id_second=?`
  65. err = tx.Raw(sql, r.ClassifyIdSecond).QueryRow(&r.Stage)
  66. if r.Stage == 0 {
  67. r.Stage = 1
  68. }
  69. if err != nil {
  70. return
  71. }
  72. insertId, err = tx.Insert(r)
  73. if err != nil {
  74. return
  75. }
  76. if r.State == utils.ReportStatusUp {
  77. insertPushStatus := &ReportPushStatus{
  78. ReportId: int(insertId),
  79. State: 0,
  80. Title: r.Title,
  81. Abstract: r.Abstract,
  82. Stage: r.Stage,
  83. ClassifyIdFirst: r.ClassifyIdFirst,
  84. ClassifyNameFirst: r.ClassifyNameFirst,
  85. ClassifyIdSecond: r.ClassifyIdSecond,
  86. ClassifyNameSecond: r.ClassifyNameSecond,
  87. ClassifyIdThird: r.ClassifyIdThird,
  88. ClassifyNameThird: r.ClassifyNameThird,
  89. Author: r.Author,
  90. ReportType: utils.ReportTypePdf,
  91. PublishTime: r.PublishTime,
  92. CreateTime: time.Now(),
  93. ModifyTime: time.Now(),
  94. }
  95. _, err = tx.Insert(insertPushStatus)
  96. }
  97. return
  98. }
  99. func (r *ReportPdf) ToView() (item *ReportPdfView) {
  100. item = new(ReportPdfView)
  101. item.ReportPdfId = r.ReportPdfId
  102. item.PdfUrl = r.PdfUrl
  103. item.PdfName = r.PdfName
  104. item.Title = r.Title
  105. item.Author = r.Author
  106. item.Abstract = r.Abstract
  107. item.ClassifyIdFirst = r.ClassifyIdFirst
  108. item.ClassifyNameFirst = r.ClassifyNameFirst
  109. item.ClassifyIdSecond = r.ClassifyIdSecond
  110. item.ClassifyNameSecond = r.ClassifyNameSecond
  111. item.Stage = r.Stage
  112. item.State = r.State
  113. item.PublishTime = r.PublishTime.Format(utils.FormatDateTime)
  114. item.ModifyTime = r.ModifyTime
  115. item.Pv = r.Pv
  116. item.Uv = r.Uv
  117. item.SysUserId = r.SysUserId
  118. item.SysRealName = r.SysRealName
  119. return
  120. }
  121. func (r *ReportPdf) Update(cols []string) (err error) {
  122. // reportPushStatus := new(ReportPushStatus)
  123. var reportPushStatus *ReportPushStatus
  124. o := orm.NewOrm()
  125. sql := `SELECT * FROM report_push_status WHERE report_id=? AND report_type=?`
  126. err = o.Raw(sql, r.ReportPdfId, utils.ReportTypePdf).QueryRow(&reportPushStatus)
  127. if err != nil && err.Error() != utils.ErrNoRow() {
  128. return
  129. }
  130. tx, err := o.Begin()
  131. if err != nil {
  132. return
  133. }
  134. defer func() {
  135. if err != nil {
  136. tx.Rollback()
  137. } else {
  138. tx.Commit()
  139. }
  140. }()
  141. _, err = tx.Update(r, cols...)
  142. if err != nil {
  143. return
  144. }
  145. if reportPushStatus == nil && r.State == utils.ReportStatusUp {
  146. insertPushStatus := &ReportPushStatus{
  147. ReportId: r.ReportPdfId,
  148. State: 0,
  149. Title: r.Title,
  150. Abstract: r.Abstract,
  151. Stage: r.Stage,
  152. ClassifyIdFirst: r.ClassifyIdFirst,
  153. ClassifyNameFirst: r.ClassifyNameFirst,
  154. ClassifyIdSecond: r.ClassifyIdSecond,
  155. ClassifyNameSecond: r.ClassifyNameSecond,
  156. ClassifyIdThird: r.ClassifyIdThird,
  157. ClassifyNameThird: r.ClassifyNameThird,
  158. Author: r.Author,
  159. ReportType: utils.ReportTypePdf,
  160. PublishTime: r.PublishTime,
  161. CreateTime: time.Now(),
  162. ModifyTime: time.Now(),
  163. }
  164. _, err = tx.Insert(insertPushStatus)
  165. } else if reportPushStatus != nil {
  166. // pdf报告发布后编辑,则更新report_push_status表, 若取消发布,则在推送表中删除
  167. if r.State == utils.ReportStatusDown {
  168. err = reportPushStatus.Delete()
  169. } else {
  170. reportPushStatus.Title = r.Title
  171. reportPushStatus.Abstract = r.Abstract
  172. reportPushStatus.Stage = r.Stage
  173. reportPushStatus.ClassifyIdFirst = r.ClassifyIdFirst
  174. reportPushStatus.ClassifyNameFirst = r.ClassifyNameFirst
  175. reportPushStatus.ClassifyIdSecond = r.ClassifyIdSecond
  176. reportPushStatus.ClassifyNameSecond = r.ClassifyNameSecond
  177. reportPushStatus.ClassifyIdThird = r.ClassifyIdThird
  178. reportPushStatus.ClassifyNameThird = r.ClassifyNameThird
  179. reportPushStatus.Author = r.Author
  180. reportPushStatus.PublishTime = r.PublishTime
  181. reportPushStatus.ModifyTime = time.Now()
  182. updateCols := []string{"title", "abstract", "stage", "classify_id_first", "classify_name_first", "classify_id_second", "classify_name_second", "classify_id_third", "classify_name_third", "author", "publish_time", "modify_time"}
  183. _, err = tx.Update(reportPushStatus, updateCols...)
  184. }
  185. }
  186. return
  187. }
  188. func (r *ReportPdf) Delete() (err error) {
  189. o := orm.NewOrm()
  190. _, err = o.Delete(r)
  191. return
  192. }
  193. func GetReportPdfCountByCondition(condition string, pars []interface{}) (count int, err error) {
  194. o := orm.NewOrm()
  195. sql := `SELECT COUNT(*) AS count FROM report_pdf WHERE 1=1 `
  196. if condition != "" {
  197. sql += condition
  198. }
  199. err = o.Raw(sql, pars).QueryRow(&count)
  200. return
  201. }
  202. func GetReportPdfByCondition(condition, sortCondition string, pars []interface{}, startPage, pageSize int) (items []*ReportPdf, err error) {
  203. o := orm.NewOrm()
  204. sql := `SELECT * FROM report_pdf WHERE 1=1 `
  205. if condition != "" {
  206. sql += condition
  207. }
  208. if sortCondition != "" {
  209. sql += sortCondition // 排序
  210. }
  211. sql += ` LIMIT ?,?`
  212. _, err = o.Raw(sql, pars, startPage, pageSize).QueryRows(&items)
  213. return
  214. }
  215. func GetReportPdfById(id int) (item *ReportPdf, err error) {
  216. o := orm.NewOrm()
  217. sql := `SELECT * FROM report_pdf WHERE report_pdf_id=?`
  218. err = o.Raw(sql, id).QueryRow(&item)
  219. return
  220. }
  221. func GetBatchPdfReport(maxId, batchSize int) (items []*ReportPdf, err error) {
  222. o := orm.NewOrm()
  223. sql := `SELECT * FROM report_pdf WHERE report_pdf_id>? AND state=1 LIMIT ?`
  224. _, err = o.Raw(sql, maxId, batchSize).QueryRows(&items)
  225. return
  226. }