PatentInstructionService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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).last("Limit 1");
  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. //先把实体类对象赋好值
  161. PatentInstruction patentInstruction = new PatentInstruction();
  162. patentInstruction.setUrl(pdf.getPath());
  163. patentInstruction.setFileName(pdf.getFileName());
  164. patentInstruction.setSize(pdf.getFileSize());
  165. patentInstruction.setPatentNo(patentNo);
  166. patentInstruction.setType(1);
  167. //根据专利号和pdf类型(1公开 2授权)查询该专利号是否已有公开的pdf
  168. List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().eq(PatentInstruction::getPatentNo, patentNo).eq(PatentInstruction::getType, 1));
  169. if (patentInstructions != null && patentInstructions.size() > 0) { //若有则更新
  170. patentInstruction.setId(patentInstructions.get(0).getId());
  171. this.updateById(patentInstruction);
  172. } else { //若没有则新增
  173. this.save(patentInstruction);
  174. }
  175. return Response.success(true);
  176. }
  177. @Transactional
  178. public String edit2(String patentNo, UploadFileDTO pdf2) {
  179. //先把实体类对象赋好值
  180. PatentInstruction patentInstruction = new PatentInstruction();
  181. patentInstruction.setUrl(pdf2.getPath());
  182. patentInstruction.setFileName(pdf2.getFileName());
  183. patentInstruction.setSize(pdf2.getFileSize());
  184. patentInstruction.setPatentNo(patentNo);
  185. patentInstruction.setType(2);
  186. //根据专利号和pdf类型(1公开 2授权)查询该专利号是否已有公开的pdf
  187. List<PatentInstruction> patentInstructions = this.list(new LambdaQueryWrapper<PatentInstruction>().eq(PatentInstruction::getPatentNo, patentNo).eq(PatentInstruction::getType, 2));
  188. //若有则更新
  189. if (patentInstructions != null && patentInstructions.size() > 0) {
  190. patentInstruction.setId(patentInstructions.get(0).getId());
  191. this.updateById(patentInstruction);
  192. } else { //若没有则新增
  193. this.save(patentInstruction);
  194. }
  195. return Response.success(true);
  196. }
  197. @Transactional
  198. public String edit12(String patentNo, UploadFileDTO pdf1, UploadFileDTO pdf2) {
  199. //处理公开pdf文档(更新或新增)
  200. PatentInstruction patentInstruction1 = new PatentInstruction();
  201. patentInstruction1.setUrl(pdf1.getPath());
  202. patentInstruction1.setFileName(pdf1.getFileName());
  203. patentInstruction1.setSize(pdf1.getFileSize());
  204. patentInstruction1.setPatentNo(patentNo);
  205. patentInstruction1.setType(1);
  206. //先查询库中是否已有该专利号的公开pdf文档
  207. List<PatentInstruction> patentInstructions1 = this.list(new LambdaQueryWrapper<PatentInstruction>().eq(PatentInstruction::getPatentNo, patentNo).eq(PatentInstruction::getType, 1));
  208. //若有则更新
  209. if (patentInstructions1 != null && patentInstructions1.size() > 0) {
  210. patentInstruction1.setId(patentInstructions1.get(0).getId());
  211. this.updateById(patentInstruction1);
  212. } else { //若没有则新增
  213. this.save(patentInstruction1);
  214. }
  215. //处理授权pdf文档(更新或新增)
  216. PatentInstruction patentInstruction2 = new PatentInstruction();
  217. patentInstruction2.setUrl(pdf2.getPath());
  218. patentInstruction2.setFileName(pdf2.getFileName());
  219. patentInstruction2.setSize(pdf2.getFileSize());
  220. patentInstruction2.setPatentNo(patentNo);
  221. patentInstruction2.setType(2);
  222. //再查询库中是否已有该专利号的授权pdf文档
  223. List<PatentInstruction> patentInstructions2 = this.list(new LambdaQueryWrapper<PatentInstruction>().eq(PatentInstruction::getPatentNo, patentNo).eq(PatentInstruction::getType, 2));
  224. //若有则更新
  225. if (patentInstructions2 != null && patentInstructions2.size() > 0) {
  226. patentInstruction2.setId(patentInstructions2.get(0).getId());
  227. this.updateById(patentInstruction2);
  228. } else { //若没有则新增
  229. this.save(patentInstruction2);
  230. }
  231. return Response.success(true);
  232. }
  233. public String delete(Integer id) {
  234. PatentInstruction temp = this.getById(id);
  235. this.removeById(id);
  236. if (StringUtils.isNotEmpty(temp.getUrl())) {
  237. FileUtil.del(fileUtils.getSystemPath(temp.getUrl()));
  238. }
  239. return Response.success(true);
  240. }
  241. public void deleteByPatentNoAndType(String patentNo, Integer type) {
  242. this.remove(Wrappers.<PatentInstruction>lambdaQuery().eq(PatentInstruction::getPatentNo, patentNo).eq(PatentInstruction::getType, type));
  243. }
  244. public void importPatentInstruction(Integer userId, String tempPath, String patentIdPatentNoJson, String patentInstructionJson) {
  245. List<Patent> importPatentList = JsonUtils.jsonToList(patentIdPatentNoJson, Patent.class);
  246. List<PatentInstruction> importPatentInstructionList = JsonUtils.jsonToList(patentInstructionJson, PatentInstruction.class);
  247. List<PatentInstruction> localPatentInstructionList = this.getPatentInstructionByPatentNo(importPatentList.stream().map(Patent::getPatentNo).collect(Collectors.toList()));
  248. for (PatentInstruction patentInstruction : importPatentInstructionList) {
  249. String fileName = IdUtil.simpleUUID() + "." + FileUtil.extName(patentInstruction.getFileName());
  250. String saveUrl = fileUtils.getDirectory(fileName);
  251. String savePath = fileUtils.getSystemPath(saveUrl);
  252. String tempInstruction = tempPath + FileUtils.FILE_SEPARATOR + Constants.PATENT_INSTRUCTION_DIRECTORY_NAME + FileUtils.FILE_SEPARATOR + patentInstruction.getFileName();
  253. patentInstruction.setId(null);
  254. patentInstruction.setFileName(fileName);
  255. patentInstruction.setUrl(saveUrl);
  256. patentInstruction.setCreateBy(userId);
  257. patentInstruction.setCreateTime(new Date());
  258. if (FileUtil.exist(tempInstruction)) {
  259. patentInstruction.insert();
  260. FileUtil.copy(tempInstruction, savePath, true);
  261. }
  262. }
  263. localPatentInstructionList.forEach(item -> {
  264. item.deleteById();
  265. FileUtil.del(fileUtils.getSystemPath(item.getUrl()));
  266. });
  267. }
  268. }