Переглянути джерело

Merge remote-tracking branch 'origin/master'

lrj 9 місяців тому
батько
коміт
0dbe48a671

+ 5 - 3
src/main/java/cn/cslg/pas/factorys/EsBuilderFactory/WildcardNosQueryBuilder.java

@@ -29,9 +29,11 @@ public class WildcardNosQueryBuilder implements IQueryBuilder {
 
         for (int i = 0; i < valueStrs.length; i++) {
             String temValue = valueStrs[i];
-            String sub = temValue.substring(0, 2);
-            if ((!temValue.contains(".") && !(StringUtils.isPositiveInteger(sub)) && temValue.length() == 15) || (!temValue.contains(".") && StringUtils.isPositiveInteger(sub) && temValue.length() == 13)) {
-                temValue = temValue.substring(0, temValue.length() - 1);
+            if (temValue.length() > 2) {
+                String sub = temValue.substring(0, 2);
+                if ((!temValue.contains(".") && !(StringUtils.isPositiveInteger(sub)) && temValue.length() == 15) || (!temValue.contains(".") && StringUtils.isPositiveInteger(sub) && temValue.length() == 13)) {
+                    temValue = temValue.substring(0, temValue.length() - 1);
+                }
             }
             temValue = "*" + temValue + "*";
             String finalValue = temValue;

+ 683 - 0
src/main/java/cn/cslg/pas/service/business/es/EsExportService.java

@@ -11,11 +11,17 @@ import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
 import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
 import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
 import cn.cslg.pas.common.vo.PatentData;
+import cn.cslg.pas.common.vo.PatentWithIdVO;
+import cn.cslg.pas.common.vo.StarPatentVO;
 import cn.cslg.pas.common.vo.business.EsExportRefreshVO;
+import cn.cslg.pas.domain.es.ESCustomField;
+import cn.cslg.pas.domain.es.EsProjectTask;
 import cn.cslg.pas.domain.es.Patent;
+import cn.cslg.pas.domain.es.PatentJoin;
 import cn.cslg.pas.exception.XiaoShiException;
 import cn.cslg.pas.service.business.ImportTaskService;
 import cn.cslg.pas.service.common.PatentStarApiService;
+import cn.cslg.pas.service.importPatent.ImportSinglePatentService;
 import cn.cslg.pas.service.query.FormatQueryService;
 import co.elastic.clients.elasticsearch.ElasticsearchClient;
 import co.elastic.clients.elasticsearch._types.SortOptions;
@@ -71,6 +77,12 @@ public class EsExportService {
     @Autowired
     private PatentStarApiService patentStarApiService;
 
+    @Autowired
+    private EsService esService;
+
+    @Autowired
+    private ImportSinglePatentService importSinglePatentService;
+
     public void exportTree(File file) throws Exception {
         Sheet sheet = ReadExcelUtils.readExcel(file);
         int total = sheet.getPhysicalNumberOfRows() - 1;
@@ -868,4 +880,675 @@ public class EsExportService {
         }
         return res;
     }
+
+
+    //-------------------------------------专利缺少信息的脏数据进行删除并且对于缺少信息的专利重新进行刷新---------------------------
+    // 导入接口
+    public String importPatent(List<String> list,Integer projectId) throws IOException {
+        ImportTaskDTO vo = new ImportTaskDTO();
+        vo.setImportContent(1111);
+        vo.setImportToId(projectId);
+        vo.setImportToType(0);
+        vo.setType(2);
+        vo.setFieldDTOS(new ArrayList<>());
+        vo.setSearchCondition(StringUtils.join(list, ","));
+        String param = new Gson().toJson(vo);
+        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
+        OkHttpClient okHttpClient = new OkHttpClient.Builder()
+                .connectTimeout(60, TimeUnit.SECONDS)
+                .writeTimeout(60, TimeUnit.SECONDS)
+                .readTimeout(60, TimeUnit.SECONDS)
+                .build();
+        Request request = new Request.Builder()
+                .url("http://192.168.2.107:8087/api/xiaoshi/importTask/addImportTask")
+                .addHeader("Cookie", "token=5T_nvys1wCh4QKm4t_XYSxxNz0Me6O4PrV__")
+                .post(requestBody)
+                .build();
+        return Objects.requireNonNull(okHttpClient.newCall(request).execute().body()).string();
+    }
+
+    //查询出中国缺少信息的专利总数用于分页遍历
+    public void cnPatent() throws Exception {
+        System.out.println("The Refresh patent is starting");
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+
+        Query q1 = QueryBuilders.exists(i -> i.field("applicant.name"));
+        Query n1 = QueryBuilders.nested(i -> i.path("applicant").query(q1));
+        Query b1 = QueryBuilders.bool(i -> i.mustNot(n1));
+        Query q2 = QueryBuilders.exists(i -> i.field("right_holder.name"));
+        Query n2 = QueryBuilders.nested(i -> i.path("right_holder").query(q2));
+        Query b2 = QueryBuilders.bool(i -> i.mustNot(n2));
+        Query q3 = QueryBuilders.exists(i -> i.field("inventor.name"));
+        Query n3 = QueryBuilders.nested(i -> i.path("inventor").query(q3));
+        Query b3 = QueryBuilders.bool(i -> i.mustNot(n3));
+        Query query = QueryBuilders.bool(i -> i.should(b1, b2, b3));
+        Query q = QueryBuilders.prefix(i -> i.field("patent_no.keyword").value("CN"));
+        Query bool = QueryBuilders.bool(i -> i.must(q, query));
+        builder.query(bool);
+        builder.from(0).size(10);
+        builder.trackTotalHits(i -> i.enabled(true));
+        SearchResponse<Patent> response = null;
+        try {
+            response = client.search(builder.build(), Patent.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new XiaoShiException(e.getMessage());
+        }
+        long total = response.hits().total().value();
+        System.out.println("total:" + total);
+        //分页
+        long pageNum = 1;
+        long pageSize = 1000;
+        long startSize;
+        long endSize;
+        do {
+            startSize = (pageNum - 1) * pageSize;
+            endSize = Math.min(startSize + pageSize, total);
+            this.queryCnPatentInfo(startSize, pageSize);
+            pageNum += 1;
+        } while (endSize < total);
+        System.out.println("The Refresh patent is finished");
+    }
+
+    //查询出中国缺少信息的专利信息
+    public void queryCnPatentInfo(Long startSize, Long endSize) throws Exception {
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+
+        Query q1 = QueryBuilders.exists(i -> i.field("applicant.name"));
+        Query n1 = QueryBuilders.nested(i -> i.path("applicant").query(q1));
+        Query b1 = QueryBuilders.bool(i -> i.mustNot(n1));
+        Query q2 = QueryBuilders.exists(i -> i.field("right_holder.name"));
+        Query n2 = QueryBuilders.nested(i -> i.path("right_holder").query(q2));
+        Query b2 = QueryBuilders.bool(i -> i.mustNot(n2));
+        Query q3 = QueryBuilders.exists(i -> i.field("inventor.name"));
+        Query n3 = QueryBuilders.nested(i -> i.path("inventor").query(q3));
+        Query b3 = QueryBuilders.bool(i -> i.mustNot(n3));
+        Query query = QueryBuilders.bool(i -> i.should(b1, b2, b3));
+        Query q = QueryBuilders.prefix(i -> i.field("patent_no.keyword").value("CN"));
+        Query bool = QueryBuilders.bool(i -> i.must(q, query));
+        builder.query(bool);
+        List<SortOptions> optionsList = new ArrayList<>();
+        SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field("patent_no.keyword").order(SortOrder.Desc).missing(-1)));
+        optionsList.add(sortOptions);
+        builder.sort(optionsList);
+        builder.from(startSize.intValue()).size(endSize.intValue());
+        builder.trackTotalHits(i -> i.enabled(true));
+        SearchResponse<Patent> response = null;
+        try {
+            response = client.search(builder.build(), Patent.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new XiaoShiException(e.getMessage());
+        }
+        List<Hit<Patent>> hits = response.hits().hits();
+        List<String> list = new ArrayList<>();
+        if (!CollectionUtils.isEmpty(hits)) {
+            for (Hit<Patent> hit : hits) {
+                Patent patent = hit.source();
+                String patentNo = patent.getPatentNo();
+                list.add(patentNo);
+            }
+        }
+        this.formatPatentNO(list);
+    }
+
+    public void worldPatent() throws Exception {
+        System.out.println("The Refresh patent is starting");
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+
+        Query q1 = QueryBuilders.exists(i -> i.field("applicant.name"));
+        Query n1 = QueryBuilders.nested(i -> i.path("applicant").query(q1));
+        Query b1 = QueryBuilders.bool(i -> i.mustNot(n1));
+        Query q2 = QueryBuilders.exists(i -> i.field("right_holder.name"));
+        Query n2 = QueryBuilders.nested(i -> i.path("right_holder").query(q2));
+        Query b2 = QueryBuilders.bool(i -> i.mustNot(n2));
+        Query q3 = QueryBuilders.exists(i -> i.field("inventor.name"));
+        Query n3 = QueryBuilders.nested(i -> i.path("inventor").query(q3));
+        Query b3 = QueryBuilders.bool(i -> i.mustNot(n3));
+        Query query = QueryBuilders.bool(i -> i.should(b1, b2, b3));
+        Query q = QueryBuilders.wildcard(i -> i.field("patent_no.keyword").value("*CN*"));
+        Query bool1 = QueryBuilders.bool(i -> i.mustNot(q));
+        Query q4 = QueryBuilders.exists(i -> i.field("patent_no"));
+        Query bool = QueryBuilders.bool(i -> i.must(q4, bool1,query));
+        builder.query(bool);
+        builder.from(0).size(10);
+        builder.trackTotalHits(i -> i.enabled(true));
+        SearchResponse<Patent> response = null;
+        try {
+            response = client.search(builder.build(), Patent.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new XiaoShiException(e.getMessage());
+        }
+        long total = response.hits().total().value();
+        System.out.println("total:" + total);
+        //分页
+        long pageNum = 1;
+        long pageSize = 1000;
+        long startSize;
+        long endSize;
+        do {
+            startSize = (pageNum - 1) * pageSize;
+            endSize = Math.min(startSize + pageSize, total);
+            this.queryWorldPatentInfo(startSize, pageSize);
+            pageNum += 1;
+        } while (endSize < total);
+        System.out.println("The Refresh patent is finished");
+    }
+
+    public void queryWorldPatentInfo(Long startSize, Long endSize) throws Exception {
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+
+        Query q1 = QueryBuilders.exists(i -> i.field("applicant.name"));
+        Query n1 = QueryBuilders.nested(i -> i.path("applicant").query(q1));
+        Query b1 = QueryBuilders.bool(i -> i.mustNot(n1));
+        Query q2 = QueryBuilders.exists(i -> i.field("right_holder.name"));
+        Query n2 = QueryBuilders.nested(i -> i.path("right_holder").query(q2));
+        Query b2 = QueryBuilders.bool(i -> i.mustNot(n2));
+        Query q3 = QueryBuilders.exists(i -> i.field("inventor.name"));
+        Query n3 = QueryBuilders.nested(i -> i.path("inventor").query(q3));
+        Query b3 = QueryBuilders.bool(i -> i.mustNot(n3));
+        Query query = QueryBuilders.bool(i -> i.should(b1, b2, b3));
+        Query q = QueryBuilders.wildcard(i -> i.field("patent_no.keyword").value("*CN*"));
+        Query bool1 = QueryBuilders.bool(i -> i.mustNot(q));
+        Query q4 = QueryBuilders.exists(i -> i.field("patent_no"));
+        Query bool = QueryBuilders.bool(i -> i.must(q4, bool1,query));
+        builder.query(bool);
+        List<SortOptions> optionsList = new ArrayList<>();
+        SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field("patent_no.keyword").order(SortOrder.Desc).missing(-1)));
+        optionsList.add(sortOptions);
+        builder.sort(optionsList);
+        builder.from(startSize.intValue()).size(endSize.intValue());
+        builder.trackTotalHits(i -> i.enabled(true));
+        SearchResponse<Patent> response = null;
+        try {
+            response = client.search(builder.build(), Patent.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new XiaoShiException(e.getMessage());
+        }
+        List<Hit<Patent>> hits = response.hits().hits();
+        List<String> list = new ArrayList<>();
+        if (!CollectionUtils.isEmpty(hits)) {
+            for (Hit<Patent> hit : hits) {
+                Patent patent = hit.source();
+                String patentNo = patent.getPatentNo();
+                list.add(patentNo);
+            }
+        }
+        this.formatPatentNO(list);
+    }
+
+    public void formatPatentNO(List<String> list) throws Exception {
+        if (!CollectionUtils.isEmpty(list)) {
+            for (String patentNo : list) {
+                String oldPatentId = this.queryOldByPatentNo(patentNo);
+                if (StringUtils.isEmpty(oldPatentId)) {
+                    continue;
+                }
+                String newPatentId = "";
+                if (patentNo.startsWith("CN")) {
+                    //中国专利
+                    if (patentNo.length() == 12 && patentNo.contains(".")) {
+                        String newPatentNo = this.formatCnNumber(patentNo);
+                        newPatentId = this.queryNewByPatentNo(newPatentNo, oldPatentId);
+                    } else if (patentNo.length() == 15){
+                        String newPatentNo = this.formatNumber(patentNo);
+                        newPatentId = this.queryNewByPatentNo(newPatentNo, oldPatentId);
+                    } else {
+                        newPatentId = this.queryNewByPatentNo(patentNo, oldPatentId);
+                    }
+                } else {
+                    //世界专利
+                    newPatentId = this.queryNewByPatentNo(patentNo, oldPatentId);
+                }
+
+                if (StringUtils.isEmpty(newPatentId)) {
+                    continue;
+                }
+                this.getProjectPatent(oldPatentId,newPatentId);
+                this.getProductPatent(oldPatentId,newPatentId);
+                this.getProjectTaskPatent(oldPatentId,newPatentId);
+                this.getCustomFieldPatent(oldPatentId,newPatentId);
+                esService.deleteByIds(Collections.singletonList(oldPatentId));
+            }
+        }
+    }
+
+    public String formatNumber(String str) {
+        return str.substring(0, 14) +
+                "." +
+                str.substring(14, 15);
+    }
+
+    public String formatCnNumber(String str) {
+        StringBuilder builder = new StringBuilder();
+        builder.append(str, 0, 2);
+        int num = Integer.parseInt(str.substring(2, 3));
+        if (num > 6) {
+            builder.append("19");
+        } else {
+            builder.append("20");
+        }
+        builder.append(str, 2, 5);
+        builder.append("00");
+        builder.append(str, 5, str.indexOf("."));
+        String s = builder.toString();
+        String s1 = s.substring(2);
+        char c = this.calculateChecksum(s1);
+        builder.append(".").append(c);
+        return builder.toString();
+    }
+
+    public char calculateChecksum(String input) {
+        int sum = 0;
+        int[] weights = {2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5};
+
+        // 遍历字符串中的每个字符,并计算乘积之和
+        for (int i = 0; i < input.length(); i++) {
+            char digit = input.charAt(i);
+            int num = Character.getNumericValue(digit);
+            sum += num * weights[i];
+        }
+
+        // 计算总和除以11的余数
+        int remainder = sum % 11;
+
+        // 根据余数确定校验位
+        char checksum;
+        if (remainder < 10) {
+            checksum = (char) ('0' + remainder);
+        } else {
+            checksum = 'X';
+        }
+
+        return checksum;
+    }
+
+    //根据问题类型中的专利号查询
+    public String queryOldByPatentNo(String oldPatentNo) throws Exception {
+        String patentId = "";
+        if (StringUtils.isNotEmpty(oldPatentNo)) {
+            SearchRequest.Builder builder = new SearchRequest.Builder();
+            //设置查询索引
+            builder.index("patent");
+            Query query = QueryBuilders.term(t -> t.field("patent_no.keyword").value(oldPatentNo));
+            builder.query(query);
+            SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
+            List<Hit<Patent>> hits = response.hits().hits();
+            if (!CollectionUtils.isEmpty(hits)) {
+                patentId = hits.get(0).id();
+            }
+        }
+        return patentId;
+    }
+
+    //根据格式化后的专利号查询
+    public String queryNewByPatentNo(String newPatentNo,String oldPatentId) throws Exception {
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+        Query query = QueryBuilders.term(t -> t.field("patent_no.keyword").value(newPatentNo));
+        builder.query(query);
+        SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
+        List<Hit<Patent>> hits = response.hits().hits();
+        List<String> list = new ArrayList<>();
+        String newPatentId = "";
+        if (!CollectionUtils.isEmpty(hits)) {
+            for (Hit<Patent> hit : hits) {
+                String patentId = hit.id();
+                if (!patentId.equals(oldPatentId)) {
+                    Patent patent = hit.source();
+                    if (!CollectionUtils.isEmpty(patent.getApplicant()) &&
+                            !CollectionUtils.isEmpty(patent.getRightHolder()) &&
+                            !CollectionUtils.isEmpty(patent.getInventor())) {
+                        newPatentId = patentId;
+                        break;
+                    }
+                }
+            }
+            if (StringUtils.isEmpty(newPatentId)) {
+                Hit<Patent> hit = hits.stream().filter(i -> !i.id().equals(oldPatentId)).findFirst().orElse(null);
+                if (ObjectUtils.isNotEmpty(hit) && hit != null) {
+                    newPatentId = hit.id();
+                } else {
+                    newPatentId = this.queryWDExtra(newPatentNo);
+//                    Boolean flag = this.queryExtra(newPatentNo);
+//                    if (Boolean.TRUE.equals(flag)) {
+//                        this.importPatent(Collections.singletonList(newPatentNo), 614);
+//                        Thread.sleep(3000);
+//                        newPatentId = this.queryOldByPatentNo(newPatentNo);
+//                    }
+                }
+            }
+            for (Hit<Patent> hit : hits) {
+                String patentId = hit.id();
+                if (!patentId.equals(oldPatentId) && !patentId.equals(newPatentId)) {
+                    list.add(patentId);
+                }
+            }
+        } else {
+            newPatentId = this.queryWDExtra(newPatentNo);
+//            Boolean flag = this.queryExtra(newPatentNo);
+//            if (Boolean.TRUE.equals(flag)) {
+//                this.importPatent(Collections.singletonList(newPatentNo), 614);
+//                Thread.sleep(3000);
+//                newPatentId = this.queryOldByPatentNo(newPatentNo);
+//            }
+        }
+        if (!CollectionUtils.isEmpty(list)) {
+            esService.deleteByIds(list);
+        }
+        return newPatentId;
+    }
+
+    public Boolean queryExtra(String patentNo) throws IOException {
+        boolean flag = true;
+        PatentStarListDTO vo = new PatentStarListDTO();
+        vo.setCurrentQuery("F XX (" + patentNo + "/PN-CN/GJ)");
+        vo.setDBType("WD");
+        vo.setPageNum(1);
+        vo.setRowCount(10);
+        vo.setOrderBy("AD");
+        vo.setOrderByType("DESC");
+        vo.setFormed(true);
+        Map<String, Object> resultMap = patentStarApiService.patentStarSearchApi(vo);
+        if (resultMap == null || (Integer) resultMap.get("total") == 0) {
+            return false;
+        }
+        List<StarPatentVO> starPatents = (List<StarPatentVO>) resultMap.get("records");
+        if (CollectionUtils.isEmpty(starPatents)) {
+            flag = false;
+        }
+        return flag;
+    }
+
+    public String queryWDExtra(String patentNo) throws Exception {
+        String patentId = "";
+        PatentStarListDTO vo = new PatentStarListDTO();
+        vo.setCurrentQuery("F XX (" + patentNo + "/PN-CN/GJ)");
+        vo.setDBType("WD");
+        vo.setPageNum(1);
+        vo.setRowCount(10);
+        vo.setOrderBy("AD");
+        vo.setOrderByType("DESC");
+        vo.setFormed(true);
+        Map<String, Object> resultMap = null;
+        try {
+            resultMap = patentStarApiService.patentStarSearchApi(vo);
+        } catch (Exception e) {
+            return null;
+        }
+        if (resultMap == null || (Integer) resultMap.get("total") == 0) {
+            return null;
+        }
+        List<StarPatentVO> starPatents = (List<StarPatentVO>) resultMap.get("records");
+        if (!CollectionUtils.isEmpty(starPatents)) {
+            StarPatentVO starPatentVO = starPatents.get(0);
+            patentId = this.addNewPatent(starPatentVO);
+        }
+        return patentId;
+    }
+
+
+    public String addNewPatent(StarPatentVO starPatentVO) throws Exception {
+        Patent patent = importSinglePatentService.getPatentCataloguingFromWeb(starPatentVO);
+        PatentJoin patentJoin = new PatentJoin();
+        patentJoin.setName("patent");
+        patent.setPatentJoin(patentJoin);
+        return esService.addPatent(patent);
+    }
+
+    //获取项目
+    public void getProjectPatent(String oldPatentId,String newPatentId) throws Exception {
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+        Query q1 = QueryBuilders.exists(i -> i.field("project_id"));
+        Query q2 = QueryBuilders.ids(i -> i.values(oldPatentId));
+        Query q3 = QueryBuilders.hasParent(i -> i.parentType("patent").query(q2));
+        Query bool = QueryBuilders.bool(i -> i.must(q1, q3));
+        builder.query(bool);
+        builder.from(0).size(200);
+        builder.trackTotalHits(i -> i.enabled(true));
+        SearchResponse<Patent> response = null;
+        try {
+            response = client.search(builder.build(), Patent.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new XiaoShiException(e.getMessage());
+        }
+        List<Hit<Patent>> hits = response.hits().hits();
+        if (!CollectionUtils.isEmpty(hits)) {
+            for (Hit<Patent> hit : hits) {
+                Patent patent = hit.source();
+                Integer projectId = patent.getProjectId();
+                Patent patentChild = new Patent();
+                PatentJoin patentJoin = new PatentJoin();
+                patentJoin.setParent(newPatentId);
+                patentJoin.setName("project");
+                patentChild.setPatentJoin(patentJoin);
+                patentChild.setProjectId(projectId);
+                esService.addChildPatent(patentChild, newPatentId);
+            }
+        }
+    }
+
+    //获取任务
+    public void getProjectTaskPatent(String oldPatentId,String newPatentId) {
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+        Query q1 = QueryBuilders.exists(i -> i.field("project_task"));
+        Query q2 = QueryBuilders.ids(i -> i.values(oldPatentId));
+        Query q3 = QueryBuilders.hasParent(i -> i.parentType("patent").query(q2));
+        Query bool = QueryBuilders.bool(i -> i.must(q1, q3));
+        builder.query(bool);
+        builder.from(0).size(200);
+        builder.trackTotalHits(i -> i.enabled(true));
+        SearchResponse<Patent> response = null;
+        try {
+            response = client.search(builder.build(), Patent.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new XiaoShiException(e.getMessage());
+        }
+        List<Hit<Patent>> hits = response.hits().hits();
+        if (!CollectionUtils.isEmpty(hits)) {
+            for (Hit<Patent> hit : hits) {
+                Patent patent = hit.source();
+                EsProjectTask projectTask = patent.getProjectTask();
+                Patent patent1 = new Patent();
+                EsProjectTask esProjectTask = new EsProjectTask();
+                esProjectTask.setProjectId(projectTask.getProjectId());
+                esProjectTask.setTaskId(projectTask.getTaskId());
+                PatentJoin patentJoin = new PatentJoin();
+                patentJoin.setParent(newPatentId);
+                patentJoin.setName("task");
+                patent1.setPatentJoin(patentJoin);
+                patent1.setProjectTask(esProjectTask);
+            }
+        }
+    }
+
+    //获取导入任务
+    public void getImportTaskPatent(String oldPatentId,String newPatentId) {
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+        Query q1 = QueryBuilders.exists(i -> i.field("import_task"));
+        Query q2 = QueryBuilders.ids(i -> i.values(oldPatentId));
+        Query q3 = QueryBuilders.hasParent(i -> i.parentType("patent").query(q2));
+        Query bool = QueryBuilders.bool(i -> i.must(q1, q3));
+        builder.query(bool);
+        builder.from(0).size(200);
+        builder.trackTotalHits(i -> i.enabled(true));
+        SearchResponse<Patent> response = null;
+        try {
+            response = client.search(builder.build(), Patent.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new XiaoShiException(e.getMessage());
+        }
+        List<Hit<Patent>> hits = response.hits().hits();
+        if (!CollectionUtils.isEmpty(hits)) {
+            for (Hit<Patent> hit : hits) {
+                Patent patent = hit.source();
+
+            }
+        }
+    }
+
+    //获取产品
+    public void getProductPatent(String oldPatentId,String newPatentId) throws Exception {
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+        Query q1 = QueryBuilders.exists(i -> i.field("product_id"));
+        Query q2 = QueryBuilders.ids(i -> i.values(oldPatentId));
+        Query q3 = QueryBuilders.hasParent(i -> i.parentType("patent").query(q2));
+        Query bool = QueryBuilders.bool(i -> i.must(q1, q3));
+        builder.query(bool);
+        builder.from(0).size(200);
+        builder.trackTotalHits(i -> i.enabled(true));
+        SearchResponse<Patent> response = null;
+        try {
+            response = client.search(builder.build(), Patent.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new XiaoShiException(e.getMessage());
+        }
+        List<Hit<Patent>> hits = response.hits().hits();
+        if (!CollectionUtils.isEmpty(hits)) {
+            for (Hit<Patent> hit : hits) {
+                Patent patent = hit.source();
+                Integer productId = patent.getProductId();
+                Patent patentChild = new Patent();
+                PatentJoin patentJoin = new PatentJoin();
+                patentJoin.setParent(newPatentId);
+                patentJoin.setName("product");
+                patentChild.setPatentJoin(patentJoin);
+                patentChild.setProductId(productId);
+                esService.addChildPatent(patentChild, newPatentId);
+            }
+        }
+    }
+
+    //获取自定义字段栏位
+    public void getCustomFieldPatent(String oldPatentId,String newPatentId) throws Exception {
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+        Query q1 = QueryBuilders.exists(i -> i.field("custom_field"));
+        Query q2 = QueryBuilders.ids(i -> i.values(oldPatentId));
+        Query q3 = QueryBuilders.hasParent(i -> i.parentType("patent").query(q2));
+        Query bool = QueryBuilders.bool(i -> i.must(q1, q3));
+        builder.query(bool);
+        builder.from(0).size(200);
+        builder.trackTotalHits(i -> i.enabled(true));
+        SearchResponse<Patent> response = null;
+        try {
+            response = client.search(builder.build(), Patent.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new XiaoShiException(e.getMessage());
+        }
+        List<Hit<Patent>> hits = response.hits().hits();
+        if (!CollectionUtils.isEmpty(hits)) {
+            for (Hit<Patent> hit : hits) {
+                Patent patent = hit.source();
+                ESCustomField esCustomField = patent.getESCustomField();
+                Patent patentChild = new Patent();
+                PatentJoin patentJoin = new PatentJoin();
+                patentJoin.setParent(newPatentId);
+                patentJoin.setName("project_customfield");
+                patentChild.setPatentJoin(patentJoin);
+                patentChild.setESCustomField(esCustomField);
+                esService.addChildPatent(patentChild, newPatentId);
+            }
+        }
+    }
+
+    //获取标注
+    public void getMarkingsPatent(String oldPatentId,String newPatentId) {
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+        Query q1 = QueryBuilders.exists(i -> i.field("markings"));
+        Query q2 = QueryBuilders.ids(i -> i.values(oldPatentId));
+        Query q3 = QueryBuilders.hasParent(i -> i.parentType("patent").query(q2));
+        Query bool = QueryBuilders.bool(i -> i.must(q1, q3));
+        builder.query(bool);
+        builder.from(0).size(200);
+        builder.trackTotalHits(i -> i.enabled(true));
+        SearchResponse<Patent> response = null;
+        try {
+            response = client.search(builder.build(), Patent.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new XiaoShiException(e.getMessage());
+        }
+        List<Hit<Patent>> hits = response.hits().hits();
+        if (!CollectionUtils.isEmpty(hits)) {
+            for (Hit<Patent> hit : hits) {
+                Patent patent = hit.source();
+
+            }
+        }
+    }
+
+    public void getWOErrorPatent() throws Exception {
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+        Query q1 = QueryBuilders.prefix(i -> i.field("public_no.keyword").value("WO"));
+        Query q2 = QueryBuilders.prefix(i -> i.field("patent_no.keyword").value("CN"));
+        Query bool = QueryBuilders.bool(i -> i.must(q1, q2));
+        builder.query(bool);
+        builder.from(0).size(1000);
+        builder.trackTotalHits(i -> i.enabled(true));
+        SearchResponse<Patent> response = null;
+        try {
+            response = client.search(builder.build(), Patent.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new XiaoShiException(e.getMessage());
+        }
+        List<Hit<Patent>> hits = response.hits().hits();
+        Map<String, String> map = new HashMap<>();
+        if (!CollectionUtils.isEmpty(hits)) {
+            for (Hit<Patent> hit : hits) {
+                String oldPatentId = hit.id();
+                Patent patent = hit.source();
+                String publicNo = patent.getPublicNo();
+                String newPatentId = this.queryNewByPatentNo(publicNo, oldPatentId);
+                if (StringUtils.isEmpty(newPatentId)) {
+                    continue;
+                }
+                map.put(oldPatentId, newPatentId);
+            }
+        }
+
+        if (!CollectionUtils.isEmpty(map)) {
+            for (String oldPatentId : map.keySet()) {
+                String newPatentId = map.get(oldPatentId);
+                if (StringUtils.isEmpty(newPatentId)) {
+                    continue;
+                }
+                this.getProjectPatent(oldPatentId,newPatentId);
+                this.getProductPatent(oldPatentId,newPatentId);
+                this.getProjectTaskPatent(oldPatentId,newPatentId);
+                this.getCustomFieldPatent(oldPatentId,newPatentId);
+                esService.deleteByIds(Collections.singletonList(oldPatentId));
+            }
+        }
+    }
 }

+ 14 - 2
src/main/java/cn/cslg/pas/service/importPatent/ImportSinglePatentService.java

@@ -147,7 +147,8 @@ public class ImportSinglePatentService {
      */
     public Patent getPatentCataloguingFromWeb(StarPatentVO starPatentVO) {
         String patentZhuLuStr = "";
-        if (starPatentVO.getAN().startsWith("CN")) {
+        String number = this.getNumber(starPatentVO.getGN(), starPatentVO.getPN(), starPatentVO.getAN());
+        if (number.startsWith("CN")) {
             patentZhuLuStr = patentStarApiService.getCnBibApi(starPatentVO.getANO());
         } else {
             patentZhuLuStr = patentStarApiService.getENBibApi(starPatentVO.getPN());
@@ -157,7 +158,7 @@ public class ImportSinglePatentService {
         uploadPatentWebDTO.setStarPatentVO(starPatentVO);
 
         if (patentZhuLuStr != null && !patentZhuLuStr.trim().equals("") && !patentZhuLuStr.equals("{}") && !patentZhuLuStr.contains("请求不合法")) {
-            if (starPatentVO.getAN().startsWith("CN")) {
+            if (number.startsWith("CN")) {
                 webVOTransformService.loadCNPatent(patentZhuLuStr, uploadPatentWebDTO);
             } else {
                 webVOTransformService.loadWorldPatent(patentZhuLuStr, uploadPatentWebDTO);
@@ -166,6 +167,17 @@ public class ImportSinglePatentService {
         return uploadPatentWebDTO.getPatent();
     }
 
+    public String getNumber(String grantNo, String publicNo, String appNo) {
+        String res = "";
+        if (org.apache.commons.lang3.StringUtils.isNotEmpty(grantNo)) {
+            res = grantNo;
+        } else if (org.apache.commons.lang3.StringUtils.isNotEmpty(publicNo)) {
+            res = publicNo;
+        } else {
+            res = appNo;
+        }
+        return res;
+    }
 
     public void getFullXmlStr(Patent patent, StarPatentVO starPatentVO, List<Integer> contents) throws Exception {
         String cnFullXmlStr = patentStarApiService.getCnFullXmlApi(starPatentVO.getANO());

+ 79 - 3
src/test/java/cn/cslg/pas/service/EventServiceTests.java

@@ -16,6 +16,7 @@ import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
 import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
 import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
 import cn.cslg.pas.common.vo.EsExplainTextVO;
+import cn.cslg.pas.common.vo.StarPatentVO;
 import cn.cslg.pas.common.vo.business.*;
 import cn.cslg.pas.controller.EventController;
 import cn.cslg.pas.controller.PatentController;
@@ -37,9 +38,9 @@ import co.elastic.clients.elasticsearch._types.query_dsl.Query;
 import co.elastic.clients.elasticsearch.core.SearchRequest;
 import co.elastic.clients.elasticsearch.core.SearchResponse;
 import co.elastic.clients.elasticsearch.core.search.Hit;
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import com.alibaba.fastjson2.JSON;
 import org.apache.http.entity.ContentType;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -95,6 +96,8 @@ public class EventServiceTests {
     private TranslateService translateService;
     @Autowired
     private EsExportService esExportService;
+    @Autowired
+    private PatentStarApiService patentStarApiService;
 
     @Test
     void test() throws Exception {
@@ -831,7 +834,80 @@ public class EventServiceTests {
 //        System.out.println(s);
 //        final String deficency = esExportService.getCNDeficency("CN202420077302.8");
 //        esExportService.getCNNum();
-        final Long worldNumber = esExportService.getWorldNumber();
-        System.out.println("--------------------finished-------------------");
+//        final Long worldNumber = esExportService.getWorldNumber();
+//        System.out.println("--------------------finished-------------------");
+        // CN 19 998 00 15538.2     CN199910027176.0
+        //CN 998 15538.1            CN 991  27176.9
+//        String str = "CN03267359.0";
+//        StringBuilder result = new StringBuilder();
+//        StringBuilder builder = new StringBuilder();
+//        if (str.length() == 12) {
+//            builder.append(str, 0, 2);
+//            final String s2 = str.substring(2, 3);
+//            System.out.println(s2);
+//            if (s2.equals("9")) {
+//                builder.append("19");
+//            } else if (s2.equals("0")) {
+//                builder.append("20");
+//            }
+//            builder.append(str, 2, 5);
+//            builder.append("00");
+//            builder.append(str, 5, str.indexOf("."));
+//            final String s = builder.toString();
+//            final String s1 = s.substring(2);
+//            final char c = this.calculateChecksum(s1);
+//            builder.append(".").append(c);
+//        } else {
+//            System.out.println("BBBBBBBBBBB");
+//
+//        }
+//        System.out.println("AAA:" + builder);
+//        esExportService.cnPatent();
+        esExportService.worldPatent();
+
+//        PatentStarListDTO vo = new PatentStarListDTO();
+//        vo.setCurrentQuery("F XX (WO2023051704A1/PN-CN/GJ)");
+//        vo.setDBType("WD");
+//        vo.setPageNum(1);
+//        vo.setRowCount(10);
+//        vo.setOrderBy("AD");
+//        vo.setOrderByType("DESC");
+//        vo.setFormed(true);
+//        Map<String, Object> resultMap = patentStarApiService.patentStarSearchApi(vo);
+//        List<StarPatentVO> starPatents = (List<StarPatentVO>) resultMap.get("records");
+//        System.out.println(starPatents);
+
+//        final Boolean aBoolean = esExportService.queryExtra("WO226025S");
+//        System.out.println(aBoolean);
+//        List<String> list = new ArrayList<>();
+//        list.add("CN2023111117357");
+//        list.add("CN2022229019364");
+//        esExportService.formatPatentNO(list);
+    }
+
+
+    public char calculateChecksum(String input) {
+        int sum = 0;
+        int[] weights = {2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5};
+
+        // 遍历字符串中的每个字符,并计算乘积之和
+        for (int i = 0; i < input.length(); i++) {
+            char digit = input.charAt(i);
+            int num = Character.getNumericValue(digit);
+            sum += num * weights[i];
+        }
+
+        // 计算总和除以11的余数
+        int remainder = sum % 11;
+
+        // 根据余数确定校验位
+        char checksum;
+        if (remainder < 10) {
+            checksum = (char) ('0' + remainder);
+        } else {
+            checksum = 'X';
+        }
+
+        return checksum;
     }
 }