package cn.cslg.pas.service.business; import cn.cslg.pas.common.dto.GetAllPersonDTO; import cn.cslg.pas.common.dto.MergePersonQueryDTO; import cn.cslg.pas.common.dto.PatentColumnDTO; import cn.cslg.pas.common.dto.patentCount.GetEsAllPersonDTO; import cn.cslg.pas.common.model.cronModel.PersonnelVO; import cn.cslg.pas.common.model.cronModel.Records; import cn.cslg.pas.common.utils.CacheUtils; import cn.cslg.pas.common.utils.LoginUtils; import cn.cslg.pas.common.utils.parseQueryToTree.expressManager; import cn.cslg.pas.common.utils.parseQueryToTree.operateNode; import cn.cslg.pas.common.utils.parseQueryToTree.treeNode; import cn.cslg.pas.common.vo.business.GetAllPersonVO; import cn.cslg.pas.common.vo.business.MergePersonIdVO; import cn.cslg.pas.common.vo.business.MergePersonQueryVO; import cn.cslg.pas.common.vo.business.MergePersonVO; import cn.cslg.pas.domain.BaseEntity; import cn.cslg.pas.domain.business.FollowUp; import cn.cslg.pas.domain.business.MergePerson; import cn.cslg.pas.domain.es.Patent; import cn.cslg.pas.domain.es.PatentMergePerson; import cn.cslg.pas.domain.es.PersonAddress; import cn.cslg.pas.exception.UnLoginException; import cn.cslg.pas.mapper.MergePersonMapper; import cn.cslg.pas.service.business.es.EsPatentService; import cn.cslg.pas.service.business.es.EsService; import cn.cslg.pas.service.query.FormatQueryService; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.PageUtil; import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.elasticsearch._types.aggregations.Aggregation; import co.elastic.clients.elasticsearch._types.aggregations.AggregationBuilders; import co.elastic.clients.elasticsearch._types.aggregations.NestedAggregation; import co.elastic.clients.elasticsearch._types.aggregations.TermsAggregation; import co.elastic.clients.elasticsearch._types.query_dsl.Query; import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders; import co.elastic.clients.elasticsearch.core.SearchRequest; import co.elastic.clients.elasticsearch.core.SearchResponse; import co.elastic.clients.elasticsearch.core.search.Hit; import com.alibaba.fastjson.JSONArray; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.github.pagehelper.PageHelper; import com.google.gson.JsonArray; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.io.IOException; import java.util.*; import java.util.stream.Collectors; /** * @Author xiexiang * @Date 2024/01/02 */ @Slf4j @Service public class MergePersonService extends ServiceImpl { @Autowired private ElasticsearchClient client; @Autowired private CacheUtils cacheUtils; @Autowired private LoginUtils loginUtils; @Autowired private MergePersonMapper mergePersonMapper; @Autowired private FormatQueryService formatQueryService; @Autowired private EsPatentService esPatentService; @Autowired private EsService esService; @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class) public Integer mergePerson(MergePersonVO vo) throws IOException { Integer type = vo.getType(); List mergedNames = vo.getMergedName(); //获取登陆人信息 用于设置创建人 PersonnelVO personnelVO = new PersonnelVO(); try { personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); } catch (Exception e) { throw new UnLoginException("未登录"); } MergePerson person = new MergePerson(); BeanUtils.copyProperties(vo, person); person.setCreateId(personnelVO.getId()); person.setCreateTime(new Date()); person.insert(); //todo 关联相关专利 SearchRequest.Builder builder = new SearchRequest.Builder(); //设置查询索引 builder.index("patent"); List queries = new ArrayList<>(); if (type == 0) { for (String mergedName : mergedNames) { Query q = QueryBuilders.term(i -> i.field("applicant.name").value(mergedName)); Query query = QueryBuilders.nested(i -> i.path("applicant").query(q)); queries.add(query); } } else { for (String mergedName : mergedNames) { Query q = QueryBuilders.term(i -> i.field("inventor.name").value(mergedName)); Query query = QueryBuilders.nested(i -> i.path("inventor").query(q)); queries.add(query); } } Query query = QueryBuilders.bool(i -> i.should(queries)); builder.query(query); SearchResponse response = client.search(builder.build(), Patent.class); List> hits = response.hits().hits(); List ids = new ArrayList<>(); Map map = new HashMap<>(); for (Hit hit : hits) { String id = hit.id(); map.put(id, hit.source()); } if (!CollectionUtils.isEmpty(ids)) { List mergePersonList = new ArrayList<>(); PatentMergePerson mergePerson = new PatentMergePerson(); mergePerson.setName(vo.getName()); mergePerson.setProjectId(vo.getProjectId().toString()); mergePerson.setType("1"); mergePerson.setOrder(1); mergePersonList.add(mergePerson); for (String id : map.keySet()) { Patent patent = map.get(id); List mergeApplicant = patent.getMergeApplicant(); patent.setMergeApplicant(mergePersonList); patent.setMergeRightHolder(mergePersonList); esService.updateMergePerson(patent, id); } } return person.getId(); } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class) public Integer updateMergePerson(MergePersonVO vo) { //获取登陆人信息 用于设置创建人 PersonnelVO personnelVO = new PersonnelVO(); try { personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); } catch (Exception e) { throw new UnLoginException("未登录"); } MergePerson person = this.getById(vo.getId()); if (ObjectUtil.isNotEmpty(person)) { BeanUtils.copyProperties(vo, person); person.setCreateId(personnelVO.getId()); person.setCreateTime(new Date()); person.updateById(); //todo 关联相关专利 } else { person = new MergePerson(); } return person.getId(); } public Records selectMergePersonList(MergePersonQueryVO vo) { List list = new ArrayList<>(); IPage page = new Page<>(vo.getPageNum(), vo.getPageSize()); LambdaQueryWrapper wrapper = new LambdaQueryWrapper() .eq(StringUtils.isNotEmpty(vo.getCountry()), MergePerson::getCountry, vo.getCountry()) .like(StringUtils.isNotEmpty(vo.getName()), MergePerson::getName, vo.getName()); IPage record = mergePersonMapper.selectPage(page, wrapper); for (MergePerson person : record.getRecords()) { MergePersonQueryDTO dto = new MergePersonQueryDTO(); dto.setId(person.getId()); dto.setName(person.getName()); dto.setAddress(person.getAddress()); dto.setCountry(person.getCountry()); dto.setRemark(person.getRemark()); dto.setCreateTime(person.getCreateTime()); list.add(dto); } Records records = new Records(); records.setCurrent(record.getCurrent()); records.setSize(record.getSize()); records.setData(list); records.setTotal(record.getTotal()); return records; } public Records getMergePerson(GetAllPersonVO vo) throws Exception { String searchCondition = ""; Integer projectId = vo.getProjectId(); Integer taskId = vo.getTaskId(); Integer pageNum = vo.getPageNum(); Integer pageSize = vo.getPageSize(); if (taskId != null) { searchCondition = "taskId = " + taskId; } else { if (projectId != null) { searchCondition = "projectId = " + projectId; } } SearchRequest.Builder builder = new SearchRequest.Builder(); //设置查询索引 builder.index("patent"); //1. 解析检索条件 treeNode tree = expressManager.getInstance().Parse(searchCondition, false); //格式化检索式 //3. 从es中检索数据 Query q = formatQueryService.EsQueryToQuery((operateNode) tree, "patent"); //分页 if (pageNum > 0 && pageSize > 0) { builder.from((pageNum - 1) * pageSize).size(pageSize); } builder.query(q); SearchResponse response = client.search(builder.build(), Patent.class); List> hits = response.hits().hits(); long total = response.hits().total().value(); List personDTOS = new ArrayList<>(); for (Hit hit : hits) { Patent esMess = hit.source(); PersonAddress applicantAddr = null; GetEsAllPersonDTO personDTO = new GetEsAllPersonDTO(); if (ObjectUtil.isNotEmpty(esMess.getApplicantAddr())) { applicantAddr = esMess.getApplicantAddr(); personDTO.setAddress(applicantAddr.getAddress()); personDTO.setCountry(applicantAddr.getCountry()); } personDTO.setApplicant(esPatentService.loadName(esMess.getApplicant())); personDTO.setRightHolder(esPatentService.loadName(esMess.getRightHolder())); personDTO.setInventor(esPatentService.loadName(esMess.getInventor())); personDTOS.add(personDTO); } List applicants = new ArrayList<>(); List inventors = new ArrayList<>(); for (GetEsAllPersonDTO personDTO : personDTOS) { applicants.addAll(personDTO.getApplicant()); inventors.addAll(personDTO.getInventor()); } List applicantList = applicants.stream().distinct().collect(Collectors.toList()); List inventorList = inventors.stream().distinct().collect(Collectors.toList()); IPage page = new Page<>(vo.getPageNum(), vo.getPageSize()); LambdaQueryWrapper wrapper = new LambdaQueryWrapper() .eq(StringUtils.isNotEmpty(vo.getProjectId().toString()), MergePerson::getProjectId, vo.getProjectId()); IPage record = mergePersonMapper.selectPage(page, wrapper); List applicantMergeList = new ArrayList<>(); List inventorMergeList = new ArrayList<>(); for (MergePerson person : record.getRecords()) { if (person.getType() == 0) { applicantMergeList = JSONArray.parseArray(person.getMergedName(), String.class); } else if (person.getType() == 2) { inventorMergeList = JSONArray.parseArray(person.getMergedName(), String.class); } } applicantList.removeAll(applicantMergeList); inventorList.removeAll(inventorMergeList); List applicantDTOS = new ArrayList<>(); List inventorDTOS = new ArrayList<>(); for (String applicant : applicantList) { GetAllPersonDTO dto = new GetAllPersonDTO(); dto.setName(applicant); dto.setType(0); applicantDTOS.add(dto); } for (String inventor : inventorList) { GetAllPersonDTO dto = new GetAllPersonDTO(); dto.setName(inventor); dto.setType(2); inventorDTOS.add(dto); } Records records = new Records(); records.setCurrent(vo.getPageNum().longValue()); records.setSize(vo.getPageSize().longValue()); if (vo.getType() == 0) { int size = applicantDTOS.size(); records.setData(applicantDTOS.stream().limit(vo.getPageSize()).collect(Collectors.toList())); if (total >= size) { records.setTotal((long)size); } else { records.setTotal(total); } } else if (vo.getType() == 2) { int size = inventorDTOS.size(); records.setData(inventorDTOS.stream().limit(vo.getPageSize()).collect(Collectors.toList())); if (total >= size) { records.setTotal((long)size); } else { records.setTotal(total); } } return records; } public Integer delMergePerson(MergePersonIdVO vo) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper() .eq(BaseEntity::getId, vo.getId()); this.remove(wrapper); //todo 删掉相关合并的名称 return vo.getId(); } }