InitService.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package com.fdkankan.fusion.task;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.log.Log;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.fdkankan.fusion.common.util.LocalToOssUtil;
  8. import com.fdkankan.fusion.config.CacheUtil;
  9. import com.fdkankan.fusion.entity.*;
  10. import com.fdkankan.fusion.response.DownloadProcessVo;
  11. import com.fdkankan.fusion.service.*;
  12. import com.fdkankan.redis.util.RedisUtil;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.apache.commons.io.FileUtils;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.stereotype.Service;
  19. import javax.annotation.PostConstruct;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.nio.charset.StandardCharsets;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Set;
  29. import java.util.stream.Collectors;
  30. @Service
  31. @Slf4j
  32. public class InitService {
  33. @Autowired
  34. IHotIconService hotIconService;
  35. @Value("${upload.type}")
  36. private String uploadType;
  37. @Value("${fdkk.installPath}")
  38. private String installPath;
  39. @Autowired
  40. IPathService pathService;
  41. @PostConstruct
  42. public void run() {
  43. initConfig();
  44. checkDefaultImag();
  45. delMediaLibrary();
  46. writerStateFile();
  47. delRedisKey();
  48. }
  49. public void initConfig(){
  50. CacheUtil.uploadType = uploadType;
  51. CacheUtil.installPath = new File(installPath).getParentFile().getPath() +File.separator;
  52. Path path = pathService.getBasePath();
  53. if(path != null){
  54. CacheUtil.mapping = path.getMapping() + File.separator;
  55. CacheUtil.basePath = path.getDir() ;
  56. }else {
  57. CacheUtil.mapping = "profile" + File.separator;
  58. CacheUtil.basePath = CacheUtil.installPath +"/4DKK_PROGRAM_STATIC/";
  59. }
  60. }
  61. public void checkDefaultImag() {
  62. try {
  63. List<HotIcon> defaultIcon = hotIconService.getDefaultIcon();
  64. HotIcon hotIcon = null;
  65. if(defaultIcon.isEmpty()){
  66. hotIcon = new HotIcon();
  67. hotIcon.setIconTitle("系统默认");
  68. hotIcon.setIconUrl("profile"+ File.separator+"fusion" + File.separator +"default"+File.separator+"tag_icon_default.svg" );
  69. hotIcon.setIsSystem(1);
  70. hotIconService.save(hotIcon);
  71. log.info("默认热点数据不存在新建,{}",hotIcon.getIconUrl());
  72. }else {
  73. hotIcon = defaultIcon.get(0);
  74. }
  75. if(hotIcon != null){
  76. File file = new File(hotIcon.getIconUrl().replace("profile",CacheUtil.installPath +"/4DKK_PROGRAM_STATIC"));
  77. if(!file.exists()){
  78. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/default/tag_icon_default.svg");
  79. FileUtils.copyInputStreamToFile(inputStream,file);
  80. }
  81. }
  82. }catch (Exception e){
  83. log.info("checkDefaultImag",e);
  84. }
  85. }
  86. @Autowired
  87. ICommonUploadService commonUploadService;
  88. @Autowired
  89. IModelService modelService;
  90. @Autowired
  91. ICaseFilesService caseFilesService;
  92. @Autowired
  93. IFusionNumService fusionNumService;
  94. @Autowired
  95. IDictFileService dictFileService;
  96. public void delMediaLibrary() {
  97. try {
  98. List<CommonUpload> commonUploadList = commonUploadService.list();
  99. if(commonUploadList.isEmpty()){
  100. return;
  101. }
  102. List<Integer> ids = commonUploadList.stream().map(CommonUpload::getId).collect(Collectors.toList());
  103. HashMap<Integer,List<DictFile>> map = dictFileService.getByUploadIds(ids);
  104. List<CommonUpload> delUploadList = new ArrayList<>();
  105. for (CommonUpload commonUpload : commonUploadList) {
  106. List<DictFile> dictFiles = map.get(commonUpload.getId());
  107. if(dictFiles != null && !dictFiles.isEmpty()){
  108. continue;
  109. }
  110. if(StringUtils.isNotBlank(commonUpload.getFileUrl())){
  111. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  112. JSONArray jsonArray = new JSONArray();
  113. jsonArray.add(commonUpload.getFileUrl());
  114. wrapper.like(Model::getModelGlbUrl,jsonArray.toJSONString());
  115. List<Model> list = modelService.list(wrapper);
  116. List<Integer> modelIds = list.stream().map(Model::getModelId).collect(Collectors.toList());
  117. List<FusionNum> fusionNums = fusionNumService.getByModelId(modelIds);
  118. LambdaQueryWrapper<CaseFiles> wrapper2 = new LambdaQueryWrapper<>();
  119. wrapper2.like(CaseFiles::getFilesUrl,commonUpload.getFileUrl());
  120. Long count2 = caseFilesService.count(wrapper2);
  121. if(fusionNums.size() + count2 <=0){
  122. delUploadList.add(commonUpload);
  123. }
  124. }else {
  125. delUploadList.add(commonUpload);
  126. }
  127. }
  128. if(delUploadList.isEmpty()){
  129. return;
  130. }
  131. for (CommonUpload commonUpload : delUploadList) {
  132. FileUtil.del(commonUpload.getUnzipPath());
  133. log.info("删除文件资源:{},{}",commonUpload.getFileUrl(),commonUpload.getUnzipPath());
  134. }
  135. commonUploadService.removeByIds(delUploadList.stream().map(CommonUpload::getId).collect(Collectors.toList()));
  136. }catch (Exception e){
  137. log.info("删除文件失败:{}",e);
  138. }
  139. }
  140. private void writerStateFile() {
  141. String path = CacheUtil.installPath + "bin" + File.separator + "resources" + File.separator + "static" + File.separator +".fusionstate";
  142. log.info("写入status文件:{}",path);
  143. FileUtil.writeString("1",new File(path), StandardCharsets.UTF_8);
  144. }
  145. @Autowired
  146. RedisUtil redisUtil;
  147. @Autowired
  148. ICaseService caseService;
  149. private void delRedisKey() {
  150. try {
  151. String downProcessKey = "fusion:down:offline:process:caseId:*";
  152. Set<String> keys = redisUtil.keys(downProcessKey);
  153. for (String key : keys) {
  154. log.info("删除案件下载未完成进度:{}",key);
  155. String s = redisUtil.get(key);
  156. DownloadProcessVo downloadProcessVo = JSONObject.parseObject(s, DownloadProcessVo.class);
  157. if(downloadProcessVo != null && downloadProcessVo.getCaseId() != null && downloadProcessVo.getPercent() != 100 ){
  158. caseService.updateOfflineStatus(downloadProcessVo.getCaseId(),0,null);
  159. }
  160. redisUtil.del(key);
  161. }
  162. }catch (Exception e){
  163. log.info("删除失败:{}",e);
  164. }
  165. }
  166. }