PatentInstructionService.java 11 KB

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