index.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package index
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "github.com/go-playground/validator/v10"
  7. "github.com/xuri/excelize/v2"
  8. "hongze/mysteel_watch/controller/resp"
  9. "hongze/mysteel_watch/global"
  10. "hongze/mysteel_watch/models/index"
  11. "hongze/mysteel_watch/utils"
  12. "hongze/mysteel_watch/watch"
  13. )
  14. type IndexController struct {
  15. }
  16. func (s *IndexController) ServerCheck(c *gin.Context) {
  17. resp.OkData("检测成功", 1, c)
  18. return
  19. }
  20. // Add 生成指标
  21. func (s *IndexController) Create(c *gin.Context) {
  22. req := new(index.IndexAddReq)
  23. err := c.Bind(&req)
  24. if err != nil {
  25. errs, ok := err.(validator.ValidationErrors)
  26. if !ok {
  27. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  28. return
  29. }
  30. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  31. return
  32. }
  33. fmt.Println("indexCode:" + req.IndexCode)
  34. //fileName := req.IndexName + "_" + req.IndexCode + ".xlsx"
  35. var fileName string
  36. if req.UpdateWeek != "" {
  37. fileName = req.IndexCode + "_" + req.UpdateWeek + ".xlsx" //保存的文件名称
  38. } else {
  39. fileName = req.IndexCode + ".xlsx" //保存的文件名称
  40. }
  41. filePath := utils.IndexSaveDir + fileName
  42. templatePath := utils.IndexSaveDir + "index_template.xlsx"
  43. templateFile, err := excelize.OpenFile(templatePath)
  44. if err != nil {
  45. resp.FailData("打开文件失败", "打开文件失败,Err:"+err.Error(), c)
  46. return
  47. }
  48. defer func() {
  49. templateFile.Close()
  50. }()
  51. commentStr := `"BlankValue":"0","CanMark":true,"ChartLineType":"0","DateBlock":0,"DateBlockCount":1,"DateFormat":0,"DateTimeTag":"637979007914770000","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":"ID01027043","IndexCode":"ID01027043","IndexFormula":"ID01027043","PointValue":0,"UnionStart":""}],"Position":"A1","RangeData":"A2:B2791","ShowBlankLines":false,"StartDate":"","Transpose":false,"UpdateMode":0,"lookModel":{"IsLast":false,"LookValue":0,"lookType":0},"ver":3}`
  52. commentMap := make(map[string]interface{})
  53. commentMap["author"] = "{"
  54. commentMap["text"] = commentStr
  55. //commentMap["text"] = commentItem
  56. commentJson, err := json.Marshal(commentMap)
  57. if err != nil {
  58. fmt.Println("json.Marshal err:" + err.Error())
  59. }
  60. fmt.Println("commentJson")
  61. fmt.Println(string(commentJson))
  62. templateFile.DeleteComment("Sheet1", "A1")
  63. templateFile.AddComment("Sheet1", "A1", string(commentJson))
  64. if err := templateFile.SaveAs(filePath); err != nil {
  65. fmt.Println(err)
  66. resp.FailData("保存失败", "保存失败,Err:"+err.Error(), c)
  67. return
  68. }
  69. resp.Ok("保存成功", c)
  70. return
  71. }
  72. func (s *IndexController) Test(c *gin.Context) {
  73. watch.TestWatch()
  74. resp.OkData("检测成功", 1, c)
  75. return
  76. }
  77. type IndexComment struct {
  78. BlankValue string `json:"BlankValue"`
  79. CanMark bool `json:"CanMark"`
  80. ChartLineType string `json:"ChartLineType"`
  81. DateBlock int64 `json:"DateBlock"`
  82. DateBlockCount int64 `json:"DateBlockCount"`
  83. DateFormat int64 `json:"DateFormat"`
  84. DateTimeTag string `json:"DateTimeTag"`
  85. EndDate string `json:"EndDate"`
  86. ExportType int64 `json:"ExportType"`
  87. HasDescription bool `json:"HasDescription"`
  88. HasEmptyRows bool `json:"HasEmptyRows"`
  89. HasFrequency bool `json:"HasFrequency"`
  90. HasIndexID bool `json:"HasIndexID"`
  91. HasLastDate bool `json:"HasLastDate"`
  92. HasSourceName bool `json:"HasSourceName"`
  93. HasTimeInterval bool `json:"HasTimeInterval"`
  94. HasUnit bool `json:"HasUnit"`
  95. HasUpdateDate bool `json:"HasUpdateDate"`
  96. IsCreateChart bool `json:"IsCreateChart"`
  97. IsDataSort bool `json:"IsDataSort"`
  98. IsNewSheet bool `json:"IsNewSheet"`
  99. IsNewWorkbook bool `json:"IsNewWorkbook"`
  100. Models []ModelsInfo `json:"Models"`
  101. Position string `json:"Position"`
  102. RangeData string `json:"RangeData"`
  103. ShowBlankLines bool `json:"ShowBlankLines"`
  104. StartDate string `json:"StartDate"`
  105. Transpose bool `json:"Transpose"`
  106. UpdateMode int64 `json:"UpdateMode"`
  107. LookModel struct {
  108. IsLast bool `json:"IsLast"`
  109. LookValue int64 `json:"LookValue"`
  110. LookType int64 `json:"lookType"`
  111. } `json:"lookModel"`
  112. Ver int64 `json:"ver"`
  113. }
  114. type ModelsInfo struct {
  115. DataFormat int64 `json:"DataFormat"`
  116. DataStartDate string `json:"DataStartDate"`
  117. DefineName string `json:"DefineName"`
  118. DefineUnit string `json:"DefineUnit"`
  119. DisplayIndexCode string `json:"DisplayIndexCode"`
  120. IndexCode string `json:"IndexCode"`
  121. IndexFormula string `json:"IndexFormula"`
  122. PointValue int64 `json:"PointValue"`
  123. UnionStart string `json:"UnionStart"`
  124. }
  125. //
  126. //commentItem := new(IndexComment)
  127. //commentItem.BlankValue = "0"
  128. //commentItem.CanMark = true
  129. //commentItem.ChartLineType = "0"
  130. //commentItem.DateBlock = 0
  131. //commentItem.DateBlockCount = 1
  132. //commentItem.DateFormat = 0
  133. //commentItem.DateTimeTag = "637979007914770000"
  134. //commentItem.EndDate = ""
  135. //commentItem.ExportType = 0
  136. //commentItem.HasDescription = true
  137. //commentItem.HasEmptyRows = false
  138. //commentItem.HasFrequency = true
  139. //commentItem.HasIndexID = true
  140. //commentItem.HasSourceName = true
  141. //commentItem.HasTimeInterval = true
  142. //commentItem.HasUnit = true
  143. //commentItem.HasUpdateDate = true
  144. //commentItem.IsCreateChart = false
  145. //commentItem.IsDataSort = true
  146. //commentItem.IsNewSheet = false
  147. //commentItem.IsNewWorkbook = false
  148. //
  149. //model := new(ModelsInfo)
  150. //model.DataFormat = 0
  151. //model.DataStartDate = "2011-03-25"
  152. //model.DefineName = ""
  153. //model.DefineUnit = ""
  154. //model.DisplayIndexCode = req.IndexCode
  155. //model.IndexCode = req.IndexCode
  156. //model.IndexFormula = req.IndexCode
  157. //model.PointValue = 0
  158. //model.UnionStart = ""
  159. //commentItem.Models = append(commentItem.Models, *model)
  160. //commentItem.Position = "A1"
  161. //commentItem.RangeData = "A2:B2791"
  162. //commentItem.ShowBlankLines = false
  163. //commentItem.StartDate = ""
  164. //commentItem.Transpose = false
  165. //commentItem.UpdateMode = 0
  166. //commentItem.LookModel.IsLast = false
  167. //commentItem.LookModel.LookValue = 0
  168. //commentItem.LookModel.LookType = 0
  169. //commentItem.Ver = 3
  170. //8.136.199.33
  171. //datamysteel.hzinsights.com