Sfoglia il codice sorgente

fixed 批量删除对比文献

zero 1 anno fa
parent
commit
8c600634ed

+ 1 - 1
src/main/java/cn/cslg/pas/controller/CompareLiteratureController.java

@@ -84,7 +84,7 @@ public class CompareLiteratureController {
     }
 
 
-    @Operation(summary = "批量添加或编辑")
+    @Operation(summary = "批量删除对比文献")
     @PostMapping("/deleteCompareLiterature")
     public Response deleteCompareLiterature(@RequestBody List<Integer> ids) throws Exception {
         try {

+ 18 - 1
src/main/java/cn/cslg/pas/service/business/CompareLiteratureService.java

@@ -34,7 +34,11 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.CollectionUtils;
 
+import java.io.IOException;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Date;
@@ -70,6 +74,8 @@ public class CompareLiteratureService extends ServiceImpl<CompareLiteratureMappe
     private EsService esService;
     @Autowired
     private MessageService messageService;
+    @Autowired
+    private CompareLiteratureMapper compareLiteratureMapper;
 
     //添加专利对比文献
     public Integer addPatentCompareLiterature(Patent patent, Integer projectId, String createId) {
@@ -520,10 +526,21 @@ public class CompareLiteratureService extends ServiceImpl<CompareLiteratureMappe
         return null;
     }
 
-    public Boolean deleteCompareLiterature(List<Integer> ids) {
+    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
+    public Boolean deleteCompareLiterature(List<Integer> ids) throws IOException {
         if (ids == null || ids.size() <= 0) {
             throw new XiaoShiException("请选择对比文献");
         }
+        List<CompareLiterature> literatures = compareLiteratureMapper.selectBatchIds(ids);
+        if (!CollectionUtils.isEmpty(literatures)) {
+            List<CompareLiterature> literatureList = literatures.stream().filter(i -> i.getType().equals(0)).collect(Collectors.toList());
+            for (CompareLiterature literature : literatureList) {
+                Integer number = esService.getPatent(literature.getLiteratureNo(), literature.getProjectId());
+                if (number < 1) {
+                    throw new XiaoShiException("删除失败");
+                }
+            }
+        }
         return this.removeBatchByIds(ids);
 
     }

+ 32 - 0
src/main/java/cn/cslg/pas/service/business/es/EsService.java

@@ -1338,6 +1338,38 @@ public class EsService {
         return list;
     }
 
+    public Integer getPatent(String patentNo, Integer projectId) throws IOException {
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+        Query query = QueryBuilders.term(i -> i.field("project_id").value(projectId));
+        Query q = QueryBuilders.term(i -> i.field("patent_no.keyword").value(patentNo));
+        Query query1 = QueryBuilders.hasParent(i -> i.parentType("patent").query(q));
+        Query bool = QueryBuilders.bool(i -> i.must(query, query1));
+        builder.query(bool);
+        //解除最大条数限制
+        builder.trackTotalHits(i -> i.enabled(true));
+        SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
+        List<Hit<Patent>> hits = response.hits().hits();
+        List<String> list = new ArrayList<>();
+        for (Hit<Patent> hit : hits) {
+            String id = hit.id();
+            list.add(id);
+        }
+        return this.deleteByIds(list);
+    }
+
+    public Integer deleteByIds(List<String> ids) {
+        Query query = QueryBuilders.ids(n -> n.values(ids));
+        DeleteByQueryRequest request = DeleteByQueryRequest.of(i -> i.index("patent").query(query));
+        try {
+            client.deleteByQuery(request);
+            return 1;
+        } catch (IOException e) {
+            throw new XiaoShiException("删除失败");
+        }
+    }
+
 }