Browse Source

add:增加定时任务同步pdf研报分类

zqbao 8 months ago
parent
commit
446144822f
4 changed files with 226 additions and 0 deletions
  1. 2 0
      main.go
  2. 10 0
      models/classify.go
  3. 58 0
      models/report_pdf.go
  4. 156 0
      scheduler/task.go

+ 2 - 0
main.go

@@ -3,6 +3,7 @@ package main
 import (
 	"eta/eta_mini_bridge/controllers"
 	_ "eta/eta_mini_bridge/routers"
+	"eta/eta_mini_bridge/scheduler"
 	"fmt"
 	"runtime"
 
@@ -22,6 +23,7 @@ func main() {
 	web.BConfig.MaxMemory = 1024 * 1024 * 128
 
 	web.BConfig.RecoverFunc = Recover
+	go scheduler.InitJob()
 
 	web.Run()
 }

+ 10 - 0
models/classify.go

@@ -58,6 +58,16 @@ func GetClassifyListByIds(ids []int) (items []*ClassifyView, err error) {
 	return
 }
 
+func GetChildClassifyListByIds(ids []int) (items []*ClassifyView, err error) {
+	if len(ids) == 0 {
+		return
+	}
+	o := orm.NewOrmUsingDB("rddp")
+	sql := `SELECT * FROM classify WHERE enabled=1 AND parent_id IN (` + utils.GetOrmReplaceHolder(len(ids)) + `)`
+	_, err = o.Raw(sql, ids).QueryRows(&items)
+	return
+}
+
 func GetClassifyById(id int) (item *ClassifyView, err error) {
 	o := orm.NewOrmUsingDB("rddp")
 	sql := `SELECT * FROM classify WHERE id = ?`

+ 58 - 0
models/report_pdf.go

@@ -0,0 +1,58 @@
+package models
+
+import (
+	"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:"状态"`
+}
+
+func GetReportPdfClasssify() (items []*ReportPdf, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT report_pdf_id,classify_id_first, classify_id_second, classify_id_third, classify_name_first, classify_name_second, classify_name_third FROM report_pdf `
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}
+
+// MultiUpdateReportPdfClassify修改pdf研报的分类
+func MultiUpdateReportPdfClassify(items []*ReportPdf) (err error) {
+	if len(items) == 0 {
+		return
+	}
+	o := orm.NewOrm()
+
+	sql := `UPDATE report_pdf SET classify_id_first=?, classify_id_second=?, classify_id_third=?, classify_name_first=?,classify_name_second=?,classify_name_third=? WHERE report_pdf_id=?`
+	db, err := o.Raw(sql).Prepare()
+	if err != nil {
+		return
+	}
+	for _, v := range items {
+		_, err = db.Exec(v.ClassifyIdFirst, v.ClassifyIdSecond, v.ClassifyIdThird, v.ClassifyNameFirst, v.ClassifyNameSecond, v.ClassifyNameThird, v.ReportPdfId)
+		if err != nil {
+			return
+		}
+	}
+	return
+}

+ 156 - 0
scheduler/task.go

@@ -0,0 +1,156 @@
+package scheduler
+
+import (
+	"context"
+	"eta/eta_mini_bridge/models"
+	"eta/eta_mini_bridge/utils"
+	"fmt"
+	"strconv"
+	"strings"
+
+	"github.com/beego/beego/v2/task"
+)
+
+func InitJob() {
+	fmt.Println("定时任务开启。。。")
+	// 每天凌晨12点10分检测, 发送消息
+	tk1 := task.NewTask("UpdateReportClassify", "0 */1 * * * *", UpdateReportClassify)
+	task.AddTask("UpdateReportClassify", tk1)
+	task.StartTask()
+}
+
+func UpdateReportClassify(ctx context.Context) (err error) {
+	reportPdfClassifys, err := models.GetReportPdfClasssify()
+	if err != nil {
+		return
+	}
+	classifyList, err := models.GetClassifyList()
+	if err != nil {
+		return
+	}
+	updateReportOriginClassifyName(reportPdfClassifys, classifyList)
+	classifyIdFirsts := make([]int, 0)
+	classifyIdSeconds := make([]int, 0)
+	for _, v := range reportPdfClassifys {
+		if v.ClassifyIdFirst == 0 {
+			utils.FileLog.Info("分类id错误,获取失败,错误pdf研报id:" + fmt.Sprintf("%d", v.ReportPdfId))
+			continue
+		}
+		if v.ClassifyIdThird == 0 && v.ClassifyIdSecond == 0 {
+			classifyIdFirsts = append(classifyIdFirsts, v.ClassifyIdFirst)
+		} else if v.ClassifyIdThird == 0 {
+			classifyIdSeconds = append(classifyIdSeconds, v.ClassifyIdSecond)
+		}
+
+	}
+
+	updateReportClassify(reportPdfClassifys, classifyIdFirsts, 1)
+	updateReportClassify(reportPdfClassifys, classifyIdSeconds, 2)
+	return nil
+}
+
+// updateReportOriginClassifyName 更新原始的pdf报告的分类
+func updateReportOriginClassifyName(reportList []*models.ReportPdf, classifyList []*models.ClassifyView) (err error) {
+	classifyMap := make(map[int]*models.ClassifyView)
+	for _, v := range classifyList {
+		classifyMap[v.Id] = v
+	}
+	modifyReportPdfList := make([]*models.ReportPdf, 0)
+	var isModify bool
+	for _, v := range reportList {
+		isModify = false
+		if v.ClassifyIdFirst != 0 {
+			firstName := classifyMap[v.ClassifyIdFirst].ClassifyName
+			if v.ClassifyNameFirst != firstName {
+				v.ClassifyNameFirst = firstName
+				isModify = true
+			}
+		}
+		if v.ClassifyIdSecond != 0 {
+			secondName := classifyMap[v.ClassifyIdSecond].ClassifyName
+			if v.ClassifyNameSecond != secondName {
+				v.ClassifyNameSecond = secondName
+				isModify = true
+			}
+		}
+		if v.ClassifyIdThird != 0 {
+			thirdName := classifyMap[v.ClassifyIdThird].ClassifyName
+			if v.ClassifyNameThird != thirdName {
+				v.ClassifyNameThird = thirdName
+				isModify = true
+			}
+		}
+		if isModify {
+			modifyReportPdfList = append(modifyReportPdfList, v)
+		}
+	}
+
+	err = models.MultiUpdateReportPdfClassify(modifyReportPdfList)
+	if err != nil {
+		utils.FileLog.Warn("研报更新分类失败")
+	}
+	if len(modifyReportPdfList) > 0 {
+		var reportIds []string
+		for _, v := range modifyReportPdfList {
+			reportIds = append(reportIds, strconv.Itoa(v.ReportPdfId))
+		}
+		utils.FileLog.Info(fmt.Sprintf("更新分类成功,更新研报为:%s", strings.Join(reportIds, ",")))
+	}
+	return
+}
+
+// updateReportClassify 更新增加分类pdf报告
+func updateReportClassify(reportList []*models.ReportPdf, classifyList []int, level int) (err error) {
+	modifyReportList := make([]*models.ReportPdf, 0)
+	preClassifyList, err := models.GetClassifyListByIds(classifyList)
+	if err != nil {
+		utils.FileLog.Info("获取一级分类失败,错误分类id:" + fmt.Sprintf("%v", classifyList))
+	}
+	if len(preClassifyList) > 0 {
+		tmpClassifyIds := make([]int, 0)
+		for _, v := range preClassifyList {
+			if v.HasChild == 1 {
+				tmpClassifyIds = append(tmpClassifyIds, v.Id)
+			}
+		}
+		childClassify, err := models.GetChildClassifyListByIds(tmpClassifyIds)
+		if err != nil {
+			utils.FileLog.Info("获取二级分类失败,错误分类id:" + fmt.Sprintf("%v", tmpClassifyIds))
+		}
+		var isModify bool
+		for _, v := range reportList {
+			isModify = false
+			for _, vv := range childClassify {
+				switch level {
+				case 1:
+					if v.ClassifyIdFirst == vv.ParentId {
+						v.ClassifyIdSecond = vv.Id
+						v.ClassifyNameSecond = vv.ClassifyName
+						isModify = true
+					}
+				case 2:
+					if v.ClassifyIdSecond == vv.ParentId {
+						v.ClassifyIdThird = vv.Id
+						v.ClassifyNameThird = vv.ClassifyName
+						isModify = true
+					}
+				}
+			}
+			if isModify {
+				modifyReportList = append(modifyReportList, v)
+			}
+		}
+		err = models.MultiUpdateReportPdfClassify(modifyReportList)
+		if err != nil {
+			utils.FileLog.Warn("研报更新分类失败")
+		}
+		if len(modifyReportList) > 0 {
+			var reportIds []string
+			for _, v := range modifyReportList {
+				reportIds = append(reportIds, strconv.Itoa(v.ReportPdfId))
+			}
+			utils.FileLog.Info(fmt.Sprintf("更新分类成功,更新研报为:%s", strings.Join(reportIds, ",")))
+		}
+	}
+	return
+}