InitService.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.fdkankan.fusion.common.util.LocalToOssUtil;
  7. import com.fdkankan.fusion.config.CacheUtil;
  8. import com.fdkankan.fusion.entity.*;
  9. import com.fdkankan.fusion.service.*;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.apache.commons.io.FileUtils;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.stereotype.Service;
  16. import javax.annotation.PostConstruct;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.nio.charset.StandardCharsets;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.stream.Collectors;
  25. @Service
  26. @Slf4j
  27. public class InitService {
  28. @Autowired
  29. IHotIconService hotIconService;
  30. @Value("${upload.type}")
  31. private String uploadType;
  32. @Value("${fdkk.installPath}")
  33. private String installPath;
  34. @Autowired
  35. IPathService pathService;
  36. @PostConstruct
  37. public void run() {
  38. initConfig();
  39. checkDefaultImag();
  40. delMediaLibrary();
  41. writerStateFile();
  42. }
  43. public void initConfig(){
  44. CacheUtil.uploadType = uploadType;
  45. CacheUtil.installPath = new File(installPath).getParentFile().getPath() +File.separator;
  46. Path path = pathService.getBasePath();
  47. if(path != null){
  48. CacheUtil.mapping = path.getMapping() + File.separator;
  49. CacheUtil.basePath = path.getDir() ;
  50. }else {
  51. CacheUtil.mapping = "profile" + File.separator;
  52. CacheUtil.basePath = CacheUtil.installPath +"/4DKK_PROGRAM_STATIC/";
  53. }
  54. }
  55. public void checkDefaultImag() {
  56. try {
  57. List<HotIcon> defaultIcon = hotIconService.getDefaultIcon();
  58. HotIcon hotIcon = null;
  59. if(defaultIcon.isEmpty()){
  60. hotIcon = new HotIcon();
  61. hotIcon.setIconTitle("系统默认");
  62. hotIcon.setIconUrl("profile"+ File.separator+"fusion" + File.separator +"default"+File.separator+"tag_icon_default.svg" );
  63. hotIcon.setIsSystem(1);
  64. hotIconService.save(hotIcon);
  65. log.info("默认热点数据不存在新建,{}",hotIcon.getIconUrl());
  66. }else {
  67. hotIcon = defaultIcon.get(0);
  68. }
  69. if(hotIcon != null){
  70. File file = new File(hotIcon.getIconUrl().replace("profile",CacheUtil.installPath +"/4DKK_PROGRAM_STATIC"));
  71. if(!file.exists()){
  72. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/default/tag_icon_default.svg");
  73. FileUtils.copyInputStreamToFile(inputStream,file);
  74. }
  75. }
  76. }catch (Exception e){
  77. }
  78. }
  79. @Autowired
  80. ICommonUploadService commonUploadService;
  81. @Autowired
  82. IModelService modelService;
  83. @Autowired
  84. ICaseFilesService caseFilesService;
  85. @Autowired
  86. IFusionNumService fusionNumService;
  87. public void delMediaLibrary() {
  88. List<CommonUpload> commonUploadList = commonUploadService.getDelData();
  89. if(commonUploadList.isEmpty()){
  90. return;
  91. }
  92. List<CommonUpload> delUploadList = new ArrayList<>();
  93. for (CommonUpload commonUpload : commonUploadList) {
  94. if(StringUtils.isNotBlank(commonUpload.getFileUrl())){
  95. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  96. JSONArray jsonArray = new JSONArray();
  97. jsonArray.add(commonUpload.getFileUrl());
  98. wrapper.like(Model::getModelGlbUrl,jsonArray.toJSONString());
  99. List<Model> list = modelService.list(wrapper);
  100. List<Integer> modelIds = list.stream().map(Model::getModelId).collect(Collectors.toList());
  101. List<FusionNum> fusionNums = fusionNumService.getByModelId(modelIds);
  102. LambdaQueryWrapper<CaseFiles> wrapper2 = new LambdaQueryWrapper<>();
  103. wrapper2.like(CaseFiles::getFilesUrl,commonUpload.getFileUrl());
  104. Long count2 = caseFilesService.count(wrapper2);
  105. if(fusionNums.size() + count2 <=0){
  106. delUploadList.add(commonUpload);
  107. }
  108. }else {
  109. delUploadList.add(commonUpload);
  110. }
  111. }
  112. if(delUploadList.isEmpty()){
  113. return;
  114. }
  115. for (CommonUpload commonUpload : delUploadList) {
  116. FileUtil.del(commonUpload.getFileUrl());
  117. FileUtil.del(commonUpload.getUnzipPath());
  118. log.info("删除文件资源:{},{}",commonUpload.getFileUrl(),commonUpload.getUnzipPath());
  119. }
  120. commonUploadService.delByIds(delUploadList.stream().map(CommonUpload::getId).collect(Collectors.toList()));
  121. }
  122. private void writerStateFile() {
  123. String path = CacheUtil.installPath + "bin" + File.separator + "resources" + File.separator + "static" + File.separator +".fusionstate";
  124. log.info("写入status文件:{}",path);
  125. FileUtil.writeString("1",new File(path), StandardCharsets.UTF_8);
  126. }
  127. }