Răsfoiți Sursa

涌溢生猪定时任务

xyxie 1 an în urmă
părinte
comite
992327ccf2
5 a modificat fișierele cu 33 adăugiri și 54 ștergeri
  1. 0 38
      controllers/data_init.go
  2. 1 9
      routers/router.go
  3. 14 6
      services/base_from_yongyi.go
  4. 9 1
      services/task.go
  5. 9 0
      utils/config.go

+ 0 - 38
controllers/data_init.go

@@ -1,38 +0,0 @@
-package controllers
-
-import (
-	"eta/eta_data_analysis/models"
-	"eta/eta_data_analysis/services"
-	"fmt"
-	beego "github.com/beego/beego/v2/server/web"
-)
-
-// Operations about Users
-type DataInitController struct {
-	beego.Controller
-}
-
-// @Title 初始化联化工指标
-// @Description 初始化联化工指标
-// @Param   FileName    query   string true       "文件名称"
-// @Success Ret=200
-// @router /mysteel_chemical [get]
-func (this *DataInitController) MySteelChemical() {
-	br := new(models.BaseResponse).Init()
-	defer func() {
-		this.Data["json"] = br
-		this.ServeJSON()
-	}()
-
-	fileName := this.GetString("FileName")
-	if fileName == "" {
-		br.Msg = "文件名称不能为空"
-		return
-	}
-
-	filePath := "/docs/" + fileName
-	fmt.Println("filePath:" + filePath)
-	services.HandleYongyiExcelDaily(filePath)
-	br.Ret = 200
-	br.Msg = "保存成功"
-}

+ 1 - 9
routers/router.go

@@ -8,18 +8,10 @@
 package routers
 
 import (
-	"eta/eta_data_analysis/controllers"
-
 	beego "github.com/beego/beego/v2/server/web"
 )
 
 func init() {
-	ns := beego.NewNamespace("/v1",
-		beego.NSNamespace("/data_init",
-			beego.NSInclude(
-				&controllers.DataInitController{},
-			),
-		),
-	)
+	ns := beego.NewNamespace("/v1")
 	beego.AddNamespace(ns)
 }

+ 14 - 6
services/base_from_yongyi.go

@@ -1,12 +1,14 @@
 package services
 
 import (
+	"context"
 	"encoding/json"
 	"eta/eta_data_analysis/models"
 	"eta/eta_data_analysis/services/base_from_yongyi"
 	"eta/eta_data_analysis/utils"
 	"fmt"
 	"github.com/tealeg/xlsx"
+	"time"
 )
 
 /*
@@ -32,11 +34,12 @@ import (
 月度-大猪存栏(2020年5月新增)
 月度-商品猪出栏量
 */
-func HandleYongyiExcelDaily(uploadPath string) (err error) {
-	xlFile, err := xlsx.OpenFile(uploadPath)
+func HandleYongyiExcelDaily(cont context.Context) (err error) {
+	filePath := fmt.Sprintf("%s/%s_day.xlsx", utils.YongyiFilePath, time.Now().Format(utils.FormatDate))
+	xlFile, err := xlsx.OpenFile(filePath)
 	if err != nil {
 		err = fmt.Errorf("打开文件失败, Err: %s", err)
-		utils.FileLog.Info(fmt.Sprintf("打开文件:%s 失败, Err: %s", uploadPath, err))
+		utils.FileLog.Info(fmt.Sprintf("打开文件:%s 失败, Err: %s", filePath, err))
 		return
 	}
 	terminalCode := ""
@@ -83,11 +86,16 @@ func HandleYongyiExcelDaily(uploadPath string) (err error) {
 	return
 }
 
-func HandleYongyiExcelWeekly(uploadPath string) (err error) {
-	xlFile, err := xlsx.OpenFile(uploadPath)
+func HandleYongyiExcelWeekly(cont context.Context) (err error) {
+	week := time.Now().Weekday()
+	if week != time.Thursday { //每周四,处理
+		return
+	}
+	filePath := fmt.Sprintf("%s/%s_week.xlsx", utils.YongyiFilePath, time.Now().Format(utils.FormatDate))
+	xlFile, err := xlsx.OpenFile(filePath)
 	if err != nil {
 		err = fmt.Errorf("打开文件失败, Err: %s", err)
-		utils.FileLog.Info(fmt.Sprintf("打开文件:%s 失败, Err: %s", uploadPath, err))
+		utils.FileLog.Info(fmt.Sprintf("打开文件:%s 失败, Err: %s", filePath, err))
 		return
 	}
 	terminalCode := ""

+ 9 - 1
services/task.go

@@ -2,11 +2,19 @@ package services
 
 import (
 	"fmt"
+	"github.com/beego/beego/v2/task"
 )
 
 func Task() {
 	fmt.Println("task start")
+	handleYongyiExcelDaily := task.NewTask("handleYongyiExcelDaily", "0 30 13,23 * * *", HandleYongyiExcelDaily)
+	task.AddTask("涌益咨询日度指标处理", handleYongyiExcelDaily)
 
-	//HandleYongyiExcelDaily("/Users/xiexiaoyuan/Downloads/test.xlsx")
+	handleYongyiExcelWeekly := task.NewTask("handleYongyiExcelWeekly", "0 0 17,23 * * *", HandleYongyiExcelWeekly)
+	task.AddTask("涌益咨询周度指标处理", handleYongyiExcelWeekly)
+
+	task.StartTask()
+	//HandleYongyiExcelDaily("/Users/xiexiaoyuan/Downloads/2023年11月21日涌益咨询日度数据 (1).xlsx")
+	//HandleYongyiExcelWeekly("/Users/xiexiaoyuan/Downloads/涌益生猪项目数据库/2023.11.10-2023.11.16涌益咨询 周度数据.xlsx")
 	fmt.Println("task end")
 }

+ 9 - 0
utils/config.go

@@ -36,6 +36,11 @@ var (
 	LogMaxDays int //日志最大保留天数
 )
 
+// 涌益生猪excel文件地址
+var (
+	YongyiFilePath string
+)
+
 func init() {
 	tmpRunMode, err := web.AppConfig.String("run_mode")
 	if err != nil {
@@ -73,6 +78,10 @@ func init() {
 		logMaxDaysStr := config["log_max_day"]
 		LogMaxDays, _ = strconv.Atoi(logMaxDaysStr)
 	}
+	//涌益咨询文件夹配置
+	{
+		YongyiFilePath = config["yongyi_file_path"]
+	}
 }
 
 //修改接口文档