package cn.cslg.pas.service.business; import cn.cslg.pas.common.dto.AddTechnicalCaseDTO; import cn.cslg.pas.common.dto.QuerySimilarInventionPointDTO; import cn.cslg.pas.common.dto.TechnicalCaseIdDTO; import cn.cslg.pas.common.model.cronModel.Personnel; import cn.cslg.pas.common.model.cronModel.PersonnelVO; import cn.cslg.pas.common.model.cronModel.SystemFile; import cn.cslg.pas.common.utils.CacheUtils; import cn.cslg.pas.common.utils.LoginUtils; import cn.cslg.pas.common.vo.EditInventionPointVO; import cn.cslg.pas.common.vo.QueryInventionPointVO; import cn.cslg.pas.common.vo.TechnicalCaseVO; import cn.cslg.pas.domain.BaseEntity; import cn.cslg.pas.domain.business.*; import cn.cslg.pas.domain.es.Patent; import cn.cslg.pas.domain.es.TechnicalCasePatent; import cn.cslg.pas.exception.XiaoShiException; import cn.cslg.pas.mapper.AssoTechnicalCaseFileMapper; import cn.cslg.pas.mapper.ProjectMapper; import cn.cslg.pas.mapper.TechnicalCaseMapper; import cn.cslg.pas.service.common.FileManagerService; import cn.cslg.pas.service.permissions.PermissionService; import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.elasticsearch._types.query_dsl.Like; import co.elastic.clients.elasticsearch._types.query_dsl.Query; import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders; import co.elastic.clients.elasticsearch.core.*; import co.elastic.clients.elasticsearch.core.search.Hit; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson2.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; 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.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @Service @Slf4j @RequiredArgsConstructor(onConstructor_ = {@Lazy}) public class TechnicalCaseService extends ServiceImpl { private final CacheUtils cacheUtils; private final LoginUtils loginUtils; private final ElasticsearchClient client; @Autowired private FileManagerService fileManagerService; @Autowired private PermissionService permissionService; @Autowired private AssoTechnicalCaseFileMapper assoTechnicalCaseFileMapper; @Autowired private AssoTechnicalCaseFileService assoTechnicalCaseFileService; @Autowired private TechnicalCaseMapper technicalCaseMapper; @Autowired private ProjectMapper projectMapper; @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class) public TechnicalCaseIdDTO addOrUpdateTechnicalCase(AddTechnicalCaseDTO vo) throws Exception { PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); TechnicalCaseIdDTO caseIdDTO = new TechnicalCaseIdDTO(); if (vo.getTechnicalCaseId() == null) { if (vo.getProjectId() == null) { Project project = new Project(); project.setType(4); project.setCreateId(personnelVO.getId()); project.setTenantId(personnelVO.getTenantId()); project.insert(); vo.setProjectId(project.getId()); NoveltyProject noveltyProject = new NoveltyProject(); noveltyProject.setProjectId(project.getId()); noveltyProject.insert(); } TechnicalCase technicalCase = new TechnicalCase(); BeanUtils.copyProperties(vo, technicalCase); technicalCase.insert(); caseIdDTO.setProjectId(vo.getProjectId()); caseIdDTO.setTechnicalCaseId(technicalCase.getId()); //保存es TechnicalCasePatent casePatent = new TechnicalCasePatent(); casePatent.setProjectId(vo.getProjectId()); casePatent.setInventionPoint(technicalCase.getInventionPoint()); String technicalCasePatentId = this.addTechnicalCasePatent(casePatent); if (StringUtils.isEmpty(technicalCasePatentId)) { throw new XiaoShiException("发明点保存Es失败"); } } else { TechnicalCase technicalCase = technicalCaseMapper.selectById(vo.getTechnicalCaseId()); BeanUtils.copyProperties(vo, technicalCase); technicalCase.updateById(); caseIdDTO.setTechnicalCaseId(technicalCase.getId()); caseIdDTO.setProjectId(technicalCase.getProjectId()); //保存es // EditInventionPointVO pointVO = this.selectTechnicalCasePatent(technicalCase.getProjectId()); // if (!StringUtils.equals(vo.getInventionPoint(), pointVO.getInventionPoint())) { // TechnicalCasePatent casePatent = new TechnicalCasePatent(); //// casePatent.setProjectId(vo.getProjectId()); // casePatent.setInventionPoint(vo.getInventionPoint()); // Integer count = this.updateTechnicalCasePatent(casePatent, pointVO.getPatentId()); // if (count < 1) { // throw new XiaoShiException("发明点更新Es失败"); // } // } } List technicalCaseFiles = assoTechnicalCaseFileMapper.selectList(new LambdaQueryWrapper() .eq(AssoTechnicalCaseFile::getTechnicalCaseId, caseIdDTO.getTechnicalCaseId())); if (!CollectionUtils.isEmpty(technicalCaseFiles)) { List collect = technicalCaseFiles.stream().map(BaseEntity::getId).collect(Collectors.toList()); assoTechnicalCaseFileMapper.deleteBatchIds(collect); } //添加技术方案和图片关联 List assoTechnicalCaseFiles = new ArrayList<>(); List fileGuids = vo.getFileGuids(); if (!CollectionUtils.isEmpty(fileGuids)) { for (String fileGuid : fileGuids) { AssoTechnicalCaseFile assoTechnicalCaseFile = new AssoTechnicalCaseFile(); assoTechnicalCaseFile.setTechnicalCaseId(caseIdDTO.getTechnicalCaseId()); assoTechnicalCaseFile.setFileGuid(fileGuid); assoTechnicalCaseFile.setCreateId(personnelVO.getId()); assoTechnicalCaseFiles.add(assoTechnicalCaseFile); } if (!CollectionUtils.isEmpty(assoTechnicalCaseFiles)) { assoTechnicalCaseFileService.saveBatch(assoTechnicalCaseFiles); } } return caseIdDTO; } public TechnicalCaseVO queryTechnicalCase(TechnicalCaseIdDTO vo) { TechnicalCase technicalCase = this.getOne(new LambdaQueryWrapper() .eq(TechnicalCase::getProjectId, vo.getProjectId())); TechnicalCaseVO technicalCaseVO = new TechnicalCaseVO(); if (ObjectUtils.isNotEmpty(technicalCase)) { BeanUtils.copyProperties(technicalCase, technicalCaseVO); technicalCaseVO.setTechnicalCaseId(technicalCase.getId()); List assoTechnicalCaseFiles = assoTechnicalCaseFileMapper.selectList(new LambdaQueryWrapper() .eq(AssoTechnicalCaseFile::getTechnicalCaseId, technicalCase.getId())); if (!CollectionUtils.isEmpty(assoTechnicalCaseFiles)) { List guids = assoTechnicalCaseFiles.stream().map(AssoTechnicalCaseFile::getFileGuid).collect(Collectors.toList()); String res = null; try { res = fileManagerService.getSystemFileFromFMS(guids); } catch (IOException e) { e.printStackTrace(); } if (StringUtils.isNotEmpty(res)) { List systemFiles = JSON.parseArray(res, SystemFile.class); technicalCaseVO.setSystemFileList(systemFiles); } } } return technicalCaseVO; } public void deleteInventPoint(Integer projectId) throws Exception { EditInventionPointVO pointVO = this.selectTechnicalCasePatent(projectId); if (ObjectUtils.isNotEmpty(pointVO) && pointVO.getPatentId() != null) { Integer count = delete(Arrays.asList(pointVO.getPatentId())); if (count < 1) { throw new XiaoShiException("删除发明点失败"); } } } public List querySimilarInventionPoint(QuerySimilarInventionPointDTO vo) throws IOException { String content = vo.getContent(); Integer projectId = vo.getProjectId(); SearchRequest.Builder builder = new SearchRequest.Builder(); //设置查询索引 builder.index("technicalPatent"); Like likeContent = Like.of(i -> i.text(content)); Query query = QueryBuilders.moreLikeThis(i -> i.fields("invention_point").like(likeContent) .minTermFreq(1).maxQueryTerms(12).minDocFreq(1).minWordLength(2)); Query bool = null; if (projectId != null) { Query query1 = QueryBuilders.term(i -> i.field("project_id").value(projectId)); bool = QueryBuilders.bool(i -> i.must(query).mustNot(query1)); } else { bool = QueryBuilders.bool(i -> i.must(query)); } builder.query(bool); builder.size(5); //解除最大条数限制 // builder.trackTotalHits(i -> i.enabled(true)); SearchResponse response = client.search(builder.build(), TechnicalCasePatent.class); List> hits = response.hits().hits(); List inventionPointVOS = new ArrayList<>(); for (Hit hit : hits) { TechnicalCasePatent casePatent = hit.source(); QueryInventionPointVO inventionPointVO = new QueryInventionPointVO(); inventionPointVO.setProjectId(casePatent.getProjectId()); inventionPointVO.setInventionPoint(casePatent.getInventionPoint()); inventionPointVOS.add(inventionPointVO); } if (!CollectionUtils.isEmpty(inventionPointVOS)) { List projectIds = inventionPointVOS.stream().map(QueryInventionPointVO::getProjectId).collect(Collectors.toList()); List projects = projectMapper.selectBatchIds(projectIds); List createIds = projects.stream().map(Project::getCreateId).collect(Collectors.toList()); //查询创建人名称 List personnels = new ArrayList<>(); if (!CollectionUtils.isEmpty(createIds)) { String res = permissionService.getPersonnelByIdsFromPCS(createIds); JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject(res); personnels = JSON.parseArray(jsonObject.getString("data"), Personnel.class); } for (QueryInventionPointVO pointVO : inventionPointVOS) { Project project = projects.stream().filter(i -> i.getId().equals(pointVO.getProjectId())).findFirst().orElse(null); if (ObjectUtils.isNotEmpty(project)) { pointVO.setCreateId(project.getCreateId()); pointVO.setCreateTime(project.getCreateTime()); //装载人员信息 Personnel personnel = personnels.stream().filter(item -> item.getId().equals(pointVO.getCreateId())).findFirst().orElse(null); if (personnel != null) { pointVO.setCreateName(personnel.getPersonnelName()); } } } } return inventionPointVOS; } //查询TechnicalCasePatent public EditInventionPointVO selectTechnicalCasePatent(Integer projectId) throws Exception { EditInventionPointVO pointVO = new EditInventionPointVO(); SearchRequest.Builder builder = new SearchRequest.Builder(); //设置查询索引 builder.index("technicalPatent"); Query query = QueryBuilders.term(i -> i.field("project_id").value(projectId)); builder.query(query); SearchResponse response = client.search(builder.build(), TechnicalCasePatent.class); List> hits = response.hits().hits(); for (Hit hit : hits) { pointVO.setPatentId(hit.id()); TechnicalCasePatent patent = hit.source(); pointVO.setProjectId(patent.getProjectId()); pointVO.setInventionPoint(patent.getInventionPoint()); break; } return pointVO; } //添加TechnicalCasePatent public String addTechnicalCasePatent(TechnicalCasePatent casePatent) throws Exception { IndexResponse indexResponse = client.index(i -> i .index("technicalPatent") //传入user对象 .document(casePatent) ); return indexResponse.id(); } //更新TechnicalCasePatent public Integer updateTechnicalCasePatent(TechnicalCasePatent patent, String id) { UpdateRequest req; req = UpdateRequest.of( b -> b.index("technicalPatent").id(id) .doc(patent)); try { client.update(req, TechnicalCasePatent.class); return 1; } catch (IOException e) { return -1; } } //删除TechnicalCasePatent public Integer delete(List ids) { Query query = QueryBuilders.ids(n -> n.values(ids)); DeleteByQueryRequest request = DeleteByQueryRequest.of(i -> i.index("technicalPatent").refresh(true).query(query)); try { client.deleteByQuery(request); return 1; } catch (IOException e) { throw new XiaoShiException("删除发明点失败"); } } public TechnicalCase getByProjectId(Integer projectId) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(TechnicalCase::getProjectId, projectId); TechnicalCase technicalCase = this.getOne(queryWrapper, false); if (technicalCase == null) { technicalCase = new TechnicalCase(); } return technicalCase; } }