autoUpdateConfigServices.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package services
  2. import (
  3. "bufio"
  4. "fmt"
  5. "fwh-toolbox-server/domain"
  6. "fwh-toolbox-server/dto"
  7. "fwh-toolbox-server/jobs"
  8. "fwh-toolbox-server/repositories"
  9. "fwh-toolbox-server/utils/minio"
  10. "io"
  11. "path"
  12. "strings"
  13. "time"
  14. )
  15. type AutoUpdateConfigFileInfoPage = dto.ResPage[dto.AutoUpdateConfigFileInfo]
  16. var (
  17. configFileKeyPrefix = "configFile/"
  18. )
  19. func GetAutoUpdateConfigOption() (*domain.AutoUpdateConfigOption, error) {
  20. return repositories.GetAutoUpdateConfigOption()
  21. }
  22. func GetAutoUpdatePackageInfo(fileName string) (*domain.AutoUpdatePackages, error) {
  23. return repositories.GetAutoUpdatePackages(fileName)
  24. }
  25. func CreateAutoUpdatePackageInfo(packageType string, info domain.AutoUpdatePackages) error {
  26. return repositories.CreateAutoUpdatePackage(packageType, info)
  27. }
  28. func UpdateAutoUpdatePackageInfo(fileName string, info domain.AutoUpdatePackages) error {
  29. return repositories.UpdateAutoUpdatePackage(fileName, info)
  30. }
  31. func DeleteAutoUpdatePackageInfo(packageType string, fileName string) error {
  32. return repositories.DeleteAutoUpdatePackage(fileName, fileName)
  33. }
  34. func GetAutoUpdateAPowerVersion() (*[]domain.AutoUpdateAPowerVersion, error) {
  35. return repositories.GetAutoUpdateAPowerVersion()
  36. }
  37. func UpdateAutoUpdateAPowerVersion(version string, info domain.AutoUpdateAPowerVersion) error {
  38. return repositories.UpdateAutoUpdateAPowerVersion(version, info)
  39. }
  40. func DeleteAutoUpdateAPowerVersion(version string) error {
  41. return repositories.DeleteAutoUpdateAPowerVersion(version)
  42. }
  43. func GetAutoUpdateConfig(aGateVersion string) (*domain.AutoUpdateConfig, error) {
  44. return repositories.GetAutoUpdateConfig(aGateVersion)
  45. }
  46. func UpdateAutoUpdateConfig(aGateVersion string, config domain.AutoUpdateConfig) error {
  47. return repositories.UpdateAutoUpdateConfig(aGateVersion, config)
  48. }
  49. func DeleteAutoUpdateConfig(aGateVersion string) error {
  50. return repositories.DeleteAutoUpdateConfig(aGateVersion)
  51. }
  52. func CreateAutoUpdateSpecialConfig(aGateVersion string, config domain.AutoUpdateSpecialTypeConfig) error {
  53. return repositories.CreateAutoUpdateSpecialConfig(aGateVersion, config)
  54. }
  55. func DeleteAutoUpdateSpecialConfig(aGateVersion string, id string) error {
  56. return repositories.DeleteAutoUpdateSpecialConfig(aGateVersion, id)
  57. }
  58. func refreshAutoUpdateConfigFileInfo() error {
  59. // 从Minio查询数据
  60. objectList, err := minio.ListFilesObjectFromBucket("fwh-toolbox-auto-update", configFileKeyPrefix, false)
  61. if err != nil {
  62. return err
  63. }
  64. // 数据写入缓存
  65. if err := repositories.RefreshAutoUpdateConfigFileInfo(objectList); err != nil {
  66. return err
  67. }
  68. return nil
  69. }
  70. // Excel文件生成管理相关服务
  71. func GetAutoUpdateConfigFileInfo(page uint, size uint) (*AutoUpdateConfigFileInfoPage, error) {
  72. var fileInfoList []minio.MinioFileObject
  73. // 先查缓存
  74. total, err := repositories.GetAutoUpdateConfigFileInfoTotal()
  75. if err != nil {
  76. return nil, err
  77. } else if total > 0 {
  78. if fileInfoList, err = repositories.GetAutoUpdateConfigFileInfoByPage(page, size); err != nil {
  79. return nil, err
  80. }
  81. } else {
  82. // 缓存无数据则刷新缓存
  83. if err := refreshAutoUpdateConfigFileInfo(); err != nil {
  84. return nil, err
  85. }
  86. // 从新查缓存
  87. total, err = repositories.GetAutoUpdateConfigFileInfoTotal()
  88. if err != nil {
  89. return nil, err
  90. } else if total > 0 {
  91. if fileInfoList, err = repositories.GetAutoUpdateConfigFileInfoByPage(page, size); err != nil {
  92. return nil, err
  93. }
  94. }
  95. }
  96. // 提取数据信息
  97. result := make([]dto.AutoUpdateConfigFileInfo, 0, len(fileInfoList))
  98. for _, obj := range fileInfoList {
  99. fileName := path.Base(obj.Key)
  100. result = append(result, dto.AutoUpdateConfigFileInfo{
  101. FileName: fileName,
  102. Key: obj.Key,
  103. LastModified: obj.LastModified.Format(time.DateTime),
  104. })
  105. }
  106. if total < 0 {
  107. total = 0
  108. }
  109. infoPage := AutoUpdateConfigFileInfoPage{
  110. Data: result,
  111. PageSize: size,
  112. PageNum: page,
  113. Total: uint(total),
  114. }
  115. return &infoPage, nil
  116. }
  117. func CreateAutoUpdateConfigFile() error {
  118. now := time.Now()
  119. date := strings.ReplaceAll(now.Format(time.DateOnly), "-", "")
  120. nano := now.UnixNano() % 1e6 // 取后6位,够用
  121. filePath := fmt.Sprintf("【生产环境】自动升级配置表_%s_%06d.xlsx", date, nano)
  122. key := configFileKeyPrefix + filePath
  123. if err := jobs.CreateAutoUpdateConfigFile(filePath, "fwh-toolbox-auto-update", key); err != nil {
  124. return err
  125. }
  126. // 创建文件后,刷新缓存
  127. refreshAutoUpdateConfigFileInfo()
  128. return nil
  129. }
  130. func DownloadAutoUpdateConfigFile(fileName string, writer *bufio.Writer) error {
  131. obj, err := minio.GetObjectFromBucket("fwh-toolbox-auto-update", configFileKeyPrefix+fileName)
  132. if err != nil {
  133. return err
  134. }
  135. defer obj.Close()
  136. // Fiber 负责读取 pr 并发送给客户端(消费者)
  137. _, err = io.Copy(writer, obj) // 等 pipe EOF 自动结束
  138. if err != nil {
  139. return err
  140. }
  141. return nil
  142. }