InitService.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.CaseFiles;
  9. import com.fdkankan.fusion.entity.CommonUpload;
  10. import com.fdkankan.fusion.entity.HotIcon;
  11. import com.fdkankan.fusion.entity.Model;
  12. import com.fdkankan.fusion.service.*;
  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.List;
  27. import java.util.stream.Collectors;
  28. @Service
  29. @Slf4j
  30. public class InitService {
  31. @Autowired
  32. IHotIconService hotIconService;
  33. @Value("${upload.type}")
  34. private String uploadType;
  35. @Value("${fdkk.installPath}")
  36. private String installPath;
  37. @Autowired
  38. IPathService pathService;
  39. @PostConstruct
  40. public void run() {
  41. initConfig();
  42. checkDefaultImag();
  43. delMediaLibrary();
  44. writerStateFile();
  45. }
  46. public void initConfig(){
  47. CacheUtil.uploadType = uploadType;
  48. CacheUtil.installPath = installPath;
  49. String path = pathService.getBasePath();
  50. if(path != null){
  51. CacheUtil.basePath = path ;
  52. }else {
  53. CacheUtil.basePath = installPath +"/4DKK_PROGRAM_STATIC/";
  54. }
  55. }
  56. public void checkDefaultImag() {
  57. try {
  58. List<HotIcon> defaultIcon = hotIconService.getDefaultIcon();
  59. HotIcon hotIcon = null;
  60. if(defaultIcon.isEmpty()){
  61. hotIcon = new HotIcon();
  62. hotIcon.setIconTitle("系统默认");
  63. hotIcon.setIconUrl(CacheUtil.basePath+ File.separator+"fusion" + File.separator +"default"+File.separator+"tag_icon_default.svg" );
  64. hotIcon.setIsSystem(1);
  65. hotIconService.save(hotIcon);
  66. log.info("默认热点数据不存在新建,{}",hotIcon.getIconUrl());
  67. }else {
  68. hotIcon = defaultIcon.get(0);
  69. }
  70. if(hotIcon != null){
  71. File file = new File(hotIcon.getIconUrl());
  72. if(!file.exists()){
  73. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/default/tag_icon_default.svg");
  74. FileUtils.copyInputStreamToFile(inputStream,file);
  75. }
  76. }
  77. }catch (Exception e){
  78. }
  79. }
  80. @Autowired
  81. ICommonUploadService commonUploadService;
  82. @Autowired
  83. IModelService modelService;
  84. @Autowired
  85. ICaseFilesService caseFilesService;
  86. public void delMediaLibrary() {
  87. List<CommonUpload> commonUploadList = commonUploadService.getDelData();
  88. if(commonUploadList.isEmpty()){
  89. return;
  90. }
  91. List<CommonUpload> delUploadList = new ArrayList<>();
  92. for (CommonUpload commonUpload : commonUploadList) {
  93. if(StringUtils.isNotBlank(commonUpload.getFileUrl())){
  94. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  95. JSONArray jsonArray = new JSONArray();
  96. jsonArray.add(commonUpload.getFileUrl());
  97. wrapper.like(Model::getModelGlbUrl,jsonArray.toJSONString());
  98. Long count1 = modelService.count(wrapper);
  99. LambdaQueryWrapper<CaseFiles> wrapper2 = new LambdaQueryWrapper<>();
  100. wrapper2.like(CaseFiles::getFilesUrl,commonUpload.getFileUrl());
  101. Long count2 = caseFilesService.count(wrapper2);
  102. if(count1 + count2 <=0){
  103. delUploadList.add(commonUpload);
  104. }
  105. }else {
  106. delUploadList.add(commonUpload);
  107. }
  108. }
  109. if(delUploadList.isEmpty()){
  110. return;
  111. }
  112. for (CommonUpload commonUpload : delUploadList) {
  113. FileUtil.del(commonUpload.getFileUrl());
  114. FileUtil.del(commonUpload.getUnzipPath());
  115. log.info("删除文件资源:{},{}",commonUpload.getFileUrl(),commonUpload.getUnzipPath());
  116. }
  117. commonUploadService.delByIds(delUploadList.stream().map(CommonUpload::getId).collect(Collectors.toList()));
  118. }
  119. private void writerStateFile() {
  120. String path = installPath + File.separator + "bin" + File.separator + "resource" + File.separator + "static" + File.separator +".fusionstate";
  121. FileUtil.writeString("1",new File(path), StandardCharsets.UTF_8);
  122. }
  123. }