clarksons_watch.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_data_analysis/models"
  5. "eta/eta_data_analysis/utils"
  6. "fmt"
  7. "github.com/patrickmn/go-cache"
  8. "github.com/tealeg/xlsx"
  9. "io/fs"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "syscall"
  14. "time"
  15. )
  16. func clarksonsWatch() {
  17. fmt.Println("clarksonsWatch start")
  18. var err error
  19. defer func() {
  20. if err != nil {
  21. fmt.Println("clarksonsWatch Err:" + err.Error())
  22. }
  23. }()
  24. var cacheClient *cache.Cache
  25. if cacheClient == nil {
  26. cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
  27. }
  28. err = filepath.Walk(utils.ClarkSonsFilePath, func(path string, info fs.FileInfo, err error) error {
  29. if err != nil {
  30. return err
  31. }
  32. if !info.IsDir() {
  33. fileInfo, err := os.Stat(path)
  34. if err != nil {
  35. fmt.Println("os.Stat:", err.Error())
  36. }
  37. winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
  38. modifyTimeStr := utils.SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
  39. existModifyTime, ok := cacheClient.Get(path)
  40. if ok {
  41. existModifyTimeStr := existModifyTime.(string)
  42. if existModifyTimeStr != modifyTimeStr {
  43. if strings.Contains(path, "克拉克森") {
  44. err = Clarksons(path)
  45. }
  46. }
  47. } else {
  48. if strings.Contains(path, "克拉克森") {
  49. err = Clarksons(path)
  50. }
  51. }
  52. cacheClient.Delete(path)
  53. cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
  54. }
  55. return nil
  56. })
  57. }
  58. func Clarksons(path string) (err error) {
  59. defer func() {
  60. if err != nil {
  61. fmt.Println("Clarksons Err:" + err.Error())
  62. utils.FileLog.Info(fmt.Sprintf("Clarksons, Err: %s", err))
  63. }
  64. }()
  65. path = "/Users/xi/Desktop/SIN_Timeseries_20241217022653.xlsx"
  66. var xlFile *xlsx.File
  67. exist, err := PathExists(path)
  68. if err != nil {
  69. fmt.Println(err)
  70. return
  71. }
  72. if exist {
  73. xlFile, err = xlsx.OpenFile(path)
  74. if err != nil {
  75. fmt.Println("OpenFile err:", err)
  76. return
  77. }
  78. } else {
  79. fmt.Println("Not Exist")
  80. return
  81. }
  82. sheetDatas := make([]models.SheetData, 0)
  83. for i, sheet := range xlFile.Sheets {
  84. if i > 0 {
  85. break
  86. }
  87. data := sheet
  88. sheetData := models.SheetData{
  89. Name: data.Name,
  90. MaxRow: data.MaxRow,
  91. MaxCol: data.MaxCol,
  92. Hidden: data.Hidden,
  93. Selected: data.Selected,
  94. }
  95. rows := make([]models.Row, 0)
  96. for _, v := range data.Rows {
  97. cells := make([]models.Cell, 0)
  98. for _, cell := range v.Cells {
  99. cells = append(cells, models.Cell{
  100. Value: cell.String(),
  101. })
  102. }
  103. row := models.Row{
  104. Cells: cells,
  105. }
  106. rows = append(rows, row)
  107. }
  108. sheetData.Rows = rows
  109. sheetDatas = append(sheetDatas, sheetData)
  110. }
  111. params := make(map[string]interface{})
  112. params["SheetData"] = sheetDatas
  113. result, e := PostEdbLib(params, utils.LIB_ROUTE_CLARKSONS)
  114. if e != nil {
  115. b, _ := json.Marshal(params)
  116. fmt.Println(e)
  117. utils.FileLog.Info(fmt.Sprintf("PostEdbLib err: %s, params: %s", e.Error(), string(b)))
  118. return
  119. }
  120. resp := new(models.BaseEdbLibResponse)
  121. if e := json.Unmarshal(result, &resp); e != nil {
  122. utils.FileLog.Info(fmt.Sprintf("json.Unmarshal err: %s", e))
  123. return
  124. }
  125. if resp.Ret != 200 {
  126. utils.FileLog.Info(fmt.Sprintf("Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
  127. return
  128. }
  129. return
  130. }