index_create.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package services
  2. import (
  3. "eta/mysteel_watch/global"
  4. "eta/mysteel_watch/models/index"
  5. "eta/mysteel_watch/utils"
  6. "eta/mysteel_watch/watch"
  7. "fmt"
  8. "github.com/xuri/excelize/v2"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "time"
  14. )
  15. const (
  16. IndexCreatePageSize = 100 //单个Sheet页,指标个数
  17. )
  18. var indexCreateCheckLock sync.RWMutex
  19. // 有更新的指标
  20. var hasRefreshIndexMap = make(map[string]int)
  21. func IndexCreateCheckMerge() (err error) {
  22. indexCreateCheckLock.Lock()
  23. defer func() {
  24. indexCreateCheckLock.Unlock()
  25. }()
  26. fmt.Println("IndexCreateCheck")
  27. findList, err := watch.GetIndexCreate()
  28. if err != nil {
  29. return
  30. }
  31. lenFindList := len(findList)
  32. fmt.Println("查出来的指标数量:", lenFindList)
  33. time.Sleep(3 * time.Second)
  34. if lenFindList <= 0 {
  35. return nil
  36. }
  37. // 超限的指标编码
  38. codeList := make([]string, 0)
  39. // 需要去查询的指标编码
  40. list := make([]index.BaseFromMysteelChemicalIndex, 0)
  41. for _, v := range findList {
  42. if tmpV, ok := hasRefreshIndexMap[v.IndexCode]; ok {
  43. // 超过5次没刷到数据,就不处理了
  44. if tmpV >= 5 {
  45. codeList = append(codeList, v.IndexCode)
  46. continue
  47. }
  48. }
  49. list = append(list, v)
  50. }
  51. if len(codeList) > 0 {
  52. fmt.Println(strings.Join(codeList, " , "), ";刷新超限了")
  53. }
  54. lenList := len(list)
  55. fmt.Println("需要生成的指标数量:", lenList)
  56. if lenList <= 0 {
  57. return nil
  58. }
  59. var filePre string
  60. filePre = list[0].IndexCode
  61. startDate := "1990-01-01"
  62. fileName := "create_" + filePre + ".xlsx" //保存的文件名称
  63. mergeIndexCreateFilePath := global.CONFIG.Serve.IndexSaveDir + fileName
  64. if utils.FileIsExist(mergeIndexCreateFilePath) { //修改文件
  65. err = os.Remove(mergeIndexCreateFilePath)
  66. if err != nil {
  67. fmt.Println("os.Remove Err:" + err.Error() + "; mergeIndexCreateFilePath:" + mergeIndexCreateFilePath)
  68. return err
  69. }
  70. }
  71. // 将指标标记下,不重复刷新数据
  72. for _, v := range list {
  73. tmpV, ok := hasRefreshIndexMap[v.IndexCode]
  74. if !ok {
  75. tmpV = 0
  76. }
  77. hasRefreshIndexMap[v.IndexCode] = tmpV + 1
  78. }
  79. templateFile := excelize.NewFile()
  80. defer func() {
  81. if err := templateFile.Close(); err != nil {
  82. fmt.Println("templateFile.Close() err:" + err.Error())
  83. }
  84. }()
  85. // 总共需要生成的sheet数量
  86. num := lenList / IndexCreatePageSize
  87. if lenList%IndexCreatePageSize > 0 {
  88. num++
  89. }
  90. fmt.Println("需要生成的sheet页数量:", num)
  91. endIndex := num - 1
  92. for i := 0; i <= endIndex; i++ {
  93. pageList := make([]index.BaseFromMysteelChemicalIndex, 0)
  94. if i < endIndex {
  95. pageList = list[IndexCreatePageSize*i : IndexCreatePageSize*(i+1)]
  96. } else {
  97. pageList = list[IndexCreatePageSize*i:]
  98. }
  99. //fmt.Println(pageList)
  100. // 生成批注
  101. commentResult, tmpErr := GetIndexCommentV3(mergeIndexCreateFilePath, startDate, pageList)
  102. if tmpErr != nil {
  103. fmt.Println("GetIndexCommentV3 Err:" + tmpErr.Error())
  104. err = tmpErr
  105. return
  106. }
  107. // 写入到对应的sheet页
  108. sheetName := "Sheet" + strconv.Itoa(i+1)
  109. templateFile.NewSheet(sheetName)
  110. templateFile.DeleteComment(sheetName, "A1")
  111. templateFile.AddComment(sheetName, "A1", commentResult)
  112. //templateFile.SetSheetRow(sheetName, "A1", "钢联数据")
  113. templateFile.SetCellStr(sheetName, "A1", "钢联数据")
  114. }
  115. if err = templateFile.SaveAs(mergeIndexCreateFilePath); err != nil {
  116. fmt.Println("保存失败,Err:" + err.Error())
  117. templateFile.Close()
  118. return
  119. }
  120. defer templateFile.Close()
  121. time.Sleep(3 * time.Second)
  122. // 将待刷新的excel文件路径传入到list里面处理
  123. AddIndexRefreshToLPush(mergeIndexCreateFilePath)
  124. return nil
  125. }