TechnicalCaseService.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package cn.cslg.pas.service.business;
  2. import cn.cslg.pas.common.dto.AddTechnicalCaseDTO;
  3. import cn.cslg.pas.common.dto.QuerySimilarInventionPointDTO;
  4. import cn.cslg.pas.common.dto.TechnicalCaseIdDTO;
  5. import cn.cslg.pas.common.model.cronModel.Personnel;
  6. import cn.cslg.pas.common.model.cronModel.PersonnelVO;
  7. import cn.cslg.pas.common.model.cronModel.SystemFile;
  8. import cn.cslg.pas.common.utils.CacheUtils;
  9. import cn.cslg.pas.common.utils.LoginUtils;
  10. import cn.cslg.pas.common.vo.EditInventionPointVO;
  11. import cn.cslg.pas.common.vo.QueryInventionPointVO;
  12. import cn.cslg.pas.common.vo.TechnicalCaseVO;
  13. import cn.cslg.pas.domain.BaseEntity;
  14. import cn.cslg.pas.domain.business.*;
  15. import cn.cslg.pas.domain.es.Patent;
  16. import cn.cslg.pas.domain.es.TechnicalCasePatent;
  17. import cn.cslg.pas.exception.XiaoShiException;
  18. import cn.cslg.pas.mapper.AssoTechnicalCaseFileMapper;
  19. import cn.cslg.pas.mapper.ProjectMapper;
  20. import cn.cslg.pas.mapper.TechnicalCaseMapper;
  21. import cn.cslg.pas.service.common.FileManagerService;
  22. import cn.cslg.pas.service.permissions.PermissionService;
  23. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  24. import co.elastic.clients.elasticsearch._types.query_dsl.Like;
  25. import co.elastic.clients.elasticsearch._types.query_dsl.Query;
  26. import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
  27. import co.elastic.clients.elasticsearch.core.*;
  28. import co.elastic.clients.elasticsearch.core.search.Hit;
  29. import com.alibaba.fastjson.JSONObject;
  30. import com.alibaba.fastjson2.JSON;
  31. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  32. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  33. import lombok.RequiredArgsConstructor;
  34. import lombok.extern.slf4j.Slf4j;
  35. import org.apache.commons.lang3.ObjectUtils;
  36. import org.apache.commons.lang3.StringUtils;
  37. import org.springframework.beans.BeanUtils;
  38. import org.springframework.beans.factory.annotation.Autowired;
  39. import org.springframework.context.annotation.Lazy;
  40. import org.springframework.stereotype.Service;
  41. import org.springframework.transaction.annotation.Propagation;
  42. import org.springframework.transaction.annotation.Transactional;
  43. import org.springframework.util.CollectionUtils;
  44. import java.io.IOException;
  45. import java.util.ArrayList;
  46. import java.util.Arrays;
  47. import java.util.List;
  48. import java.util.stream.Collectors;
  49. @Service
  50. @Slf4j
  51. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  52. public class TechnicalCaseService extends ServiceImpl<TechnicalCaseMapper, TechnicalCase> {
  53. private final CacheUtils cacheUtils;
  54. private final LoginUtils loginUtils;
  55. private final ElasticsearchClient client;
  56. @Autowired
  57. private FileManagerService fileManagerService;
  58. @Autowired
  59. private PermissionService permissionService;
  60. @Autowired
  61. private AssoTechnicalCaseFileMapper assoTechnicalCaseFileMapper;
  62. @Autowired
  63. private AssoTechnicalCaseFileService assoTechnicalCaseFileService;
  64. @Autowired
  65. private TechnicalCaseMapper technicalCaseMapper;
  66. @Autowired
  67. private ProjectMapper projectMapper;
  68. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  69. public TechnicalCaseIdDTO addOrUpdateTechnicalCase(AddTechnicalCaseDTO vo) throws Exception {
  70. PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  71. TechnicalCaseIdDTO caseIdDTO = new TechnicalCaseIdDTO();
  72. if (vo.getTechnicalCaseId() == null) {
  73. if (vo.getProjectId() == null) {
  74. Project project = new Project();
  75. project.setType(4);
  76. project.setCreateId(personnelVO.getId());
  77. project.setTenantId(personnelVO.getTenantId());
  78. project.insert();
  79. vo.setProjectId(project.getId());
  80. NoveltyProject noveltyProject = new NoveltyProject();
  81. noveltyProject.setProjectId(project.getId());
  82. noveltyProject.insert();
  83. }
  84. TechnicalCase technicalCase = new TechnicalCase();
  85. BeanUtils.copyProperties(vo, technicalCase);
  86. technicalCase.insert();
  87. caseIdDTO.setProjectId(vo.getProjectId());
  88. caseIdDTO.setTechnicalCaseId(technicalCase.getId());
  89. //保存es
  90. TechnicalCasePatent casePatent = new TechnicalCasePatent();
  91. casePatent.setProjectId(vo.getProjectId());
  92. casePatent.setInventionPoint(technicalCase.getInventionPoint());
  93. String technicalCasePatentId = this.addTechnicalCasePatent(casePatent);
  94. if (StringUtils.isEmpty(technicalCasePatentId)) {
  95. throw new XiaoShiException("发明点保存Es失败");
  96. }
  97. } else {
  98. TechnicalCase technicalCase = technicalCaseMapper.selectById(vo.getTechnicalCaseId());
  99. BeanUtils.copyProperties(vo, technicalCase);
  100. technicalCase.updateById();
  101. caseIdDTO.setTechnicalCaseId(technicalCase.getId());
  102. caseIdDTO.setProjectId(technicalCase.getProjectId());
  103. //保存es
  104. // EditInventionPointVO pointVO = this.selectTechnicalCasePatent(technicalCase.getProjectId());
  105. // if (!StringUtils.equals(vo.getInventionPoint(), pointVO.getInventionPoint())) {
  106. // TechnicalCasePatent casePatent = new TechnicalCasePatent();
  107. //// casePatent.setProjectId(vo.getProjectId());
  108. // casePatent.setInventionPoint(vo.getInventionPoint());
  109. // Integer count = this.updateTechnicalCasePatent(casePatent, pointVO.getPatentId());
  110. // if (count < 1) {
  111. // throw new XiaoShiException("发明点更新Es失败");
  112. // }
  113. // }
  114. }
  115. List<AssoTechnicalCaseFile> technicalCaseFiles = assoTechnicalCaseFileMapper.selectList(new LambdaQueryWrapper<AssoTechnicalCaseFile>()
  116. .eq(AssoTechnicalCaseFile::getTechnicalCaseId, caseIdDTO.getTechnicalCaseId()));
  117. if (!CollectionUtils.isEmpty(technicalCaseFiles)) {
  118. List<Integer> collect = technicalCaseFiles.stream().map(BaseEntity::getId).collect(Collectors.toList());
  119. assoTechnicalCaseFileMapper.deleteBatchIds(collect);
  120. }
  121. //添加技术方案和图片关联
  122. List<AssoTechnicalCaseFile> assoTechnicalCaseFiles = new ArrayList<>();
  123. List<String> fileGuids = vo.getFileGuids();
  124. if (!CollectionUtils.isEmpty(fileGuids)) {
  125. for (String fileGuid : fileGuids) {
  126. AssoTechnicalCaseFile assoTechnicalCaseFile = new AssoTechnicalCaseFile();
  127. assoTechnicalCaseFile.setTechnicalCaseId(caseIdDTO.getTechnicalCaseId());
  128. assoTechnicalCaseFile.setFileGuid(fileGuid);
  129. assoTechnicalCaseFile.setCreateId(personnelVO.getId());
  130. assoTechnicalCaseFiles.add(assoTechnicalCaseFile);
  131. }
  132. if (!CollectionUtils.isEmpty(assoTechnicalCaseFiles)) {
  133. assoTechnicalCaseFileService.saveBatch(assoTechnicalCaseFiles);
  134. }
  135. }
  136. return caseIdDTO;
  137. }
  138. public TechnicalCaseVO queryTechnicalCase(TechnicalCaseIdDTO vo) {
  139. TechnicalCase technicalCase = this.getOne(new LambdaQueryWrapper<TechnicalCase>()
  140. .eq(TechnicalCase::getProjectId, vo.getProjectId()));
  141. TechnicalCaseVO technicalCaseVO = new TechnicalCaseVO();
  142. if (ObjectUtils.isNotEmpty(technicalCase)) {
  143. BeanUtils.copyProperties(technicalCase, technicalCaseVO);
  144. technicalCaseVO.setTechnicalCaseId(technicalCase.getId());
  145. List<AssoTechnicalCaseFile> assoTechnicalCaseFiles = assoTechnicalCaseFileMapper.selectList(new LambdaQueryWrapper<AssoTechnicalCaseFile>()
  146. .eq(AssoTechnicalCaseFile::getTechnicalCaseId, technicalCase.getId()));
  147. if (!CollectionUtils.isEmpty(assoTechnicalCaseFiles)) {
  148. List<String> guids = assoTechnicalCaseFiles.stream().map(AssoTechnicalCaseFile::getFileGuid).collect(Collectors.toList());
  149. String res = null;
  150. try {
  151. res = fileManagerService.getSystemFileFromFMS(guids);
  152. } catch (IOException e) {
  153. e.printStackTrace();
  154. }
  155. if (StringUtils.isNotEmpty(res)) {
  156. List<SystemFile> systemFiles = JSON.parseArray(res, SystemFile.class);
  157. technicalCaseVO.setSystemFileList(systemFiles);
  158. }
  159. }
  160. }
  161. return technicalCaseVO;
  162. }
  163. public void deleteInventPoint(Integer projectId) throws Exception {
  164. EditInventionPointVO pointVO = this.selectTechnicalCasePatent(projectId);
  165. if (ObjectUtils.isNotEmpty(pointVO) && pointVO.getPatentId() != null) {
  166. Integer count = delete(Arrays.asList(pointVO.getPatentId()));
  167. if (count < 1) {
  168. throw new XiaoShiException("删除发明点失败");
  169. }
  170. }
  171. }
  172. public List<QueryInventionPointVO> querySimilarInventionPoint(QuerySimilarInventionPointDTO vo) throws IOException {
  173. String content = vo.getContent();
  174. Integer projectId = vo.getProjectId();
  175. SearchRequest.Builder builder = new SearchRequest.Builder();
  176. //设置查询索引
  177. builder.index("technicalPatent");
  178. Like likeContent = Like.of(i -> i.text(content));
  179. Query query = QueryBuilders.moreLikeThis(i -> i.fields("invention_point").like(likeContent)
  180. .minTermFreq(1).maxQueryTerms(12).minDocFreq(1).minWordLength(2));
  181. Query bool = null;
  182. if (projectId != null) {
  183. Query query1 = QueryBuilders.term(i -> i.field("project_id").value(projectId));
  184. bool = QueryBuilders.bool(i -> i.must(query).mustNot(query1));
  185. } else {
  186. bool = QueryBuilders.bool(i -> i.must(query));
  187. }
  188. builder.query(bool);
  189. builder.size(5);
  190. //解除最大条数限制
  191. // builder.trackTotalHits(i -> i.enabled(true));
  192. SearchResponse<TechnicalCasePatent> response = client.search(builder.build(), TechnicalCasePatent.class);
  193. List<Hit<TechnicalCasePatent>> hits = response.hits().hits();
  194. List<QueryInventionPointVO> inventionPointVOS = new ArrayList<>();
  195. for (Hit<TechnicalCasePatent> hit : hits) {
  196. TechnicalCasePatent casePatent = hit.source();
  197. QueryInventionPointVO inventionPointVO = new QueryInventionPointVO();
  198. inventionPointVO.setProjectId(casePatent.getProjectId());
  199. inventionPointVO.setInventionPoint(casePatent.getInventionPoint());
  200. inventionPointVOS.add(inventionPointVO);
  201. }
  202. if (!CollectionUtils.isEmpty(inventionPointVOS)) {
  203. List<Integer> projectIds = inventionPointVOS.stream().map(QueryInventionPointVO::getProjectId).collect(Collectors.toList());
  204. List<Project> projects = projectMapper.selectBatchIds(projectIds);
  205. List<String> createIds = projects.stream().map(Project::getCreateId).collect(Collectors.toList());
  206. //查询创建人名称
  207. List<Personnel> personnels = new ArrayList<>();
  208. if (!CollectionUtils.isEmpty(createIds)) {
  209. String res = permissionService.getPersonnelByIdsFromPCS(createIds);
  210. JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject(res);
  211. personnels = JSON.parseArray(jsonObject.getString("data"), Personnel.class);
  212. }
  213. for (QueryInventionPointVO pointVO : inventionPointVOS) {
  214. Project project = projects.stream().filter(i -> i.getId().equals(pointVO.getProjectId())).findFirst().orElse(null);
  215. if (ObjectUtils.isNotEmpty(project)) {
  216. pointVO.setCreateId(project.getCreateId());
  217. pointVO.setCreateTime(project.getCreateTime());
  218. //装载人员信息
  219. Personnel personnel = personnels.stream().filter(item -> item.getId().equals(pointVO.getCreateId())).findFirst().orElse(null);
  220. if (personnel != null) {
  221. pointVO.setCreateName(personnel.getPersonnelName());
  222. }
  223. }
  224. }
  225. }
  226. return inventionPointVOS;
  227. }
  228. //查询TechnicalCasePatent
  229. public EditInventionPointVO selectTechnicalCasePatent(Integer projectId) throws Exception {
  230. EditInventionPointVO pointVO = new EditInventionPointVO();
  231. SearchRequest.Builder builder = new SearchRequest.Builder();
  232. //设置查询索引
  233. builder.index("technicalPatent");
  234. Query query = QueryBuilders.term(i -> i.field("project_id").value(projectId));
  235. builder.query(query);
  236. SearchResponse<TechnicalCasePatent> response = client.search(builder.build(), TechnicalCasePatent.class);
  237. List<Hit<TechnicalCasePatent>> hits = response.hits().hits();
  238. for (Hit<TechnicalCasePatent> hit : hits) {
  239. pointVO.setPatentId(hit.id());
  240. TechnicalCasePatent patent = hit.source();
  241. pointVO.setProjectId(patent.getProjectId());
  242. pointVO.setInventionPoint(patent.getInventionPoint());
  243. break;
  244. }
  245. return pointVO;
  246. }
  247. //添加TechnicalCasePatent
  248. public String addTechnicalCasePatent(TechnicalCasePatent casePatent) throws Exception {
  249. IndexResponse indexResponse = client.index(i -> i
  250. .index("technicalPatent")
  251. //传入user对象
  252. .document(casePatent)
  253. );
  254. return indexResponse.id();
  255. }
  256. //更新TechnicalCasePatent
  257. public Integer updateTechnicalCasePatent(TechnicalCasePatent patent, String id) {
  258. UpdateRequest<TechnicalCasePatent, TechnicalCasePatent> req;
  259. req = UpdateRequest.of(
  260. b -> b.index("technicalPatent").id(id)
  261. .doc(patent));
  262. try {
  263. client.update(req, TechnicalCasePatent.class);
  264. return 1;
  265. } catch (IOException e) {
  266. return -1;
  267. }
  268. }
  269. //删除TechnicalCasePatent
  270. public Integer delete(List<String> ids) {
  271. Query query = QueryBuilders.ids(n -> n.values(ids));
  272. DeleteByQueryRequest request = DeleteByQueryRequest.of(i -> i.index("technicalPatent").refresh(true).query(query));
  273. try {
  274. client.deleteByQuery(request);
  275. return 1;
  276. } catch (IOException e) {
  277. throw new XiaoShiException("删除发明点失败");
  278. }
  279. }
  280. public TechnicalCase getByProjectId(Integer projectId) {
  281. LambdaQueryWrapper<TechnicalCase> queryWrapper = new LambdaQueryWrapper<>();
  282. queryWrapper.eq(TechnicalCase::getProjectId, projectId);
  283. TechnicalCase technicalCase = this.getOne(queryWrapper, false);
  284. if (technicalCase == null) {
  285. technicalCase = new TechnicalCase();
  286. }
  287. return technicalCase;
  288. }
  289. }