find_date_service.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package milk
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "kpt-pasture/config"
  7. "kpt-pasture/model"
  8. "net/http"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "sync"
  13. "time"
  14. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  15. "go.uber.org/zap"
  16. )
  17. type FindDataService struct {
  18. fileCache sync.Map // 用于存储文件信息
  19. cfg config.MilkHallSetting
  20. }
  21. // FileInfo 存储文件信息
  22. type FileInfo struct {
  23. ModTime time.Time
  24. Size int64
  25. }
  26. func NewMilkDataService(cfg config.MilkHallSetting) *FindDataService {
  27. return &FindDataService{
  28. cfg: cfg,
  29. }
  30. }
  31. // ReadFiles 读取指定路径下的所有文件
  32. func (s *FindDataService) ReadFiles() ([]string, error) {
  33. var files []string
  34. for _, path := range s.cfg.FilePath {
  35. fullPath := filepath.Join(s.cfg.BasePath, path)
  36. if err := filepath.Walk(fullPath, func(path string, info os.FileInfo, err error) error {
  37. if err != nil {
  38. return err
  39. }
  40. if !info.IsDir() {
  41. files = append(files, path)
  42. }
  43. return nil
  44. }); err != nil {
  45. return files, err
  46. }
  47. }
  48. return files, nil
  49. }
  50. func (s *FindDataService) Run(fileList []string) {
  51. ticker := time.NewTicker(30 * time.Second) // 增加间隔到30秒
  52. defer ticker.Stop()
  53. // 创建退出通道
  54. done := make(chan struct{})
  55. defer close(done)
  56. for {
  57. select {
  58. case <-ticker.C:
  59. for _, file := range fileList {
  60. // 检查文件是否存在
  61. info, err := os.Stat(file)
  62. if err != nil {
  63. continue
  64. }
  65. // 检查文件大小,如果超过10MB则跳过
  66. /*if info.Size() > 10*1024*1024 {
  67. continue
  68. }*/
  69. // 检查文件是否被修改
  70. if cachedInfo, ok := s.fileCache.Load(file); ok {
  71. if cachedInfo.(FileInfo).ModTime.Equal(info.ModTime()) {
  72. continue // 文件未修改,跳过
  73. }
  74. }
  75. if len(info.Name()) < 30 || strings.Index(info.Name(), "done.") < 0 || strings.Index(info.Name(), "Parlor.json") < 0 {
  76. continue
  77. }
  78. // 读取文件内容
  79. content, err := os.ReadFile(file)
  80. if err != nil {
  81. continue
  82. }
  83. // 更新文件缓存
  84. s.fileCache.Store(file, FileInfo{
  85. ModTime: info.ModTime(),
  86. Size: info.Size(),
  87. })
  88. s.HandleFileContent(content)
  89. }
  90. case <-done:
  91. return
  92. }
  93. }
  94. }
  95. func (s *FindDataService) HandleFileContent(contentBody []byte) {
  96. requestBody := model.MilkHallBody{
  97. Brand: s.cfg.Brand,
  98. FarmId: s.cfg.FarmId,
  99. Content: contentBody,
  100. }
  101. // 将内容转换为 JSON 字节
  102. jsonData, err := json.Marshal(requestBody)
  103. if err != nil {
  104. zaplog.Error("HandleFileContent", zap.Any("error", err))
  105. return
  106. }
  107. // 创建 HTTP 请求
  108. req, err := http.NewRequest("POST", strings.Join([]string{s.cfg.BackPath, s.cfg.UrlPath}, ""), bytes.NewBuffer(jsonData))
  109. if err != nil {
  110. zaplog.Error("HandleFileContent", zap.Any("error", err))
  111. return
  112. }
  113. // 设置请求头
  114. req.Header.Set("Content-Type", "application/json")
  115. req.Header.Set("Authorization", "Bearer your-token") // 如果需要认证,替换为实际的 token
  116. // 创建 HTTP 客户端
  117. client := &http.Client{
  118. Timeout: 10 * time.Second,
  119. }
  120. // 发送请求
  121. resp, err := client.Do(req)
  122. if err != nil {
  123. zaplog.Error("HandleFileContent", zap.Any("error", err))
  124. return
  125. }
  126. defer resp.Body.Close()
  127. // 读取响应
  128. body, err := io.ReadAll(resp.Body)
  129. if err != nil {
  130. zaplog.Error("HandleFileContent", zap.Any("error", err))
  131. return
  132. }
  133. // 检查响应状态码
  134. if resp.StatusCode != http.StatusOK {
  135. zaplog.Error("HandleFileContent", zap.Any("error", err))
  136. return
  137. }
  138. zaplog.Info("HandleFileContent", zap.Any("请求成功,响应内容:", string(body)))
  139. }