<%_ if (position === 'import') { _%>
import cn.xuyanwu.spring.file.storage.FileStorageService;
import cn.xuyanwu.spring.file.storage.platform.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.util.concurrent.CopyOnWriteArrayList;
import <%= packageName %>.SpringBootUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import java.util.Map;
<%_ } _%>
<%_ if (position === 'classExtend') { _%>
import cn.xuyanwu.spring.file.storage.FileStorageService;
<%_ } _%>
<%_ if (position === 'extendsImport') { _%>
import cn.xuyanwu.spring.file.storage.FileStorageService;
<%_ } _%>
<%_ if (position === 'classImplements') { _%>
<%_ } _%>
<%_ if (position === 'methodInterface') { _%>
<%_ } _%>
<%_ if (position === 'afterUpdate') { _%>
        this.initPlatforms();
<%_ } _%>
<%_ if (position === 'afterDelete') { _%>
        this.initPlatforms();
<%_ } _%>
<%_ if (position === 'method') { _%>
    public void initPlatforms() {
        <%_ if (ormTool === 'mybatis') { _%>
        LambdaQueryWrapper<OssConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OssConfig::getEnabled, true);
        List<OssConfig> ossConfigs = ossConfigRepository.selectList(queryWrapper);
        <%_ } else { _%>
        List<OssConfig> ossConfigs = ossConfigRepository
            .findAll(Example.of(new OssConfig().enabled(true), ExampleMatcher.matching().withIgnoreNullValues()));
        <%_ } _%>
        //获得存储平台 List
        CopyOnWriteArrayList<FileStorage> storageList = fileStorageService.getFileStorageList();
        for (FileStorage storage : storageList) {
            storage.close();
        }
        storageList.clear();
        if (CollectionUtils.isEmpty(ossConfigs)) {
            LocalPlusFileStorage storage = new LocalPlusFileStorage();
            String storagePath = "data/";
            String domain = "http://127.0.0.1:<%= serverPort %>";
            String basePath = "/upload/";
            storagePath = SpringBootUtil.getApplicationPathEndWithSeparator() + storagePath;
            storage.setStoragePath(storagePath);
            storage.setBasePath(basePath);
            storage.setDomain(domain);
            storage.setPlatform("local");
            storageList.add(storage);
        }
        for (OssConfig ossConfig : ossConfigs) {
            switch (ossConfig.getProvider()) {
                case LOCAL -> {
                    LocalPlusFileStorage storage = new LocalPlusFileStorage();
                    ObjectMapper objectMapper = new ObjectMapper();
                    // 将json字符串转为对象
                    String json = ossConfig.getConfigData();
                    try {
                        Map map = objectMapper.readValue(json, Map.class);
                        String storagePath = (String) map.get("storagePath");
                        if (StringUtils.isBlank(storagePath)) {
                            storagePath = "data/";
                        }
                        String domain = (String) map.get("domain");
                        if (StringUtils.isBlank(domain)) {
                            domain = "";
                        }
                        String basePath = (String) map.get("basePath");
                        if (StringUtils.isBlank(basePath)) {
                            basePath = "/upload/";
                        }
                        if (!storagePath.startsWith("/")) {
                            storagePath = SpringBootUtil.getApplicationPathEndWithSeparator() + storagePath;
                        }
                        storage.setStoragePath(storagePath);
                        storage.setBasePath(basePath);
                        storage.setDomain(domain);
                        storage.setPlatform(ossConfig.getPlatform());
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                    storageList.add(storage);
                }
                case ALI -> {
                    AliyunOssFileStorage aliyunOssFileStorage = new AliyunOssFileStorage();
                    storageList.add(aliyunOssFileStorage);
                }
                case MINIO -> {
                    MinIOFileStorage minIOFileStorage = new MinIOFileStorage();
                    storageList.add(minIOFileStorage);
                }
                case QINIU -> {
                    QiniuKodoFileStorage qiniuKodoFileStorage = new QiniuKodoFileStorage();
                    storageList.add(qiniuKodoFileStorage);
                }
                case TENCENT -> {
                    TencentCosFileStorage tencentCosFileStorage = new TencentCosFileStorage();
                    storageList.add(tencentCosFileStorage);
                }
                default -> LOG.warn("未知的存储平台:{}", ossConfig.getProvider());
            }
        }
    }
<%_ } _%>
