package services import ( "bufio" "fmt" "fwh-toolbox-server/domain" "fwh-toolbox-server/dto" "fwh-toolbox-server/jobs" "fwh-toolbox-server/repositories" "fwh-toolbox-server/utils/minio" "io" "path" "strings" "time" ) type AutoUpdateConfigFileInfoPage = dto.ResPage[dto.AutoUpdateConfigFileInfo] var ( configFileKeyPrefix = "configFile/" ) func GetAutoUpdateConfigOption() (*domain.AutoUpdateConfigOption, error) { return repositories.GetAutoUpdateConfigOption() } func GetAutoUpdatePackageInfo(fileName string) (*domain.AutoUpdatePackages, error) { return repositories.GetAutoUpdatePackages(fileName) } func CreateAutoUpdatePackageInfo(packageType string, info domain.AutoUpdatePackages) error { return repositories.CreateAutoUpdatePackage(packageType, info) } func UpdateAutoUpdatePackageInfo(fileName string, info domain.AutoUpdatePackages) error { return repositories.UpdateAutoUpdatePackage(fileName, info) } func DeleteAutoUpdatePackageInfo(packageType string, fileName string) error { return repositories.DeleteAutoUpdatePackage(fileName, fileName) } func GetAutoUpdateAPowerVersion() (*[]domain.AutoUpdateAPowerVersion, error) { return repositories.GetAutoUpdateAPowerVersion() } func UpdateAutoUpdateAPowerVersion(version string, info domain.AutoUpdateAPowerVersion) error { return repositories.UpdateAutoUpdateAPowerVersion(version, info) } func DeleteAutoUpdateAPowerVersion(version string) error { return repositories.DeleteAutoUpdateAPowerVersion(version) } func GetAutoUpdateConfig(aGateVersion string) (*domain.AutoUpdateConfig, error) { return repositories.GetAutoUpdateConfig(aGateVersion) } func UpdateAutoUpdateConfig(aGateVersion string, config domain.AutoUpdateConfig) error { return repositories.UpdateAutoUpdateConfig(aGateVersion, config) } func DeleteAutoUpdateConfig(aGateVersion string) error { return repositories.DeleteAutoUpdateConfig(aGateVersion) } func CreateAutoUpdateSpecialConfig(aGateVersion string, config domain.AutoUpdateSpecialTypeConfig) error { return repositories.CreateAutoUpdateSpecialConfig(aGateVersion, config) } func DeleteAutoUpdateSpecialConfig(aGateVersion string, id string) error { return repositories.DeleteAutoUpdateSpecialConfig(aGateVersion, id) } func refreshAutoUpdateConfigFileInfo() error { // 从Minio查询数据 objectList, err := minio.ListFilesObjectFromBucket("fwh-toolbox-auto-update", configFileKeyPrefix, false) if err != nil { return err } // 数据写入缓存 if err := repositories.RefreshAutoUpdateConfigFileInfo(objectList); err != nil { return err } return nil } // Excel文件生成管理相关服务 func GetAutoUpdateConfigFileInfo(page uint, size uint) (*AutoUpdateConfigFileInfoPage, error) { var fileInfoList []minio.MinioFileObject // 先查缓存 total, err := repositories.GetAutoUpdateConfigFileInfoTotal() if err != nil { return nil, err } else if total > 0 { if fileInfoList, err = repositories.GetAutoUpdateConfigFileInfoByPage(page, size); err != nil { return nil, err } } else { // 缓存无数据则刷新缓存 if err := refreshAutoUpdateConfigFileInfo(); err != nil { return nil, err } // 从新查缓存 total, err = repositories.GetAutoUpdateConfigFileInfoTotal() if err != nil { return nil, err } else if total > 0 { if fileInfoList, err = repositories.GetAutoUpdateConfigFileInfoByPage(page, size); err != nil { return nil, err } } } // 提取数据信息 result := make([]dto.AutoUpdateConfigFileInfo, 0, len(fileInfoList)) for _, obj := range fileInfoList { fileName := path.Base(obj.Key) result = append(result, dto.AutoUpdateConfigFileInfo{ FileName: fileName, Key: obj.Key, LastModified: obj.LastModified.Format(time.DateTime), }) } if total < 0 { total = 0 } infoPage := AutoUpdateConfigFileInfoPage{ Data: result, PageSize: size, PageNum: page, Total: uint(total), } return &infoPage, nil } func CreateAutoUpdateConfigFile() error { now := time.Now() date := strings.ReplaceAll(now.Format(time.DateOnly), "-", "") nano := now.UnixNano() % 1e6 // 取后6位,够用 filePath := fmt.Sprintf("【生产环境】自动升级配置表_%s_%06d.xlsx", date, nano) key := configFileKeyPrefix + filePath if err := jobs.CreateAutoUpdateConfigFile(filePath, "fwh-toolbox-auto-update", key); err != nil { return err } // 创建文件后,刷新缓存 refreshAutoUpdateConfigFileInfo() return nil } func DownloadAutoUpdateConfigFile(fileName string, writer *bufio.Writer) error { obj, err := minio.GetObjectFromBucket("fwh-toolbox-auto-update", configFileKeyPrefix+fileName) if err != nil { return err } defer obj.Close() // Fiber 负责读取 pr 并发送给客户端(消费者) _, err = io.Copy(writer, obj) // 等 pipe EOF 自动结束 if err != nil { return err } return nil }