package com.fdkankan.fusion.task; import cn.hutool.core.io.FileUtil; import cn.hutool.log.Log; import com.alibaba.fastjson.JSONArray; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.fdkankan.fusion.common.util.LocalToOssUtil; import com.fdkankan.fusion.config.CacheUtil; import com.fdkankan.fusion.entity.*; import com.fdkankan.fusion.service.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @Service @Slf4j public class InitService { @Autowired IHotIconService hotIconService; @Value("${upload.type}") private String uploadType; @Value("${fdkk.installPath}") private String installPath; @Autowired IPathService pathService; @PostConstruct public void run() { initConfig(); checkDefaultImag(); delMediaLibrary(); writerStateFile(); } public void initConfig(){ CacheUtil.uploadType = uploadType; CacheUtil.installPath = new File(installPath).getParentFile().getPath() +File.separator; Path path = pathService.getBasePath(); if(path != null){ CacheUtil.mapping = path.getMapping() + File.separator; CacheUtil.basePath = path.getDir() ; }else { CacheUtil.mapping = "profile" + File.separator; CacheUtil.basePath = CacheUtil.installPath +"/4DKK_PROGRAM_STATIC/"; } } public void checkDefaultImag() { try { List defaultIcon = hotIconService.getDefaultIcon(); HotIcon hotIcon = null; if(defaultIcon.isEmpty()){ hotIcon = new HotIcon(); hotIcon.setIconTitle("系统默认"); hotIcon.setIconUrl("profile"+ File.separator+"fusion" + File.separator +"default"+File.separator+"tag_icon_default.svg" ); hotIcon.setIsSystem(1); hotIconService.save(hotIcon); log.info("默认热点数据不存在新建,{}",hotIcon.getIconUrl()); }else { hotIcon = defaultIcon.get(0); } if(hotIcon != null){ File file = new File(hotIcon.getIconUrl().replace("profile",CacheUtil.installPath +"/4DKK_PROGRAM_STATIC")); if(!file.exists()){ InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/default/tag_icon_default.svg"); FileUtils.copyInputStreamToFile(inputStream,file); } } }catch (Exception e){ } } @Autowired ICommonUploadService commonUploadService; @Autowired IModelService modelService; @Autowired ICaseFilesService caseFilesService; public void delMediaLibrary() { List commonUploadList = commonUploadService.getDelData(); if(commonUploadList.isEmpty()){ return; } List delUploadList = new ArrayList<>(); for (CommonUpload commonUpload : commonUploadList) { if(StringUtils.isNotBlank(commonUpload.getFileUrl())){ LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); JSONArray jsonArray = new JSONArray(); jsonArray.add(commonUpload.getFileUrl()); wrapper.like(Model::getModelGlbUrl,jsonArray.toJSONString()); Long count1 = modelService.count(wrapper); LambdaQueryWrapper wrapper2 = new LambdaQueryWrapper<>(); wrapper2.like(CaseFiles::getFilesUrl,commonUpload.getFileUrl()); Long count2 = caseFilesService.count(wrapper2); if(count1 + count2 <=0){ delUploadList.add(commonUpload); } }else { delUploadList.add(commonUpload); } } if(delUploadList.isEmpty()){ return; } for (CommonUpload commonUpload : delUploadList) { FileUtil.del(commonUpload.getFileUrl()); FileUtil.del(commonUpload.getUnzipPath()); log.info("删除文件资源:{},{}",commonUpload.getFileUrl(),commonUpload.getUnzipPath()); } commonUploadService.delByIds(delUploadList.stream().map(CommonUpload::getId).collect(Collectors.toList())); } private void writerStateFile() { String path = installPath + File.separator + "bin" + File.separator + "resources" + File.separator + "static" + File.separator +".fusionstate"; FileUtil.writeString("1",new File(path), StandardCharsets.UTF_8); } }