InitService.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. public void delMediaLibrary() {
  86. List<CommonUpload> commonUploadList = commonUploadService.getDelData();
  87. if(commonUploadList.isEmpty()){
  88. return;
  89. }
  90. List<CommonUpload> delUploadList = new ArrayList<>();
  91. for (CommonUpload commonUpload : commonUploadList) {
  92. if(StringUtils.isNotBlank(commonUpload.getFileUrl())){
  93. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  94. JSONArray jsonArray = new JSONArray();
  95. jsonArray.add(commonUpload.getFileUrl());
  96. wrapper.like(Model::getModelGlbUrl,jsonArray.toJSONString());
  97. Long count1 = modelService.count(wrapper);
  98. LambdaQueryWrapper<CaseFiles> wrapper2 = new LambdaQueryWrapper<>();
  99. wrapper2.like(CaseFiles::getFilesUrl,commonUpload.getFileUrl());
  100. Long count2 = caseFilesService.count(wrapper2);
  101. if(count1 + count2 <=0){
  102. delUploadList.add(commonUpload);
  103. }
  104. }else {
  105. delUploadList.add(commonUpload);
  106. }
  107. }
  108. if(delUploadList.isEmpty()){
  109. return;
  110. }
  111. for (CommonUpload commonUpload : delUploadList) {
  112. FileUtil.del(commonUpload.getFileUrl());
  113. FileUtil.del(commonUpload.getUnzipPath());
  114. log.info("删除文件资源:{},{}",commonUpload.getFileUrl(),commonUpload.getUnzipPath());
  115. }
  116. commonUploadService.delByIds(delUploadList.stream().map(CommonUpload::getId).collect(Collectors.toList()));
  117. }
  118. private void writerStateFile() {
  119. String path = installPath + File.separator + "bin" + File.separator + "resources" + File.separator + "static" + File.separator +".fusionstate";
  120. FileUtil.writeString("1",new File(path), StandardCharsets.UTF_8);
  121. }
  122. }