index.go 8.5 KB

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