123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- package models
- import (
- "eta/eta_mini_crm/utils"
- "time"
- "github.com/beego/beego/v2/client/orm"
- )
- type ReportPdf struct {
- ReportPdfId int `orm:"pk" description:"id"`
- PdfUrl string `description:"pdf文件URL"`
- PdfName string `description:"pdf文件名称"`
- Title string `description:"pdf文件标题"`
- Author string `description:"作者"`
- Abstract string `description:"摘要"`
- ClassifyIdFirst int `description:"一级分类id"`
- ClassifyNameFirst string `description:"一级分类名称"`
- ClassifyIdSecond int `description:"二级分类id"`
- ClassifyNameSecond string `description:"二级分类名称"`
- ClassifyIdThird int `description:"三级分类id"`
- ClassifyNameThird string `description:"三级分类名称"`
- Stage int `description:"期数"`
- PublishTime time.Time `description:"发布时间"`
- ModifyTime time.Time `description:"更新时间"`
- Pv int `description:"pv"`
- Uv int `description:"uv"`
- SysUserId int `description:"创建人id"`
- SysRealName string `description:"创建人姓名"`
- State int `description:"状态,1:已发布;2:未发布"`
- }
- type ReportPdfView struct {
- ReportPdfId int `orm:"pk" description:"id"`
- PdfUrl string `description:"pdf文件URL"`
- PdfName string `description:"pdf文件名称"`
- Title string `description:"pdf文件标题"`
- Author string `description:"作者"`
- Abstract string `description:"摘要"`
- ClassifyIdFirst int `description:"一级分类id"`
- ClassifyNameFirst string `description:"一级分类名称"`
- ClassifyIdSecond int `description:"二级分类id"`
- ClassifyNameSecond string `description:"二级分类名称"`
- ClassifyIdThird int `description:"三级分类id"`
- ClassifyNameThird string `description:"三级分类名称"`
- Stage int `description:"期数"`
- PublishTime string `description:"发布时间"`
- ModifyTime time.Time `description:"更新时间"`
- Pv int `description:"pv"`
- Uv int `description:"uv"`
- SysUserId int `description:"创建人id"`
- SysRealName string `description:"创建人姓名"`
- State int `description:"状态,1:已发布;2:未发布"`
- }
- func (r *ReportPdf) Insert() (insertId int64, err error) {
- tx, err := orm.NewOrm().Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- // 计算研报期数
- sql := `SELECT MAX(stage) + 1 AS count FROM report_pdf WHERE classify_id_second=?`
- err = tx.Raw(sql, r.ClassifyIdSecond).QueryRow(&r.Stage)
- if r.Stage == 0 {
- r.Stage = 1
- }
- if err != nil {
- return
- }
- insertId, err = tx.Insert(r)
- if err != nil {
- return
- }
- if r.State == utils.ReportStatusUp {
- insertPushStatus := &ReportPushStatus{
- ReportId: int(insertId),
- State: 0,
- Title: r.Title,
- Abstract: r.Abstract,
- Stage: r.Stage,
- ClassifyIdFirst: r.ClassifyIdFirst,
- ClassifyNameFirst: r.ClassifyNameFirst,
- ClassifyIdSecond: r.ClassifyIdSecond,
- ClassifyNameSecond: r.ClassifyNameSecond,
- ClassifyIdThird: r.ClassifyIdThird,
- ClassifyNameThird: r.ClassifyNameThird,
- Author: r.Author,
- ReportType: utils.ReportTypePdf,
- PublishTime: r.PublishTime,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- _, err = tx.Insert(insertPushStatus)
- }
- return
- }
- func (r *ReportPdf) ToView() (item *ReportPdfView) {
- item = new(ReportPdfView)
- item.ReportPdfId = r.ReportPdfId
- item.PdfUrl = r.PdfUrl
- item.PdfName = r.PdfName
- item.Title = r.Title
- item.Author = r.Author
- item.Abstract = r.Abstract
- item.ClassifyIdFirst = r.ClassifyIdFirst
- item.ClassifyNameFirst = r.ClassifyNameFirst
- item.ClassifyIdSecond = r.ClassifyIdSecond
- item.ClassifyNameSecond = r.ClassifyNameSecond
- item.Stage = r.Stage
- item.State = r.State
- item.PublishTime = r.PublishTime.Format(utils.FormatDateTime)
- item.ModifyTime = r.ModifyTime
- item.Pv = r.Pv
- item.Uv = r.Uv
- item.SysUserId = r.SysUserId
- item.SysRealName = r.SysRealName
- return
- }
- func (r *ReportPdf) Update(cols []string) (err error) {
- // reportPushStatus := new(ReportPushStatus)
- var reportPushStatus *ReportPushStatus
- o := orm.NewOrm()
- sql := `SELECT * FROM report_push_status WHERE report_id=? AND report_type=?`
- err = o.Raw(sql, r.ReportPdfId, utils.ReportTypePdf).QueryRow(&reportPushStatus)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- _, err = tx.Update(r, cols...)
- if err != nil {
- return
- }
- if reportPushStatus == nil && r.State == utils.ReportStatusUp {
- insertPushStatus := &ReportPushStatus{
- ReportId: r.ReportPdfId,
- State: 0,
- Title: r.Title,
- Abstract: r.Abstract,
- Stage: r.Stage,
- ClassifyIdFirst: r.ClassifyIdFirst,
- ClassifyNameFirst: r.ClassifyNameFirst,
- ClassifyIdSecond: r.ClassifyIdSecond,
- ClassifyNameSecond: r.ClassifyNameSecond,
- ClassifyIdThird: r.ClassifyIdThird,
- ClassifyNameThird: r.ClassifyNameThird,
- Author: r.Author,
- ReportType: utils.ReportTypePdf,
- PublishTime: r.PublishTime,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- _, err = tx.Insert(insertPushStatus)
- } else if reportPushStatus != nil {
- // pdf报告发布后编辑,则更新report_push_status表, 若取消发布,则在推送表中删除
- if r.State == utils.ReportStatusDown {
- err = reportPushStatus.Delete()
- } else {
- reportPushStatus.Title = r.Title
- reportPushStatus.Abstract = r.Abstract
- reportPushStatus.Stage = r.Stage
- reportPushStatus.ClassifyIdFirst = r.ClassifyIdFirst
- reportPushStatus.ClassifyNameFirst = r.ClassifyNameFirst
- reportPushStatus.ClassifyIdSecond = r.ClassifyIdSecond
- reportPushStatus.ClassifyNameSecond = r.ClassifyNameSecond
- reportPushStatus.ClassifyIdThird = r.ClassifyIdThird
- reportPushStatus.ClassifyNameThird = r.ClassifyNameThird
- reportPushStatus.Author = r.Author
- reportPushStatus.PublishTime = r.PublishTime
- reportPushStatus.ModifyTime = time.Now()
- 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"}
- _, err = tx.Update(reportPushStatus, updateCols...)
- }
- }
- return
- }
- func (r *ReportPdf) Delete() (err error) {
- o := orm.NewOrm()
- _, err = o.Delete(r)
- return
- }
- func GetReportPdfCountByCondition(condition string, pars []interface{}) (count int, err error) {
- o := orm.NewOrm()
- sql := `SELECT COUNT(*) AS count FROM report_pdf WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- err = o.Raw(sql, pars).QueryRow(&count)
- return
- }
- func GetReportPdfByCondition(condition, sortCondition string, pars []interface{}, startPage, pageSize int) (items []*ReportPdf, err error) {
- o := orm.NewOrm()
- sql := `SELECT * FROM report_pdf WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- if sortCondition != "" {
- sql += sortCondition // 排序
- }
- sql += ` LIMIT ?,?`
- _, err = o.Raw(sql, pars, startPage, pageSize).QueryRows(&items)
- return
- }
- func GetReportPdfById(id int) (item *ReportPdf, err error) {
- o := orm.NewOrm()
- sql := `SELECT * FROM report_pdf WHERE report_pdf_id=?`
- err = o.Raw(sql, id).QueryRow(&item)
- return
- }
- func GetBatchPdfReport(maxId, batchSize int) (items []*ReportPdf, err error) {
- o := orm.NewOrm()
- sql := `SELECT * FROM report_pdf WHERE report_pdf_id>? AND state=1 LIMIT ?`
- _, err = o.Raw(sql, maxId, batchSize).QueryRows(&items)
- return
- }
|