index.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package index
  2. import (
  3. "encoding/json"
  4. "eta/mysteel_watch/controller/resp"
  5. "eta/mysteel_watch/global"
  6. "eta/mysteel_watch/models/index"
  7. "eta/mysteel_watch/services"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "github.com/go-playground/validator/v10"
  11. "github.com/xuri/excelize/v2"
  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. fileName = req.IndexCode + "_" + req.RunMode + ".xlsx" //保存的文件名称
  41. filePath := global.CONFIG.Serve.IndexSaveDir + fileName
  42. templatePath := global.CONFIG.Serve.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. sheetList := templateFile.GetSheetList()
  52. for k, v := range sheetList {
  53. if k > 0 {
  54. templateFile.DeleteSheet(v)
  55. }
  56. }
  57. //timeTag := time.Now().UnixNano() / 1e6
  58. //timeTagStr := fmt.Sprintf("%d", timeTag)
  59. //UpdateMode:1:增量更新,0:全量更新
  60. //commentStr := `"BlankValue":"0","CanMark":true,"ChartLineType":"0","DateBlock":0,"DateBlockCount":1,"DateFormat":0,"DateTimeTag":` + timeTagStr + `,"EndDate":"",
  61. // "ExportType":0,"HasDescription":true,"HasEmptyRows":false,"HasFrequency":true,"HasIndexID":true,"HasLastDate":true,"HasSourceName":true,"HasTimeInterval":true,
  62. // "HasUnit":true,"HasUpdateDate":true,"IsCreateChart":false,"IsDataSort":true,"IsNewSheet":false,"IsNewWorkbook":false,
  63. // "Models":[{"DataFormat":0,"DataStartDate":"2011-03-25","DefineName":"","DefineUnit":"","DisplayIndexCode":"` + req.IndexCode + `",
  64. // "IndexCode":"` + req.IndexCode + `","IndexFormula":"` + req.IndexCode + `","PointValue":0,"UnionStart":""}],"Position":"A1","RangeData":"A2:B2791",
  65. // "ShowBlankLines":false,"StartDate":"","Transpose":false,"UpdateMode":1,"lookModel":{"IsLast":false,"LookValue":0,"lookType":0},"ver":3}`
  66. startDate := "1990-01-01"
  67. 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}
  68. `
  69. commentMap := make(map[string]interface{})
  70. commentMap["author"] = "{"
  71. commentMap["text"] = commentStr
  72. //commentMap["text"] = commentItem
  73. commentJson, err := json.Marshal(commentMap)
  74. if err != nil {
  75. fmt.Println("json.Marshal err:" + err.Error())
  76. }
  77. fmt.Println("commentJson")
  78. fmt.Println(string(commentJson))
  79. templateFile.DeleteComment("Sheet1", "A1")
  80. templateFile.AddComment("Sheet1", "A1", string(commentJson))
  81. if err := templateFile.SaveAs(filePath); err != nil {
  82. fmt.Println(err)
  83. resp.FailData("保存失败", "保存失败,Err:"+err.Error(), c)
  84. return
  85. }
  86. time.Sleep(1 * time.Second)
  87. fmt.Println("start MysteelChemicalRefresh")
  88. //services.MysteelChemicalRefresh(filePath)
  89. services.AddIndexRefreshToLPush(filePath)
  90. resp.Ok("保存成功", c)
  91. return
  92. }
  93. // Delete 删除指标文件
  94. func (s *IndexController) Delete(c *gin.Context) {
  95. req := new(index.IndexAddReq)
  96. err := c.Bind(&req)
  97. if err != nil {
  98. errs, ok := err.(validator.ValidationErrors)
  99. if !ok {
  100. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  101. return
  102. }
  103. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  104. return
  105. }
  106. fmt.Println("params start")
  107. fmt.Println(req.RunMode)
  108. fmt.Println("params end")
  109. var fileName string
  110. if req.UpdateWeek != "" {
  111. fileName = req.IndexCode + "_" + req.UpdateWeek + "_" + req.RunMode + ".xlsx" //保存的文件名称
  112. } else {
  113. fileName = req.IndexCode + "_" + req.RunMode + ".xlsx" //保存的文件名称
  114. }
  115. filePath := global.CONFIG.Serve.IndexSaveDir + fileName
  116. err = os.Remove(filePath)
  117. if err != nil {
  118. fmt.Println("filePath:" + filePath)
  119. fmt.Println("err:" + err.Error())
  120. resp.Ok("删除失败", c)
  121. return
  122. }
  123. resp.Ok("删除成功", c)
  124. return
  125. }
  126. func (s *IndexController) Test(c *gin.Context) {
  127. resp.OkData("检测成功", 1, c)
  128. return
  129. }
  130. // Add 刷新指标
  131. func (s *IndexController) Refresh(c *gin.Context) {
  132. req := new(index.IndexRefreshReq)
  133. err := c.Bind(&req)
  134. if err != nil {
  135. errs, ok := err.(validator.ValidationErrors)
  136. if !ok {
  137. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  138. return
  139. }
  140. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  141. return
  142. }
  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. //}