index.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/services"
  12. "hongze/mysteel_watch/utils"
  13. "os"
  14. "time"
  15. )
  16. type IndexController struct {
  17. }
  18. func (s *IndexController) ServerCheck(c *gin.Context) {
  19. resp.OkData("检测成功", 1, c)
  20. return
  21. }
  22. // Add 生成指标
  23. func (s *IndexController) Create(c *gin.Context) {
  24. req := new(index.IndexAddReq)
  25. err := c.Bind(&req)
  26. if err != nil {
  27. errs, ok := err.(validator.ValidationErrors)
  28. if !ok {
  29. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  30. return
  31. }
  32. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  33. return
  34. }
  35. fmt.Println("indexCode:" + req.IndexCode)
  36. if req.RunMode == "" {
  37. req.RunMode = "debug"
  38. }
  39. //fileName := req.IndexName + "_" + req.IndexCode + ".xlsx"
  40. var fileName string
  41. if req.UpdateWeek != "" {
  42. fileName = req.IndexCode + "_" + req.UpdateWeek + "_" + req.RunMode + ".xlsx" //保存的文件名称
  43. } else {
  44. fileName = req.IndexCode + "_" + req.RunMode + ".xlsx" //保存的文件名称
  45. }
  46. filePath := utils.IndexSaveDir + fileName
  47. templatePath := utils.IndexSaveDir + "index_template.xlsx"
  48. templateFile, err := excelize.OpenFile(templatePath)
  49. if err != nil {
  50. resp.FailData("打开文件失败", "打开文件失败,Err:"+err.Error(), c)
  51. return
  52. }
  53. defer func() {
  54. templateFile.Close()
  55. }()
  56. //timeTag := time.Now().UnixNano() / 1e6
  57. //timeTagStr := fmt.Sprintf("%d", timeTag)
  58. //UpdateMode:1:增量更新,0:全量更新
  59. //commentStr := `"BlankValue":"0","CanMark":true,"ChartLineType":"0","DateBlock":0,"DateBlockCount":1,"DateFormat":0,"DateTimeTag":` + timeTagStr + `,"EndDate":"",
  60. // "ExportType":0,"HasDescription":true,"HasEmptyRows":false,"HasFrequency":true,"HasIndexID":true,"HasLastDate":true,"HasSourceName":true,"HasTimeInterval":true,
  61. // "HasUnit":true,"HasUpdateDate":true,"IsCreateChart":false,"IsDataSort":true,"IsNewSheet":false,"IsNewWorkbook":false,
  62. // "Models":[{"DataFormat":0,"DataStartDate":"2011-03-25","DefineName":"","DefineUnit":"","DisplayIndexCode":"` + req.IndexCode + `",
  63. // "IndexCode":"` + req.IndexCode + `","IndexFormula":"` + req.IndexCode + `","PointValue":0,"UnionStart":""}],"Position":"A1","RangeData":"A2:B2791",
  64. // "ShowBlankLines":false,"StartDate":"","Transpose":false,"UpdateMode":1,"lookModel":{"IsLast":false,"LookValue":0,"lookType":0},"ver":3}`
  65. startDate := "1990-01-01"
  66. 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}
  67. `
  68. commentMap := make(map[string]interface{})
  69. commentMap["author"] = "{"
  70. commentMap["text"] = commentStr
  71. //commentMap["text"] = commentItem
  72. commentJson, err := json.Marshal(commentMap)
  73. if err != nil {
  74. fmt.Println("json.Marshal err:" + err.Error())
  75. }
  76. fmt.Println("commentJson")
  77. fmt.Println(string(commentJson))
  78. templateFile.DeleteComment("Sheet1", "A1")
  79. templateFile.AddComment("Sheet1", "A1", string(commentJson))
  80. if err := templateFile.SaveAs(filePath); err != nil {
  81. fmt.Println(err)
  82. resp.FailData("保存失败", "保存失败,Err:"+err.Error(), c)
  83. return
  84. }
  85. time.Sleep(1 * time.Second)
  86. fmt.Println("start MysteelChemicalRefresh")
  87. services.MysteelChemicalRefresh(filePath)
  88. resp.Ok("保存成功", c)
  89. return
  90. }
  91. // Delete 删除指标文件
  92. func (s *IndexController) Delete(c *gin.Context) {
  93. req := new(index.IndexAddReq)
  94. err := c.Bind(&req)
  95. if err != nil {
  96. errs, ok := err.(validator.ValidationErrors)
  97. if !ok {
  98. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  99. return
  100. }
  101. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  102. return
  103. }
  104. fmt.Println("params start")
  105. fmt.Println(req.RunMode)
  106. fmt.Println("params end")
  107. var fileName string
  108. if req.UpdateWeek != "" {
  109. fileName = req.IndexCode + "_" + req.UpdateWeek + "_" + req.RunMode + ".xlsx" //保存的文件名称
  110. } else {
  111. fileName = req.IndexCode + "_" + req.RunMode + ".xlsx" //保存的文件名称
  112. }
  113. filePath := utils.IndexSaveDir + fileName
  114. err = os.Remove(filePath)
  115. if err != nil {
  116. fmt.Println("filePath:" + filePath)
  117. fmt.Println("err:" + err.Error())
  118. resp.Ok("删除失败", c)
  119. return
  120. }
  121. resp.Ok("删除成功", c)
  122. return
  123. }
  124. func (s *IndexController) Test(c *gin.Context) {
  125. resp.OkData("检测成功", 1, c)
  126. return
  127. }
  128. //
  129. //func init() {
  130. // fmt.Println("start")
  131. // Update()
  132. // fmt.Println("end")
  133. //}
  134. //
  135. //// Add 生成指标
  136. //func Update() {
  137. // templatePath := "D:\\hz\\mysteel_data\\ID00185031.xlsx"
  138. // templateFile, err := excelize.OpenFile(templatePath)
  139. // if err != nil {
  140. // fmt.Println("OpenFile Err:" + err.Error())
  141. // return
  142. // }
  143. // defer func() {
  144. // templateFile.Close()
  145. // }()
  146. //
  147. // commentArr := templateFile.GetComments()
  148. // for k, v := range commentArr {
  149. // fmt.Println(k, v)
  150. // for _, sv := range v {
  151. // fmt.Println("text:", sv.Text)
  152. // if strings.Contains(sv.Text, `"UpdateMode":0`) {
  153. // newText := strings.Replace(sv.Text, `"UpdateMode":0`, `"UpdateMode":1`, -1)
  154. // newText = strings.Trim(newText, "{")
  155. // fmt.Println("newText", newText)
  156. //
  157. // commentMap := make(map[string]interface{})
  158. // commentMap["author"] = "{"
  159. // commentMap["text"] = newText
  160. // //commentMap["text"] = commentItem
  161. //
  162. // commentJson, err := json.Marshal(commentMap)
  163. // if err != nil {
  164. // fmt.Println("json.Marshal err:" + err.Error())
  165. // }
  166. // fmt.Println("add text:" + string(commentJson))
  167. // templateFile.DeleteComment("Sheet1", "A1")
  168. // err = templateFile.AddComment("Sheet1", "A1", string(commentJson))
  169. // if err != nil {
  170. // fmt.Println("AddComment Err:" + err.Error())
  171. // }
  172. // }
  173. // }
  174. // }
  175. // //
  176. // // startDate := "1990-01-01"
  177. // // 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}
  178. // //`
  179. // // commentMap := make(map[string]interface{})
  180. // // commentMap["author"] = "{"
  181. // // commentMap["text"] = commentStr
  182. // // //commentMap["text"] = commentItem
  183. // //
  184. // // commentJson, err := json.Marshal(commentMap)
  185. // // if err != nil {
  186. // // fmt.Println("json.Marshal err:" + err.Error())
  187. // // }
  188. // //
  189. // // fmt.Println("commentJson")
  190. // // fmt.Println(string(commentJson))
  191. // // templateFile.DeleteComment("Sheet1", "A1")
  192. // // templateFile.AddComment("Sheet1", "A1", string(commentJson))
  193. // filePath := "D:\\hz\\mysteel_data\\ID00185031_01.xlsx"
  194. // if err := templateFile.SaveAs(filePath); err != nil {
  195. // fmt.Println("templateFile.SaveAs Err:", err)
  196. // return
  197. // }
  198. // return
  199. //}