package cn.cslg.pas.service.common; import cn.cslg.pas.common.config.XDns; import cn.cslg.pas.common.core.IgnoreDTDEntityResolver; import cn.cslg.pas.common.dto.*; import cn.cslg.pas.common.dto.business.SelectClaimDTO; import cn.cslg.pas.common.model.cronModel.PersonnelVO; import cn.cslg.pas.common.model.cronModel.Records; import cn.cslg.pas.common.model.importTaskModel.PatentApplicant; import cn.cslg.pas.common.utils.*; import cn.cslg.pas.common.vo.ContentVO; import cn.cslg.pas.common.vo.NoCacheVO; import cn.cslg.pas.common.vo.QueryExternalFamilyVO; import cn.cslg.pas.common.vo.StarPatentVO; import cn.cslg.pas.common.vo.business.PatentNoVO; import cn.cslg.pas.domain.WebLoginConfig; import cn.cslg.pas.domain.business.RetrieveRecord; import cn.cslg.pas.domain.es.*; import cn.cslg.pas.exception.XiaoShiException; import cn.cslg.pas.service.WebLoginConfigService; import cn.cslg.pas.service.query.FormatQueryService; import cn.hutool.core.util.IdUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import lombok.RequiredArgsConstructor; import lombok.experimental.Accessors; import lombok.extern.slf4j.Slf4j; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.XPath; import org.dom4j.io.SAXReader; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.io.File; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; /** * 调用外部接口的Service类 PCS:权限系统 * * @Author chenyu * @Date 2023/4/25 */ @RequiredArgsConstructor @Slf4j @Service public class PatentStarApiService { @Autowired private RetrieveRecordService retrieveRecordService; @Autowired private CacheUtils cacheUtils; @Autowired private LoginUtils loginUtils; @Autowired private WebLoginConfigService webLoginConfigService; @Autowired private FormatQueryService formatQueryService; @Autowired private NOSCacheService cacheService; public Records patentStarSearchLocal(PatentStarListDTO patentStarListDTO) throws IOException { RetrieveRecord retrieveRecord = new RetrieveRecord(); retrieveRecord.setConditions(patentStarListDTO.getCurrentQuery()); retrieveRecord.setSearchType(0); if (patentStarListDTO.getRetrieveRecordId() != null) { retrieveRecord = retrieveRecordService.getById(patentStarListDTO.getRetrieveRecordId()); patentStarListDTO.setCurrentQuery(retrieveRecord.getConditions()); patentStarListDTO.setDBType(retrieveRecord.getDbType()); } String redisKey = patentStarListDTO.getRedisKey(); List nos = new ArrayList<>(); Long total = 0L; if (StringUtils.isNotEmpty(patentStarListDTO.getNumberQuery())) { NoCacheVO cacheVO = cacheService.savaByList(patentStarListDTO); retrieveRecord.setConditions(patentStarListDTO.getNumberQuery()); retrieveRecord.setSearchType(1); redisKey = cacheVO.getKey(); total = cacheVO.getTotal(); nos.addAll(cacheVO.getList()); } if (Boolean.TRUE.equals(StringUtils.isEmpty(patentStarListDTO.getNumberQuery())) && StringUtils.isNotEmpty(redisKey)) { NoCacheVO cacheVO = cacheService.getByList(patentStarListDTO); retrieveRecord.setConditions(patentStarListDTO.getNumberQuery()); retrieveRecord.setSearchType(1); nos.addAll(cacheVO.getList()); total = cacheVO.getTotal(); redisKey = cacheVO.getKey(); } Map map = this.patentStarSearchApi(patentStarListDTO); if (map == null) { throw new XiaoShiException("检索失败,请检查检索式"); } //记录检索历史 retrieveRecord.setRetrieveTime(new Date()); retrieveRecord.setTotalNum(Integer.parseInt(map.get("total").toString())); retrieveRecord.setDbType(patentStarListDTO.getDBType()); if (patentStarListDTO.getRetrieveRecordId() == null) { //获取创建人信息 PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); retrieveRecord.setCreateTime(new Date()); retrieveRecord.setCreateId(personnelVO.getId()); retrieveRecord.setTenantId(personnelVO.getTenantId()); retrieveRecord.insert(); } else { retrieveRecord.updateById(); } List starPatentVOS = (List) map.get("records"); //装载starPatentVOS到 PatentColumnDTO Records records = new Records(); records.setRetrieveRecordId(retrieveRecord.getId()); records.setCurrent(Long.parseLong(map.get("current").toString())); records.setSize(Long.parseLong(map.get("size").toString())); List columnDTOS = this.loadPatent(starPatentVOS); records.setData(this.loadPatentList(columnDTOS, nos, patentStarListDTO.getDBType())); if (total != 0) { records.setTotal(total); } else { records.setTotal(Long.parseLong(map.get("total").toString())); } records.setRedisKey(redisKey); return records; } /** * 格式化条件后并请求外部接口 * @param PatentStarListDTO * @return * @throws IOException */ public Map patentStarSearchApi(PatentStarListDTO PatentStarListDTO) throws IOException { try { if (PatentStarListDTO.getFormed() == null || PatentStarListDTO.getFormed() == false) { String formQuery = PatentStarListDTO.getCurrentQuery(); PatentStarListDTO.setCurrentQuery(formatQueryService.reQuery(formQuery, "webSearchConfig")); if (PatentStarListDTO.getDBType().equals("WD")) { StringBuilder stringBuilder = new StringBuilder(PatentStarListDTO.getCurrentQuery()); stringBuilder.insert(PatentStarListDTO.getCurrentQuery().length() - 1, "-CN/GJ"); PatentStarListDTO.setCurrentQuery(stringBuilder.toString()); } PatentStarListDTO.setFormed(true); } } catch (Exception e) { e.printStackTrace(); return null; } JSONObject configObject = this.getConfigObject(4, 1); String appId = configObject.getString("appId"); String appKey = configObject.getString("appKey"); PatentStarDTO patentStarDto = new PatentStarDTO(); BeanUtils.copyProperties(PatentStarListDTO, patentStarDto); String json = JSONObject.toJSONString(patentStarDto); String url = "http://s.patentstar.com.cn/SearchAPI/PatentSearch/ResultGet"; Long currentTimeMillis = System.currentTimeMillis() / 1000; String Sign = appKey + currentTimeMillis.toString(); String signMd5 = FormatUtil.MD5(Sign); // 创建一个OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .dns(new XDns(100000)) .build(); // 创建一个RequestBody(参数1:数据类型 参数2传递的json串) FormBody.Builder builder = new FormBody.Builder(); builder.add("AppID", appId); builder.add("Stamp", currentTimeMillis.toString()); builder.add("Sign", signMd5); builder.add("QueryJson", json); FormBody requestBody = builder.build(); // 创建一个请求对象 Request request = new Request.Builder().url(url).post(requestBody).build(); // 发送请求获取响应 try { Response response = okHttpClient.newCall(request).execute(); // 判断请求是否成功 if (response.isSuccessful()) { JSONObject jsonObject = JSONObject.parseObject(Objects.requireNonNull(response.body()).string()); if (jsonObject.get("Ret").equals(500) || jsonObject.get("Data") == null) { return null; } JSONObject Data = (JSONObject) jsonObject.get("Data"); List starPatentVOS = JSONArray.parseArray(Data.get("List").toString(), StarPatentVO.class); Map reMap = new HashMap<>(); reMap.put("size", PatentStarListDTO.getRowCount()); reMap.put("current", PatentStarListDTO.getPageNum()); reMap.put("records", starPatentVOS); reMap.put("total", Data.get("HitCount")); return reMap; } } catch (IOException e) { // e.printStackTrace(); // return null; throw new XiaoShiException("外部接口检索超时"); } return null; } //封装patent column数据返回 private List loadPatentList(List columnDTOS, List list, String dbType) { if (CollectionUtils.isEmpty(list) || CollectionUtils.isEmpty(columnDTOS)) { return list.isEmpty() ? columnDTOS : createNewDTOs(list); } List result = new ArrayList<>(columnDTOS.size()); Set searchedNumbers = new HashSet<>(); // 用于跟踪已搜索的专利号 for (String patentNo : list) { boolean found = false; String formattedNo = dbType.equals("CN") ? PatentNoUtil.formatApNo(patentNo) : patentNo; for (PatentColumnDTO columnDTO : columnDTOS) { if (containsNumber(columnDTO, formattedNo)) { PatentColumnDTO dto = new PatentColumnDTO(); BeanUtils.copyProperties(columnDTO,dto); dto.setSearchNo(patentNo); result.add(dto); searchedNumbers.add(patentNo); // 标记为已搜索 found = true; break; // 找到匹配项后跳出内层循环 } } if (!found) { // 如果没有在columnDTOS中找到匹配项,则创建一个新的DTO PatentColumnDTO newDTO = new PatentColumnDTO(); newDTO.setPatentNo(patentNo); newDTO.setSearchNo(patentNo); newDTO.setIfSearch(false); result.add(newDTO); } } return result; } private boolean containsNumber(PatentColumnDTO dto, String number) { return dto.getAppNo().contains(number) || dto.getPublicNo().contains(number) || dto.getGrantNo().contains(number) || dto.getRowApplicationNo().contains(number); } private List createNewDTOs(List list) { List newDTOs = new ArrayList<>(); for (String s : list) { PatentColumnDTO dto = new PatentColumnDTO(); dto.setPatentNo(s); dto.setSearchNo(s); dto.setIfSearch(false); newDTOs.add(dto); } return newDTOs; } public List getSplitedConditions(PatentStarListDTO patentStarListDTO, int patentNum) throws IOException { DateTime nowDate = new DateTime(); DateTime date = DateUtils2.formStrToDateTime("1900-01-01"); //超过1万的列表 List PatentStarListDTOs = new ArrayList<>(); //返回的列表 List reDtos = new ArrayList<>(); patentStarListDTO.setStartTime(date); patentStarListDTO.setEndTime(nowDate); patentStarListDTO.setOrginCondition(patentStarListDTO.getCurrentQuery()); String formQuery = patentStarListDTO.getCurrentQuery(); // patentStarListDTO.setOrginCondition(PatentStarApiService.formatQuery(formQuery)); PatentStarListDTOs.add(patentStarListDTO); while (PatentStarListDTOs.size() > 0) { PatentStarListDTO dto = PatentStarListDTOs.get(0); PatentStarListDTOs.remove(dto); Map map1 = this.patentStarSearchApi(dto); Integer total1 = Integer.parseInt(map1.get("total").toString()); dto.setTotal(total1); patentStarListDTO.setFormed(true); if (total1 > patentNum) { DateTime startTime1 = dto.getStartTime(); DateTime endTime1 = dto.getEndTime(); List dateTimes = DateUtils2.formStrToDateTime(startTime1, endTime1); PatentStarListDTO dto1 = new PatentStarListDTO(); BeanUtils.copyProperties(dto, dto1); dto1.setStartTime(dateTimes.get(0)); dto1.setEndTime(dateTimes.get(1)); this.setConditions(dto1); PatentStarListDTO dto2 = new PatentStarListDTO(); BeanUtils.copyProperties(dto, dto2); dto2.setStartTime(dateTimes.get(2)); dto2.setEndTime(dateTimes.get(3)); this.setConditions(dto2); PatentStarListDTOs.add(dto1); PatentStarListDTOs.add(dto2); } else if (total1 != 0) { reDtos.add(dto); } } return reDtos; } public void setConditions(PatentStarListDTO PatentStarListDTO) { PatentStarListDTO.setFormed(false); DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMdd"); String startStr = formatter.print(PatentStarListDTO.getStartTime()); String endStr = formatter.print(PatentStarListDTO.getEndTime()); String cond = " AND AD=" + startStr + "~" + endStr; Integer len = PatentStarListDTO.getOrginCondition().length(); StringBuilder stringBuilder = new StringBuilder(PatentStarListDTO.getOrginCondition()); stringBuilder.insert(len - 1, cond); PatentStarListDTO.setCurrentQuery(stringBuilder.toString()); } /** * @param appNo * @return * @throws IOException * @author 李仁杰 * 从专利之星获取中国专利著录 */ public String getCnBibApi(String appNo) { String url = "https://api.patentstar.com.cn/api/Patent/CnBibo/" + appNo; JSONObject configObject = this.getConfigObject(4, 2); String appId = configObject.getString("appId"); String appKey = configObject.getString("appKey"); Long currentTimeMillis = System.currentTimeMillis() / 1000; String Sign = appId + appKey + currentTimeMillis.toString(); String signMd5 = FormatUtil.MD5(Sign); // 创建一个OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .dns(new XDns(100000)) .build(); // 创建一个请求对象 Request request = new Request.Builder().url(url) .addHeader("_appid", appId) .addHeader("_timestamp", currentTimeMillis.toString()) .addHeader("_sign", signMd5) .get().build(); // 发送请求获取响应 try { Response response = okHttpClient.newCall(request).execute(); // 判断请求是否成功 if (response.isSuccessful()) { // 打印服务端返回结果 return Objects.requireNonNull(response.body()).string(); } } catch (IOException e) { e.printStackTrace(); } return "{}"; } /** * @param * @return * @throws IOException * @author 李仁杰 * 从专利之星获取中国专利摘要附图 */ public String getPictureApi(String appNo) { String url = "https://api.patentstar.com.cn/api/Patent/CnMainImage/" + appNo; JSONObject configObject = this.getConfigObject(4, 2); String appId = configObject.getString("appId"); String appKey = configObject.getString("appKey"); Long currentTimeMillis = System.currentTimeMillis() / 1000; String Sign = appId + appKey + currentTimeMillis.toString(); String signMd5 = FormatUtil.MD5(Sign); // 创建一个OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .dns(new XDns(100000)) .build(); // 创建一个请求对象 Request request = new Request.Builder().url(url) .addHeader("_appid", appId) .addHeader("_timestamp", currentTimeMillis.toString()) .addHeader("_sign", signMd5) .get().build(); // 发送请求获取响应 try { Response response = okHttpClient.newCall(request).execute(); // 判断请求是否成功 if (response.isSuccessful() && response.code() == 200) { // 打印服务端返回结果 return Objects.requireNonNull(response.body()).string(); } } catch (IOException e) { e.printStackTrace(); return null; } return "{}"; } /** * @param appNo * @return * @throws IOException * @author 李仁杰 * 从专利之星获取中国专利外观图 */ public String getWGPictureApi(String appNo) throws IOException { String url = "https://api.patentstar.com.cn/api/Patent/CnWGImage/" + appNo; JSONObject configObject = this.getConfigObject(4, 2); String appId = configObject.getString("appId"); String appKey = configObject.getString("appKey"); Long currentTimeMillis = System.currentTimeMillis() / 1000; String Sign = appId + appKey + currentTimeMillis.toString(); String signMd5 = FormatUtil.MD5(Sign); // 创建一个OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .dns(new XDns(100000)) .build(); // 创建一个请求对象 Request request = new Request.Builder().url(url) .addHeader("_appid", appId) .addHeader("_timestamp", currentTimeMillis.toString()) .addHeader("_sign", signMd5) .get().build(); // 发送请求获取响应 try { Response response = okHttpClient.newCall(request).execute(); // 判断请求是否成功 if (response.isSuccessful()) { // 打印服务端返回结果 return Objects.requireNonNull(response.body()).string(); } else { response.close(); } } catch (IOException e) { e.printStackTrace(); } return "{}"; } /** * @param appNo * @return * @throws IOException * @author 李仁杰 * 从专利之星获取中国专利法律状态 */ public String getCnLegalApi(String appNo) { String url = "https://api.patentstar.com.cn/api/Patent/CnLegal/" + appNo; JSONObject configObject = this.getConfigObject(4, 2); String appId = configObject.getString("appId"); String appKey = configObject.getString("appKey"); Long currentTimeMillis = System.currentTimeMillis() / 1000; String Sign = appId + appKey + currentTimeMillis.toString(); String signMd5 = FormatUtil.MD5(Sign); // 创建一个OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .dns(new XDns(100000)) .build(); // 创建一个请求对象 Request request = new Request.Builder().url(url) .addHeader("_appid", appId) .addHeader("_timestamp", currentTimeMillis.toString()) .addHeader("_sign", signMd5) .get().build(); // 发送请求获取响应 try { Response response = okHttpClient.newCall(request).execute(); // 判断请求是否成功 if (response.isSuccessful()) { // 打印服务端返回结果 return Objects.requireNonNull(response.body()).string(); } } catch (IOException e) { e.printStackTrace(); } return "{}"; } /** * @param appNo * @return * @throws IOException * @author 李仁杰 * 从专利之星获取中国专利全文图片 */ public String getCnPdfApi(String appNo) throws IOException { String url = "https://api.patentstar.com.cn/api/Patent/CnPdf/" + appNo; JSONObject configObject = this.getConfigObject(4, 2); String appId = configObject.getString("appId"); String appKey = configObject.getString("appKey"); Long currentTimeMillis = System.currentTimeMillis() / 1000; String Sign = appId + appKey + currentTimeMillis.toString(); String signMd5 = FormatUtil.MD5(Sign); // 创建一个OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .dns(new XDns(100000)) .build(); // 创建一个请求对象 Request request = new Request.Builder().url(url) .addHeader("_appid", appId) .addHeader("_timestamp", currentTimeMillis.toString()) .addHeader("_sign", signMd5) .get().build(); // 发送请求获取响应 try { Response response = okHttpClient.newCall(request).execute(); // 判断请求是否成功 if (response.isSuccessful()) { // 打印服务端返回结果 return Objects.requireNonNull(response.body()).string(); } else { response.close(); } } catch (IOException e) { e.printStackTrace(); } return "{}"; } /** * @param appNo * @return * @throws IOException * @author 李仁杰 * 从专利之星获取中国专利全文文本 */ public String getCnFullXmlApi(String appNo) throws IOException { String url = "https://api.patentstar.com.cn/api/Patent/CnFullXml/" + appNo; JSONObject configObject = this.getConfigObject(4, 2); String appId = configObject.getString("appId"); String appKey = configObject.getString("appKey"); Long currentTimeMillis = System.currentTimeMillis() / 1000; String Sign = appId + appKey + currentTimeMillis.toString(); String signMd5 = FormatUtil.MD5(Sign); // 创建一个OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .dns(new XDns(100000)) .build(); // 创建一个请求对象 Request request = new Request.Builder().url(url) .addHeader("_appid", appId) .addHeader("_timestamp", currentTimeMillis.toString()) .addHeader("_sign", signMd5) .get().build(); // 发送请求获取响应 try { Response response = okHttpClient.newCall(request).execute(); // 判断请求是否成功 if (response.isSuccessful()) { // 打印服务端返回结果 return Objects.requireNonNull(response.body()).string(); } } catch (IOException e) { e.printStackTrace(); } return "{}"; } //获取权利要求文本 public String getExternalClaim(String str) throws DocumentException { SAXReader reader = new SAXReader(); reader.setEntityResolver(new IgnoreDTDEntityResolver()); Reader stringReader = new StringReader(str); Document document = reader.read(stringReader); XPath xpath = document.createXPath("//claim-text"); List element = (List) xpath.selectNodes(document); if (element.size() == 0) { xpath = document.createXPath("//business:ClaimText"); element = (List) xpath.selectNodes(document); } List reClaims = new ArrayList<>(); element.forEach(item -> { String claim = item.getText(); claim = claim.replaceAll("\r\n|\r|\n| ", ""); reClaims.add(claim); }); String reClaim = ""; if (reClaims.size() != 0) { reClaim = cn.cslg.pas.common.utils.StringUtils.join(reClaims, ""); } return reClaim; } //获取说明书文本 public String getFullText(String str) throws DocumentException { SAXReader reader = new SAXReader(); reader.setEntityResolver(new IgnoreDTDEntityResolver()); Reader stringReader = new StringReader(str); Document document = reader.read(stringReader); XPath xpath = document.createXPath("//description//p"); List elements = (List) xpath.selectNodes(document); if (elements.size() == 0) { xpath = document.createXPath("//business:Description//base:Paragraphs"); elements = (List) xpath.selectNodes(document); } List reStrs = new ArrayList<>(); Integer count = 1; for (Element item : elements) { String claim = item.getText().replaceAll("
", ""); if (!claim.trim().equals("技术领域") && !claim.trim().equals("发明内容") && !claim.trim().equals("附图说明") && !claim.trim().equals("具体实施方式") && !claim.trim().equals("背景技术")) { String formattedNum = String.format("%04d", count); claim = "[" + formattedNum + "]" + claim; count++; } reStrs.add(claim); } String reStr = ""; if (reStrs.size() != 0) { reStr = StringUtils.join(reStrs, "\r\n"); } return reStr; } /** * @param patentNo * @return * @throws IOException * @author 李仁杰 * 从专利之星获取世界专利pdf */ public String getEnPdfApi(String patentNo) { String url = " https://api.patentstar.com.cn/api/Patent/EnPdf/" + patentNo; JSONObject configObject = this.getConfigObject(4, 2); String appId = configObject.getString("appId"); String appKey = configObject.getString("appKey"); // String appId = "1000046"; // String appkey = "6AE6D4DC6AF94F26862501EDEE9E27A2"; Long currentTimeMillis = System.currentTimeMillis() / 1000; String Sign = appId + appKey + currentTimeMillis.toString(); String signMd5 = FormatUtil.MD5(Sign); // 创建一个OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .dns(new XDns(100000)) .build(); // 创建一个请求对象 Request request = new Request.Builder().url(url) .addHeader("_appid", appId) .addHeader("_timestamp", currentTimeMillis.toString()) .addHeader("_sign", signMd5) .get().build(); // 发送请求获取响应 try { Response response = okHttpClient.newCall(request).execute(); // 判断请求是否成功 if (response.isSuccessful()) { // 打印服务端返回结果 return Objects.requireNonNull(response.body()).string(); } else { response.close(); } } catch (IOException e) { e.printStackTrace(); } return "{}"; } /** * @param patentNo * @return * @throws IOException * @author 李仁杰 * 从专利之星获取世界专利著录信息 */ public String getENBibApi(String patentNo) { String url = "https://api.patentstar.com.cn/api/Patent/EnBib/" + patentNo; JSONObject configObject = this.getConfigObject(4, 2); String appId = configObject.getString("appId"); String appKey = configObject.getString("appKey"); Long currentTimeMillis = System.currentTimeMillis() / 1000; String Sign = appId + appKey + currentTimeMillis.toString(); String signMd5 = FormatUtil.MD5(Sign); // 创建一个OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .dns(new XDns(100000)) .build(); // 创建一个请求对象 Request request = new Request.Builder().url(url) .addHeader("_appid", appId) .addHeader("_timestamp", currentTimeMillis.toString()) .addHeader("_sign", signMd5) .get().build(); // 发送请求获取响应 try { Response response = okHttpClient.newCall(request).execute(); // 判断请求是否成功 if (response.isSuccessful()) { // 打印服务端返回结果 return Objects.requireNonNull(response.body()).string(); } } catch (IOException e) { e.printStackTrace(); } return "{}"; } /** * @param patentNo * @return * @throws IOException * @author 李仁杰 * 从专利之星获取同族专利 */ public String getFamilyByPubNoApi(String patentNo) { String url = "https://api.patentstar.com.cn/api/Patent/FamilyByPubNo/" + patentNo; JSONObject configObject = this.getConfigObject(4, 2); String appId = configObject.getString("appId"); String appKey = configObject.getString("appKey"); Long currentTimeMillis = System.currentTimeMillis() / 1000; String Sign = appId + appKey + currentTimeMillis.toString(); String signMd5 = FormatUtil.MD5(Sign); // 创建一个OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .dns(new XDns(100000)) .build(); // 创建一个请求对象 Request request = new Request.Builder().url(url) .addHeader("_appid", appId) .addHeader("_timestamp", currentTimeMillis.toString()) .addHeader("_sign", signMd5) .get().build(); // 发送请求获取响应 try { Response response = okHttpClient.newCall(request).execute(); // 判断请求是否成功 if (response.isSuccessful()) { // 打印服务端返回结果 return Objects.requireNonNull(response.body()).string(); } } catch (IOException e) { e.printStackTrace(); } return "{}"; } /** * @param patentNo * @return * @throws IOException * @author 李仁杰 * 获得专利被引用次数api */ public String getCitedNumByPubNoApi(String patentNo) throws IOException { String url = "https://api.patentstar.com.cn/api/Patent/CitedNumByPubNo/" + patentNo; JSONObject configObject = this.getConfigObject(4, 2); String appId = configObject.getString("appId"); String appKey = configObject.getString("appKey"); Long currentTimeMillis = System.currentTimeMillis() / 1000; String Sign = appId + appKey + currentTimeMillis.toString(); String signMd5 = FormatUtil.MD5(Sign); // 创建一个OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .dns(new XDns(100000)) .build(); // 创建一个请求对象 Request request = new Request.Builder().url(url) .addHeader("_appid", appId) .addHeader("_timestamp", currentTimeMillis.toString()) .addHeader("_sign", signMd5) .get().build(); // 发送请求获取响应 try { Response response = okHttpClient.newCall(request).execute(); // 判断请求是否成功 if (response.isSuccessful()) { // 打印服务端返回结果 return Objects.requireNonNull(response.body()).string(); } } catch (IOException e) { e.printStackTrace(); } return "{}"; } public JSONObject getConfigObject(Integer webId, Integer webGroup) { WebLoginConfig webLoginConfig = webLoginConfigService.getConfig(webId, webGroup); if (webLoginConfig == null) { return null; } String config = webLoginConfig.getWebConfig(); JSONObject jsonObject = JSONObject.parseObject(config); return jsonObject; } public List splitPatentNoByType(List originalList, PatentStarListDTO patentStarListDTO) { List cnStrings = new ArrayList<>(); List wdStrings = new ArrayList<>(); List> result = new ArrayList<>(); result.add(cnStrings); result.add(wdStrings); // 记录CN类型的字符串数量 int cnCount = 0; // 记录WD类型的字符串数量 int wdCount = 0; List patentStarListDTOS = new ArrayList<>(); //遍历初始集合 for (String str : originalList) { if (str.startsWith("CN")) { cnStrings.add(str); cnCount++; if (cnCount >= 100) { cnStrings = new ArrayList<>(); result.add(cnStrings); cnCount = 0; } } else { wdStrings.add(str); wdCount++; if (wdCount >= 100) { wdStrings = new ArrayList<>(); result.add(wdStrings); wdCount = 0; } } } result.forEach(item -> { if (item.size() > 0) { String dbType = "CN"; String patentNo = item.get(0); if (!patentNo.startsWith("CN")) { dbType = "WD"; } String join = StringUtils.join(item, " OR "); String conditions = "AN=(" + join + ") OR PN=(" + join + ") OR GN=(" + join + ")"; PatentStarListDTO patentStarListDTO1 = new PatentStarListDTO(); patentStarListDTO1.setOrderByType(patentStarListDTO.getOrderByType()); patentStarListDTO1.setCurrentQuery(conditions); patentStarListDTO1.setPageNum(patentStarListDTO.getPageNum()); patentStarListDTO1.setDBType(dbType); patentStarListDTO1.setCurrentQuery(conditions); patentStarListDTO1.setRowCount(50); patentStarListDTO1.setTotal(item.size()); patentStarListDTO1.setNos(item); patentStarListDTOS.add(patentStarListDTO1); } }); return patentStarListDTOS; } public StarPatentVO getPatentByNo(String patentNo) { StarPatentVO starPatentVO = null; String condition = "AN=(" + patentNo + ") OR PN=(" + patentNo + ") OR GN=(" + patentNo + ") OR ANO=(" + patentNo + ")"; String dbType = "CN"; if (!patentNo.startsWith("CN")) { dbType = "WD"; } PatentStarListDTO patentStarListDTO = new PatentStarListDTO(); patentStarListDTO.setCurrentQuery(condition); patentStarListDTO.setPageNum(1); patentStarListDTO.setRowCount(50); patentStarListDTO.setDBType(dbType); try { Map resultMap = this.patentStarSearchApi(patentStarListDTO); if (resultMap == null || (Integer) resultMap.get("total") == 0) { return starPatentVO; } List starPatents = (List) resultMap.get("records"); starPatentVO = starPatents.get(0); } catch (Exception e) { } return starPatentVO; } private List loadPatent(List starPatentVOS) { List patentColumnDTOS = new ArrayList<>(); starPatentVOS.forEach(item -> { PatentColumnDTO patentColumnDTO = new PatentColumnDTO(); //申请号 patentColumnDTO.setAppNo(item.getApplicationNo()); //装载公开号 patentColumnDTO.setPublicNo(item.getPublicNo()); //装载公告号 patentColumnDTO.setGrantNo(item.getPublicAccreditNo()); //装载专利状态 if (item.getLG() != null) { patentColumnDTO.setSimpleStatus(item.getLG().toString()); } //装载专利类型 if (item.getPatentType() != null) { patentColumnDTO.setPatentType(item.getPatentType().toString()); } //装载摘要 List abstractList = new ArrayList<>(); Text text = new Text(); text.setTextContent(item.getAbstractStr()); text.setIfOrigin(true); abstractList.add(text); patentColumnDTO.setAbstractStr(abstractList); //装载标题 List titleList = new ArrayList<>(); Text title = new Text(); title.setTextContent(item.getName()); title.setIfOrigin(true); titleList.add(title); // text.setLanguage(patent.getAppCountry()); patentColumnDTO.setTitle(titleList); //装载权利要求 List claimList = new ArrayList<>(); Text claim = new Text(); claim.setTextContent(item.getClaimStr()); claim.setIfOrigin(true); claimList.add(claim); patentColumnDTO.setClaim(claimList); //装载专利号 if (patentColumnDTO.getAppNo() != null) { if (patentColumnDTO.getAppNo().trim().startsWith("CN")) { patentColumnDTO.setPatentNo(patentColumnDTO.getAppNo()); } else { if (patentColumnDTO.getPublicNo() != null) { patentColumnDTO.setPatentNo(patentColumnDTO.getPublicNo()); } else if (patentColumnDTO.getGrantNo() != null) { patentColumnDTO.setPatentNo(patentColumnDTO.getGrantNo()); } else { patentColumnDTO.setPatentNo(patentColumnDTO.getAppNo()); } } } //装载申请日 if (item.getApplicationDate() != null && !item.getApplicationDate().trim().equals("")) { patentColumnDTO.setAppDate(DateUtils.strToDate(item.getApplicationDate())); } // patentColumnDTO.setAppCountry(item.getBureau()); //装载申请人地址 PersonAddress appAddress = new PersonAddress(); appAddress.setAddress(item.getApplicationAddress()); appAddress.setProvince(item.getProvinceStr()); patentColumnDTO.setApplicantAddr(appAddress); patentColumnDTO.setAppAddress(item.getApplicationAddress()); // patentColumnDTO.setApplicantCountry(item.getProvinceStr()); String provinceStr = item.getProvinceStr(); if (provinceStr.contains("(") && provinceStr.contains(")")) { String substring = provinceStr.substring(provinceStr.indexOf("(") + 1, provinceStr.indexOf(")")); patentColumnDTO.setAppProvince(substring); } //装载ipc(主)分类号 PatentClassify mipcClassify = new PatentClassify(); mipcClassify.setLevel5(item.getMainIpc()); patentColumnDTO.setMipc(mipcClassify); //装载ipc分类号 List classifies = new ArrayList<>(); String[] split = item.getIpcListStr().split(";"); for (String ipc : split) { PatentClassify ipcClassify = new PatentClassify(); ipcClassify.setLevel5(ipc); } patentColumnDTO.setIpc(classifies); //获取摘要附图 // String pictureApi = this.getPictureApi(item.getRowApplicationNo()); // patentColumnDTO.setPictureGuid(pictureApi); //获取外部专利原始申请号 patentColumnDTO.setRowApplicationNo(item.getRowApplicationNo()); //获取法律状态 // String cnLegalApi = this.getCnLegalApi(item.getRowApplicationNo()); // patentColumnDTO.setLegalStatus(Arrays.asList(cnLegalApi)); //获取被引用次数 //装载公开日 if (item.getPublicDate() != null && !item.getPublicDate().trim().equals("")) { patentColumnDTO.setPublicDate(DateUtils.strToDate(item.getPublicDate())); } //装载公开日 if (item.getPublicAccreditDate() != null && !item.getPublicAccreditDate().trim().equals("")) { patentColumnDTO.setGrantDate(DateUtils.strToDate(item.getPublicAccreditDate())); } //装载申请人 if (item.getApplicantStr() != null && !item.getApplicantStr().trim().equals("")) { List names = Arrays.asList(item.getApplicantStr().split(";")); patentColumnDTO.setApplicant(names); } else { patentColumnDTO.setApplicant(new ArrayList<>()); } //装载发明人 if (item.getInventorStr() != null && !item.getInventorStr().trim().equals("")) { List names = Arrays.asList(item.getInventorStr().split(";")); patentColumnDTO.setInventor(names); } else { patentColumnDTO.setInventor(new ArrayList<>()); } //装载权利人 if (item.getCurrentApplicantStr() != null && !item.getCurrentApplicantStr().trim().equals("")) { List names = Arrays.asList(item.getCurrentApplicantStr().split(";")); patentColumnDTO.setRightHolder(names); } else { patentColumnDTO.setRightHolder(new ArrayList<>()); } //装载代理人 if (item.getAgentStr() != null && !item.getAgentStr().trim().equals("")) { List names = Arrays.asList(item.getAgentStr().split(";")); patentColumnDTO.setAgent(names); } else { patentColumnDTO.setAgent(new ArrayList<>()); } //装载代理机构 if (item.getAgencyStr() != null && !item.getAgencyStr().trim().equals("")) { patentColumnDTO.setAgency(item.getAgencyStr()); } //装载优先权 patentColumnDTO.setPriorityNo(item.getPriorityNo()); patentColumnDTO.setPriorityDate(item.getPriorityDate()); patentColumnDTO.setAppCountry(item.getBureau()); patentColumnDTOS.add(patentColumnDTO); }); return patentColumnDTOS; } /** * 根据专利号查询外部权利要求 * * @param appNo * @return * @throws IOException */ public SelectClaimDTO queryExternalClaim(String appNo) throws IOException { List claimList = new ArrayList<>(); //获取公开说明书 String cnFullXmlApi = null; try { cnFullXmlApi = this.getCnFullXmlApi(appNo); } catch (IOException e) { e.printStackTrace(); } try { String fullText = this.getExternalClaim(cnFullXmlApi); Text pubText = new Text(); if (fullText != null && !fullText.trim().equals("")) { fullText = PatentRightUtils.getFormatClaim(fullText, "CN"); } pubText.setTextContent(fullText); pubText.setIfOrigin(true); claimList.add(pubText); } catch (DocumentException e) { e.printStackTrace(); } SelectClaimDTO dto = new SelectClaimDTO(); if (!CollectionUtils.isEmpty(claimList)) { dto.setClaimContent(claimList); } return dto; } /** * 根据专利号查询外部权利要求 * * @param vo * @return * @throws IOException */ public PatentColumnDTO queryExternalDetail(QueryExternalFamilyVO vo) throws IOException { PatentStarListDTO patentStarListDTO = new PatentStarListDTO(); String condition = "AN=" + vo.getPatentNo() + " OR " + "PN=" + vo.getPatentNo(); patentStarListDTO.setCurrentQuery(condition); String index = vo.getPatentNo().substring(0, 2); if (index.equals("CN")) { patentStarListDTO.setDBType("CN"); } else { patentStarListDTO.setDBType("WD"); } patentStarListDTO.setOrderBy("AD"); patentStarListDTO.setOrderByType("DESC"); patentStarListDTO.setPageNum(1); patentStarListDTO.setRowCount(10); Records records = this.patentStarSearchLocal(patentStarListDTO); Object data = records.getData(); String s = JSONArray.toJSONString(data); List list = JSONArray.parseArray(s, PatentColumnDTO.class); PatentColumnDTO dto = new PatentColumnDTO(); if (!CollectionUtils.isEmpty(list)) { for (PatentColumnDTO columnDTO : list) { BeanUtils.copyProperties(columnDTO, dto); } } return dto; } /** * 查询外部专利同族专利 * * @param vo * @return * @throws IOException */ public Records queryExternalFamily(QueryExternalFamilyVO vo) throws IOException { Records records = new Records(); String family = this.getFamilyByPubNoApi(vo.getPatentNo()); QueryExternalFamilyDTO familyDTO = JSONObject.parseObject(family, QueryExternalFamilyDTO.class); String familyInfos = familyDTO.getFamilyinfo(); List publicNos = new ArrayList<>(); if (StringUtils.isNotEmpty(familyInfos) && !familyInfos.equals("{}")) { publicNos = Arrays.asList(familyInfos.split(";")); } List list = new ArrayList<>(); if (!CollectionUtils.isEmpty(publicNos)) { for (String publicNo : publicNos) { PatentStarListDTO patentStarListDTO = new PatentStarListDTO(); String condition = "PN=" + publicNo; patentStarListDTO.setCurrentQuery(condition); String index = publicNo.substring(0, 2); if (index.equals("CN")) { patentStarListDTO.setDBType("CN"); } else { patentStarListDTO.setDBType("WD"); } patentStarListDTO.setOrderBy("AD"); patentStarListDTO.setOrderByType("DESC"); patentStarListDTO.setPageNum(vo.getPageNum()); patentStarListDTO.setRowCount(vo.getPageSize()); records = this.patentStarSearchLocal(patentStarListDTO); list.addAll((Collection) records.getData()); } } Records record = new Records(); record.setCurrent(vo.getPageNum().longValue()); record.setSize(vo.getPageSize().longValue()); record.setData(list); record.setTotal(Long.valueOf(String.valueOf(list.size()))); return record; } /** * 获取公开说明书 * * @param appNo * @return */ public List getCnFullText(String appNo) { List pubTextList = new ArrayList<>(); //获取公开说明书 String cnFullXmlApi = null; try { cnFullXmlApi = this.getCnFullXmlApi(appNo); } catch (IOException e) { e.printStackTrace(); } try { String fullText = this.getFullText(cnFullXmlApi); Text pubText = new Text(); pubText.setTextContent(fullText); pubText.setIfOrigin(true); pubTextList.add(pubText); } catch (DocumentException e) { e.printStackTrace(); } return pubTextList; } /** * 获得中国专利pdf全文 * * @param appNo * @return * @throws IOException */ public List getExternalTextPdf(String appNo, String rowApplicationNo, String publicNo, String grantNo) throws IOException { List list = new ArrayList<>(); String pdfUrl = "", pdfUrl1 = "", pdfUrl2 = ""; if (StringUtils.isNotEmpty(appNo) && appNo.contains("CN")) { String pdfUrlStr = this.getCnPdfApi(rowApplicationNo); //若没有取到说明书pdf,则将当前申请号和备注信息存入问题记录表,并返回空对象 if (StringUtils.isNotEmpty(pdfUrlStr)) { if (pdfUrlStr.contains("|http")) { //若包含公开和授权两个pdf String[] pdfUrlArr = pdfUrlStr.split("\\|http"); pdfUrl1 = pdfUrlArr[0].substring(pdfUrlArr[0].indexOf("http"), pdfUrlArr[0].indexOf("?")); pdfUrlArr[1] = "http" + pdfUrlArr[1]; pdfUrl2 = pdfUrlArr[1].substring(pdfUrlArr[1].indexOf("http"), pdfUrlArr[1].indexOf("?")); if (pdfUrl1.contains("0A_CN_0.pdf")) { InnerPatentPdfDTO dto = new InnerPatentPdfDTO(); dto.setPdfGuid(pdfUrl1); dto.setType(0); list.add(dto); //授权说明书pdf InnerPatentPdfDTO dto1 = new InnerPatentPdfDTO(); dto1.setPdfGuid(pdfUrl2); dto1.setType(1); list.add(dto1); } else { InnerPatentPdfDTO dto1 = new InnerPatentPdfDTO(); dto1.setPdfGuid(pdfUrl1); dto1.setType(1); list.add(dto1); InnerPatentPdfDTO dto = new InnerPatentPdfDTO(); dto.setPdfGuid(pdfUrl2); dto.setType(0); list.add(dto); } } else { //若只有一个 pdfUrl1 = pdfUrlStr.substring(pdfUrlStr.indexOf("http")); if (pdfUrl1.contains("0A_CN_0.pdf")) { InnerPatentPdfDTO dto = new InnerPatentPdfDTO(); dto.setPdfGuid(pdfUrl1); dto.setType(0); list.add(dto); } else { InnerPatentPdfDTO dto = new InnerPatentPdfDTO(); dto.setPdfGuid(pdfUrl1); dto.setType(1); list.add(dto); } } } } else { String usedPatentNo; if (publicNo != null && !publicNo.equals("")) { usedPatentNo = publicNo; } else { usedPatentNo = grantNo; } if (usedPatentNo != null) { pdfUrl = this.getEnPdfApi(usedPatentNo); if (!pdfUrl.equals("")) { InnerPatentPdfDTO dto = new InnerPatentPdfDTO(); dto.setPdfGuid(pdfUrl); dto.setType(0); list.add(dto); } } } return list; } /** * 获取附图 * * @param appNo * @return */ public List getExternalFigure(String appNo) throws IOException { List list = new ArrayList<>(); String wgPictureApi = this.getWGPictureApi(appNo); if (StringUtils.isNotEmpty(wgPictureApi)) { if (wgPictureApi.contains("|http")) { String[] urlArr = wgPictureApi.split("\\|"); list.addAll(Arrays.asList(urlArr)); } else { list.add(wgPictureApi); } } return list; } /** * 获取摘要附图 * * @param appNo * @return */ public String getPictureGuid(String appNo) { return this.getPictureApi(appNo); } /** * @param appNo * @return */ public ExternalLegalInfoDTO getLegalStatus(String appNo) throws ParseException { ExternalLegalInfoDTO infoDTO = new ExternalLegalInfoDTO(); List list = new ArrayList<>(); List legalEvents = new ArrayList<>(); String cnLegalApi = this.getCnLegalApi(appNo); if (StringUtils.isNotEmpty(cnLegalApi) && !cnLegalApi.equals("{}") && !cnLegalApi.contains("408")) { List statusDTOS = JSONArray.parseArray(cnLegalApi, ExternalLegalStatusDTO.class); if (!CollectionUtils.isEmpty(statusDTOS)) { List statusList = statusDTOS.stream().sorted(Comparator.comparing(ExternalLegalStatusDTO::getLegalDate).reversed()).collect(Collectors.toList()); if (!CollectionUtils.isEmpty(statusList)) { ExternalLegalStatusDTO statusDTO = statusList.get(0); list.add(statusDTO.getLegalStatus()); } for (ExternalLegalStatusDTO dto : statusDTOS) { LegalEvent event = new LegalEvent(); event.setCode(dto.getLegalCode()); event.setName(dto.getLegalStatus()); event.setDescription(dto.getLegalStatusInfo()); if (StringUtils.isNotEmpty(dto.getLegalDate())) { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); Date date = format.parse(dto.getLegalDate()); event.setEventDate(date); } legalEvents.add(event); } } } infoDTO.setLegalStatus(list); infoDTO.setLegalEvents(legalEvents); return infoDTO; } public SelectClaimDTO getOrginCalim(String appNo) throws IOException { List claimList = new ArrayList<>(); //获取公开说明书 String cnFullXmlApi = null; try { cnFullXmlApi = this.getCnFullXmlApi(appNo); } catch (IOException e) { e.printStackTrace(); } try { String fullText = this.getExternalClaim(cnFullXmlApi); Text pubText = new Text(); pubText.setTextContent(fullText); pubText.setIfOrigin(true); claimList.add(pubText); } catch (DocumentException e) { e.printStackTrace(); } SelectClaimDTO dto = new SelectClaimDTO(); if (!CollectionUtils.isEmpty(claimList)) { dto.setClaimContent(claimList); } return dto; } }