batchDownloadServices.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package services
  2. import (
  3. "archive/zip"
  4. "fwh-toolbox-server/domain"
  5. "fwh-toolbox-server/dto"
  6. "fwh-toolbox-server/repositories"
  7. "fwh-toolbox-server/taskQueue"
  8. "fwh-toolbox-server/utils/minio"
  9. "io"
  10. "go.mongodb.org/mongo-driver/bson/primitive"
  11. "go.uber.org/zap"
  12. )
  13. // 从数据库获取批量数据下载列表
  14. func GetBatchDownloadManageList(page, size uint, sn, progress *string) (*dto.ResPage[domain.BatchDownloadInfo], error) {
  15. return repositories.GetBatchDownloadManageList(page, size, sn, progress)
  16. }
  17. // 插入一条批量数据下载配置到数据库
  18. func InsertBatchDownloadConfig(writeConfig domain.BatchDownloadConfigWrite) error {
  19. id, err := repositories.InsertBatchDownloadConfig(writeConfig)
  20. if err != nil {
  21. return err
  22. }
  23. taskQueue.EnqueueBatchDownloadTask(domain.TaskBatchDownload{
  24. Id: id,
  25. Sn: writeConfig.Sn,
  26. TimePeriod: writeConfig.TimePeriod,
  27. Options: writeConfig.Options,
  28. })
  29. return nil
  30. }
  31. // 更新一条批量数据下载配置到数据库
  32. func UpdateBatchDownloadDescription(id primitive.ObjectID, desc string) error {
  33. err := repositories.UpdateBatchDownloadDescription(id, desc)
  34. if err != nil {
  35. return err
  36. }
  37. return nil
  38. }
  39. // 删除一条批量数据下载配置
  40. func DeleteBatchDownloadConfig(id primitive.ObjectID) error {
  41. err := repositories.DeleteBatchDownloadConfig(id)
  42. if err != nil {
  43. return err
  44. }
  45. // TODO 需删除相关数据
  46. return nil
  47. }
  48. // 从数据库获取批量数据下载文件地址并打包成zip文件
  49. func GetBatchDownloadData(id primitive.ObjectID) (*io.PipeReader, error) {
  50. dataPathMap, err := repositories.GetBatchDownloadData(id)
  51. if err != nil {
  52. return nil, err
  53. }
  54. pr, pw := io.Pipe()
  55. zw := zip.NewWriter(pw)
  56. go func() {
  57. defer zw.Close()
  58. defer pw.Close()
  59. var InsertZipFile = func(path string) error {
  60. obj, err := minio.GetObject(path)
  61. if err != nil {
  62. zap.L().Error("获取对象失败", zap.String("objName", path), zap.String("err", err.Error()))
  63. return err
  64. }
  65. zipFileWriter, err := zw.Create(path)
  66. if err != nil {
  67. zap.L().Error("创建zip条目失败", zap.String("path", path), zap.String("err", err.Error()))
  68. return err
  69. }
  70. if _, err := io.Copy(zipFileWriter, obj); err != nil {
  71. zap.L().Error("写入zip失败", zap.String("path", path), zap.String("err", err.Error()))
  72. return err
  73. }
  74. return nil
  75. }
  76. // map[sn]batchData
  77. for _, batchData := range dataPathMap {
  78. // PowerFlow
  79. // map[date]path
  80. for _, path := range batchData.PowerFlow {
  81. if err := InsertZipFile(path); err != nil {
  82. continue
  83. }
  84. }
  85. // MqttLogs
  86. // map[date]logs
  87. for _, logs := range batchData.MqttLogs {
  88. // map[logType]path
  89. for _, path := range logs {
  90. if err := InsertZipFile(path); err != nil {
  91. continue
  92. }
  93. }
  94. }
  95. // EventAlarm
  96. // map[date]path
  97. for _, path := range batchData.EventAlarm {
  98. if err := InsertZipFile(path); err != nil {
  99. continue
  100. }
  101. }
  102. // EmsLogs
  103. // map[date]logs
  104. for _, logs := range batchData.EmsLogs {
  105. // map[logType]path
  106. for _, path := range logs {
  107. if err := InsertZipFile(path); err != nil {
  108. continue
  109. }
  110. }
  111. }
  112. // FaultRecord
  113. // map[date]path
  114. for _, path := range batchData.FaultRecord {
  115. if err := InsertZipFile(path); err != nil {
  116. continue
  117. }
  118. }
  119. // UpdateLog
  120. if batchData.UpdateLog != "" {
  121. if err := InsertZipFile(batchData.UpdateLog); err != nil {
  122. continue
  123. }
  124. }
  125. // Settings
  126. if batchData.Settings != "" {
  127. if err := InsertZipFile(batchData.Settings); err != nil {
  128. continue
  129. }
  130. }
  131. }
  132. }()
  133. // 返回流给客户端
  134. return pr, nil
  135. }