PythonApiService.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package cn.cslg.pas.service.common;
  2. import cn.cslg.pas.common.core.base.RedisConf;
  3. import cn.cslg.pas.common.model.pythonModel.GetPatentSimilarScoreDTO;
  4. import cn.cslg.pas.common.model.pythonModel.GetPatentSimilarScoreVO;
  5. import cn.cslg.pas.common.model.pythonModel.PatentScoreDTO;
  6. import cn.cslg.pas.common.model.pythonModel.PatentScoreVO;
  7. import cn.cslg.pas.common.utils.LoginUtils;
  8. import cn.cslg.pas.common.utils.MD5Util;
  9. import cn.cslg.pas.common.utils.RedisUtil;
  10. import cn.cslg.pas.common.utils.StringUtils;
  11. import cn.cslg.pas.common.vo.StarPatentVO;
  12. import cn.cslg.pas.domain.business.TechnicalCase;
  13. import cn.cslg.pas.exception.ExceptionEnum;
  14. import cn.cslg.pas.exception.XiaoShiException;
  15. import cn.cslg.pas.service.business.TechnicalCaseService;
  16. import cn.cslg.pas.service.importPatent.WebVOTransformService;
  17. import com.alibaba.fastjson.JSON;
  18. import com.alibaba.fastjson2.JSONObject;
  19. import com.google.gson.Gson;
  20. import okhttp3.MediaType;
  21. import okhttp3.OkHttpClient;
  22. import okhttp3.Request;
  23. import okhttp3.RequestBody;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.stereotype.Service;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Objects;
  30. import java.util.concurrent.TimeUnit;
  31. @Service
  32. public class PythonApiService {
  33. @Value("${PythonUrl}")
  34. private String PythonUrl;
  35. @Autowired
  36. private RedisUtil redisUtil;
  37. @Autowired
  38. private PatentStarApiService patentStarApiService;
  39. @Autowired
  40. private WebVOTransformService webVOTransformService;
  41. @Autowired
  42. private TechnicalCaseService technicalCaseService;
  43. public GetPatentSimilarScoreVO getPatentSimilarScore(GetPatentSimilarScoreDTO getPatentSimilarScoreDTO) {
  44. GetPatentSimilarScoreVO getPatentSimilarScoreVo = new GetPatentSimilarScoreVO();
  45. try {
  46. OkHttpClient okHttpClient = new OkHttpClient();
  47. String param = new Gson().toJson(getPatentSimilarScoreDTO);
  48. RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
  49. Request request = new Request.Builder()
  50. .url(PythonUrl + "/getPatentSimilarScore")
  51. .post(requestBody)
  52. .build();
  53. String reStr = Objects.requireNonNull(okHttpClient.newCall(request).execute().body()).string();
  54. getPatentSimilarScoreVo = JSONObject.parseObject(reStr, GetPatentSimilarScoreVO.class);
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR, "获取专利相似度失败");
  58. }
  59. return getPatentSimilarScoreVo;
  60. }
  61. public GetPatentSimilarScoreVO getPatentSimilarMess(GetPatentSimilarScoreDTO getPatentSimilarScoreDTO) {
  62. GetPatentSimilarScoreVO getPatentSimilarScoreVO = new GetPatentSimilarScoreVO();
  63. Integer projectId = getPatentSimilarScoreDTO.getProjectId();
  64. TechnicalCase technicalCase = technicalCaseService.getByProjectId(projectId);
  65. String text = technicalCase.getInventionPoint();
  66. if (text == null || text.trim().equals("")) {
  67. return getPatentSimilarScoreVO;
  68. }
  69. List<PatentScoreVO> donePatentScoreVo = new ArrayList<>();
  70. List<PatentScoreDTO> waitTOGetDTOs = new ArrayList<>();
  71. String code = MD5Util.toMD5(text);
  72. List<PatentScoreDTO> patentScoreDTOList = getPatentSimilarScoreDTO.getPatentScoreDTOList();
  73. patentScoreDTOList.forEach(item -> {
  74. String json = redisUtil.get(RedisConf.GET_PATENT_SIMILAR_SCORE + ":" + item.getPatentNo()+":"+code);
  75. if (!StringUtils.isEmpty(json)) {
  76. PatentScoreVO patentScoreVO = JSONObject.parseObject(json, PatentScoreVO.class);
  77. donePatentScoreVo.add(patentScoreVO);
  78. } else {
  79. waitTOGetDTOs.add(item);
  80. }
  81. });
  82. List<PatentScoreVO> patentScoreVOS = this.getPatentScore(waitTOGetDTOs, text);
  83. donePatentScoreVo.addAll(patentScoreVOS);
  84. getPatentSimilarScoreVO.setText(text);
  85. getPatentSimilarScoreVO.setPatentScoreDTOList(donePatentScoreVo);
  86. return getPatentSimilarScoreVO;
  87. }
  88. public List<PatentScoreVO> getPatentScore(List<PatentScoreDTO> waitTOGetDTOs, String text) {
  89. String code = MD5Util.toMD5(text);
  90. List<PatentScoreVO> patentScoreVOS = new ArrayList<>();
  91. if (waitTOGetDTOs.size() > 0) {
  92. this.formatPatentScoreVO(waitTOGetDTOs);
  93. GetPatentSimilarScoreDTO getSearchScoreDTO = new GetPatentSimilarScoreDTO();
  94. getSearchScoreDTO.setText(text);
  95. getSearchScoreDTO.setPatentScoreDTOList(waitTOGetDTOs);
  96. GetPatentSimilarScoreVO getPatentSimilarScoreVO = this.getPatentSimilarScore(getSearchScoreDTO);
  97. patentScoreVOS = getPatentSimilarScoreVO.getPatentScoreDTOList();
  98. patentScoreVOS.forEach(item -> {
  99. String jsons = JSONObject.toJSONString(item);
  100. redisUtil.setEx(RedisConf.GET_PATENT_SIMILAR_SCORE + ":" + item.getPatentNo()+":"+code, jsons, 1l, TimeUnit.DAYS);
  101. });
  102. }
  103. return patentScoreVOS;
  104. }
  105. public List<PatentScoreDTO> formatPatentScoreVO(List<PatentScoreDTO> waitTOGetDTOs) {
  106. waitTOGetDTOs.forEach(item -> {
  107. String rowAppNo = item.getRowApplicationNo();
  108. String patentNo = item.getPatentNo();
  109. String publicNo =item.getPublicNo();
  110. if (patentNo.startsWith("CN")) {
  111. String patentZhuLuStr = patentStarApiService.getCnBibApi(rowAppNo);
  112. if (patentZhuLuStr != null && !patentZhuLuStr.trim().equals("") && !patentZhuLuStr.equals("{}") && !patentZhuLuStr.contains("请求不合法")) {
  113. List<StarPatentVO> chinaPatentZhuLus = JSON.parseArray(patentZhuLuStr, StarPatentVO.class);
  114. StarPatentVO chinaPatentZhuLu = chinaPatentZhuLus.get(0);
  115. item.setAbstractStr(chinaPatentZhuLu.getAB());
  116. }
  117. } else {
  118. String patentZhuLuStr = patentStarApiService.getENBibApi(publicNo);
  119. if (patentZhuLuStr != null && !patentZhuLuStr.trim().equals("") && !patentZhuLuStr.equals("{}") && !patentZhuLuStr.contains("请求不合法")) {
  120. List<StarPatentVO> worldPatentZhuLus = JSON.parseArray(patentZhuLuStr, StarPatentVO.class);
  121. StarPatentVO worldPatentZhuLu = worldPatentZhuLus.get(0);
  122. item.setAbstractStr(worldPatentZhuLu.getAB());
  123. }
  124. }
  125. });
  126. return waitTOGetDTOs;
  127. }
  128. }