PatentStarApiService.java 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. package cn.cslg.pas.service.common;
  2. import cn.cslg.pas.common.config.XDns;
  3. import cn.cslg.pas.common.core.IgnoreDTDEntityResolver;
  4. import cn.cslg.pas.common.dto.*;
  5. import cn.cslg.pas.common.dto.business.SelectClaimDTO;
  6. import cn.cslg.pas.common.model.cronModel.PersonnelVO;
  7. import cn.cslg.pas.common.model.cronModel.Records;
  8. import cn.cslg.pas.common.model.importTaskModel.PatentApplicant;
  9. import cn.cslg.pas.common.utils.*;
  10. import cn.cslg.pas.common.vo.ContentVO;
  11. import cn.cslg.pas.common.vo.NoCacheVO;
  12. import cn.cslg.pas.common.vo.QueryExternalFamilyVO;
  13. import cn.cslg.pas.common.vo.StarPatentVO;
  14. import cn.cslg.pas.common.vo.business.PatentNoVO;
  15. import cn.cslg.pas.domain.WebLoginConfig;
  16. import cn.cslg.pas.domain.business.RetrieveRecord;
  17. import cn.cslg.pas.domain.es.*;
  18. import cn.cslg.pas.service.WebLoginConfigService;
  19. import cn.cslg.pas.service.importPatent.WebVOTransformService;
  20. import cn.cslg.pas.service.novelty.AssoRetrieveRecordProjectService;
  21. import cn.cslg.pas.service.query.FormatQueryService;
  22. import com.alibaba.fastjson.JSONArray;
  23. import com.alibaba.fastjson.JSONObject;
  24. import com.alibaba.fastjson2.JSON;
  25. import com.sun.istack.NotNull;
  26. import lombok.RequiredArgsConstructor;
  27. import lombok.experimental.Accessors;
  28. import lombok.extern.slf4j.Slf4j;
  29. import okhttp3.*;
  30. import okhttp3.Response;
  31. import org.apache.commons.lang3.ObjectUtils;
  32. import org.dom4j.Document;
  33. import org.dom4j.DocumentException;
  34. import org.dom4j.Element;
  35. import org.dom4j.XPath;
  36. import org.dom4j.io.SAXReader;
  37. import org.joda.time.DateTime;
  38. import org.joda.time.format.DateTimeFormat;
  39. import org.joda.time.format.DateTimeFormatter;
  40. import org.springframework.beans.BeanUtils;
  41. import org.springframework.beans.factory.annotation.Autowired;
  42. import org.springframework.context.ApplicationContext;
  43. import org.springframework.context.annotation.Lazy;
  44. import org.springframework.stereotype.Service;
  45. import org.springframework.util.CollectionUtils;
  46. import org.springframework.web.multipart.MultipartFile;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.io.Reader;
  50. import java.io.StringReader;
  51. import java.text.ParseException;
  52. import java.text.SimpleDateFormat;
  53. import java.util.*;
  54. import java.util.concurrent.TimeUnit;
  55. import java.util.regex.Matcher;
  56. import java.util.regex.Pattern;
  57. import java.util.stream.Collectors;
  58. /**
  59. * 调用外部接口的Service类 PCS:权限系统
  60. *
  61. * @Author chenyu
  62. * @Date 2023/4/25
  63. */
  64. @RequiredArgsConstructor
  65. @Slf4j
  66. @Service
  67. public class PatentStarApiService {
  68. @Autowired
  69. private RetrieveRecordService retrieveRecordService;
  70. @Autowired
  71. private CacheUtils cacheUtils;
  72. @Autowired
  73. private LoginUtils loginUtils;
  74. @Autowired
  75. private WebLoginConfigService webLoginConfigService;
  76. @Autowired
  77. private FormatQueryService formatQueryService;
  78. @Autowired
  79. @Lazy
  80. private WebVOTransformService webVOTransformService;
  81. @Autowired
  82. private AssoRetrieveRecordProjectService assoRetrieveRecordProjectService;
  83. @Autowired
  84. private NOSCacheService cacheService;
  85. @Autowired
  86. private FileManagerService fileManagerService;
  87. public Records patentStarSearchLocal(PatentStarListDTO patentStarListDTO) throws IOException {
  88. RetrieveRecord retrieveRecord = retrieveRecordService.setRetrieveRecord(patentStarListDTO);
  89. String redisKey = patentStarListDTO.getRedisKey();
  90. List<String> nos = new ArrayList<>();
  91. Long total = 0L;
  92. if (StringUtils.isNotEmpty(patentStarListDTO.getNumberQuery())) {
  93. NoCacheVO cacheVO = cacheService.savaByList(patentStarListDTO);
  94. retrieveRecord.setConditions(patentStarListDTO.getNumberQuery());
  95. retrieveRecord.setSearchType(1);
  96. redisKey = cacheVO.getKey();
  97. total = cacheVO.getTotal();
  98. nos.addAll(cacheVO.getList());
  99. }
  100. if (Boolean.TRUE.equals(StringUtils.isEmpty(patentStarListDTO.getNumberQuery())) && StringUtils.isNotEmpty(redisKey)) {
  101. NoCacheVO cacheVO = cacheService.getByList(patentStarListDTO);
  102. retrieveRecord.setConditions(patentStarListDTO.getNumberQuery());
  103. retrieveRecord.setSearchType(1);
  104. nos.addAll(cacheVO.getList());
  105. total = cacheVO.getTotal();
  106. redisKey = cacheVO.getKey();
  107. }
  108. long start = System.currentTimeMillis();
  109. Map<String, Object> map = this.patentStarSearchApi(patentStarListDTO);
  110. long end = System.currentTimeMillis();
  111. System.out.println("search time:" + (end - start));
  112. if (map == null) {
  113. ThrowException.throwXiaoShiException("检索失败,请检查检索式");
  114. }
  115. retrieveRecord.setTotalNum(Integer.parseInt(map.get("total").toString()));
  116. if (patentStarListDTO.getRetrieveRecordId() == null) {
  117. retrieveRecord.insert();
  118. Integer projectId = patentStarListDTO.getProjectId();
  119. if (projectId != null) {
  120. assoRetrieveRecordProjectService.add(retrieveRecord.getId(), projectId);
  121. }
  122. } else {
  123. retrieveRecord = retrieveRecordService.getById(patentStarListDTO.getRetrieveRecordId());
  124. retrieveRecord.setRetrieveTime(new Date());
  125. retrieveRecord.updateById();
  126. }
  127. List<StarPatentVO> starPatentVOS = (List<StarPatentVO>) map.get("records");
  128. //装载starPatentVOS到 PatentColumnDTO
  129. Records records = new Records();
  130. records.setRetrieveRecordId(retrieveRecord.getId());
  131. records.setCurrent(Long.parseLong(map.get("current").toString()));
  132. records.setSize(Long.parseLong(map.get("size").toString()));
  133. // records.setData(this.loadPatent(starPatentVOS, patentStarListDTO.getDBType()));
  134. // records.setTotal(Long.parseLong(map.get("total").toString()));
  135. List<PatentColumnDTO> columnDTOS = this.loadPatent(starPatentVOS, patentStarListDTO.getDBType());
  136. records.setData(this.loadPatentList(columnDTOS, nos, patentStarListDTO.getDBType()));
  137. if (total != 0) {
  138. records.setTotal(total);
  139. } else {
  140. records.setTotal(Long.parseLong(map.get("total").toString()));
  141. }
  142. records.setRedisKey(redisKey);
  143. return records;
  144. }
  145. /**
  146. * 格式化条件后并请求外部接口
  147. *
  148. * @param PatentStarListDTO
  149. * @return
  150. * @throws IOException
  151. */
  152. public Map<String, Object> patentStarSearchApi(PatentStarListDTO PatentStarListDTO) throws IOException {
  153. try {
  154. if (PatentStarListDTO.getFormed() == null || PatentStarListDTO.getFormed() == false) {
  155. String formQuery = PatentStarListDTO.getCurrentQuery();
  156. PatentStarListDTO.setCurrentQuery(formatQueryService.reQuery(formQuery, "webSearchConfig"));
  157. if (PatentStarListDTO.getDBType().equals("WD")) {
  158. StringBuilder stringBuilder = new StringBuilder(PatentStarListDTO.getCurrentQuery());
  159. stringBuilder.insert(PatentStarListDTO.getCurrentQuery().length() - 1, "-CN/GJ");
  160. PatentStarListDTO.setCurrentQuery(stringBuilder.toString());
  161. }
  162. PatentStarListDTO.setFormed(true);
  163. }
  164. } catch (Exception e) {
  165. e.printStackTrace();
  166. return null;
  167. }
  168. JSONObject configObject = this.getConfigObject(4, 1);
  169. String appId = configObject.getString("appId");
  170. String appKey = configObject.getString("appKey");
  171. PatentStarDTO patentStarDto = new PatentStarDTO();
  172. BeanUtils.copyProperties(PatentStarListDTO, patentStarDto);
  173. if (patentStarDto.getOrderBy() == null) {
  174. patentStarDto.setOrderBy("AD");
  175. patentStarDto.setOrderByType("DESC");
  176. }
  177. String json = JSONObject.toJSONString(patentStarDto);
  178. String url = "http://s.patentstar.com.cn/SearchAPI/PatentSearch/ResultGet";
  179. Long currentTimeMillis = System.currentTimeMillis() / 1000;
  180. String Sign = appKey + currentTimeMillis.toString();
  181. String signMd5 = FormatUtil.MD5(Sign);
  182. // 创建一个OkHttpClient对象
  183. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  184. .connectTimeout(5, TimeUnit.SECONDS)
  185. .writeTimeout(5, TimeUnit.SECONDS)
  186. .readTimeout(5, TimeUnit.SECONDS)
  187. .addInterceptor(new PatentStarApiService.OkhttpInterceptor(3))
  188. .build();
  189. // 创建一个RequestBody(参数1:数据类型 参数2传递的json串)
  190. FormBody.Builder builder = new FormBody.Builder();
  191. builder.add("AppID", appId);
  192. builder.add("Stamp", currentTimeMillis.toString());
  193. builder.add("Sign", signMd5);
  194. builder.add("QueryJson", json);
  195. FormBody requestBody = builder.build();
  196. // 创建一个请求对象
  197. Request request = new Request.Builder().url(url).post(requestBody).build();
  198. // 发送请求获取响应
  199. try {
  200. long start = System.currentTimeMillis();
  201. Response response = okHttpClient.newCall(request).execute();
  202. long end = System.currentTimeMillis();
  203. System.out.println("search11time:" + (end - start));
  204. // 判断请求是否成功
  205. if (response.isSuccessful()) {
  206. JSONObject jsonObject = JSONObject.parseObject(Objects.requireNonNull(response.body()).string());
  207. if (jsonObject.get("Ret").equals(500) || jsonObject.get("Data") == null) {
  208. return null;
  209. }
  210. JSONObject Data = (JSONObject) jsonObject.get("Data");
  211. List<StarPatentVO> starPatentVOS = JSONArray.parseArray(Data.get("List").toString(), StarPatentVO.class);
  212. Map<String, Object> reMap = new HashMap<>();
  213. reMap.put("size", PatentStarListDTO.getRowCount());
  214. reMap.put("current", PatentStarListDTO.getPageNum());
  215. reMap.put("records", starPatentVOS);
  216. reMap.put("total", Data.get("HitCount"));
  217. return reMap;
  218. }
  219. } catch (IOException e) {
  220. e.printStackTrace();
  221. return null;
  222. }
  223. return null;
  224. }
  225. private List<PatentColumnDTO> loadPatentList(List<PatentColumnDTO> columnDTOS, List<String> list, String dbType) {
  226. if (CollectionUtils.isEmpty(list) || CollectionUtils.isEmpty(columnDTOS)) {
  227. return list.isEmpty() ? columnDTOS : createNewDTOs(list);
  228. }
  229. List<PatentColumnDTO> result = new ArrayList<>(columnDTOS.size());
  230. Set<String> searchedNumbers = new HashSet<>(); // 用于跟踪已搜索的专利号
  231. for (String patentNo : list) {
  232. boolean found = false;
  233. String formattedNo = dbType.equals("CN") ? PatentNoUtil.formatApNo(patentNo) : patentNo;
  234. for (PatentColumnDTO columnDTO : columnDTOS) {
  235. if (containsNumber(columnDTO, formattedNo)) {
  236. PatentColumnDTO dto = new PatentColumnDTO();
  237. BeanUtils.copyProperties(columnDTO, dto);
  238. dto.setSearchNo(patentNo);
  239. result.add(dto);
  240. searchedNumbers.add(patentNo); // 标记为已搜索
  241. found = true;
  242. break; // 找到匹配项后跳出内层循环
  243. }
  244. }
  245. if (!found) {
  246. // 如果没有在columnDTOS中找到匹配项,则创建一个新的DTO
  247. PatentColumnDTO newDTO = new PatentColumnDTO();
  248. newDTO.setPatentNo(patentNo);
  249. newDTO.setSearchNo(patentNo);
  250. newDTO.setIfSearch(false);
  251. result.add(newDTO);
  252. }
  253. }
  254. return result;
  255. }
  256. private boolean containsNumber(PatentColumnDTO dto, String number) {
  257. String appNo = StringUtils.isNotEmpty(dto.getAppNo()) ? dto.getAppNo() : "";
  258. String publicNo = StringUtils.isNotEmpty(dto.getPublicNo()) ? dto.getPublicNo() : "";
  259. String grantNo = StringUtils.isNotEmpty(dto.getGrantNo()) ? dto.getGrantNo() : "";
  260. String rowAppNo = StringUtils.isNotEmpty(dto.getRowApplicationNo()) ? dto.getRowApplicationNo() : "";
  261. return appNo.contains(number) || publicNo.contains(number)
  262. || grantNo.contains(number) || rowAppNo.contains(number);
  263. }
  264. private List<PatentColumnDTO> createNewDTOs(List<String> list) {
  265. List<PatentColumnDTO> newDTOs = new ArrayList<>();
  266. for (String s : list) {
  267. PatentColumnDTO dto = new PatentColumnDTO();
  268. dto.setPatentNo(s);
  269. dto.setSearchNo(s);
  270. dto.setIfSearch(false);
  271. newDTOs.add(dto);
  272. }
  273. return newDTOs;
  274. }
  275. public String getFormatCondition(String condition) {
  276. String s = condition.substring(condition.indexOf("=") + 1);
  277. String s1 = s.replaceAll("[()]", "");
  278. String[] valueStrs = s1.split("[,,]|(\\r\\n)+|\\r+|\\n+");
  279. StringBuilder stringBuilder = new StringBuilder();
  280. for (int i = 0; i < valueStrs.length; i++) {
  281. final String value = valueStrs[i];
  282. if (i != valueStrs.length - 1) {
  283. stringBuilder.append(value).append(",");
  284. } else {
  285. stringBuilder.append(value);
  286. }
  287. }
  288. return stringBuilder.toString();
  289. }
  290. public List<PatentStarListDTO> getSplitedConditions(PatentStarListDTO patentStarListDTO, int patentNum) throws IOException {
  291. DateTime nowDate = new DateTime();
  292. DateTime date = DateUtils2.formStrToDateTime("1900-01-01");
  293. //超过1万的列表
  294. List<PatentStarListDTO> PatentStarListDTOs = new ArrayList<>();
  295. //返回的列表
  296. List<PatentStarListDTO> reDtos = new ArrayList<>();
  297. patentStarListDTO.setStartTime(date);
  298. patentStarListDTO.setEndTime(nowDate);
  299. patentStarListDTO.setOrginCondition(patentStarListDTO.getCurrentQuery());
  300. String formQuery = patentStarListDTO.getCurrentQuery();
  301. // patentStarListDTO.setOrginCondition(PatentStarApiService.formatQuery(formQuery));
  302. PatentStarListDTOs.add(patentStarListDTO);
  303. while (PatentStarListDTOs.size() > 0) {
  304. PatentStarListDTO dto = PatentStarListDTOs.get(0);
  305. PatentStarListDTOs.remove(dto);
  306. Map<String, Object> map1 = this.patentStarSearchApi(dto);
  307. Integer total1 = Integer.parseInt(map1.get("total").toString());
  308. dto.setTotal(total1);
  309. patentStarListDTO.setFormed(true);
  310. if (total1 > patentNum) {
  311. DateTime startTime1 = dto.getStartTime();
  312. DateTime endTime1 = dto.getEndTime();
  313. List<DateTime> dateTimes = DateUtils2.formStrToDateTime(startTime1, endTime1);
  314. PatentStarListDTO dto1 = new PatentStarListDTO();
  315. BeanUtils.copyProperties(dto, dto1);
  316. dto1.setStartTime(dateTimes.get(0));
  317. dto1.setEndTime(dateTimes.get(1));
  318. this.setConditions(dto1);
  319. PatentStarListDTO dto2 = new PatentStarListDTO();
  320. BeanUtils.copyProperties(dto, dto2);
  321. dto2.setStartTime(dateTimes.get(2));
  322. dto2.setEndTime(dateTimes.get(3));
  323. this.setConditions(dto2);
  324. PatentStarListDTOs.add(dto1);
  325. PatentStarListDTOs.add(dto2);
  326. } else if (total1 != 0) {
  327. reDtos.add(dto);
  328. }
  329. }
  330. return reDtos;
  331. }
  332. public void setConditions(PatentStarListDTO PatentStarListDTO) {
  333. PatentStarListDTO.setFormed(false);
  334. DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMdd");
  335. String startStr = formatter.print(PatentStarListDTO.getStartTime());
  336. String endStr = formatter.print(PatentStarListDTO.getEndTime());
  337. String cond = " AND AD=" + startStr + "~" + endStr;
  338. Integer len = PatentStarListDTO.getOrginCondition().length();
  339. StringBuilder stringBuilder = new StringBuilder(PatentStarListDTO.getOrginCondition());
  340. stringBuilder.insert(len - 1, cond);
  341. PatentStarListDTO.setCurrentQuery(stringBuilder.toString());
  342. }
  343. /**
  344. * @param appNo
  345. * @return
  346. * @throws IOException
  347. * @author 李仁杰
  348. * 从专利之星获取中国专利著录
  349. */
  350. public String getCnBibApi(String appNo) {
  351. Long start = System.currentTimeMillis();
  352. String url = "https://api.patentstar.com.cn/api/Patent/CnBibo/" + appNo;
  353. JSONObject configObject = this.getConfigObject(4, 2);
  354. String appId = configObject.getString("appId");
  355. String appKey = configObject.getString("appKey");
  356. Long currentTimeMillis = System.currentTimeMillis() / 1000;
  357. String Sign = appId + appKey + currentTimeMillis.toString();
  358. String signMd5 = FormatUtil.MD5(Sign);
  359. // 创建一个OkHttpClient对象
  360. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  361. .connectTimeout(5, TimeUnit.SECONDS)
  362. .writeTimeout(5, TimeUnit.SECONDS)
  363. .readTimeout(5, TimeUnit.SECONDS)
  364. .addInterceptor(new PatentStarApiService.OkhttpInterceptor(3))
  365. .dns(new XDns(100000))
  366. .build();
  367. // 创建一个请求对象
  368. Request request = new Request.Builder().url(url)
  369. .addHeader("_appid", appId)
  370. .addHeader("_timestamp", currentTimeMillis.toString())
  371. .addHeader("_sign", signMd5)
  372. .get().build();
  373. // 发送请求获取响应
  374. try {
  375. Response response = okHttpClient.newCall(request).execute();
  376. Long end = System.currentTimeMillis();
  377. System.out.println("get zhulu cost" + (end - start));
  378. // 判断请求是否成功
  379. if (response.isSuccessful()) {
  380. // 打印服务端返回结果
  381. return Objects.requireNonNull(response.body()).string();
  382. }
  383. } catch (IOException e) {
  384. e.printStackTrace();
  385. }
  386. return "{}";
  387. }
  388. /**
  389. * @param
  390. * @return
  391. * @throws IOException
  392. * @author 李仁杰
  393. * 从专利之星获取中国专利摘要附图
  394. */
  395. public String getPictureApi(String appNo) {
  396. String url = "https://api.patentstar.com.cn/api/Patent/CnMainImage/" + appNo;
  397. JSONObject configObject = this.getConfigObject(4, 2);
  398. String appId = configObject.getString("appId");
  399. String appKey = configObject.getString("appKey");
  400. Long currentTimeMillis = System.currentTimeMillis() / 1000;
  401. String Sign = appId + appKey + currentTimeMillis.toString();
  402. String signMd5 = FormatUtil.MD5(Sign);
  403. // 创建一个OkHttpClient对象
  404. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  405. .connectTimeout(60, TimeUnit.SECONDS)
  406. .writeTimeout(60, TimeUnit.SECONDS)
  407. .readTimeout(60, TimeUnit.SECONDS)
  408. .dns(new XDns(100000))
  409. .build();
  410. // 创建一个请求对象
  411. Request request = new Request.Builder().url(url)
  412. .addHeader("_appid", appId)
  413. .addHeader("_timestamp", currentTimeMillis.toString())
  414. .addHeader("_sign", signMd5)
  415. .get().build();
  416. // 发送请求获取响应
  417. try {
  418. Response response = okHttpClient.newCall(request).execute();
  419. // 判断请求是否成功
  420. if (response.isSuccessful() && response.code() == 200) {
  421. // 打印服务端返回结果
  422. return Objects.requireNonNull(response.body()).string();
  423. }
  424. } catch (IOException e) {
  425. e.printStackTrace();
  426. return null;
  427. }
  428. return null;
  429. }
  430. /**
  431. * @param appNo
  432. * @return
  433. * @throws IOException
  434. * @author 李仁杰
  435. * 从专利之星获取中国专利外观图
  436. */
  437. public String getWGPictureApi(String appNo) {
  438. String url = "https://api.patentstar.com.cn/api/Patent/CnWGImage/" + appNo;
  439. JSONObject configObject = this.getConfigObject(4, 2);
  440. String appId = configObject.getString("appId");
  441. String appKey = configObject.getString("appKey");
  442. Long currentTimeMillis = System.currentTimeMillis() / 1000;
  443. String Sign = appId + appKey + currentTimeMillis.toString();
  444. String signMd5 = FormatUtil.MD5(Sign);
  445. // 创建一个OkHttpClient对象
  446. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  447. .connectTimeout(60, TimeUnit.SECONDS)
  448. .writeTimeout(60, TimeUnit.SECONDS)
  449. .readTimeout(60, TimeUnit.SECONDS)
  450. .dns(new XDns(100000))
  451. .build();
  452. // 创建一个请求对象
  453. Request request = new Request.Builder().url(url)
  454. .addHeader("_appid", appId)
  455. .addHeader("_timestamp", currentTimeMillis.toString())
  456. .addHeader("_sign", signMd5)
  457. .get().build();
  458. // 发送请求获取响应
  459. try {
  460. Response response = okHttpClient.newCall(request).execute();
  461. // 判断请求是否成功
  462. if (response.isSuccessful()) {
  463. // 打印服务端返回结果
  464. return Objects.requireNonNull(response.body()).string();
  465. } else {
  466. response.close();
  467. }
  468. } catch (IOException e) {
  469. e.printStackTrace();
  470. }
  471. return null;
  472. }
  473. /**
  474. * @param appNo
  475. * @return
  476. * @throws IOException
  477. * @author 李仁杰
  478. * 从专利之星获取中国专利法律状态
  479. */
  480. public String getCnLegalApi(String appNo) {
  481. String url = "https://api.patentstar.com.cn/api/Patent/CnLegal/" + appNo;
  482. JSONObject configObject = this.getConfigObject(4, 2);
  483. String appId = configObject.getString("appId");
  484. String appKey = configObject.getString("appKey");
  485. Long currentTimeMillis = System.currentTimeMillis() / 1000;
  486. String Sign = appId + appKey + currentTimeMillis.toString();
  487. String signMd5 = FormatUtil.MD5(Sign);
  488. // 创建一个OkHttpClient对象
  489. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  490. .connectTimeout(60, TimeUnit.SECONDS)
  491. .writeTimeout(60, TimeUnit.SECONDS)
  492. .readTimeout(60, TimeUnit.SECONDS)
  493. .dns(new XDns(100000))
  494. .build();
  495. // 创建一个请求对象
  496. Request request = new Request.Builder().url(url)
  497. .addHeader("_appid", appId)
  498. .addHeader("_timestamp", currentTimeMillis.toString())
  499. .addHeader("_sign", signMd5)
  500. .get().build();
  501. // 发送请求获取响应
  502. try {
  503. Response response = okHttpClient.newCall(request).execute();
  504. // 判断请求是否成功
  505. if (response.isSuccessful()) {
  506. // 打印服务端返回结果
  507. return Objects.requireNonNull(response.body()).string();
  508. }
  509. } catch (IOException e) {
  510. e.printStackTrace();
  511. }
  512. return null;
  513. }
  514. /**
  515. * @param appNo
  516. * @return
  517. * @throws IOException
  518. * @author 李仁杰
  519. * 从专利之星获取中国专利全文图片
  520. */
  521. public String getCnPdfApi(String appNo) throws IOException {
  522. String url = "https://api.patentstar.com.cn/api/Patent/CnPdf/" + appNo;
  523. JSONObject configObject = this.getConfigObject(4, 2);
  524. String appId = configObject.getString("appId");
  525. String appKey = configObject.getString("appKey");
  526. Long currentTimeMillis = System.currentTimeMillis() / 1000;
  527. String Sign = appId + appKey + currentTimeMillis.toString();
  528. String signMd5 = FormatUtil.MD5(Sign);
  529. // 创建一个OkHttpClient对象
  530. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  531. .connectTimeout(60, TimeUnit.SECONDS)
  532. .writeTimeout(60, TimeUnit.SECONDS)
  533. .readTimeout(60, TimeUnit.SECONDS)
  534. .dns(new XDns(100000))
  535. .build();
  536. // 创建一个请求对象
  537. Request request = new Request.Builder().url(url)
  538. .addHeader("_appid", appId)
  539. .addHeader("_timestamp", currentTimeMillis.toString())
  540. .addHeader("_sign", signMd5)
  541. .get().build();
  542. // 发送请求获取响应
  543. try {
  544. Response response = okHttpClient.newCall(request).execute();
  545. // 判断请求是否成功
  546. if (response.isSuccessful()) {
  547. // 打印服务端返回结果
  548. return Objects.requireNonNull(response.body()).string();
  549. } else {
  550. response.close();
  551. }
  552. } catch (IOException e) {
  553. e.printStackTrace();
  554. }
  555. return "{}";
  556. }
  557. /**
  558. * @param appNo
  559. * @return
  560. * @throws IOException
  561. * @author 李仁杰
  562. * 从专利之星获取中国专利全文文本
  563. */
  564. public String getCnFullXmlApi(String appNo) throws IOException {
  565. String url = "https://api.patentstar.com.cn/api/Patent/CnFullXml/" + appNo;
  566. JSONObject configObject = this.getConfigObject(4, 2);
  567. String appId = configObject.getString("appId");
  568. String appKey = configObject.getString("appKey");
  569. Long currentTimeMillis = System.currentTimeMillis() / 1000;
  570. String Sign = appId + appKey + currentTimeMillis.toString();
  571. String signMd5 = FormatUtil.MD5(Sign);
  572. // 创建一个OkHttpClient对象
  573. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  574. .connectTimeout(60, TimeUnit.SECONDS)
  575. .writeTimeout(60, TimeUnit.SECONDS)
  576. .readTimeout(60, TimeUnit.SECONDS)
  577. .dns(new XDns(100000))
  578. .build();
  579. // 创建一个请求对象
  580. Request request = new Request.Builder().url(url)
  581. .addHeader("_appid", appId)
  582. .addHeader("_timestamp", currentTimeMillis.toString())
  583. .addHeader("_sign", signMd5)
  584. .get().build();
  585. // 发送请求获取响应
  586. try {
  587. Response response = okHttpClient.newCall(request).execute();
  588. // 判断请求是否成功
  589. if (response.isSuccessful()) {
  590. // 打印服务端返回结果
  591. return Objects.requireNonNull(response.body()).string();
  592. }
  593. } catch (IOException e) {
  594. e.printStackTrace();
  595. }
  596. return "{}";
  597. }
  598. //获取权利要求文本
  599. public String getExternalClaim(String str) throws DocumentException {
  600. SAXReader reader = new SAXReader();
  601. reader.setEntityResolver(new IgnoreDTDEntityResolver());
  602. Reader stringReader = new StringReader(str);
  603. Document document = reader.read(stringReader);
  604. XPath xpath = document.createXPath("//claim-text");
  605. List<Element> element = (List<Element>) xpath.selectNodes(document);
  606. if (element.size() == 0) {
  607. xpath = document.createXPath("//business:ClaimText");
  608. element = (List<Element>) xpath.selectNodes(document);
  609. }
  610. List<String> reClaims = new ArrayList<>();
  611. element.forEach(item -> {
  612. String claim = item.getText();
  613. claim = claim.replaceAll("\r\n|\r|\n| ", "");
  614. reClaims.add(claim);
  615. });
  616. String reClaim = "";
  617. if (reClaims.size() != 0) {
  618. reClaim = cn.cslg.pas.common.utils.StringUtils.join(reClaims, "");
  619. }
  620. return reClaim;
  621. }
  622. //获取说明书文本
  623. public String getFullText(String str) throws DocumentException {
  624. SAXReader reader = new SAXReader();
  625. reader.setEntityResolver(new IgnoreDTDEntityResolver());
  626. Reader stringReader = new StringReader(str);
  627. Document document = reader.read(stringReader);
  628. XPath xpath = document.createXPath("//description//p");
  629. List<Element> elements = (List<Element>) xpath.selectNodes(document);
  630. if (elements.size() == 0) {
  631. xpath = document.createXPath("//business:Description//base:Paragraphs");
  632. elements = (List<Element>) xpath.selectNodes(document);
  633. }
  634. List<String> reStrs = new ArrayList<>();
  635. Integer count = 1;
  636. for (Element item : elements) {
  637. String claim = item.getText().replaceAll("<br/>", "");
  638. if (!claim.trim().equals("技术领域")
  639. && !claim.trim().equals("发明内容")
  640. && !claim.trim().equals("附图说明")
  641. && !claim.trim().equals("具体实施方式")
  642. && !claim.trim().equals("背景技术")) {
  643. String formattedNum = String.format("%04d", count);
  644. claim = "[" + formattedNum + "]" + claim;
  645. count++;
  646. }
  647. reStrs.add(claim);
  648. }
  649. String reStr = "";
  650. if (reStrs.size() != 0) {
  651. reStr = StringUtils.join(reStrs, "\r\n");
  652. }
  653. return reStr;
  654. }
  655. /**
  656. * @param patentNo
  657. * @return
  658. * @throws IOException
  659. * @author 李仁杰
  660. * 从专利之星获取世界专利pdf
  661. */
  662. public String getEnPdfApi(String patentNo) {
  663. String url = " https://api.patentstar.com.cn/api/Patent/EnPdf/" + patentNo;
  664. JSONObject configObject = this.getConfigObject(4, 2);
  665. String appId = configObject.getString("appId");
  666. String appKey = configObject.getString("appKey");
  667. // String appId = "1000046";
  668. // String appkey = "6AE6D4DC6AF94F26862501EDEE9E27A2";
  669. Long currentTimeMillis = System.currentTimeMillis() / 1000;
  670. String Sign = appId + appKey + currentTimeMillis.toString();
  671. String signMd5 = FormatUtil.MD5(Sign);
  672. // 创建一个OkHttpClient对象
  673. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  674. .connectTimeout(60, TimeUnit.SECONDS)
  675. .writeTimeout(60, TimeUnit.SECONDS)
  676. .readTimeout(60, TimeUnit.SECONDS)
  677. .dns(new XDns(100000))
  678. .build();
  679. // 创建一个请求对象
  680. Request request = new Request.Builder().url(url)
  681. .addHeader("_appid", appId)
  682. .addHeader("_timestamp", currentTimeMillis.toString())
  683. .addHeader("_sign", signMd5)
  684. .get().build();
  685. // 发送请求获取响应
  686. try {
  687. Response response = okHttpClient.newCall(request).execute();
  688. // 判断请求是否成功
  689. if (response.isSuccessful()) {
  690. // 打印服务端返回结果
  691. return Objects.requireNonNull(response.body()).string();
  692. } else {
  693. response.close();
  694. }
  695. } catch (IOException e) {
  696. e.printStackTrace();
  697. }
  698. return "{}";
  699. }
  700. /**
  701. * @param patentNo
  702. * @return
  703. * @throws IOException
  704. * @author 李仁杰
  705. * 从专利之星获取世界专利著录信息
  706. */
  707. public String getENBibApi(String patentNo) {
  708. String url = "https://api.patentstar.com.cn/api/Patent/EnBib/" + patentNo;
  709. JSONObject configObject = this.getConfigObject(4, 2);
  710. String appId = configObject.getString("appId");
  711. String appKey = configObject.getString("appKey");
  712. Long currentTimeMillis = System.currentTimeMillis() / 1000;
  713. String Sign = appId + appKey + currentTimeMillis.toString();
  714. String signMd5 = FormatUtil.MD5(Sign);
  715. // 创建一个OkHttpClient对象
  716. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  717. .connectTimeout(5, TimeUnit.SECONDS)
  718. .writeTimeout(5, TimeUnit.SECONDS)
  719. .readTimeout(5, TimeUnit.SECONDS)
  720. .addInterceptor(new PatentStarApiService.OkhttpInterceptor(3))
  721. .build();
  722. // 创建一个请求对象
  723. Request request = new Request.Builder().url(url)
  724. .addHeader("_appid", appId)
  725. .addHeader("_timestamp", currentTimeMillis.toString())
  726. .addHeader("_sign", signMd5)
  727. .get().build();
  728. // 发送请求获取响应
  729. try {
  730. Response response = okHttpClient.newCall(request).execute();
  731. // 判断请求是否成功
  732. if (response.isSuccessful()) {
  733. // 打印服务端返回结果
  734. return Objects.requireNonNull(response.body()).string();
  735. }
  736. } catch (IOException e) {
  737. e.printStackTrace();
  738. }
  739. return "{}";
  740. }
  741. /**
  742. * @param patentNo
  743. * @return
  744. * @throws IOException
  745. * @author 李仁杰
  746. * 从专利之星获取同族专利
  747. */
  748. public String getFamilyByPubNoApi(String patentNo) {
  749. Long start = System.currentTimeMillis();
  750. String url = "https://api.patentstar.com.cn/api/Patent/FamilyByPubNo/" + patentNo;
  751. JSONObject configObject = this.getConfigObject(4, 2);
  752. String appId = configObject.getString("appId");
  753. String appKey = configObject.getString("appKey");
  754. Long currentTimeMillis = System.currentTimeMillis() / 1000;
  755. String Sign = appId + appKey + currentTimeMillis.toString();
  756. String signMd5 = FormatUtil.MD5(Sign);
  757. // 创建一个OkHttpClient对象
  758. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  759. .connectTimeout(60, TimeUnit.SECONDS)
  760. .writeTimeout(60, TimeUnit.SECONDS)
  761. .readTimeout(60, TimeUnit.SECONDS)
  762. .dns(new XDns(100000))
  763. .build();
  764. // 创建一个请求对象
  765. Request request = new Request.Builder().url(url)
  766. .addHeader("_appid", appId)
  767. .addHeader("_timestamp", currentTimeMillis.toString())
  768. .addHeader("_sign", signMd5)
  769. .get().build();
  770. // 发送请求获取响应
  771. try {
  772. Response response = okHttpClient.newCall(request).execute();
  773. Long end = System.currentTimeMillis();
  774. System.out.println("get family cost" + (end - start));
  775. // 判断请求是否成功
  776. if (response.isSuccessful()) {
  777. // 打印服务端返回结果
  778. return Objects.requireNonNull(response.body()).string();
  779. }
  780. } catch (IOException e) {
  781. e.printStackTrace();
  782. }
  783. return "{}";
  784. }
  785. /**
  786. * @param patentNo
  787. * @return
  788. * @throws IOException
  789. * @author 李仁杰
  790. * 获得专利被引用次数api
  791. */
  792. public String getCitedNumByPubNoApi(String patentNo) throws IOException {
  793. String url = "https://api.patentstar.com.cn/api/Patent/CitedNumByPubNo/" + patentNo;
  794. JSONObject configObject = this.getConfigObject(4, 2);
  795. String appId = configObject.getString("appId");
  796. String appKey = configObject.getString("appKey");
  797. Long currentTimeMillis = System.currentTimeMillis() / 1000;
  798. String Sign = appId + appKey + currentTimeMillis.toString();
  799. String signMd5 = FormatUtil.MD5(Sign);
  800. // 创建一个OkHttpClient对象
  801. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  802. .connectTimeout(60, TimeUnit.SECONDS)
  803. .writeTimeout(60, TimeUnit.SECONDS)
  804. .readTimeout(60, TimeUnit.SECONDS)
  805. .dns(new XDns(100000))
  806. .build();
  807. // 创建一个请求对象
  808. Request request = new Request.Builder().url(url)
  809. .addHeader("_appid", appId)
  810. .addHeader("_timestamp", currentTimeMillis.toString())
  811. .addHeader("_sign", signMd5)
  812. .get().build();
  813. // 发送请求获取响应
  814. try {
  815. Response response = okHttpClient.newCall(request).execute();
  816. // 判断请求是否成功
  817. if (response.isSuccessful()) {
  818. // 打印服务端返回结果
  819. return Objects.requireNonNull(response.body()).string();
  820. }
  821. } catch (IOException e) {
  822. e.printStackTrace();
  823. }
  824. return "{}";
  825. }
  826. public JSONObject getConfigObject(Integer webId, Integer webGroup) {
  827. WebLoginConfig webLoginConfig = webLoginConfigService.getConfig(webId, webGroup);
  828. if (webLoginConfig == null) {
  829. return null;
  830. }
  831. String config = webLoginConfig.getWebConfig();
  832. JSONObject jsonObject = JSONObject.parseObject(config);
  833. return jsonObject;
  834. }
  835. public List<PatentStarListDTO> splitPatentNoByType(List<String> originalList, PatentStarListDTO patentStarListDTO) {
  836. List<String> cnStrings = new ArrayList<>();
  837. List<String> wdStrings = new ArrayList<>();
  838. List<List<String>> result = new ArrayList<>();
  839. result.add(cnStrings);
  840. result.add(wdStrings);
  841. // 记录CN类型的字符串数量
  842. int cnCount = 0;
  843. // 记录WD类型的字符串数量
  844. int wdCount = 0;
  845. List<PatentStarListDTO> patentStarListDTOS = new ArrayList<>();
  846. //遍历初始集合
  847. for (String str : originalList) {
  848. if (str.startsWith("CN")) {
  849. if (str.length() == 12 && str.contains(".")) {
  850. String cnNumber = this.formatCnNumber(str);
  851. cnStrings.add(cnNumber);
  852. } else {
  853. cnStrings.add(str);
  854. }
  855. cnCount++;
  856. if (cnCount >= 100) {
  857. cnStrings = new ArrayList<>();
  858. result.add(cnStrings);
  859. cnCount = 0;
  860. }
  861. } else {
  862. wdStrings.add(str);
  863. wdCount++;
  864. if (wdCount >= 100) {
  865. wdStrings = new ArrayList<>();
  866. result.add(wdStrings);
  867. wdCount = 0;
  868. }
  869. }
  870. }
  871. result.forEach(item -> {
  872. if (item.size() > 0) {
  873. String dbType = "CN";
  874. String patentNo = item.get(0);
  875. if (!patentNo.startsWith("CN")) {
  876. dbType = "WD";
  877. }
  878. String join = StringUtils.join(item, " OR ");
  879. String conditions = "AN=(" + join + ") OR PN=(" + join + ") OR GN=(" + join + ")";
  880. PatentStarListDTO patentStarListDTO1 = new PatentStarListDTO();
  881. patentStarListDTO1.setOrderByType(patentStarListDTO.getOrderByType());
  882. patentStarListDTO1.setCurrentQuery(conditions);
  883. patentStarListDTO1.setPageNum(patentStarListDTO.getPageNum());
  884. patentStarListDTO1.setDBType(dbType);
  885. patentStarListDTO1.setCurrentQuery(conditions);
  886. patentStarListDTO1.setRowCount(50);
  887. patentStarListDTO1.setTotal(item.size());
  888. patentStarListDTO1.setNos(item);
  889. patentStarListDTOS.add(patentStarListDTO1);
  890. }
  891. });
  892. return patentStarListDTOS;
  893. }
  894. public String formatCnNumber(String str) {
  895. StringBuilder builder = new StringBuilder();
  896. builder.append(str, 0, 2);
  897. int num = Integer.parseInt(str.substring(2, 3));
  898. if (num > 6) {
  899. builder.append("19");
  900. } else {
  901. builder.append("20");
  902. }
  903. builder.append(str, 2, 5);
  904. builder.append("00");
  905. builder.append(str, 5, str.indexOf("."));
  906. String s = builder.toString();
  907. String s1 = s.substring(2);
  908. char c = this.calculateChecksum(s1);
  909. builder.append(".").append(c);
  910. return builder.toString();
  911. }
  912. public char calculateChecksum(String input) {
  913. int sum = 0;
  914. int[] weights = {2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5};
  915. // 遍历字符串中的每个字符,并计算乘积之和
  916. for (int i = 0; i < input.length(); i++) {
  917. char digit = input.charAt(i);
  918. int num = Character.getNumericValue(digit);
  919. sum += num * weights[i];
  920. }
  921. // 计算总和除以11的余数
  922. int remainder = sum % 11;
  923. // 根据余数确定校验位
  924. char checksum;
  925. if (remainder < 10) {
  926. checksum = (char) ('0' + remainder);
  927. } else {
  928. checksum = 'X';
  929. }
  930. return checksum;
  931. }
  932. public StarPatentVO getPatentByNo(String patentNo) {
  933. StarPatentVO starPatentVO = null;
  934. String condition = "AN=(" + patentNo + ") OR PN=(" + patentNo + ") OR GN=(" + patentNo + ") OR ANO=(" + patentNo + ")";
  935. String dbType = "CN";
  936. if (!patentNo.startsWith("CN")) {
  937. dbType = "WD";
  938. }
  939. PatentStarListDTO patentStarListDTO = new PatentStarListDTO();
  940. patentStarListDTO.setCurrentQuery(condition);
  941. patentStarListDTO.setPageNum(1);
  942. patentStarListDTO.setRowCount(50);
  943. patentStarListDTO.setDBType(dbType);
  944. try {
  945. Map<String, Object> resultMap = this.patentStarSearchApi(patentStarListDTO);
  946. if (resultMap == null || (Integer) resultMap.get("total") == 0) {
  947. return starPatentVO;
  948. }
  949. List<StarPatentVO> starPatents = (List<StarPatentVO>) resultMap.get("records");
  950. starPatentVO = starPatents.get(0);
  951. } catch (Exception e) {
  952. }
  953. return starPatentVO;
  954. }
  955. private List<PatentColumnDTO> loadPatent(List<StarPatentVO> starPatentVOS, String dbType) {
  956. List<PatentColumnDTO> patentColumnDTOS = new ArrayList<>();
  957. starPatentVOS.forEach(item -> {
  958. UploadPatentWebDTO uploadPatentWebDTO = new UploadPatentWebDTO();
  959. uploadPatentWebDTO.setStarPatentVO(item);
  960. Patent patent = new Patent();
  961. uploadPatentWebDTO.setPatent(patent);
  962. webVOTransformService.loadPatent(uploadPatentWebDTO, false);
  963. if (dbType != null && dbType.equals("CN")) {
  964. webVOTransformService.loadCNPatentNo(uploadPatentWebDTO.getPatent());
  965. } else {
  966. webVOTransformService.loadWorldPatentNo(uploadPatentWebDTO.getPatent());
  967. }
  968. PatentColumnDTO patentColumnDTO = new PatentColumnDTO();
  969. BeanUtils.copyProperties(patent, patentColumnDTO);
  970. //装载权利要求
  971. List<Text> claimList = new ArrayList<>();
  972. Text claim = new Text();
  973. claim.setTextContent(item.getCL());
  974. claim.setIfOrigin(true);
  975. claimList.add(claim);
  976. patentColumnDTO.setClaim(claimList);
  977. //获取摘要附图
  978. // String pictureApi = this.getPictureApi(item.getRowApplicationNo());
  979. // patentColumnDTO.setPictureGuid(pictureApi);
  980. //获取外部专利原始申请号
  981. patentColumnDTO.setRowApplicationNo(item.getANO());
  982. //获取法律状态
  983. // String cnLegalApi = this.getCnLegalApi(item.getRowApplicationNo());
  984. // patentColumnDTO.setLegalStatus(Arrays.asList(cnLegalApi));
  985. //获取被引用次数
  986. //装载申请人
  987. if (item.getPA() != null && !item.getPA().trim().equals("")) {
  988. List<String> names = Arrays.asList(item.getPA().split(";"));
  989. patentColumnDTO.setApplicant(names);
  990. } else {
  991. patentColumnDTO.setApplicant(new ArrayList<>());
  992. }
  993. //装载发明人
  994. if (item.getIN() != null && !item.getIN().trim().equals("")) {
  995. List<String> names = Arrays.asList(item.getIN().split(";"));
  996. patentColumnDTO.setInventor(names);
  997. } else {
  998. patentColumnDTO.setInventor(new ArrayList<>());
  999. }
  1000. //装载权利人
  1001. if (item.getPE() != null && !item.getPE().trim().equals("")) {
  1002. List<String> names = Arrays.asList(item.getPE().split(";"));
  1003. patentColumnDTO.setRightHolder(names);
  1004. } else {
  1005. patentColumnDTO.setRightHolder(new ArrayList<>());
  1006. }
  1007. patentColumnDTOS.add(patentColumnDTO);
  1008. });
  1009. return patentColumnDTOS;
  1010. }
  1011. /**
  1012. * 根据专利号查询外部权利要求
  1013. *
  1014. * @param appNo
  1015. * @return
  1016. * @throws IOException
  1017. */
  1018. public SelectClaimDTO queryExternalClaim(String appNo) throws IOException {
  1019. List<Text> claimList = new ArrayList<>();
  1020. //获取公开说明书
  1021. String cnFullXmlApi = null;
  1022. try {
  1023. cnFullXmlApi = this.getCnFullXmlApi(appNo);
  1024. } catch (IOException e) {
  1025. e.printStackTrace();
  1026. }
  1027. try {
  1028. String fullText = this.getExternalClaim(cnFullXmlApi);
  1029. Text pubText = new Text();
  1030. if (fullText != null && !fullText.trim().equals("")) {
  1031. fullText = PatentRightUtils.getFormatClaim(fullText, "CN");
  1032. }
  1033. pubText.setTextContent(fullText);
  1034. pubText.setIfOrigin(true);
  1035. claimList.add(pubText);
  1036. } catch (DocumentException e) {
  1037. e.printStackTrace();
  1038. }
  1039. SelectClaimDTO dto = new SelectClaimDTO();
  1040. if (!CollectionUtils.isEmpty(claimList)) {
  1041. dto.setClaimContent(claimList);
  1042. }
  1043. return dto;
  1044. }
  1045. /**
  1046. * 根据专利号查询外部权利要求
  1047. *
  1048. * @param vo
  1049. * @return
  1050. * @throws IOException
  1051. */
  1052. public PatentColumnDTO queryExternalDetail(QueryExternalFamilyVO vo) throws IOException {
  1053. PatentStarListDTO patentStarListDTO = new PatentStarListDTO();
  1054. String condition = "AN=" + vo.getPatentNo() + " OR " + "PN=" + vo.getPatentNo();
  1055. patentStarListDTO.setCurrentQuery(condition);
  1056. String index = vo.getPatentNo().substring(0, 2);
  1057. if (index.equals("CN")) {
  1058. patentStarListDTO.setDBType("CN");
  1059. } else {
  1060. patentStarListDTO.setDBType("WD");
  1061. }
  1062. patentStarListDTO.setOrderBy("AD");
  1063. patentStarListDTO.setOrderByType("DESC");
  1064. patentStarListDTO.setPageNum(1);
  1065. patentStarListDTO.setRowCount(10);
  1066. Records records = this.patentStarSearchLocal(patentStarListDTO);
  1067. Object data = records.getData();
  1068. String s = JSONArray.toJSONString(data);
  1069. List<PatentColumnDTO> list = JSONArray.parseArray(s, PatentColumnDTO.class);
  1070. PatentColumnDTO dto = new PatentColumnDTO();
  1071. if (!CollectionUtils.isEmpty(list)) {
  1072. for (PatentColumnDTO columnDTO : list) {
  1073. BeanUtils.copyProperties(columnDTO, dto);
  1074. }
  1075. }
  1076. return dto;
  1077. }
  1078. /**
  1079. * 查询外部专利同族专利
  1080. *
  1081. * @param vo
  1082. * @return
  1083. * @throws IOException
  1084. */
  1085. public Records queryExternalFamily(QueryExternalFamilyVO vo) throws IOException {
  1086. Records records = new Records();
  1087. String family = this.getFamilyByPubNoApi(vo.getPatentNo());
  1088. QueryExternalFamilyDTO familyDTO = JSONObject.parseObject(family, QueryExternalFamilyDTO.class);
  1089. String familyInfos = familyDTO.getFamilyinfo();
  1090. List<String> publicNos = new ArrayList<>();
  1091. if (StringUtils.isNotEmpty(familyInfos) && !familyInfos.equals("{}")) {
  1092. publicNos = Arrays.asList(familyInfos.split(";"));
  1093. }
  1094. List<PatentColumnDTO> list = new ArrayList<>();
  1095. if (!CollectionUtils.isEmpty(publicNos)) {
  1096. for (String publicNo : publicNos) {
  1097. PatentStarListDTO patentStarListDTO = new PatentStarListDTO();
  1098. String condition = "PN=" + publicNo;
  1099. patentStarListDTO.setCurrentQuery(condition);
  1100. String index = publicNo.substring(0, 2);
  1101. if (index.equals("CN")) {
  1102. patentStarListDTO.setDBType("CN");
  1103. } else {
  1104. patentStarListDTO.setDBType("WD");
  1105. }
  1106. patentStarListDTO.setOrderBy("AD");
  1107. patentStarListDTO.setOrderByType("DESC");
  1108. patentStarListDTO.setPageNum(vo.getPageNum());
  1109. patentStarListDTO.setRowCount(vo.getPageSize());
  1110. records = this.patentStarSearchLocal(patentStarListDTO);
  1111. list.addAll((Collection<? extends PatentColumnDTO>) records.getData());
  1112. }
  1113. }
  1114. Records record = new Records();
  1115. record.setCurrent(vo.getPageNum().longValue());
  1116. record.setSize(vo.getPageSize().longValue());
  1117. record.setData(list);
  1118. record.setTotal(Long.valueOf(String.valueOf(list.size())));
  1119. return record;
  1120. }
  1121. /**
  1122. * 获取公开说明书
  1123. *
  1124. * @param appNo
  1125. * @return
  1126. */
  1127. public List<Text> getCnFullText(String appNo) {
  1128. List<Text> pubTextList = new ArrayList<>();
  1129. //获取公开说明书
  1130. String cnFullXmlApi = null;
  1131. try {
  1132. cnFullXmlApi = this.getCnFullXmlApi(appNo);
  1133. } catch (IOException e) {
  1134. e.printStackTrace();
  1135. }
  1136. try {
  1137. String fullText = this.getFullText(cnFullXmlApi);
  1138. Text pubText = new Text();
  1139. pubText.setTextContent(fullText);
  1140. pubText.setIfOrigin(true);
  1141. pubTextList.add(pubText);
  1142. } catch (DocumentException e) {
  1143. e.printStackTrace();
  1144. }
  1145. return pubTextList;
  1146. }
  1147. /**
  1148. * 获得中国专利pdf全文
  1149. *
  1150. * @param appNo
  1151. * @return
  1152. * @throws IOException
  1153. */
  1154. public List<InnerPatentPdfDTO> getExternalTextPdf(String appNo, String rowApplicationNo, String publicNo, String grantNo) throws IOException {
  1155. List<InnerPatentPdfDTO> list = new ArrayList<>();
  1156. String pdfUrl = "", pdfUrl1 = "", pdfUrl2 = "";
  1157. if (StringUtils.isNotEmpty(appNo) && appNo.contains("CN")) {
  1158. String pdfUrlStr = this.getCnPdfApi(rowApplicationNo);
  1159. //若没有取到说明书pdf,则将当前申请号和备注信息存入问题记录表,并返回空对象
  1160. if (StringUtils.isNotEmpty(pdfUrlStr)) {
  1161. if (pdfUrlStr.contains("|http")) { //若包含公开和授权两个pdf
  1162. String[] pdfUrlArr = pdfUrlStr.split("\\|http");
  1163. pdfUrlArr[1] = "http" + pdfUrlArr[1];
  1164. if (pdfUrlArr[0].contains("?") && pdfUrlArr[1].contains("?")) {
  1165. pdfUrl1 = pdfUrlArr[0].substring(pdfUrlArr[0].indexOf("http"), pdfUrlArr[0].indexOf("?"));
  1166. pdfUrl2 = pdfUrlArr[1].substring(pdfUrlArr[1].indexOf("http"), pdfUrlArr[1].indexOf("?"));
  1167. } else
  1168. {
  1169. pdfUrl1 =pdfUrlArr[0];
  1170. pdfUrl2 = pdfUrlArr[1];
  1171. }
  1172. if (pdfUrl1.contains("0A_CN_0.pdf")) {
  1173. InnerPatentPdfDTO dto = new InnerPatentPdfDTO();
  1174. dto.setPdfGuid(pdfUrl1);
  1175. dto.setType(0);
  1176. list.add(dto);
  1177. //授权说明书pdf
  1178. InnerPatentPdfDTO dto1 = new InnerPatentPdfDTO();
  1179. dto1.setPdfGuid(pdfUrl2);
  1180. dto1.setType(1);
  1181. list.add(dto1);
  1182. } else {
  1183. InnerPatentPdfDTO dto1 = new InnerPatentPdfDTO();
  1184. dto1.setPdfGuid(pdfUrl1);
  1185. dto1.setType(1);
  1186. list.add(dto1);
  1187. InnerPatentPdfDTO dto = new InnerPatentPdfDTO();
  1188. dto.setPdfGuid(pdfUrl2);
  1189. dto.setType(0);
  1190. list.add(dto);
  1191. }
  1192. } else { //若只有一个
  1193. pdfUrl1 = pdfUrlStr.substring(pdfUrlStr.indexOf("http"));
  1194. if (pdfUrl1.contains("0A_CN_0.pdf")) {
  1195. InnerPatentPdfDTO dto = new InnerPatentPdfDTO();
  1196. dto.setPdfGuid(pdfUrl1);
  1197. dto.setType(0);
  1198. list.add(dto);
  1199. } else {
  1200. InnerPatentPdfDTO dto = new InnerPatentPdfDTO();
  1201. dto.setPdfGuid(pdfUrl1);
  1202. dto.setType(1);
  1203. list.add(dto);
  1204. }
  1205. }
  1206. }
  1207. } else {
  1208. String usedPatentNo;
  1209. if (publicNo != null && !publicNo.equals("")) {
  1210. usedPatentNo = publicNo;
  1211. } else {
  1212. usedPatentNo = grantNo;
  1213. }
  1214. if (usedPatentNo != null) {
  1215. pdfUrl = this.getEnPdfApi(usedPatentNo);
  1216. if (!pdfUrl.equals("")) {
  1217. InnerPatentPdfDTO dto = new InnerPatentPdfDTO();
  1218. dto.setPdfGuid(pdfUrl);
  1219. dto.setType(0);
  1220. list.add(dto);
  1221. }
  1222. }
  1223. }
  1224. return list;
  1225. }
  1226. /**
  1227. * 获取附图
  1228. *
  1229. * @param appNo
  1230. * @return
  1231. */
  1232. public List<String> getExternalFigure(String appNo) throws IOException {
  1233. List<String> list = new ArrayList<>();
  1234. String wgPictureApi = this.getWGPictureApi(appNo);
  1235. if (StringUtils.isNotEmpty(wgPictureApi)) {
  1236. if (wgPictureApi.contains("|http")) {
  1237. String[] urlArr = wgPictureApi.split("\\|");
  1238. list.addAll(Arrays.asList(urlArr));
  1239. } else {
  1240. list.add(wgPictureApi);
  1241. }
  1242. }
  1243. return list;
  1244. }
  1245. /**
  1246. * 获取摘要附图
  1247. *
  1248. * @param appNo
  1249. * @return
  1250. */
  1251. public String getPictureGuid(String appNo) {
  1252. return this.getPictureApi(appNo);
  1253. }
  1254. /**
  1255. * @param appNo
  1256. * @return
  1257. */
  1258. public ExternalLegalInfoDTO getLegalStatus(String appNo) throws ParseException {
  1259. ExternalLegalInfoDTO infoDTO = new ExternalLegalInfoDTO();
  1260. List<String> list = new ArrayList<>();
  1261. List<LegalEvent> legalEvents = new ArrayList<>();
  1262. String cnLegalApi = this.getCnLegalApi(appNo);
  1263. if (StringUtils.isNotEmpty(cnLegalApi) && !cnLegalApi.equals("{}") && !cnLegalApi.contains("408")) {
  1264. List<ExternalLegalStatusDTO> statusDTOS = JSONArray.parseArray(cnLegalApi, ExternalLegalStatusDTO.class);
  1265. if (!CollectionUtils.isEmpty(statusDTOS)) {
  1266. List<ExternalLegalStatusDTO> statusList = statusDTOS.stream().sorted(Comparator.comparing(ExternalLegalStatusDTO::getLegalDate).reversed()).collect(Collectors.toList());
  1267. if (!CollectionUtils.isEmpty(statusList)) {
  1268. ExternalLegalStatusDTO statusDTO = statusList.get(0);
  1269. list.add(statusDTO.getLegalStatus());
  1270. }
  1271. for (ExternalLegalStatusDTO dto : statusDTOS) {
  1272. LegalEvent event = new LegalEvent();
  1273. event.setCode(dto.getLegalCode());
  1274. event.setName(dto.getLegalStatus());
  1275. event.setDescription(dto.getLegalStatusInfo());
  1276. if (StringUtils.isNotEmpty(dto.getLegalDate())) {
  1277. SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
  1278. Date date = format.parse(dto.getLegalDate());
  1279. event.setEventDate(date);
  1280. }
  1281. legalEvents.add(event);
  1282. }
  1283. }
  1284. }
  1285. infoDTO.setLegalStatus(list);
  1286. infoDTO.setLegalEvents(legalEvents);
  1287. return infoDTO;
  1288. }
  1289. public SelectClaimDTO getOrginCalim(String appNo) throws IOException {
  1290. List<Text> claimList = new ArrayList<>();
  1291. //获取公开说明书
  1292. String cnFullXmlApi = null;
  1293. try {
  1294. cnFullXmlApi = this.getCnFullXmlApi(appNo);
  1295. } catch (IOException e) {
  1296. e.printStackTrace();
  1297. }
  1298. try {
  1299. String fullText = this.getExternalClaim(cnFullXmlApi);
  1300. Text pubText = new Text();
  1301. pubText.setTextContent(fullText);
  1302. pubText.setIfOrigin(true);
  1303. claimList.add(pubText);
  1304. } catch (DocumentException e) {
  1305. e.printStackTrace();
  1306. }
  1307. SelectClaimDTO dto = new SelectClaimDTO();
  1308. if (!CollectionUtils.isEmpty(claimList)) {
  1309. dto.setClaimContent(claimList);
  1310. }
  1311. return dto;
  1312. }
  1313. public static class OkhttpInterceptor implements Interceptor {
  1314. // 最大重试次数
  1315. private int maxRentry;
  1316. public OkhttpInterceptor(int maxRentry) {
  1317. this.maxRentry = maxRentry;
  1318. }
  1319. @NotNull
  1320. @Override
  1321. public Response intercept(@NotNull Interceptor.Chain chain) throws IOException {
  1322. /* 递归 2次下发请求,如果仍然失败 则返回 null ,但是 intercept must not return null.
  1323. * 返回 null 会报 IllegalStateException 异常
  1324. * */
  1325. return retry(chain, 0);//这个递归真的很舒服
  1326. }
  1327. Response retry(Chain chain, int retryCent) {
  1328. Request request = chain.request();
  1329. Response response = null;
  1330. try {
  1331. System.out.println("第" + (retryCent + 1) + "次执行发http请求.");
  1332. response = chain.proceed(request);
  1333. } catch (Exception e) {
  1334. if (maxRentry > retryCent) {
  1335. response = retry(chain, retryCent + 1);
  1336. }
  1337. } finally {
  1338. return response;
  1339. }
  1340. }
  1341. }
  1342. /**
  1343. * 根据专利号查询外部权利要求
  1344. *
  1345. * @param patentNo
  1346. * @return
  1347. * @throws IOException
  1348. */
  1349. public PatentInfoDTO queryPatentDetailByPatentNo(String patentNo) throws IOException {
  1350. PatentStarListDTO patentStarListDTO = new PatentStarListDTO();
  1351. String condition = "AN=" + patentNo + " OR " + "PN=" + patentNo + " OR " + "GN=" + patentNo;
  1352. patentStarListDTO.setCurrentQuery(condition);
  1353. String index = patentNo.substring(0, 2);
  1354. if (index.equals("CN") || index.equals("ZL")) {
  1355. patentStarListDTO.setDBType("CN");
  1356. } else {
  1357. patentStarListDTO.setDBType("WD");
  1358. }
  1359. patentStarListDTO.setOrderBy("AD");
  1360. patentStarListDTO.setOrderByType("DESC");
  1361. patentStarListDTO.setPageNum(1);
  1362. patentStarListDTO.setRowCount(10);
  1363. Records records = this.patentStarSearchLocal(patentStarListDTO);
  1364. Object data = records.getData();
  1365. String s = JSON.toJSONString(data);
  1366. List<PatentColumnDTO> list = JSON.parseArray(s, PatentColumnDTO.class);
  1367. PatentInfoDTO dto = new PatentInfoDTO();
  1368. if (!CollectionUtils.isEmpty(list)) {
  1369. for (PatentColumnDTO columnDTO : list) {
  1370. BeanUtils.copyProperties(columnDTO, dto);
  1371. }
  1372. }
  1373. if (StringUtils.isNotEmpty(dto.getRowApplicationNo())) {
  1374. String txtGuid = this.getTxtGuid(dto.getRowApplicationNo(), patentNo);
  1375. dto.setTxtGuid(txtGuid);
  1376. }
  1377. List<InnerPatentPdfDTO> pdfList = this.getExternalTextPdf(dto.getAppNo(), dto.getRowApplicationNo(), dto.getPublicNo(), dto.getGrantNo());
  1378. if (!CollectionUtils.isEmpty(pdfList)) {
  1379. List<InnerPatentPdfDTO> pdfDTOS = new ArrayList<>();
  1380. for (InnerPatentPdfDTO innerPatentPdfDTO : pdfList) {
  1381. if (StringUtils.isNotEmpty(innerPatentPdfDTO.getPdfGuid())) {
  1382. File file = FileUtils.getFileByUrl(innerPatentPdfDTO.getPdfGuid());
  1383. List<String> guidList = fileManagerService.uploadFileGetGuid2(Collections.singletonList(file));
  1384. if (!CollectionUtils.isEmpty(guidList)) {
  1385. InnerPatentPdfDTO pdfDTO = new InnerPatentPdfDTO();
  1386. pdfDTO.setPdfGuid(guidList.get(0));
  1387. pdfDTO.setType(innerPatentPdfDTO.getType());
  1388. pdfDTOS.add(pdfDTO);
  1389. }
  1390. }
  1391. }
  1392. dto.setPdfDTOS(pdfDTOS);
  1393. }
  1394. return dto;
  1395. }
  1396. public String getTxtGuid(String rowApplicationNo,String patentNo) throws IOException {
  1397. StringBuilder builder = new StringBuilder();
  1398. SelectClaimDTO externalClaim = this.queryExternalClaim(rowApplicationNo);
  1399. if (ObjectUtils.isNotEmpty(externalClaim)) {
  1400. List<Text> claimContent = externalClaim.getClaimContent();
  1401. if (!CollectionUtils.isEmpty(claimContent)) {
  1402. Text text = claimContent.stream().filter(i -> i.getIfOrigin().equals(true)).findFirst().orElse(null);
  1403. if (ObjectUtils.isNotEmpty(text)) {
  1404. String claim = text.getTextContent();
  1405. String claimStr = claim.replaceAll("\t", "\n");
  1406. builder.append(" 权利要求");
  1407. builder.append("\n");
  1408. builder.append(claimStr);
  1409. }
  1410. }
  1411. }
  1412. List<Text> fullText = this.getCnFullText(rowApplicationNo);
  1413. if (!CollectionUtils.isEmpty(fullText)) {
  1414. Text text = fullText.stream().filter(i -> i.getIfOrigin().equals(true)).findFirst().orElse(null);
  1415. if (ObjectUtils.isNotEmpty(text)) {
  1416. String specification = text.getTextContent();
  1417. builder.append("\n");
  1418. builder.append("\n");
  1419. builder.append("\n");
  1420. builder.append(" 说明书");
  1421. builder.append("\n");
  1422. builder.append(specification);
  1423. }
  1424. }
  1425. String result = builder.toString();
  1426. byte[] bytes = result.getBytes();
  1427. MultipartFile multipartFile = FileUtils.convertBytesToMultipartFile(bytes, patentNo + "-" + "文本文档.txt");
  1428. List<String> list = fileManagerService.uploadFileGetGuid(Collections.singletonList(multipartFile));
  1429. if (!CollectionUtils.isEmpty(list)) {
  1430. return list.get(0);
  1431. }
  1432. return "";
  1433. }
  1434. }