123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- package index
- import (
- "encoding/json"
- "eta/mysteel_watch/controller/resp"
- "eta/mysteel_watch/global"
- "eta/mysteel_watch/models/index"
- "eta/mysteel_watch/services"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/go-playground/validator/v10"
- "github.com/xuri/excelize/v2"
- "os"
- "time"
- )
- type IndexController struct {
- }
- func (s *IndexController) ServerCheck(c *gin.Context) {
- resp.OkData("检测成功", 1, c)
- return
- }
- // Add 生成指标
- func (s *IndexController) Create(c *gin.Context) {
- req := new(index.IndexAddReq)
- err := c.Bind(&req)
- if err != nil {
- errs, ok := err.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
- return
- }
- fmt.Println("indexCode:" + req.IndexCode)
- if req.RunMode == "" {
- req.RunMode = "debug"
- }
- //fileName := req.IndexName + "_" + req.IndexCode + ".xlsx"
- var fileName string
- fileName = req.IndexCode + "_" + req.RunMode + ".xlsx" //保存的文件名称
- filePath := global.CONFIG.Serve.IndexSaveDir + fileName
- templatePath := global.CONFIG.Serve.IndexSaveDir + "index_template.xlsx"
- templateFile, err := excelize.OpenFile(templatePath)
- if err != nil {
- resp.FailData("打开文件失败", "打开文件失败,Err:"+err.Error(), c)
- return
- }
- defer func() {
- templateFile.Close()
- }()
- sheetList := templateFile.GetSheetList()
- for k, v := range sheetList {
- if k > 0 {
- templateFile.DeleteSheet(v)
- }
- }
- //timeTag := time.Now().UnixNano() / 1e6
- //timeTagStr := fmt.Sprintf("%d", timeTag)
- //UpdateMode:1:增量更新,0:全量更新
- //commentStr := `"BlankValue":"0","CanMark":true,"ChartLineType":"0","DateBlock":0,"DateBlockCount":1,"DateFormat":0,"DateTimeTag":` + timeTagStr + `,"EndDate":"",
- // "ExportType":0,"HasDescription":true,"HasEmptyRows":false,"HasFrequency":true,"HasIndexID":true,"HasLastDate":true,"HasSourceName":true,"HasTimeInterval":true,
- // "HasUnit":true,"HasUpdateDate":true,"IsCreateChart":false,"IsDataSort":true,"IsNewSheet":false,"IsNewWorkbook":false,
- // "Models":[{"DataFormat":0,"DataStartDate":"2011-03-25","DefineName":"","DefineUnit":"","DisplayIndexCode":"` + req.IndexCode + `",
- // "IndexCode":"` + req.IndexCode + `","IndexFormula":"` + req.IndexCode + `","PointValue":0,"UnionStart":""}],"Position":"A1","RangeData":"A2:B2791",
- // "ShowBlankLines":false,"StartDate":"","Transpose":false,"UpdateMode":1,"lookModel":{"IsLast":false,"LookValue":0,"lookType":0},"ver":3}`
- startDate := "1990-01-01"
- commentStr := `"BlankValue":"0","CanMark":true,"ChartLineType":"0","DateBlock":0,"DateBlockCount":1,"DateFormat":0,"DateTimeTag":"","EndDate":"","ExportType":0,"HasDescription":true,"HasEmptyRows":false,"HasFrequency":true,"HasIndexID":true,"HasLastDate":true,"HasSourceName":true,"HasTimeInterval":true,"HasUnit":true,"HasUpdateDate":true,"IsCreateChart":false,"IsDataSort":true,"IsNewSheet":false,"IsNewWorkbook":false,"Models":[{"DataFormat":0,"DataStartDate":"` + startDate + `","DefineName":"","DefineUnit":"","DisplayIndexCode":"` + req.IndexCode + `","IndexCode":"` + req.IndexCode + `","IndexFormula":"` + req.IndexCode + `","PointValue":0,"UnionStart":""}],"Position":"A1","RangeData":"A2:B280","ShowBlankLines":false,"StartDate":"","Transpose":false,"UpdateMode":1,"lookModel":{"IsLast":false,"LookValue":0,"lookType":0},"ver":3}
- `
- commentMap := make(map[string]interface{})
- commentMap["author"] = "{"
- commentMap["text"] = commentStr
- //commentMap["text"] = commentItem
- commentJson, err := json.Marshal(commentMap)
- if err != nil {
- fmt.Println("json.Marshal err:" + err.Error())
- }
- fmt.Println("commentJson")
- fmt.Println(string(commentJson))
- templateFile.DeleteComment("Sheet1", "A1")
- templateFile.AddComment("Sheet1", "A1", string(commentJson))
- if err := templateFile.SaveAs(filePath); err != nil {
- fmt.Println(err)
- resp.FailData("保存失败", "保存失败,Err:"+err.Error(), c)
- return
- }
- time.Sleep(1 * time.Second)
- fmt.Println("start MysteelChemicalRefresh")
- //services.MysteelChemicalRefresh(filePath)
- services.AddIndexRefreshToLPush(filePath)
- resp.Ok("保存成功", c)
- return
- }
- // Delete 删除指标文件
- func (s *IndexController) Delete(c *gin.Context) {
- req := new(index.IndexAddReq)
- err := c.Bind(&req)
- if err != nil {
- errs, ok := err.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
- return
- }
- fmt.Println("params start")
- fmt.Println(req.RunMode)
- fmt.Println("params end")
- var fileName string
- if req.UpdateWeek != "" {
- fileName = req.IndexCode + "_" + req.UpdateWeek + "_" + req.RunMode + ".xlsx" //保存的文件名称
- } else {
- fileName = req.IndexCode + "_" + req.RunMode + ".xlsx" //保存的文件名称
- }
- filePath := global.CONFIG.Serve.IndexSaveDir + fileName
- err = os.Remove(filePath)
- if err != nil {
- fmt.Println("filePath:" + filePath)
- fmt.Println("err:" + err.Error())
- resp.Ok("删除失败", c)
- return
- }
- resp.Ok("删除成功", c)
- return
- }
- func (s *IndexController) Test(c *gin.Context) {
- resp.OkData("检测成功", 1, c)
- return
- }
- // Add 刷新指标
- func (s *IndexController) Refresh(c *gin.Context) {
- req := new(index.IndexRefreshReq)
- err := c.Bind(&req)
- if err != nil {
- errs, ok := err.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
- return
- }
- services.AddIndexRefreshToLPush(req.MergeFilePath)
- resp.Ok("保存成功", c)
- return
- }
- //
- //func init() {
- // fmt.Println("start")
- // Update()
- // fmt.Println("end")
- //}
- //
- //// Add 生成指标
- //func Update() {
- // templatePath := "D:\\hz\\mysteel_data\\ID00185031.xlsx"
- // templateFile, err := excelize.OpenFile(templatePath)
- // if err != nil {
- // fmt.Println("OpenFile Err:" + err.Error())
- // return
- // }
- // defer func() {
- // templateFile.Close()
- // }()
- //
- // commentArr := templateFile.GetComments()
- // for k, v := range commentArr {
- // fmt.Println(k, v)
- // for _, sv := range v {
- // fmt.Println("text:", sv.Text)
- // if strings.Contains(sv.Text, `"UpdateMode":0`) {
- // newText := strings.Replace(sv.Text, `"UpdateMode":0`, `"UpdateMode":1`, -1)
- // newText = strings.Trim(newText, "{")
- // fmt.Println("newText", newText)
- //
- // commentMap := make(map[string]interface{})
- // commentMap["author"] = "{"
- // commentMap["text"] = newText
- // //commentMap["text"] = commentItem
- //
- // commentJson, err := json.Marshal(commentMap)
- // if err != nil {
- // fmt.Println("json.Marshal err:" + err.Error())
- // }
- // fmt.Println("add text:" + string(commentJson))
- // templateFile.DeleteComment("Sheet1", "A1")
- // err = templateFile.AddComment("Sheet1", "A1", string(commentJson))
- // if err != nil {
- // fmt.Println("AddComment Err:" + err.Error())
- // }
- // }
- // }
- // }
- // //
- // // startDate := "1990-01-01"
- // // commentStr := `"BlankValue":"0","CanMark":true,"ChartLineType":"0","DateBlock":0,"DateBlockCount":1,"DateFormat":0,"DateTimeTag":"","EndDate":"","ExportType":0,"HasDescription":true,"HasEmptyRows":false,"HasFrequency":true,"HasIndexID":true,"HasLastDate":true,"HasSourceName":true,"HasTimeInterval":true,"HasUnit":true,"HasUpdateDate":true,"IsCreateChart":false,"IsDataSort":true,"IsNewSheet":false,"IsNewWorkbook":false,"Models":[{"DataFormat":0,"DataStartDate":"` + startDate + `","DefineName":"","DefineUnit":"","DisplayIndexCode":"` + req.IndexCode + `","IndexCode":"` + req.IndexCode + `","IndexFormula":"` + req.IndexCode + `","PointValue":0,"UnionStart":""}],"Position":"A1","RangeData":"A2:B280","ShowBlankLines":false,"StartDate":"","Transpose":false,"UpdateMode":1,"lookModel":{"IsLast":false,"LookValue":0,"lookType":0},"ver":3}
- // //`
- // // commentMap := make(map[string]interface{})
- // // commentMap["author"] = "{"
- // // commentMap["text"] = commentStr
- // // //commentMap["text"] = commentItem
- // //
- // // commentJson, err := json.Marshal(commentMap)
- // // if err != nil {
- // // fmt.Println("json.Marshal err:" + err.Error())
- // // }
- // //
- // // fmt.Println("commentJson")
- // // fmt.Println(string(commentJson))
- // // templateFile.DeleteComment("Sheet1", "A1")
- // // templateFile.AddComment("Sheet1", "A1", string(commentJson))
- // filePath := "D:\\hz\\mysteel_data\\ID00185031_01.xlsx"
- // if err := templateFile.SaveAs(filePath); err != nil {
- // fmt.Println("templateFile.SaveAs Err:", err)
- // return
- // }
- // return
- //}
|