PatentInstructionService.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package cn.cslg.pas.service;
  2. import cn.cslg.pas.common.utils.*;
  3. import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
  4. import cn.cslg.pas.domain.Patent;
  5. import cn.cslg.pas.domain.PatentImage;
  6. import cn.cslg.pas.domain.PatentInstruction;
  7. import cn.cslg.pas.domain.ProjectFile;
  8. import cn.cslg.pas.mapper.PatentInstructionMapper;
  9. import cn.cslg.pas.common.core.base.Constants;
  10. import cn.cslg.pas.common.core.base.EStatus;
  11. import cn.cslg.pas.common.model.dto.UploadFileDTO;
  12. import cn.cslg.pas.common.model.vo.PatentInstructionVO;
  13. import cn.dev33.satoken.stp.StpUtil;
  14. import cn.hutool.core.io.FileUtil;
  15. import cn.hutool.core.util.IdUtil;
  16. import cn.hutool.core.util.ZipUtil;
  17. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  18. import com.baomidou.mybatisplus.core.metadata.IPage;
  19. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  20. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  21. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  22. import lombok.RequiredArgsConstructor;
  23. import org.springframework.context.annotation.Lazy;
  24. import org.springframework.scheduling.annotation.Async;
  25. import org.springframework.stereotype.Service;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import org.springframework.transaction.interceptor.TransactionAspectSupport;
  28. import org.springframework.web.multipart.MultipartFile;
  29. import java.io.File;
  30. import java.io.FileOutputStream;
  31. import java.util.*;
  32. import java.util.stream.Collectors;
  33. /**
  34. * <p>
  35. * 专利说明书 服务类
  36. * </p>
  37. *
  38. * @author 王岩
  39. * @since 2022-03-02
  40. */
  41. @Service
  42. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  43. public class PatentInstructionService extends ServiceImpl<PatentInstructionMapper, PatentInstruction> {
  44. private final FileUtils fileUtils;
  45. private final PatentService patentService;
  46. private final ProjectFileService projectFileService;
  47. private final LoginUtils loginUtils;
  48. public IPage<PatentInstruction> getPageList(PatentInstructionVO params) {
  49. LambdaQueryWrapper<PatentInstruction> queryWrapper = new LambdaQueryWrapper<>();
  50. if (StringUtils.isNotEmpty(params.getPatentNo())) {
  51. queryWrapper.like(PatentInstruction::getPatentNo, params.getPatentNo());
  52. }
  53. if (params.getType() != null) {
  54. queryWrapper.eq(PatentInstruction::getType, params.getType());
  55. }
  56. if (params.getPatentId() != null) {
  57. queryWrapper.eq(PatentInstruction::getPatentId, params.getPatentId());
  58. }
  59. IPage<PatentInstruction> pageList = this.page(new Page<>(params.getCurrent(), params.getSize()), queryWrapper);
  60. return pageList;
  61. }
  62. public List<PatentInstruction> getPatentInstructionByPatentNo(String patentNo) {
  63. LambdaQueryWrapper<PatentInstruction> queryWrapper = new LambdaQueryWrapper<>();
  64. queryWrapper.eq(PatentInstruction::getPatentNo, patentNo);
  65. return this.list(queryWrapper);
  66. }
  67. public List<PatentInstruction> getPatentInstructionByPatentNo(List<String> patentNo) {
  68. if (patentNo == null || patentNo.size() == 0) {
  69. return new ArrayList<>();
  70. }
  71. LambdaQueryWrapper<PatentInstruction> queryWrapper = new LambdaQueryWrapper<>();
  72. queryWrapper.in(PatentInstruction::getPatentNo, patentNo);
  73. return this.list(queryWrapper);
  74. }
  75. public PatentInstruction getPatentInstructionByPatentNoAndType(String patentNo, Integer type) {
  76. LambdaQueryWrapper<PatentInstruction> queryWrapper = new LambdaQueryWrapper<>();
  77. queryWrapper.eq(PatentInstruction::getType, type);
  78. queryWrapper.eq(PatentInstruction::getPatentNo, patentNo);
  79. return this.getOne(queryWrapper);
  80. }
  81. @Async("singleThreadAsyncTaskExecutor")
  82. @Transactional(rollbackFor = Exception.class)
  83. public void batchUpload(String url, Integer type, String remark, Integer userId) {
  84. String tempPath = null;
  85. try {
  86. String tempDirectoryName = IdUtil.simpleUUID();
  87. tempPath = fileUtils.getSystemPath(tempDirectoryName);
  88. File tempDirectory = new File(tempPath);
  89. if (!tempDirectory.exists()) {
  90. tempDirectory.mkdir();
  91. }
  92. ZipUtil.unzip(fileUtils.getSystemPath(url), tempPath);
  93. List<File> fileList = FileUtil.loopFiles(tempPath).stream().filter(item -> FileUtil.getType(item).equals("pdf")).collect(Collectors.toList());
  94. for (int i = 0; i < fileList.size(); i++) {
  95. File file = fileList.get(i);
  96. String fileName = file.getName();
  97. String patentNo = FileUtil.getPrefix(file);
  98. String saveName = IdUtil.simpleUUID() + ".pdf";
  99. String saveUrl = fileUtils.getDirectory(saveName);
  100. String savePath = fileUtils.getSystemPath(saveUrl);
  101. FileUtil.copy(file.getPath(), savePath, true);
  102. PatentInstruction temp = this.getPatentInstructionByPatentNoAndType(patentNo, type);
  103. if (temp == null) {
  104. temp = new PatentInstruction();
  105. } else {
  106. FileUtil.del(fileUtils.getSystemPath(temp.getUrl()));
  107. }
  108. temp.setPatentNo(patentNo);
  109. temp.setFileName(saveName);
  110. temp.setSize(FileUtil.size(file));
  111. temp.setUrl(saveUrl);
  112. temp.setRemark(remark);
  113. temp.setCreateBy(userId);
  114. temp.setType(type);
  115. temp.insertOrUpdate();
  116. Map<String, Object> data = new HashMap<>();
  117. data.put("index", i);
  118. data.put("total", fileList.size());
  119. WebSocketServer.sendInfo(Response.websocket(data, ResponseEnum.BATCH_UPLOAD_INSTRUCTION_TASK_SUCCESS), String.valueOf(userId));
  120. }
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. WebSocketServer.sendInfo(Response.error(ResponseEnum.BATCH_UPLOAD_INSTRUCTION_TASK_ERROR), String.valueOf(userId));
  124. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  125. } finally {
  126. FileUtil.del(tempPath);
  127. }
  128. }
  129. public String add(MultipartFile file, PatentInstruction patentInstruction) {
  130. UploadFileDTO fileDTO = fileUtils.uploadFile(file);
  131. patentInstruction.setCreateBy(loginUtils.getId());
  132. patentInstruction.setUrl(fileDTO.getPath());
  133. patentInstruction.setFileName(fileDTO.getFileName());
  134. patentInstruction.insert();
  135. return Response.success(patentInstruction.getId());
  136. }
  137. @Transactional
  138. public String edit(MultipartFile file, PatentInstruction patentInstruction) {
  139. this.deleteByPatentNoAndType(patentInstruction.getPatentNo(), patentInstruction.getType());
  140. if (StringUtils.isNotEmpty(patentInstruction.getUrl())) {
  141. FileUtil.del(fileUtils.getSystemPath(patentInstruction.getUrl()));
  142. }
  143. UploadFileDTO fileDTO = fileUtils.uploadFile(file);
  144. patentInstruction.setCreateBy(loginUtils.getId());
  145. patentInstruction.setUrl(fileDTO.getPath());
  146. patentInstruction.setFileName(fileDTO.getFileName());
  147. patentInstruction.setSize(fileDTO.getFileSize());
  148. patentInstruction.insert();
  149. return Response.success(true);
  150. }
  151. /**
  152. * 说明书pdf表数据入库
  153. *
  154. * @param patentNo
  155. * @param pdf
  156. * @return
  157. */
  158. @Transactional
  159. public String edit(String patentNo, UploadFileDTO pdf) {
  160. PatentInstruction patentInstruction = new PatentInstruction();
  161. patentInstruction.setUrl(pdf.getPath());
  162. patentInstruction.setFileName(pdf.getFileName());
  163. patentInstruction.setSize(pdf.getFileSize());
  164. patentInstruction.setPatentNo(patentNo);
  165. patentInstruction.setType(1);
  166. patentInstruction.insert();
  167. return Response.success(true);
  168. }
  169. public String delete(Integer id) {
  170. PatentInstruction temp = this.getById(id);
  171. this.removeById(id);
  172. if (StringUtils.isNotEmpty(temp.getUrl())) {
  173. FileUtil.del(fileUtils.getSystemPath(temp.getUrl()));
  174. }
  175. return Response.success(true);
  176. }
  177. public void deleteByPatentNoAndType(String patentNo, Integer type) {
  178. this.remove(Wrappers.<PatentInstruction>lambdaQuery().eq(PatentInstruction::getPatentNo, patentNo).eq(PatentInstruction::getType, type));
  179. }
  180. public void importPatentInstruction(Integer userId, String tempPath, String patentIdPatentNoJson, String patentInstructionJson) {
  181. List<Patent> importPatentList = JsonUtils.jsonToList(patentIdPatentNoJson, Patent.class);
  182. List<PatentInstruction> importPatentInstructionList = JsonUtils.jsonToList(patentInstructionJson, PatentInstruction.class);
  183. List<PatentInstruction> localPatentInstructionList = this.getPatentInstructionByPatentNo(importPatentList.stream().map(Patent::getPatentNo).collect(Collectors.toList()));
  184. for (PatentInstruction patentInstruction : importPatentInstructionList) {
  185. String fileName = IdUtil.simpleUUID() + "." + FileUtil.extName(patentInstruction.getFileName());
  186. String saveUrl = fileUtils.getDirectory(fileName);
  187. String savePath = fileUtils.getSystemPath(saveUrl);
  188. String tempInstruction = tempPath + FileUtils.FILE_SEPARATOR + Constants.PATENT_INSTRUCTION_DIRECTORY_NAME + FileUtils.FILE_SEPARATOR + patentInstruction.getFileName();
  189. patentInstruction.setId(null);
  190. patentInstruction.setFileName(fileName);
  191. patentInstruction.setUrl(saveUrl);
  192. patentInstruction.setCreateBy(userId);
  193. patentInstruction.setCreateTime(new Date());
  194. if (FileUtil.exist(tempInstruction)) {
  195. patentInstruction.insert();
  196. FileUtil.copy(tempInstruction, savePath, true);
  197. }
  198. }
  199. localPatentInstructionList.forEach(item -> {
  200. item.deleteById();
  201. FileUtil.del(fileUtils.getSystemPath(item.getUrl()));
  202. });
  203. }
  204. }