package com.fdkankan.fusion.task; import cn.hutool.core.io.FileUtil; import cn.hutool.log.Log; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; 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.response.DownloadProcessVo; import com.fdkankan.fusion.service.*; import com.fdkankan.redis.util.RedisUtil; 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.HashMap; import java.util.List; import java.util.Set; 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(); delRedisKey(); } 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){ log.info("checkDefaultImag",e); } } @Autowired ICommonUploadService commonUploadService; @Autowired IModelService modelService; @Autowired ICaseFilesService caseFilesService; @Autowired IFusionNumService fusionNumService; @Autowired IDictFileService dictFileService; public void delMediaLibrary() { try { List commonUploadList = commonUploadService.list(); if(commonUploadList.isEmpty()){ return; } List ids = commonUploadList.stream().map(CommonUpload::getId).collect(Collectors.toList()); HashMap> map = dictFileService.getByUploadIds(ids); List delUploadList = new ArrayList<>(); for (CommonUpload commonUpload : commonUploadList) { List dictFiles = map.get(commonUpload.getId()); if(dictFiles != null && !dictFiles.isEmpty()){ continue; } if(StringUtils.isNotBlank(commonUpload.getFileUrl())){ LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); JSONArray jsonArray = new JSONArray(); jsonArray.add(commonUpload.getFileUrl()); wrapper.like(Model::getModelGlbUrl,jsonArray.toJSONString()); List list = modelService.list(wrapper); List modelIds = list.stream().map(Model::getModelId).collect(Collectors.toList()); List fusionNums = fusionNumService.getByModelId(modelIds); LambdaQueryWrapper wrapper2 = new LambdaQueryWrapper<>(); wrapper2.like(CaseFiles::getFilesUrl,commonUpload.getFileUrl()); Long count2 = caseFilesService.count(wrapper2); if(fusionNums.size() + count2 <=0){ delUploadList.add(commonUpload); } }else { delUploadList.add(commonUpload); } } if(delUploadList.isEmpty()){ return; } for (CommonUpload commonUpload : delUploadList) { FileUtil.del(commonUpload.getUnzipPath()); log.info("删除文件资源:{},{}",commonUpload.getFileUrl(),commonUpload.getUnzipPath()); } commonUploadService.removeByIds(delUploadList.stream().map(CommonUpload::getId).collect(Collectors.toList())); }catch (Exception e){ log.info("删除文件失败:{}",e); } } private void writerStateFile() { String path = CacheUtil.installPath + "bin" + File.separator + "resources" + File.separator + "static" + File.separator +".fusionstate"; log.info("写入status文件:{}",path); FileUtil.writeString("1",new File(path), StandardCharsets.UTF_8); } @Autowired RedisUtil redisUtil; @Autowired ICaseService caseService; private void delRedisKey() { try { String downProcessKey = "fusion:down:offline:process:caseId:*"; Set keys = redisUtil.keys(downProcessKey); for (String key : keys) { log.info("删除案件下载未完成进度:{}",key); String s = redisUtil.get(key); DownloadProcessVo downloadProcessVo = JSONObject.parseObject(s, DownloadProcessVo.class); if(downloadProcessVo != null && downloadProcessVo.getCaseId() != null && downloadProcessVo.getPercent() != 100 ){ caseService.updateOfflineStatus(downloadProcessVo.getCaseId(),0,null); } redisUtil.del(key); } }catch (Exception e){ log.info("删除失败:{}",e); } } }