EsValueCurveService.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. package cn.cslg.pas.service.business.es;
  2. import cn.cslg.pas.common.core.base.IfConstant;
  3. import cn.cslg.pas.common.dto.es.EsCustomFieldValueDTO;
  4. import cn.cslg.pas.common.dto.es.EsValueCurveDTO;
  5. import cn.cslg.pas.common.dto.es.SelectValueCurveDTO;
  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.request.OrderDTO;
  9. import cn.cslg.pas.common.utils.CacheUtils;
  10. import cn.cslg.pas.common.utils.LoginUtils;
  11. import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
  12. import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
  13. import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
  14. import cn.cslg.pas.common.vo.EsConfigVO;
  15. import cn.cslg.pas.common.vo.es.EsCustomFieldBatchVO;
  16. import cn.cslg.pas.common.vo.es.EsValueCurveVO;
  17. import cn.cslg.pas.domain.BaseEntity;
  18. import cn.cslg.pas.domain.business.PermissionRecord;
  19. import cn.cslg.pas.domain.business.Product;
  20. import cn.cslg.pas.domain.business.ProductMarketData;
  21. import cn.cslg.pas.domain.es.Patent;
  22. import cn.cslg.pas.exception.XiaoShiException;
  23. import cn.cslg.pas.mapper.PermissionRecordMapper;
  24. import cn.cslg.pas.mapper.ProductMapper;
  25. import cn.cslg.pas.mapper.ProductMarketDataMapper;
  26. import cn.cslg.pas.service.business.CommonService;
  27. import cn.cslg.pas.service.query.FormatQueryService;
  28. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  29. import co.elastic.clients.elasticsearch._types.SortOrder;
  30. import co.elastic.clients.elasticsearch._types.query_dsl.Query;
  31. import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
  32. import co.elastic.clients.elasticsearch.core.SearchRequest;
  33. import co.elastic.clients.elasticsearch.core.SearchResponse;
  34. import co.elastic.clients.elasticsearch.core.search.Hit;
  35. import com.alibaba.fastjson.JSON;
  36. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  37. import lombok.RequiredArgsConstructor;
  38. import org.apache.commons.lang3.StringUtils;
  39. import org.springframework.beans.factory.annotation.Autowired;
  40. import org.springframework.context.annotation.Lazy;
  41. import org.springframework.stereotype.Service;
  42. import org.springframework.util.CollectionUtils;
  43. import java.io.IOException;
  44. import java.math.BigDecimal;
  45. import java.text.SimpleDateFormat;
  46. import java.util.ArrayList;
  47. import java.util.HashMap;
  48. import java.util.List;
  49. import java.util.Map;
  50. import java.util.stream.Collectors;
  51. @Service
  52. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  53. public class EsValueCurveService {
  54. private final ElasticsearchClient client;
  55. @Autowired
  56. private CacheUtils cacheUtils;
  57. @Autowired
  58. private LoginUtils loginUtils;
  59. @Autowired
  60. private EsService esService;
  61. @Autowired
  62. private FormatQueryService formatQueryService;
  63. @Autowired
  64. private ProductMarketDataMapper productMarketDataMapper;
  65. @Autowired
  66. private PermissionRecordMapper permissionRecordMapper;
  67. @Autowired
  68. private ProductMapper productMapper;
  69. /**
  70. * 查询价值曲线
  71. * @param vo
  72. * @return
  73. * @throws Exception
  74. */
  75. public List<EsValueCurveDTO> selectValueCurve(EsValueCurveVO vo) throws Exception {
  76. List<String> isAdd = vo.getIsAdd();
  77. List<String> isDel = vo.getIsDelete();
  78. Integer startNum = vo.getStartNumber();
  79. Integer endNum = vo.getEndNumber();
  80. Integer timeType = vo.getTimeType();
  81. //获取登陆人信息 用于设置创建人
  82. PersonnelVO personnelVO = new PersonnelVO();
  83. try {
  84. personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  85. } catch (Exception e) {
  86. throw new XiaoShiException("未登录");
  87. }
  88. Integer tenantId = personnelVO.getTenantId();
  89. Map<String, List<Integer>> map = new HashMap<>();
  90. if (!CollectionUtils.isEmpty(isAdd) && startNum <= 1 && endNum < 1) {
  91. isAdd.removeAll(isDel);
  92. map = this.getProductIdByPatentNo(isAdd,tenantId);
  93. } else if (startNum >= 1 && endNum > 0) {
  94. List<String> patentNos = this.getPatentNos(vo);
  95. map = this.getProductIdByPatentNo(patentNos,tenantId);
  96. }
  97. return this.getValueCurveList(map, timeType,tenantId);
  98. }
  99. /**
  100. * 提取返回数据
  101. *
  102. * @param map
  103. * @param timeType
  104. * @return
  105. */
  106. public List<EsValueCurveDTO> getValueCurveList(Map<String, List<Integer>> map, Integer timeType, Integer tenantId) {
  107. List<EsValueCurveDTO> curveDTOS = new ArrayList<>();
  108. if (!CollectionUtils.isEmpty(map)) {
  109. for (String patentNo : map.keySet()) {
  110. List<SelectValueCurveDTO> listLicensee = new ArrayList<>();
  111. List<SelectValueCurveDTO> listSale = new ArrayList<>();
  112. //获取许可记录
  113. List<SelectValueCurveDTO> licenseeList = this.selectLicensee(patentNo, timeType,tenantId);
  114. if (!CollectionUtils.isEmpty(licenseeList)) {
  115. Map<String, List<SelectValueCurveDTO>> licenseeMap = licenseeList.stream().collect(Collectors.groupingBy(SelectValueCurveDTO::getLicenseTime));
  116. for (String key : licenseeMap.keySet()) {
  117. List<SelectValueCurveDTO> licenseeDTOS = licenseeMap.get(key);
  118. BigDecimal sum = new BigDecimal(0);
  119. for (SelectValueCurveDTO licenseeDTO : licenseeDTOS) {
  120. BigDecimal licenseeFee = new BigDecimal(String.valueOf(licenseeDTO.getLicenseFee()));
  121. sum = sum.add(licenseeFee);
  122. }
  123. SelectValueCurveDTO curveDTO = new SelectValueCurveDTO();
  124. if (sum.compareTo(new BigDecimal(0)) > 0) {
  125. curveDTO.setLicenseFee(sum.doubleValue());
  126. }
  127. curveDTO.setLicenseTime(key);
  128. curveDTO.setTimeType(licenseeDTOS.get(0).getTimeType());
  129. listLicensee.add(curveDTO);
  130. }
  131. }
  132. //获取营销数据
  133. List<Integer> productIds = map.get(patentNo);
  134. if (!CollectionUtils.isEmpty(productIds)) {
  135. List<SelectValueCurveDTO> saleList = this.selectSale(productIds, timeType);
  136. if (!CollectionUtils.isEmpty(saleList)) {
  137. Map<String, List<SelectValueCurveDTO>> saleMap = saleList.stream().collect(Collectors.groupingBy(SelectValueCurveDTO::getSaleTime));
  138. for (String key : saleMap.keySet()) {
  139. List<SelectValueCurveDTO> saleLDTOS = saleMap.get(key);
  140. //销售额
  141. BigDecimal count = new BigDecimal(0);
  142. //自定义许可费
  143. BigDecimal sum = new BigDecimal(0);
  144. for (SelectValueCurveDTO saleLDTO : saleLDTOS) {
  145. BigDecimal customLicensee = new BigDecimal(String.valueOf(saleLDTO.getCustomLicense()));
  146. sum = sum.add(customLicensee);
  147. BigDecimal saleMoney = new BigDecimal(String.valueOf(saleLDTO.getSaleMoney()));
  148. count = count.add(saleMoney);
  149. }
  150. SelectValueCurveDTO curveDTO = new SelectValueCurveDTO();
  151. if (sum.compareTo(new BigDecimal(0)) > 0) {
  152. curveDTO.setLicenseFee(sum.doubleValue());
  153. }
  154. if (count.compareTo(new BigDecimal(0)) > 0) {
  155. curveDTO.setCustomLicense(count.doubleValue());
  156. }
  157. curveDTO.setSaleTime(key);
  158. curveDTO.setSaleArea(saleLDTOS.get(0).getSaleArea());
  159. curveDTO.setTimeType(saleLDTOS.get(0).getTimeType());
  160. listSale.add(curveDTO);
  161. }
  162. }
  163. }
  164. List<EsValueCurveDTO> data = this.mergeData(listLicensee, listSale);
  165. curveDTOS.addAll(data);
  166. }
  167. }
  168. return curveDTOS;
  169. }
  170. /**
  171. * 提取合并数据
  172. * @param listLicensee
  173. * @param listSale
  174. * @return
  175. */
  176. public List<EsValueCurveDTO> mergeData(List<SelectValueCurveDTO> listLicensee,List<SelectValueCurveDTO> listSale) {
  177. List<EsValueCurveDTO> list = new ArrayList<>();
  178. if (!CollectionUtils.isEmpty(listLicensee) && !CollectionUtils.isEmpty(listSale)) {
  179. for (SelectValueCurveDTO licensee : listLicensee) {
  180. String licenseTime = licensee.getLicenseTime();
  181. for (SelectValueCurveDTO sale : listSale) {
  182. String saleTime = sale.getSaleTime();
  183. if (licenseTime.equals(saleTime)) {
  184. EsValueCurveDTO dto = new EsValueCurveDTO();
  185. dto.setSaleArea(sale.getSaleArea());
  186. dto.setSaleMoney(sale.getSaleMoney());
  187. dto.setCustomLicense(sale.getCustomLicense());
  188. dto.setTime(saleTime);
  189. dto.setLicenseFee(licensee.getLicenseFee());
  190. dto.setTimeType(sale.getTimeType());
  191. list.add(dto);
  192. }
  193. }
  194. }
  195. } else if (!CollectionUtils.isEmpty(listLicensee) && CollectionUtils.isEmpty(listSale)) {
  196. for (SelectValueCurveDTO licensee : listLicensee) {
  197. EsValueCurveDTO dto = new EsValueCurveDTO();
  198. dto.setTime(licensee.getLicenseTime());
  199. dto.setLicenseFee(licensee.getLicenseFee());
  200. dto.setTimeType(licensee.getTimeType());
  201. list.add(dto);
  202. }
  203. } else if (!CollectionUtils.isEmpty(listSale) && CollectionUtils.isEmpty(listLicensee)) {
  204. for (SelectValueCurveDTO sale : listSale) {
  205. EsValueCurveDTO dto = new EsValueCurveDTO();
  206. dto.setSaleArea(sale.getSaleArea());
  207. dto.setSaleMoney(sale.getSaleMoney());
  208. dto.setCustomLicense(sale.getCustomLicense());
  209. dto.setTime(sale.getSaleTime());
  210. dto.setTimeType(sale.getTimeType());
  211. list.add(dto);
  212. }
  213. }
  214. if (!CollectionUtils.isEmpty(list)) {
  215. for (EsValueCurveDTO dto : list) {
  216. listLicensee.removeIf(licensee -> licensee.getLicenseTime().equals(dto.getTime()));
  217. }
  218. for (EsValueCurveDTO dto : list) {
  219. listSale.removeIf(sale -> sale.getSaleTime().equals(dto.getTime()));
  220. }
  221. if (!CollectionUtils.isEmpty(listLicensee)) {
  222. for (SelectValueCurveDTO licensee : listLicensee) {
  223. EsValueCurveDTO dto = new EsValueCurveDTO();
  224. dto.setTime(licensee.getLicenseTime());
  225. dto.setLicenseFee(licensee.getLicenseFee());
  226. dto.setTimeType(licensee.getTimeType());
  227. list.add(dto);
  228. }
  229. }
  230. if (!CollectionUtils.isEmpty(listSale)) {
  231. for (SelectValueCurveDTO sale : listSale) {
  232. EsValueCurveDTO dto = new EsValueCurveDTO();
  233. dto.setSaleArea(sale.getSaleArea());
  234. dto.setSaleMoney(sale.getSaleMoney());
  235. dto.setCustomLicense(sale.getCustomLicense());
  236. dto.setTime(sale.getSaleTime());
  237. dto.setTimeType(sale.getTimeType());
  238. list.add(dto);
  239. }
  240. }
  241. }
  242. return list;
  243. }
  244. /**
  245. * 查询营销数据
  246. * @param productIds
  247. * @param timeType
  248. * @return
  249. */
  250. public List<SelectValueCurveDTO> selectSale(List<Integer> productIds, Integer timeType) {
  251. List<SelectValueCurveDTO> curveDTOS = new ArrayList<>();
  252. if (timeType == 0) {
  253. curveDTOS = productMarketDataMapper.selectSaleMonthList(productIds);
  254. if (!CollectionUtils.isEmpty(curveDTOS)) {
  255. curveDTOS.forEach(i -> i.setTimeType(timeType));
  256. }
  257. } else if (timeType == 2) {
  258. curveDTOS = productMarketDataMapper.selectSaleYearList(productIds);
  259. if (!CollectionUtils.isEmpty(curveDTOS)) {
  260. curveDTOS.forEach(i -> i.setTimeType(timeType));
  261. }
  262. } else {
  263. curveDTOS = productMarketDataMapper.selectSaleQuarterList(productIds);
  264. if (!CollectionUtils.isEmpty(curveDTOS)) {
  265. curveDTOS.forEach(i -> i.setTimeType(timeType));
  266. }
  267. }
  268. return curveDTOS;
  269. }
  270. /**
  271. * 查询许可记录
  272. * @param patentNo
  273. * @param timeType
  274. * @return
  275. */
  276. public List<SelectValueCurveDTO> selectLicensee(String patentNo, Integer timeType,Integer tenantId) {
  277. List<SelectValueCurveDTO> curveDTOS = new ArrayList<>();
  278. if (timeType == 0) {
  279. curveDTOS = permissionRecordMapper.selectLicenseeMonthList(patentNo,tenantId);
  280. if (!CollectionUtils.isEmpty(curveDTOS)) {
  281. curveDTOS.forEach(i -> i.setTimeType(timeType));
  282. }
  283. } else if (timeType == 2) {
  284. curveDTOS = permissionRecordMapper.selectLicenseeYearList(patentNo,tenantId);
  285. if (!CollectionUtils.isEmpty(curveDTOS)) {
  286. curveDTOS.forEach(i -> i.setTimeType(timeType));
  287. }
  288. } else {
  289. curveDTOS = permissionRecordMapper.selectLicenseeQuarterList(patentNo,tenantId);
  290. if (!CollectionUtils.isEmpty(curveDTOS)) {
  291. curveDTOS.forEach(i -> i.setTimeType(timeType));
  292. }
  293. }
  294. return curveDTOS;
  295. }
  296. /**
  297. * 获取专利号列表
  298. *
  299. * @param vo
  300. * @return
  301. * @throws Exception
  302. */
  303. public List<String> getPatentNos(EsValueCurveVO vo) throws Exception {
  304. Integer taskId = vo.getTaskId();
  305. Integer projectId = vo.getProjectId();
  306. Integer startNum = vo.getStartNumber();
  307. Integer endNum = vo.getEndNumber();
  308. List<String> isAdd = new ArrayList<>();
  309. if (!CollectionUtils.isEmpty(vo.getIsAdd())) {
  310. isAdd = vo.getIsAdd();
  311. }
  312. List<String> isDel = new ArrayList<>();
  313. if (!CollectionUtils.isEmpty(vo.getIsDelete())) {
  314. isDel = vo.getIsDelete();
  315. }
  316. String searchCondition = "";
  317. if (StringUtils.isNotEmpty(vo.getSearchQuery())) {
  318. searchCondition = vo.getSearchQuery();
  319. }
  320. List<EsCustomFieldValueDTO> customFields = vo.getCustomFields();
  321. if (!CollectionUtils.isEmpty(customFields)) {
  322. searchCondition = esService.parseCustomField(customFields, projectId, taskId);
  323. }
  324. if (taskId != null) {
  325. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  326. searchCondition = "taskId = " + taskId + " AND " + searchCondition;
  327. } else {
  328. searchCondition = "taskId = " + taskId;
  329. }
  330. } else {
  331. if (projectId != null) {
  332. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  333. searchCondition = "projectId = " + projectId + " AND " + searchCondition;
  334. } else {
  335. searchCondition = "projectId = " + projectId;
  336. }
  337. }
  338. }
  339. SearchRequest.Builder builder = new SearchRequest.Builder();
  340. //设置查询索引
  341. builder.index("patent");
  342. //1. 解析检索条件
  343. treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
  344. //格式化检索式
  345. //3. 从es中检索数据
  346. Query q = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
  347. builder.query(q);
  348. List<OrderDTO> dtoList = vo.getOrderDTOList();
  349. if (!CollectionUtils.isEmpty(dtoList)) {
  350. String json = CommonService.readJsonFile("patent.json");
  351. List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
  352. for (OrderDTO orderDTO : dtoList) {
  353. EsConfigVO configVO = esConfigVOS.stream().filter(item -> item.getField().equals(orderDTO.getOrderBy())).findFirst().orElse(null);
  354. if (configVO != null) {
  355. if (orderDTO.getOrderType().equals(IfConstant.NO)) {
  356. builder.sort(sortOptionsBuilder -> sortOptionsBuilder
  357. .field(fieldSortBuilder -> fieldSortBuilder
  358. .field(configVO.getEsField()).order(SortOrder.Asc)));
  359. } else {
  360. builder.sort(sortOptionsBuilder -> sortOptionsBuilder
  361. .field(fieldSortBuilder -> fieldSortBuilder
  362. .field(configVO.getEsField()).order(SortOrder.Desc)));
  363. }
  364. }
  365. }
  366. } else {
  367. builder.sort(sortOptionsBuilder -> sortOptionsBuilder
  368. .field(fieldSortBuilder -> fieldSortBuilder
  369. .field("patent_no.keyword").order(SortOrder.Desc)));
  370. }
  371. int number = endNum - startNum;
  372. if (startNum > 0 && endNum > startNum) {
  373. builder.from(startNum - 1).size(number);
  374. }
  375. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  376. List<Hit<Patent>> hits = response.hits().hits();
  377. List<String> patentNos = new ArrayList<>();
  378. for (Hit<Patent> hit : hits) {
  379. Patent patent = hit.source();
  380. patentNos.add(patent.getPatentNo());
  381. }
  382. patentNos.addAll(isAdd);
  383. patentNos.removeAll(isDel);
  384. return patentNos.stream().distinct().collect(Collectors.toList());
  385. }
  386. /**
  387. * 根据专利号获取productId
  388. * @param list
  389. * @return
  390. * @throws IOException
  391. */
  392. public Map<String, List<Integer>> getProductIdByPatentNo(List<String> list,Integer tenantId) throws IOException {
  393. Map<String, List<Integer>> map = new HashMap<>();
  394. if (!CollectionUtils.isEmpty(list)) {
  395. for (String patentNo : list) {
  396. List<Integer> productIds = new ArrayList<>();
  397. SearchRequest.Builder builder = new SearchRequest.Builder();
  398. //设置查询索引
  399. builder.index("patent");
  400. //申请号
  401. Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
  402. //公开号
  403. Query q2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
  404. //授权号
  405. Query q3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
  406. Query bool = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  407. Query q = QueryBuilders.exists(i -> i.field("product_id"));
  408. Query query = QueryBuilders.hasParent(parent -> parent.parentType("patent").query(bool));
  409. Query bool1 = QueryBuilders.bool(i -> i.must(q,query));
  410. builder.query(bool1);
  411. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  412. List<Hit<Patent>> hits = response.hits().hits();
  413. for (Hit<Patent> hit : hits) {
  414. Patent patent = hit.source();
  415. if (patent.getProductId() != null) {
  416. productIds.add(patent.getProductId());
  417. }
  418. }
  419. List<Integer> ids = new ArrayList<>();
  420. List<Integer> collect = productIds.stream().distinct().collect(Collectors.toList());
  421. List<Product> products = productMapper.selectBatchIds(collect);
  422. if (!CollectionUtils.isEmpty(products)) {
  423. ids = products.stream().filter(i -> i.getTenantId().equals(tenantId)).map(BaseEntity::getId).collect(Collectors.toList());
  424. }
  425. map.put(patentNo, ids);
  426. }
  427. }
  428. return map;
  429. }
  430. }