index.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. services.AddIndexRefreshToLpush(filePath)
  89. resp.Ok("保存成功", c)
  90. return
  91. }
  92. // Delete 删除指标文件
  93. func (s *IndexController) Delete(c *gin.Context) {
  94. req := new(index.IndexAddReq)
  95. err := c.Bind(&req)
  96. if err != nil {
  97. errs, ok := err.(validator.ValidationErrors)
  98. if !ok {
  99. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  100. return
  101. }
  102. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  103. return
  104. }
  105. fmt.Println("params start")
  106. fmt.Println(req.RunMode)
  107. fmt.Println("params end")
  108. var fileName string
  109. if req.UpdateWeek != "" {
  110. fileName = req.IndexCode + "_" + req.UpdateWeek + "_" + req.RunMode + ".xlsx" //保存的文件名称
  111. } else {
  112. fileName = req.IndexCode + "_" + req.RunMode + ".xlsx" //保存的文件名称
  113. }
  114. filePath := utils.IndexSaveDir + fileName
  115. err = os.Remove(filePath)
  116. if err != nil {
  117. fmt.Println("filePath:" + filePath)
  118. fmt.Println("err:" + err.Error())
  119. resp.Ok("删除失败", c)
  120. return
  121. }
  122. resp.Ok("删除成功", c)
  123. return
  124. }
  125. func (s *IndexController) Test(c *gin.Context) {
  126. resp.OkData("检测成功", 1, c)
  127. return
  128. }
  129. // Add 刷新指标
  130. func (s *IndexController) Refresh(c *gin.Context) {
  131. req := new(index.IndexRefreshReq)
  132. err := c.Bind(&req)
  133. if err != nil {
  134. errs, ok := err.(validator.ValidationErrors)
  135. if !ok {
  136. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  137. return
  138. }
  139. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  140. return
  141. }
  142. //services.MysteelChemicalRefresh(req.MergeFilePath)
  143. services.AddIndexRefreshToLpush(req.MergeFilePath)
  144. resp.Ok("保存成功", c)
  145. return
  146. }
  147. //
  148. //func init() {
  149. // fmt.Println("start")
  150. // Update()
  151. // fmt.Println("end")
  152. //}
  153. //
  154. //// Add 生成指标
  155. //func Update() {
  156. // templatePath := "D:\\hz\\mysteel_data\\ID00185031.xlsx"
  157. // templateFile, err := excelize.OpenFile(templatePath)
  158. // if err != nil {
  159. // fmt.Println("OpenFile Err:" + err.Error())
  160. // return
  161. // }
  162. // defer func() {
  163. // templateFile.Close()
  164. // }()
  165. //
  166. // commentArr := templateFile.GetComments()
  167. // for k, v := range commentArr {
  168. // fmt.Println(k, v)
  169. // for _, sv := range v {
  170. // fmt.Println("text:", sv.Text)
  171. // if strings.Contains(sv.Text, `"UpdateMode":0`) {
  172. // newText := strings.Replace(sv.Text, `"UpdateMode":0`, `"UpdateMode":1`, -1)
  173. // newText = strings.Trim(newText, "{")
  174. // fmt.Println("newText", newText)
  175. //
  176. // commentMap := make(map[string]interface{})
  177. // commentMap["author"] = "{"
  178. // commentMap["text"] = newText
  179. // //commentMap["text"] = commentItem
  180. //
  181. // commentJson, err := json.Marshal(commentMap)
  182. // if err != nil {
  183. // fmt.Println("json.Marshal err:" + err.Error())
  184. // }
  185. // fmt.Println("add text:" + string(commentJson))
  186. // templateFile.DeleteComment("Sheet1", "A1")
  187. // err = templateFile.AddComment("Sheet1", "A1", string(commentJson))
  188. // if err != nil {
  189. // fmt.Println("AddComment Err:" + err.Error())
  190. // }
  191. // }
  192. // }
  193. // }
  194. // //
  195. // // startDate := "1990-01-01"
  196. // // 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}
  197. // //`
  198. // // commentMap := make(map[string]interface{})
  199. // // commentMap["author"] = "{"
  200. // // commentMap["text"] = commentStr
  201. // // //commentMap["text"] = commentItem
  202. // //
  203. // // commentJson, err := json.Marshal(commentMap)
  204. // // if err != nil {
  205. // // fmt.Println("json.Marshal err:" + err.Error())
  206. // // }
  207. // //
  208. // // fmt.Println("commentJson")
  209. // // fmt.Println(string(commentJson))
  210. // // templateFile.DeleteComment("Sheet1", "A1")
  211. // // templateFile.AddComment("Sheet1", "A1", string(commentJson))
  212. // filePath := "D:\\hz\\mysteel_data\\ID00185031_01.xlsx"
  213. // if err := templateFile.SaveAs(filePath); err != nil {
  214. // fmt.Println("templateFile.SaveAs Err:", err)
  215. // return
  216. // }
  217. // return
  218. //}