MergePersonService.java 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. package cn.cslg.pas.service.business;
  2. import cn.cslg.pas.common.dto.GetAllCountryDTO;
  3. import cn.cslg.pas.common.dto.GetAllPersonDTO;
  4. import cn.cslg.pas.common.dto.MergePersonQueryDTO;
  5. import cn.cslg.pas.common.dto.PatentColumnDTO;
  6. import cn.cslg.pas.common.dto.es.EsMergePersonDTO;
  7. import cn.cslg.pas.common.dto.patentCount.GetEsAllPersonDTO;
  8. import cn.cslg.pas.common.model.cronModel.PersonnelVO;
  9. import cn.cslg.pas.common.model.cronModel.Records;
  10. import cn.cslg.pas.common.utils.CacheUtils;
  11. import cn.cslg.pas.common.utils.LoginUtils;
  12. import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
  13. import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
  14. import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
  15. import cn.cslg.pas.common.vo.business.*;
  16. import cn.cslg.pas.domain.BaseEntity;
  17. import cn.cslg.pas.domain.business.FollowUp;
  18. import cn.cslg.pas.domain.business.MergePerson;
  19. import cn.cslg.pas.domain.business.SystemDict;
  20. import cn.cslg.pas.domain.es.*;
  21. import cn.cslg.pas.exception.UnLoginException;
  22. import cn.cslg.pas.exception.XiaoShiException;
  23. import cn.cslg.pas.mapper.MergePersonMapper;
  24. import cn.cslg.pas.mapper.SystemDictMapper;
  25. import cn.cslg.pas.service.business.es.EsPatentService;
  26. import cn.cslg.pas.service.business.es.EsService;
  27. import cn.cslg.pas.service.query.FormatQueryService;
  28. import cn.hutool.core.util.ObjectUtil;
  29. import cn.hutool.core.util.PageUtil;
  30. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  31. import co.elastic.clients.elasticsearch._types.InlineScript;
  32. import co.elastic.clients.elasticsearch._types.Script;
  33. import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
  34. import co.elastic.clients.elasticsearch._types.aggregations.AggregationBuilders;
  35. import co.elastic.clients.elasticsearch._types.aggregations.NestedAggregation;
  36. import co.elastic.clients.elasticsearch._types.aggregations.TermsAggregation;
  37. import co.elastic.clients.elasticsearch._types.query_dsl.IdsQuery;
  38. import co.elastic.clients.elasticsearch._types.query_dsl.Query;
  39. import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
  40. import co.elastic.clients.elasticsearch.core.*;
  41. import co.elastic.clients.elasticsearch.core.search.Hit;
  42. import co.elastic.clients.json.JsonData;
  43. import com.alibaba.fastjson.JSONArray;
  44. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  45. import com.baomidou.mybatisplus.core.metadata.IPage;
  46. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  47. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  48. import com.github.pagehelper.PageHelper;
  49. import com.google.gson.JsonArray;
  50. import lombok.extern.slf4j.Slf4j;
  51. import org.apache.commons.lang3.ObjectUtils;
  52. import org.apache.commons.lang3.StringUtils;
  53. import org.springframework.beans.BeanUtils;
  54. import org.springframework.beans.factory.annotation.Autowired;
  55. import org.springframework.stereotype.Service;
  56. import org.springframework.transaction.annotation.Propagation;
  57. import org.springframework.transaction.annotation.Transactional;
  58. import org.springframework.util.CollectionUtils;
  59. import java.io.IOException;
  60. import java.util.*;
  61. import java.util.stream.Collectors;
  62. /**
  63. * @Author xiexiang
  64. * @Date 2024/01/02
  65. */
  66. @Slf4j
  67. @Service
  68. public class MergePersonService extends ServiceImpl<MergePersonMapper, MergePerson> {
  69. @Autowired
  70. private ElasticsearchClient client;
  71. @Autowired
  72. private CacheUtils cacheUtils;
  73. @Autowired
  74. private LoginUtils loginUtils;
  75. @Autowired
  76. private MergePersonMapper mergePersonMapper;
  77. @Autowired
  78. private FormatQueryService formatQueryService;
  79. @Autowired
  80. private EsPatentService esPatentService;
  81. @Autowired
  82. private EsService esService;
  83. @Autowired
  84. private SystemDictMapper systemDictMapper;
  85. /**
  86. * 专利列表上新增发明人/权利人/申请人合并
  87. *
  88. * @param vo
  89. * @return
  90. * @throws Exception
  91. */
  92. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  93. public Integer mergePerson(MergePersonVO vo) throws Exception {
  94. Integer type = vo.getType();
  95. List<String> mergedNames = vo.getMergedName();
  96. Integer projectId = vo.getProjectId();
  97. //获取登陆人信息 用于设置创建人
  98. PersonnelVO personnelVO = new PersonnelVO();
  99. try {
  100. personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  101. } catch (Exception e) {
  102. throw new XiaoShiException("未登录");
  103. }
  104. //判断是否名称重复
  105. LambdaQueryWrapper<MergePerson> wrapper = new LambdaQueryWrapper<MergePerson>()
  106. .eq(MergePerson::getProjectId, vo.getProjectId())
  107. .eq(MergePerson::getName, vo.getName());
  108. List<MergePerson> list = mergePersonMapper.selectList(wrapper);
  109. if (list.size() > 0) {
  110. throw new XiaoShiException("合并名称不可重复");
  111. }
  112. MergePerson person = new MergePerson();
  113. BeanUtils.copyProperties(vo, person);
  114. person.setMergedName(JSONArray.toJSONString(mergedNames));
  115. person.setCreateId(personnelVO.getId());
  116. person.setCreateTime(new Date());
  117. person.insert();
  118. SearchRequest.Builder builder = new SearchRequest.Builder();
  119. //设置查询索引
  120. builder.index("patent");
  121. List<Query> queries = new ArrayList<>();
  122. if (type == 0) {
  123. for (String mergedName : mergedNames) {
  124. //合并申请人
  125. Query idQ1 = QueryBuilders.term(i -> i.field("merge_applicant.project_id").value(projectId));
  126. Query q1 = QueryBuilders.term(i -> i.field("merge_applicant.name.raw").value(mergedName));
  127. Query bool = QueryBuilders.bool(i -> i.must(idQ1, q1));
  128. Query query1 = QueryBuilders.nested(i -> i.path("merge_applicant").query(bool));
  129. Query childQ1 = QueryBuilders.hasChild(i -> i.type("merge_applicat").query(query1));
  130. //合并权利人
  131. Query idQ2 = QueryBuilders.term(i -> i.field("merge_right_holder.project_id").value(projectId));
  132. Query q2 = QueryBuilders.term(i -> i.field("merge_right_holder.name.raw").value(mergedName));
  133. Query bool1 = QueryBuilders.bool(i -> i.must(idQ2, q2));
  134. Query query2 = QueryBuilders.nested(i -> i.path("merge_right_holder").query(bool1));
  135. Query childQ2 = QueryBuilders.hasChild(i -> i.type("merge_right_holder").query(query2));
  136. queries.add(childQ1);
  137. queries.add(childQ2);
  138. }
  139. } else {
  140. for (String mergedName : mergedNames) {
  141. Query idQ = QueryBuilders.term(i -> i.field("merge_inventor.project_id").value(projectId));
  142. Query q = QueryBuilders.term(i -> i.field("merge_inventor.name.raw").value(mergedName));
  143. Query bool = QueryBuilders.bool(i -> i.must(idQ, q));
  144. Query query = QueryBuilders.nested(i -> i.path("merge_inventor").query(bool));
  145. Query childQ = QueryBuilders.hasChild(i -> i.type("merge_inventor").query(query));
  146. queries.add(childQ);
  147. }
  148. }
  149. Query query = QueryBuilders.bool(i -> i.should(queries));
  150. builder.query(query);
  151. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  152. List<Hit<Patent>> hits = response.hits().hits();
  153. Map<String, Patent> map = new HashMap<>();
  154. for (Hit<Patent> hit : hits) {
  155. String id = hit.id();
  156. map.put(id, hit.source());
  157. }
  158. if (!CollectionUtils.isEmpty(map)) {
  159. List<PatentMergePerson> mergePersonList = new ArrayList<>();
  160. PatentMergePerson mergePerson = new PatentMergePerson();
  161. mergePerson.setName(vo.getName());
  162. mergePerson.setProjectId(vo.getProjectId().toString());
  163. mergePerson.setType("1");
  164. mergePerson.setOrder(1);
  165. mergePersonList.add(mergePerson);
  166. for (String id : map.keySet()) {
  167. if (type == 0) {
  168. //申请人
  169. Map<String, List<PatentMergePerson>> appMap = this.getChildMergePerson(id, projectId, "merge_applicant");
  170. if (!CollectionUtils.isEmpty(appMap)) {
  171. for (String appId : appMap.keySet()) {
  172. List<PatentMergePerson> appMergeList = new ArrayList<>(mergePersonList);
  173. List<PatentMergePerson> appList = appMap.get(appId);
  174. List<String> collect = appList.stream().map(PatentMergePerson::getName).collect(Collectors.toList());
  175. if (!collect.contains(vo.getName())) {
  176. for (String mergedName : mergedNames) {
  177. appList.removeIf(i -> i.getName().equals(mergedName));
  178. }
  179. appMergeList.addAll(appList);
  180. //添加子文档
  181. Patent newPatent = new Patent();
  182. PatentJoin patentJoin = new PatentJoin();
  183. patentJoin.setParent(id);
  184. patentJoin.setName("merge_applicat");
  185. newPatent.setPatentJoin(patentJoin);
  186. newPatent.setMergeApplicant(appMergeList);
  187. String child = this.addChild(newPatent, id);
  188. if (StringUtils.isEmpty(child)) {
  189. throw new XiaoShiException("合并失败");
  190. } else {
  191. //删除原子文档
  192. this.delete(Arrays.asList(appId));
  193. }
  194. }
  195. }
  196. }
  197. //权利人
  198. Map<String, List<PatentMergePerson>> rightMap = this.getChildMergePerson(id, projectId, "merge_right_holder");
  199. if (!CollectionUtils.isEmpty(rightMap)) {
  200. for (String rightId : rightMap.keySet()) {
  201. List<PatentMergePerson> rightMergeList = new ArrayList<>(mergePersonList);
  202. List<PatentMergePerson> rightList = rightMap.get(rightId);
  203. List<String> collect = rightList.stream().map(PatentMergePerson::getName).collect(Collectors.toList());
  204. if (!collect.contains(vo.getName())) {
  205. for (String mergedName : mergedNames) {
  206. rightList.removeIf(i -> i.getName().equals(mergedName));
  207. }
  208. rightMergeList.addAll(rightList);
  209. //添加子文档
  210. Patent newPatent = new Patent();
  211. PatentJoin patentJoin = new PatentJoin();
  212. patentJoin.setParent(id);
  213. patentJoin.setName("merge_right_holder");
  214. newPatent.setPatentJoin(patentJoin);
  215. newPatent.setMergeInventor(rightMergeList);
  216. String child = this.addChild(newPatent, id);
  217. if (StringUtils.isEmpty(child)) {
  218. throw new XiaoShiException("合并失败");
  219. } else {
  220. //删除原子文档
  221. this.delete(Arrays.asList(rightId));
  222. }
  223. // for (String mergedName : mergedNames) {
  224. // this.delSingleMerge(rightId, type, mergedName);
  225. // }
  226. // Integer num = this.add(rightId, type, mergePersonList);
  227. // if (num < 0) {
  228. // throw new XiaoShiException("合并失败");
  229. // }
  230. }
  231. }
  232. }
  233. } else if (type == 2) {
  234. Map<String, List<PatentMergePerson>> inventorMap = this.getChildMergePerson(id, projectId, "merge_inventor");
  235. if (!CollectionUtils.isEmpty(inventorMap)) {
  236. for (String inventorId : inventorMap.keySet()) {
  237. List<PatentMergePerson> inventorMergeList = new ArrayList<>(mergePersonList);
  238. List<PatentMergePerson> inventorList = inventorMap.get(inventorId);
  239. List<String> collect = inventorList.stream().map(PatentMergePerson::getName).collect(Collectors.toList());
  240. if (!collect.contains(vo.getName())) {
  241. for (String mergedName : mergedNames) {
  242. inventorList.removeIf(i -> i.getName().equals(mergedName));
  243. }
  244. inventorMergeList.addAll(inventorList);
  245. //添加子文档
  246. Patent newPatent = new Patent();
  247. PatentJoin patentJoin = new PatentJoin();
  248. patentJoin.setParent(id);
  249. patentJoin.setName("merge_inventor");
  250. newPatent.setPatentJoin(patentJoin);
  251. newPatent.setMergeInventor(inventorMergeList);
  252. String child = this.addChild(newPatent, id);
  253. if (StringUtils.isEmpty(child)) {
  254. throw new XiaoShiException("合并失败");
  255. } else {
  256. //删除原子文档
  257. this.delete(Arrays.asList(inventorId));
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. } else {
  265. throw new XiaoShiException("合并失败");
  266. }
  267. return person.getId();
  268. }
  269. public Map<String, List<PatentMergePerson>> getChildMergePerson(String id, Integer projectId, String mergeType) throws IOException {
  270. String path = mergeType + "." + "project_id";
  271. SearchRequest.Builder builder = new SearchRequest.Builder();
  272. //设置查询索引
  273. builder.index("patent");
  274. Query idQ = QueryBuilders.term(i -> i.field(path).value(projectId));
  275. Query query = QueryBuilders.nested(i -> i.path(mergeType).query(idQ));
  276. IdsQuery idsQuery = IdsQuery.of(n -> n.values(Arrays.asList(id)));
  277. Query q = QueryBuilders.hasParent(i -> i.parentType("patent").query(j -> j.ids(idsQuery)));
  278. Query bool = QueryBuilders.bool(i -> i.must(q, query));
  279. builder.query(bool);
  280. builder.trackTotalHits(i -> i.enabled(true));
  281. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  282. List<Hit<Patent>> hits = response.hits().hits();
  283. Map<String, List<PatentMergePerson>> map = new HashMap<>();
  284. for (Hit<Patent> hit : hits) {
  285. String id1 = hit.id();
  286. Patent patent = hit.source();
  287. if (mergeType.equals("merge_applicant")) {
  288. if (!CollectionUtils.isEmpty(patent.getMergeApplicant())) {
  289. map.put(id1, patent.getMergeApplicant());
  290. }
  291. } else if (mergeType.equals("merge_right_holder")) {
  292. if (!CollectionUtils.isEmpty(patent.getMergeRightHolder())) {
  293. map.put(id1, patent.getMergeRightHolder());
  294. }
  295. } else {
  296. if (!CollectionUtils.isEmpty(patent.getMergeInventor())) {
  297. map.put(id1, patent.getMergeInventor());
  298. }
  299. }
  300. }
  301. return map;
  302. }
  303. /**
  304. * 专利列表上再次新增发明人/权利人/申请人合并
  305. *
  306. * @param vo
  307. * @return
  308. * @throws Exception
  309. */
  310. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  311. public Integer mergePersonAgain(MergePersonAgainVO vo) throws Exception {
  312. List<String> mergedNames = vo.getNeedMergedName();
  313. List<String> mergeName = vo.getMergedName();
  314. Integer type = vo.getType();
  315. Integer projectId = vo.getProjectId();
  316. List<String> nameList = new ArrayList<>();
  317. if (!CollectionUtils.isEmpty(mergedNames)) {
  318. for (String mergedName : mergedNames) {
  319. MergePerson person = mergePersonMapper.selectOne(new LambdaQueryWrapper<MergePerson>()
  320. .eq(MergePerson::getProjectId, vo.getProjectId())
  321. .eq(MergePerson::getName, mergedName));
  322. String name = person.getMergedName();
  323. List<String> getNames = JSONArray.parseArray(name, String.class);
  324. nameList.addAll(getNames);
  325. Map<String, Patent> map = this.getDelPatent(type, projectId, mergedName);
  326. if (!CollectionUtils.isEmpty(map)) {
  327. for (String id : map.keySet()) {
  328. Patent patent = map.get(id);
  329. List<PatentMergePerson> appPersonList = new ArrayList<>();
  330. List<PatentMergePerson> rightPersonList = new ArrayList<>();
  331. List<PatentMergePerson> inventorPersonList = new ArrayList<>();
  332. if (type == 0) {
  333. if (!CollectionUtils.isEmpty(patent.getApplicant())) {
  334. List<PatentPerson> applicant = patent.getApplicant();
  335. for (PatentPerson patentPerson : applicant) {
  336. PatentMergePerson merge = new PatentMergePerson();
  337. merge.setName(patentPerson.getName());
  338. merge.setType(patentPerson.getType());
  339. merge.setOrder(patentPerson.getOrder());
  340. merge.setProjectId(String.valueOf(projectId));
  341. appPersonList.add(merge);
  342. }
  343. }
  344. if (!CollectionUtils.isEmpty(patent.getRightHolder())) {
  345. List<PatentPerson> rightHolder = patent.getRightHolder();
  346. for (PatentPerson patentPerson : rightHolder) {
  347. PatentMergePerson merge = new PatentMergePerson();
  348. merge.setName(patentPerson.getName());
  349. merge.setType(patentPerson.getType());
  350. merge.setOrder(patentPerson.getOrder());
  351. merge.setProjectId(String.valueOf(projectId));
  352. rightPersonList.add(merge);
  353. }
  354. }
  355. } else {
  356. if (!CollectionUtils.isEmpty(patent.getInventor())) {
  357. List<PatentPerson> inventor = patent.getInventor();
  358. for (PatentPerson patentPerson : inventor) {
  359. PatentMergePerson merge = new PatentMergePerson();
  360. merge.setName(patentPerson.getName());
  361. merge.setType(patentPerson.getType());
  362. merge.setOrder(patentPerson.getOrder());
  363. merge.setProjectId(String.valueOf(projectId));
  364. inventorPersonList.add(merge);
  365. }
  366. }
  367. }
  368. if (type == 0) {
  369. //申请人
  370. Map<String, List<PatentMergePerson>> appMap = this.getChildMergePerson(id, projectId, "merge_applicant");
  371. if (!CollectionUtils.isEmpty(appMap)) {
  372. for (String appId : appMap.keySet()) {
  373. //添加子文档
  374. Patent newPatent = new Patent();
  375. PatentJoin patentJoin = new PatentJoin();
  376. patentJoin.setParent(id);
  377. patentJoin.setName("merge_applicat");
  378. newPatent.setPatentJoin(patentJoin);
  379. newPatent.setMergeApplicant(appPersonList);
  380. String child = this.addChild(newPatent, id);
  381. if (StringUtils.isEmpty(child)) {
  382. throw new XiaoShiException("删除失败");
  383. } else {
  384. //删除原子文档
  385. this.delete(Arrays.asList(appId));
  386. }
  387. }
  388. }
  389. //权利人
  390. Map<String, List<PatentMergePerson>> rightMap = this.getChildMergePerson(id, projectId, "merge_right_holder");
  391. if (!CollectionUtils.isEmpty(rightMap)) {
  392. for (String rightId : rightMap.keySet()) {
  393. //添加子文档
  394. Patent newPatent = new Patent();
  395. PatentJoin patentJoin = new PatentJoin();
  396. patentJoin.setParent(id);
  397. patentJoin.setName("merge_right_holder");
  398. newPatent.setPatentJoin(patentJoin);
  399. newPatent.setMergeInventor(rightPersonList);
  400. String child = this.addChild(newPatent, id);
  401. if (StringUtils.isEmpty(child)) {
  402. throw new XiaoShiException("删除失败");
  403. } else {
  404. //删除原子文档
  405. this.delete(Arrays.asList(rightId));
  406. }
  407. }
  408. }
  409. } else {
  410. //发明人
  411. Map<String, List<PatentMergePerson>> inventorMap = this.getChildMergePerson(id, projectId, "merge_inventor");
  412. if (!CollectionUtils.isEmpty(inventorMap)) {
  413. for (String inventorId : inventorMap.keySet()) {
  414. //添加子文档
  415. Patent newPatent = new Patent();
  416. PatentJoin patentJoin = new PatentJoin();
  417. patentJoin.setParent(id);
  418. patentJoin.setName("merge_inventor");
  419. newPatent.setPatentJoin(patentJoin);
  420. newPatent.setMergeInventor(inventorPersonList);
  421. String child = this.addChild(newPatent, id);
  422. if (StringUtils.isEmpty(child)) {
  423. throw new XiaoShiException("删除失败");
  424. } else {
  425. //删除原子文档
  426. this.delete(Arrays.asList(inventorId));
  427. }
  428. }
  429. }
  430. }
  431. }
  432. mergePersonMapper.deleteById(person.getId());
  433. }
  434. }
  435. }
  436. if (!CollectionUtils.isEmpty(mergeName)) {
  437. nameList.addAll(mergeName);
  438. }
  439. MergePersonVO personVO = new MergePersonVO();
  440. personVO.setProjectId(projectId);
  441. personVO.setType(type);
  442. personVO.setName(vo.getName());
  443. personVO.setAbbreviation(vo.getAbbreviation());
  444. personVO.setCountry(vo.getCountry());
  445. personVO.setProvince(vo.getProvince());
  446. personVO.setAddress(vo.getAddress());
  447. personVO.setRemark(vo.getRemark());
  448. personVO.setMergedName(nameList);
  449. Integer id = this.mergePerson(personVO);
  450. if (id < 0) {
  451. throw new XiaoShiException("再合并失败");
  452. }
  453. return id;
  454. }
  455. /**
  456. * 专利列表上编辑发明人/权利人/申请人合并
  457. *
  458. * @param vo
  459. * @return
  460. * @throws Exception
  461. */
  462. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  463. public Integer updateMergePerson(MergePersonVO vo) throws Exception {
  464. //获取登陆人信息 用于设置创建人
  465. PersonnelVO personnelVO = new PersonnelVO();
  466. Integer type = vo.getType();
  467. try {
  468. personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  469. } catch (Exception e) {
  470. throw new XiaoShiException("未登录");
  471. }
  472. //判断是否名称重复
  473. LambdaQueryWrapper<MergePerson> wrapper = new LambdaQueryWrapper<MergePerson>()
  474. .eq(MergePerson::getProjectId, vo.getProjectId())
  475. .eq(MergePerson::getName, vo.getName())
  476. .ne(BaseEntity::getId, vo.getId());
  477. List<MergePerson> list = mergePersonMapper.selectList(wrapper);
  478. if (list.size() > 0) {
  479. throw new XiaoShiException("合并名称不可重复");
  480. }
  481. //获取老专利
  482. MergePerson person = this.getById(vo.getId());
  483. String name = person.getName();
  484. Integer projectId = person.getProjectId();
  485. if (ObjectUtil.isNotEmpty(person)) {
  486. BeanUtils.copyProperties(vo, person);
  487. person.setCreateId(personnelVO.getId());
  488. person.setCreateTime(new Date());
  489. person.updateById();
  490. if (!name.equals(vo.getName())) {
  491. SearchRequest.Builder builder = new SearchRequest.Builder();
  492. //设置查询索引
  493. builder.index("patent");
  494. List<Query> queries = new ArrayList<>();
  495. if (type == 0) {
  496. //合并申请人
  497. Query idQ1 = QueryBuilders.term(i -> i.field("merge_applicant.project_id").value(projectId));
  498. Query q1 = QueryBuilders.term(i -> i.field("merge_applicant.name.raw").value(name));
  499. Query bool = QueryBuilders.bool(i -> i.must(idQ1, q1));
  500. Query query1 = QueryBuilders.nested(i -> i.path("merge_applicant").query(bool));
  501. Query childQ1 = QueryBuilders.hasChild(i -> i.type("merge_applicat").query(query1));
  502. //合并权利人
  503. Query idQ2 = QueryBuilders.term(i -> i.field("merge_right_holder.project_id").value(projectId));
  504. Query q2 = QueryBuilders.term(i -> i.field("merge_right_holder.name.raw").value(name));
  505. Query bool1 = QueryBuilders.bool(i -> i.must(idQ2, q2));
  506. Query query2 = QueryBuilders.nested(i -> i.path("merge_right_holder").query(bool1));
  507. Query childQ2 = QueryBuilders.hasChild(i -> i.type("merge_right_holder").query(query2));
  508. queries.add(childQ1);
  509. queries.add(childQ2);
  510. } else {
  511. Query idQ = QueryBuilders.term(i -> i.field("merge_inventor.project_id").value(projectId));
  512. Query q = QueryBuilders.term(i -> i.field("merge_inventor.name.raw").value(name));
  513. Query bool = QueryBuilders.bool(i -> i.must(idQ, q));
  514. Query query = QueryBuilders.nested(i -> i.path("merge_inventor").query(bool));
  515. Query childQ = QueryBuilders.hasChild(i -> i.type("merge_inventor").query(query));
  516. queries.add(childQ);
  517. }
  518. Query query = QueryBuilders.bool(i -> i.should(queries));
  519. builder.query(query);
  520. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  521. List<Hit<Patent>> hits = response.hits().hits();
  522. Map<String, Patent> map = new HashMap<>();
  523. for (Hit<Patent> hit : hits) {
  524. String id = hit.id();
  525. map.put(id, hit.source());
  526. }
  527. if (!CollectionUtils.isEmpty(map)) {
  528. for (String id : map.keySet()) {
  529. if (type == 0) {
  530. //申请人
  531. Map<String, List<PatentMergePerson>> appMap = this.getChildMergePerson(id, projectId, "merge_applicant");
  532. if (!CollectionUtils.isEmpty(appMap)) {
  533. for (String appId : appMap.keySet()) {
  534. this.edit(appId, type, name, vo.getName());
  535. }
  536. }
  537. //权利人
  538. Map<String, List<PatentMergePerson>> rightMap = this.getChildMergePerson(id, projectId, "merge_right_holder");
  539. if (!CollectionUtils.isEmpty(rightMap)) {
  540. for (String rightId : rightMap.keySet()) {
  541. this.edit(rightId, type, name, vo.getName());
  542. }
  543. }
  544. } else if (type == 2) {
  545. Map<String, List<PatentMergePerson>> inventorMap = this.getChildMergePerson(id, projectId, "merge_inventor");
  546. if (!CollectionUtils.isEmpty(inventorMap)) {
  547. for (String inventorId : inventorMap.keySet()) {
  548. this.edit(inventorId, type, name, vo.getName());
  549. }
  550. }
  551. }
  552. }
  553. } else {
  554. throw new XiaoShiException("编辑失败");
  555. }
  556. }
  557. } else {
  558. person = new MergePerson();
  559. }
  560. return person.getId();
  561. }
  562. /**
  563. * 发明人/权利人/申请人合并列表查询
  564. *
  565. * @param vo
  566. * @return
  567. */
  568. public Records selectMergePersonList(MergePersonQueryVO vo) {
  569. List<MergePersonQueryDTO> list = new ArrayList<>();
  570. IPage<MergePerson> page = new Page<>(vo.getPageNum(), vo.getPageSize());
  571. LambdaQueryWrapper<MergePerson> wrapper = new LambdaQueryWrapper<MergePerson>()
  572. .eq(MergePerson::getProjectId, vo.getProjectId())
  573. .eq(MergePerson::getType, vo.getType())
  574. .eq(StringUtils.isNotEmpty(vo.getCountry()), MergePerson::getCountry, vo.getCountry())
  575. .like(StringUtils.isNotEmpty(vo.getName()), MergePerson::getName, vo.getName());
  576. IPage<MergePerson> record = mergePersonMapper.selectPage(page, wrapper);
  577. for (MergePerson person : record.getRecords()) {
  578. MergePersonQueryDTO dto = new MergePersonQueryDTO();
  579. dto.setId(person.getId());
  580. dto.setProjectId(person.getProjectId());
  581. dto.setType(person.getType());
  582. dto.setName(person.getName());
  583. dto.setAddress(person.getAddress());
  584. dto.setCountry(person.getCountry());
  585. dto.setProvince(person.getProvince());
  586. dto.setRemark(person.getRemark());
  587. dto.setAbbreviation(person.getAbbreviation());
  588. dto.setCreateTime(person.getCreateTime());
  589. List<String> names = JSONArray.parseArray(person.getMergedName(), String.class);
  590. dto.setMergedName(names);
  591. list.add(dto);
  592. }
  593. Records records = new Records();
  594. records.setCurrent(record.getCurrent());
  595. records.setSize(record.getSize());
  596. records.setData(list);
  597. records.setTotal(record.getTotal());
  598. return records;
  599. }
  600. /**
  601. * 合并人员详情
  602. *
  603. * @param vo
  604. * @return
  605. */
  606. public MergePersonQueryDTO selectMergePersonDetail(MergePersonIdVO vo) {
  607. MergePersonQueryDTO dto = new MergePersonQueryDTO();
  608. MergePerson person = mergePersonMapper.selectById(vo.getId());
  609. if (ObjectUtil.isNotEmpty(person)) {
  610. BeanUtils.copyProperties(person, dto);
  611. List<String> names = JSONArray.parseArray(person.getMergedName(), String.class);
  612. dto.setMergedName(names);
  613. }
  614. return dto;
  615. }
  616. /**
  617. * 获取所有发明人/权利人/申请人
  618. *
  619. * @param vo
  620. * @return
  621. * @throws Exception
  622. */
  623. public Records getMergePerson(GetAllPersonVO vo) throws Exception {
  624. Integer projectId = vo.getProjectId();
  625. Long pageNum = vo.getPageNum();
  626. Long pageSize = vo.getPageSize();
  627. Integer type = vo.getType();
  628. String condition = vo.getSearchQuery();
  629. List<String> conditionList = new ArrayList<>();
  630. long total = 0l;
  631. if (StringUtils.isNotEmpty(condition)) {
  632. String s = condition.substring(condition.indexOf("=") + 1);
  633. if (s.contains("or")) {
  634. String[] split = s.split("or");
  635. conditionList.addAll(Arrays.asList(split));
  636. } else {
  637. conditionList.add(s);
  638. }
  639. }
  640. List<GetEsAllPersonDTO> personDTOS = new ArrayList<>();
  641. SearchRequest.Builder builder = new SearchRequest.Builder();
  642. //设置查询索引
  643. builder.index("patent");
  644. // Query mustQ = QueryBuilders.hasChild(i -> i.type("project").query(j -> j.term(n -> n.field("project_id").value(projectId))));
  645. if (!CollectionUtils.isEmpty(conditionList)) {
  646. for (String name : conditionList) {
  647. List<Query> queries = new ArrayList<>();
  648. if (type == 0) {
  649. //合并申请人
  650. Query idQ1 = QueryBuilders.term(i -> i.field("merge_applicant.project_id").value(projectId));
  651. Query q1 = QueryBuilders.term(i -> i.field("merge_applicant.name.raw").value(name));
  652. Query bool = QueryBuilders.bool(i -> i.must(idQ1, q1));
  653. Query query1 = QueryBuilders.nested(i -> i.path("merge_applicant").query(bool));
  654. Query childQ1 = QueryBuilders.hasChild(i -> i.type("merge_applicat").query(query1));
  655. //合并权利人
  656. Query idQ2 = QueryBuilders.term(i -> i.field("merge_right_holder.project_id").value(projectId));
  657. Query q2 = QueryBuilders.term(i -> i.field("merge_right_holder.name.raw").value(name));
  658. Query bool1 = QueryBuilders.bool(i -> i.must(idQ2, q2));
  659. Query query2 = QueryBuilders.nested(i -> i.path("merge_right_holder").query(bool1));
  660. Query childQ2 = QueryBuilders.hasChild(i -> i.type("merge_right_holder").query(query2));
  661. queries.add(childQ1);
  662. queries.add(childQ2);
  663. } else {
  664. Query idQ = QueryBuilders.term(i -> i.field("merge_inventor.project_id").value(projectId));
  665. Query q = QueryBuilders.term(i -> i.field("merge_inventor.name.raw").value(name));
  666. Query bool = QueryBuilders.bool(i -> i.must(idQ, q));
  667. Query query = QueryBuilders.nested(i -> i.path("merge_inventor").query(bool));
  668. Query childQ = QueryBuilders.hasChild(i -> i.type("merge_inventor").query(query));
  669. queries.add(childQ);
  670. }
  671. Query bool = QueryBuilders.bool(i -> i.should(queries));
  672. builder.query(bool);
  673. //分页
  674. if (pageNum > 0 && pageSize > 0) {
  675. builder.from((pageNum.intValue() - 1) * pageSize.intValue()).size(pageSize.intValue());
  676. }
  677. // builder.trackTotalHits(i -> i.enabled(true));
  678. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  679. total = response.hits().total().value();
  680. List<Hit<Patent>> hits = response.hits().hits();
  681. Map<String, Patent> map = new HashMap<>();
  682. for (Hit<Patent> hit : hits) {
  683. String id = hit.id();
  684. map.put(id, hit.source());
  685. }
  686. if (!CollectionUtils.isEmpty(map)) {
  687. for (String id : map.keySet()) {
  688. GetEsAllPersonDTO personDTO = new GetEsAllPersonDTO();
  689. if (type == 0) {
  690. //申请人
  691. Map<String, List<PatentMergePerson>> appMap = this.getChildMergePerson(id, projectId, "merge_applicant");
  692. if (!CollectionUtils.isEmpty(appMap)) {
  693. for (String appId : appMap.keySet()) {
  694. List<PatentMergePerson> appList = appMap.get(appId);
  695. List<String> collect = appList.stream().map(PatentMergePerson::getName).collect(Collectors.toList());
  696. personDTO.setMergeApplicant(collect);
  697. }
  698. }
  699. //权利人
  700. Map<String, List<PatentMergePerson>> rightMap = this.getChildMergePerson(id, projectId, "merge_right_holder");
  701. if (!CollectionUtils.isEmpty(rightMap)) {
  702. for (String rightId : rightMap.keySet()) {
  703. List<PatentMergePerson> rightList = rightMap.get(rightId);
  704. List<String> collect = rightList.stream().map(PatentMergePerson::getName).collect(Collectors.toList());
  705. personDTO.setMergeRightHolder(collect);
  706. }
  707. }
  708. } else {
  709. //发明人
  710. Map<String, List<PatentMergePerson>> inventorMap = this.getChildMergePerson(id, projectId, "merge_inventor");
  711. if (!CollectionUtils.isEmpty(inventorMap)) {
  712. for (String inventorId : inventorMap.keySet()) {
  713. List<PatentMergePerson> inventorList = inventorMap.get(inventorId);
  714. List<String> collect = inventorList.stream().map(PatentMergePerson::getName).collect(Collectors.toList());
  715. personDTO.setMergeInventor(collect);
  716. }
  717. }
  718. }
  719. personDTOS.add(personDTO);
  720. }
  721. }
  722. }
  723. } else {
  724. List<Query> queries = new ArrayList<>();
  725. if (type == 0) {
  726. Query q1 = QueryBuilders.term(i -> i.field("merge_applicant.project_id").value(projectId));
  727. Query query1 = QueryBuilders.nested(i -> i.path("merge_applicant").query(q1));
  728. Query childQ1 = QueryBuilders.hasChild(i -> i.type("merge_applicat").query(query1));
  729. queries.add(childQ1);
  730. Query q2 = QueryBuilders.term(i -> i.field("merge_right_holder.project_id").value(projectId));
  731. Query query2 = QueryBuilders.nested(i -> i.path("merge_right_holder").query(q2));
  732. Query childQ2 = QueryBuilders.hasChild(i -> i.type("merge_right_holder").query(query2));
  733. queries.add(childQ2);
  734. } else {
  735. Query q = QueryBuilders.term(i -> i.field("merge_inventor.project_id").value(projectId));
  736. Query query = QueryBuilders.nested(i -> i.path("merge_inventor").query(q));
  737. Query childQ = QueryBuilders.hasChild(i -> i.type("merge_inventor").query(query));
  738. queries.add(childQ);
  739. }
  740. Query bool = QueryBuilders.bool(i -> i.should(queries));
  741. // builder.size(99);
  742. builder.query(bool);
  743. //分页
  744. if (pageNum > 0 && pageSize > 0) {
  745. builder.from((pageNum.intValue() - 1) * pageSize.intValue()).size(pageSize.intValue());
  746. }
  747. builder.trackTotalHits(i -> i.enabled(true));
  748. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  749. List<Hit<Patent>> hits = response.hits().hits();
  750. total = response.hits().total().value();
  751. Map<String, Patent> map = new HashMap<>();
  752. for (Hit<Patent> hit : hits) {
  753. String id = hit.id();
  754. map.put(id, hit.source());
  755. }
  756. if (!CollectionUtils.isEmpty(map)) {
  757. for (String id : map.keySet()) {
  758. GetEsAllPersonDTO personDTO = new GetEsAllPersonDTO();
  759. if (type == 0) {
  760. //申请人
  761. Map<String, List<PatentMergePerson>> appMap = this.getChildMergePerson(id, projectId, "merge_applicant");
  762. if (!CollectionUtils.isEmpty(appMap)) {
  763. for (String appId : appMap.keySet()) {
  764. List<PatentMergePerson> appList = appMap.get(appId);
  765. List<String> collect = appList.stream().map(PatentMergePerson::getName).collect(Collectors.toList());
  766. personDTO.setMergeApplicant(collect);
  767. }
  768. }
  769. //权利人
  770. Map<String, List<PatentMergePerson>> rightMap = this.getChildMergePerson(id, projectId, "merge_right_holder");
  771. if (!CollectionUtils.isEmpty(rightMap)) {
  772. for (String rightId : rightMap.keySet()) {
  773. List<PatentMergePerson> rightList = rightMap.get(rightId);
  774. List<String> collect = rightList.stream().map(PatentMergePerson::getName).collect(Collectors.toList());
  775. personDTO.setMergeRightHolder(collect);
  776. }
  777. }
  778. } else {
  779. //发明人
  780. Map<String, List<PatentMergePerson>> inventorMap = this.getChildMergePerson(id, projectId, "merge_inventor");
  781. if (!CollectionUtils.isEmpty(inventorMap)) {
  782. for (String inventorId : inventorMap.keySet()) {
  783. List<PatentMergePerson> inventorList = inventorMap.get(inventorId);
  784. List<String> collect = inventorList.stream().map(PatentMergePerson::getName).collect(Collectors.toList());
  785. personDTO.setMergeInventor(collect);
  786. }
  787. }
  788. }
  789. personDTOS.add(personDTO);
  790. }
  791. }
  792. }
  793. //es申请人/权利人
  794. List<String> mergeList = new ArrayList<>();
  795. if (!CollectionUtils.isEmpty(personDTOS)) {
  796. if (type == 0) {
  797. for (GetEsAllPersonDTO personDTO : personDTOS) {
  798. List<String> appList = new ArrayList<>();
  799. if (!CollectionUtils.isEmpty(personDTO.getMergeApplicant())) {
  800. appList = personDTO.getMergeApplicant();
  801. mergeList.addAll(appList);
  802. }
  803. List<String> rightList = new ArrayList<>();
  804. if (!CollectionUtils.isEmpty(personDTO.getMergeRightHolder())) {
  805. rightList = personDTO.getMergeRightHolder();
  806. mergeList.addAll(rightList);
  807. }
  808. }
  809. } else {
  810. for (GetEsAllPersonDTO personDTO : personDTOS) {
  811. List<String> inventorList = new ArrayList<>();
  812. if (!CollectionUtils.isEmpty(personDTO.getMergeInventor())) {
  813. inventorList = personDTO.getMergeInventor();
  814. mergeList.addAll(inventorList);
  815. }
  816. }
  817. }
  818. }
  819. List<GetAllPersonDTO> personDTOList = new ArrayList<>();
  820. List<GetAllPersonDTO> mergePersonList = new ArrayList<>();
  821. List<GetAllPersonDTO> personList = new ArrayList<>();
  822. List<String> nameList = mergeList.stream().distinct().collect(Collectors.toList());
  823. List<String> mergedList = new ArrayList<>();
  824. for (String key : nameList) {
  825. MergePerson person = mergePersonMapper.selectOne(new LambdaQueryWrapper<MergePerson>()
  826. .eq(MergePerson::getName, key)
  827. .eq(MergePerson::getProjectId, projectId));
  828. if (ObjectUtils.isNotEmpty(person)) {
  829. GetAllPersonDTO dto = new GetAllPersonDTO();
  830. dto.setName(person.getName());
  831. dto.setMergeId(person.getId());
  832. dto.setType(person.getType());
  833. dto.setRemark(person.getRemark());
  834. dto.setAbbreviation(person.getAbbreviation());
  835. dto.setCountry(person.getCountry());
  836. dto.setProvince(person.getProvince());
  837. dto.setAddress(person.getAddress());
  838. String mergedName = person.getMergedName();
  839. List<String> getNames = JSONArray.parseArray(mergedName, String.class);
  840. mergedList.addAll(getNames);
  841. mergePersonList.add(dto);
  842. } else {
  843. GetAllPersonDTO dto = new GetAllPersonDTO();
  844. dto.setName(key);
  845. personList.add(dto);
  846. }
  847. }
  848. if (!CollectionUtils.isEmpty(mergePersonList)) {
  849. List<GetAllPersonDTO> collect = mergePersonList.stream().sorted(Comparator.comparing(GetAllPersonDTO::getName)).collect(Collectors.toList());
  850. personDTOList.addAll(collect);
  851. }
  852. if (!CollectionUtils.isEmpty(personList)) {
  853. if (!CollectionUtils.isEmpty(mergedList)) {
  854. for (String s : mergedList) {
  855. personList.removeIf(i -> i.getName().equals(s));
  856. }
  857. }
  858. List<GetAllPersonDTO> collect = personList.stream().filter(i -> !StringUtils.isEmpty(i.getName())).collect(Collectors.toList());
  859. List<GetAllPersonDTO> list = collect.stream().sorted(Comparator.comparing(GetAllPersonDTO::getName)).collect(Collectors.toList());
  860. personDTOList.addAll(list);
  861. }
  862. // List<GetAllPersonDTO> collect = personDTOList.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
  863. Records records = new Records();
  864. records.setCurrent(pageNum);
  865. records.setSize(pageSize);
  866. records.setData(personDTOList);
  867. records.setTotal(total);
  868. return records;
  869. }
  870. /**
  871. * 发明人/权利人/申请人合并后的名称移除
  872. *
  873. * @param vo
  874. * @return
  875. * @throws Exception
  876. */
  877. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  878. public Integer delMergedName(MergedNameVO vo) throws Exception {
  879. Integer type = vo.getType();
  880. //要移除的名字
  881. String delName = vo.getDelName();
  882. //合并后的名称
  883. String name = vo.getName();
  884. Integer projectId = vo.getProjectId();
  885. MergePerson mergePerson = mergePersonMapper.selectOne(new LambdaQueryWrapper<MergePerson>()
  886. .eq(MergePerson::getName, name)
  887. .eq(MergePerson::getProjectId, projectId));
  888. if (ObjectUtil.isNotEmpty(mergePerson)) {
  889. Integer personId = mergePerson.getId();
  890. String mergedName = mergePerson.getMergedName();
  891. List<String> list = JSONArray.parseArray(mergedName, String.class);
  892. list.remove(delName);
  893. MergePerson person = mergePersonMapper.selectById(personId);
  894. person.setMergedName(JSONArray.toJSONString(list));
  895. person.updateById();
  896. //移除后最后剩下的名称
  897. String finalMergedName = person.getMergedName();
  898. List<String> nameDatas = JSONArray.parseArray(finalMergedName, String.class);
  899. SearchRequest.Builder builder = new SearchRequest.Builder();
  900. //设置查询索引
  901. builder.index("patent");
  902. List<Query> queries = new ArrayList<>();
  903. if (type == 0) {
  904. //合并申请人
  905. Query idQ1 = QueryBuilders.term(i -> i.field("merge_applicant.project_id").value(projectId));
  906. Query q1 = QueryBuilders.term(i -> i.field("merge_applicant.name.raw").value(name));
  907. Query bool = QueryBuilders.bool(i -> i.must(idQ1, q1));
  908. Query query1 = QueryBuilders.nested(i -> i.path("merge_applicant").query(bool));
  909. Query childQ1 = QueryBuilders.hasChild(i -> i.type("merge_applicat").query(query1));
  910. //合并权利人
  911. Query idQ2 = QueryBuilders.term(i -> i.field("merge_right_holder.project_id").value(projectId));
  912. Query q2 = QueryBuilders.term(i -> i.field("merge_right_holder.name.raw").value(name));
  913. Query bool1 = QueryBuilders.bool(i -> i.must(idQ2, q2));
  914. Query query2 = QueryBuilders.nested(i -> i.path("merge_right_holder").query(bool1));
  915. Query childQ2 = QueryBuilders.hasChild(i -> i.type("merge_right_holder").query(query2));
  916. queries.add(childQ1);
  917. queries.add(childQ2);
  918. } else {
  919. Query idQ = QueryBuilders.term(i -> i.field("merge_inventor.project_id").value(projectId));
  920. Query q = QueryBuilders.term(i -> i.field("merge_inventor.name.raw").value(name));
  921. Query bool = QueryBuilders.bool(i -> i.must(idQ, q));
  922. Query query = QueryBuilders.nested(i -> i.path("merge_inventor").query(bool));
  923. Query childQ = QueryBuilders.hasChild(i -> i.type("merge_inventor").query(query));
  924. queries.add(childQ);
  925. }
  926. Query query = QueryBuilders.bool(i -> i.should(queries));
  927. builder.query(query);
  928. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  929. List<Hit<Patent>> hits = response.hits().hits();
  930. Map<String, Patent> map = new HashMap<>();
  931. for (Hit<Patent> hit : hits) {
  932. String id = hit.id();
  933. map.put(id, hit.source());
  934. }
  935. if (!CollectionUtils.isEmpty(map)) {
  936. for (String id : map.keySet()) {
  937. Patent patent = map.get(id);
  938. List<PatentMergePerson> mergePersonList = new ArrayList<>();
  939. if (type == 0) {
  940. if (!CollectionUtils.isEmpty(patent.getApplicant())) {
  941. List<PatentPerson> applicant = patent.getApplicant();
  942. List<String> collect = applicant.stream().map(PatentPerson::getName).collect(Collectors.toList());
  943. if (collect.contains(delName)) {
  944. for (PatentPerson patentPerson : applicant) {
  945. if (delName.equals(patentPerson.getName())) {
  946. PatentMergePerson merge = new PatentMergePerson();
  947. merge.setName(delName);
  948. merge.setType(patentPerson.getType());
  949. merge.setOrder(patentPerson.getOrder());
  950. merge.setProjectId(String.valueOf(projectId));
  951. mergePersonList.add(merge);
  952. }
  953. }
  954. }
  955. }
  956. if (CollectionUtils.isEmpty(mergePersonList) && !CollectionUtils.isEmpty(patent.getRightHolder())) {
  957. List<PatentPerson> rightHolder = patent.getRightHolder();
  958. List<String> collect = rightHolder.stream().map(PatentPerson::getName).collect(Collectors.toList());
  959. if (collect.contains(delName)) {
  960. for (PatentPerson patentPerson : rightHolder) {
  961. if (delName.equals(patentPerson.getName())) {
  962. PatentMergePerson merge = new PatentMergePerson();
  963. merge.setName(delName);
  964. merge.setType(patentPerson.getType());
  965. merge.setOrder(patentPerson.getOrder());
  966. merge.setProjectId(String.valueOf(projectId));
  967. mergePersonList.add(merge);
  968. }
  969. }
  970. }
  971. }
  972. } else {
  973. if (!CollectionUtils.isEmpty(patent.getInventor())) {
  974. List<PatentPerson> inventor = patent.getInventor();
  975. List<String> collect = inventor.stream().map(PatentPerson::getName).collect(Collectors.toList());
  976. if (collect.contains(delName)) {
  977. for (PatentPerson patentPerson : inventor) {
  978. if (delName.equals(patentPerson.getName())) {
  979. PatentMergePerson merge = new PatentMergePerson();
  980. merge.setName(delName);
  981. merge.setType(patentPerson.getType());
  982. merge.setOrder(patentPerson.getOrder());
  983. merge.setProjectId(String.valueOf(projectId));
  984. mergePersonList.add(merge);
  985. }
  986. }
  987. }
  988. }
  989. }
  990. if (type == 0) {
  991. //申请人
  992. Map<String, List<PatentMergePerson>> appMap = this.getChildMergePerson(id, projectId, "merge_applicant");
  993. if (!CollectionUtils.isEmpty(appMap)) {
  994. for (String appId : appMap.keySet()) {
  995. // if (CollectionUtils.isEmpty(nameDatas)) {
  996. // this.delSingleMerge(appId, type, name);
  997. // }
  998. List<PatentMergePerson> appMergeList = new ArrayList<>(mergePersonList);
  999. List<PatentMergePerson> appList = appMap.get(appId);
  1000. appMergeList.addAll(appList);
  1001. if (CollectionUtils.isEmpty(nameDatas)) {
  1002. if (!CollectionUtils.isEmpty(appMergeList)) {
  1003. appMergeList.removeIf(app -> app.getName().equals(name));
  1004. }
  1005. }
  1006. //添加子文档
  1007. Patent newPatent = new Patent();
  1008. PatentJoin patentJoin = new PatentJoin();
  1009. patentJoin.setParent(id);
  1010. patentJoin.setName("merge_applicat");
  1011. newPatent.setPatentJoin(patentJoin);
  1012. newPatent.setMergeApplicant(appMergeList);
  1013. String child = this.addChild(newPatent, id);
  1014. if (StringUtils.isEmpty(child)) {
  1015. throw new XiaoShiException("移除失败");
  1016. } else {
  1017. //删除原子文档
  1018. this.delete(Arrays.asList(appId));
  1019. }
  1020. }
  1021. }
  1022. //权利人
  1023. Map<String, List<PatentMergePerson>> rightMap = this.getChildMergePerson(id, projectId, "merge_right_holder");
  1024. if (!CollectionUtils.isEmpty(rightMap)) {
  1025. for (String rightId : rightMap.keySet()) {
  1026. // if (CollectionUtils.isEmpty(nameDatas)) {
  1027. // this.delSingleMerge(rightId, type, name);
  1028. // }
  1029. ArrayList<PatentMergePerson> rightMergeList = new ArrayList<>(mergePersonList);
  1030. List<PatentMergePerson> rightList = rightMap.get(rightId);
  1031. rightMergeList.addAll(rightList);
  1032. if (CollectionUtils.isEmpty(nameDatas)) {
  1033. if (!CollectionUtils.isEmpty(rightMergeList)) {
  1034. rightMergeList.removeIf(right -> right.getName().equals(name));
  1035. }
  1036. }
  1037. //添加子文档
  1038. Patent newPatent = new Patent();
  1039. PatentJoin patentJoin = new PatentJoin();
  1040. patentJoin.setParent(id);
  1041. patentJoin.setName("merge_right_holder");
  1042. newPatent.setPatentJoin(patentJoin);
  1043. newPatent.setMergeRightHolder(rightMergeList);
  1044. String child = this.addChild(newPatent, id);
  1045. if (StringUtils.isEmpty(child)) {
  1046. throw new XiaoShiException("移除失败");
  1047. } else {
  1048. //删除原子文档
  1049. this.delete(Arrays.asList(rightId));
  1050. }
  1051. }
  1052. }
  1053. } else if (type == 2) {
  1054. Map<String, List<PatentMergePerson>> inventorMap = this.getChildMergePerson(id, projectId, "merge_inventor");
  1055. if (!CollectionUtils.isEmpty(inventorMap)) {
  1056. for (String inventorId : inventorMap.keySet()) {
  1057. // if (CollectionUtils.isEmpty(nameDatas)) {
  1058. // this.delSingleMerge(inventorId, type, name);
  1059. // }
  1060. ArrayList<PatentMergePerson> inventorMergeList = new ArrayList<>(mergePersonList);
  1061. List<PatentMergePerson> inventorList = inventorMap.get(inventorId);
  1062. inventorMergeList.addAll(inventorList);
  1063. if (CollectionUtils.isEmpty(nameDatas)) {
  1064. if (!CollectionUtils.isEmpty(inventorMergeList)) {
  1065. inventorMergeList.removeIf(inventor -> inventor.getName().equals(name));
  1066. }
  1067. }
  1068. //添加子文档
  1069. Patent newPatent = new Patent();
  1070. PatentJoin patentJoin = new PatentJoin();
  1071. patentJoin.setParent(id);
  1072. patentJoin.setName("merge_inventor");
  1073. newPatent.setPatentJoin(patentJoin);
  1074. newPatent.setMergeInventor(inventorMergeList);
  1075. String child = this.addChild(newPatent, id);
  1076. if (StringUtils.isEmpty(child)) {
  1077. throw new XiaoShiException("移除失败");
  1078. } else {
  1079. //删除原子文档
  1080. this.delete(Arrays.asList(inventorId));
  1081. }
  1082. }
  1083. }
  1084. }
  1085. }
  1086. } else {
  1087. throw new XiaoShiException("移除失败");
  1088. }
  1089. }
  1090. return mergePerson.getId();
  1091. }
  1092. /**
  1093. * 发明人/权利人/申请人合并记录删除
  1094. *
  1095. * @param vo
  1096. * @return
  1097. * @throws Exception
  1098. */
  1099. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  1100. public Integer delMergePerson(MergePersonIdVO vo) throws Exception {
  1101. Integer type = vo.getType();
  1102. MergePerson mergePerson = mergePersonMapper.selectById(vo.getId());
  1103. //合并后的名称
  1104. String name = mergePerson.getName();
  1105. //专题库id
  1106. Integer projectId = mergePerson.getProjectId();
  1107. //合并的名称
  1108. String mergedName = mergePerson.getMergedName();
  1109. if (ObjectUtil.isNotEmpty(mergePerson)) {
  1110. LambdaQueryWrapper<MergePerson> wrapper = new LambdaQueryWrapper<MergePerson>()
  1111. .eq(BaseEntity::getId, vo.getId());
  1112. this.remove(wrapper);
  1113. List<String> list = JSONArray.parseArray(mergedName, String.class);
  1114. if (!CollectionUtils.isEmpty(list)) {
  1115. Map<String, Patent> map = this.getDelPatent(type, projectId, name);
  1116. if (!CollectionUtils.isEmpty(map)) {
  1117. for (String id : map.keySet()) {
  1118. Patent patent = map.get(id);
  1119. List<PatentMergePerson> mergePersonList = new ArrayList<>();
  1120. if (type == 0) {
  1121. if (!CollectionUtils.isEmpty(patent.getApplicant())) {
  1122. List<PatentPerson> applicant = patent.getApplicant();
  1123. for (PatentPerson patentPerson : applicant) {
  1124. if (list.contains(patentPerson.getName())) {
  1125. PatentMergePerson merge = new PatentMergePerson();
  1126. merge.setName(patentPerson.getName());
  1127. merge.setType(patentPerson.getType());
  1128. merge.setOrder(patentPerson.getOrder());
  1129. merge.setProjectId(String.valueOf(projectId));
  1130. mergePersonList.add(merge);
  1131. }
  1132. }
  1133. }
  1134. if (!CollectionUtils.isEmpty(patent.getRightHolder()) && list.size() != mergePersonList.size()) {
  1135. List<PatentPerson> rightHolder = patent.getRightHolder();
  1136. for (PatentPerson patentPerson : rightHolder) {
  1137. if (list.contains(patentPerson.getName())) {
  1138. PatentMergePerson merge = new PatentMergePerson();
  1139. merge.setName(patentPerson.getName());
  1140. merge.setType(patentPerson.getType());
  1141. merge.setOrder(patentPerson.getOrder());
  1142. merge.setProjectId(String.valueOf(projectId));
  1143. mergePersonList.add(merge);
  1144. }
  1145. }
  1146. }
  1147. } else {
  1148. if (!CollectionUtils.isEmpty(patent.getInventor())) {
  1149. List<PatentPerson> inventor = patent.getInventor();
  1150. for (PatentPerson patentPerson : inventor) {
  1151. if (list.contains(patentPerson.getName())) {
  1152. PatentMergePerson merge = new PatentMergePerson();
  1153. merge.setName(patentPerson.getName());
  1154. merge.setType(patentPerson.getType());
  1155. merge.setOrder(patentPerson.getOrder());
  1156. merge.setProjectId(String.valueOf(projectId));
  1157. mergePersonList.add(merge);
  1158. }
  1159. }
  1160. }
  1161. }
  1162. if (type == 0) {
  1163. //申请人
  1164. Map<String, List<PatentMergePerson>> appMap = this.getChildMergePerson(id, projectId, "merge_applicant");
  1165. if (!CollectionUtils.isEmpty(appMap)) {
  1166. for (String appId : appMap.keySet()) {
  1167. List<PatentMergePerson> appMergeList = new ArrayList<>(mergePersonList);
  1168. List<PatentMergePerson> appList = appMap.get(appId);
  1169. appMergeList.addAll(appList);
  1170. if (!CollectionUtils.isEmpty(appMergeList)) {
  1171. appMergeList.removeIf(app -> app.getName().equals(name));
  1172. }
  1173. //添加子文档
  1174. Patent newPatent = new Patent();
  1175. PatentJoin patentJoin = new PatentJoin();
  1176. patentJoin.setParent(id);
  1177. patentJoin.setName("merge_applicat");
  1178. newPatent.setPatentJoin(patentJoin);
  1179. newPatent.setMergeApplicant(appMergeList);
  1180. String child = this.addChild(newPatent, id);
  1181. if (StringUtils.isEmpty(child)) {
  1182. throw new XiaoShiException("删除失败");
  1183. } else {
  1184. //删除原子文档
  1185. this.delete(Arrays.asList(appId));
  1186. }
  1187. }
  1188. }
  1189. //权利人
  1190. Map<String, List<PatentMergePerson>> rightMap = this.getChildMergePerson(id, projectId, "merge_right_holder");
  1191. if (!CollectionUtils.isEmpty(rightMap)) {
  1192. for (String rightId : rightMap.keySet()) {
  1193. ArrayList<PatentMergePerson> rightMergeList = new ArrayList<>(mergePersonList);
  1194. List<PatentMergePerson> rightList = rightMap.get(rightId);
  1195. rightMergeList.addAll(rightList);
  1196. if (!CollectionUtils.isEmpty(rightMergeList)) {
  1197. rightMergeList.removeIf(right -> right.getName().equals(name));
  1198. }
  1199. //添加子文档
  1200. Patent newPatent = new Patent();
  1201. PatentJoin patentJoin = new PatentJoin();
  1202. patentJoin.setParent(id);
  1203. patentJoin.setName("merge_right_holder");
  1204. newPatent.setPatentJoin(patentJoin);
  1205. newPatent.setMergeRightHolder(rightMergeList);
  1206. String child = this.addChild(newPatent, id);
  1207. if (StringUtils.isEmpty(child)) {
  1208. throw new XiaoShiException("删除失败");
  1209. } else {
  1210. //删除原子文档
  1211. this.delete(Arrays.asList(rightId));
  1212. }
  1213. }
  1214. }
  1215. } else if (type == 2) {
  1216. Map<String, List<PatentMergePerson>> inventorMap = this.getChildMergePerson(id, projectId, "merge_inventor");
  1217. if (!CollectionUtils.isEmpty(inventorMap)) {
  1218. for (String inventorId : inventorMap.keySet()) {
  1219. ArrayList<PatentMergePerson> inventorMergeList = new ArrayList<>(mergePersonList);
  1220. List<PatentMergePerson> inventorList = inventorMap.get(inventorId);
  1221. inventorMergeList.addAll(inventorList);
  1222. if (!CollectionUtils.isEmpty(inventorMergeList)) {
  1223. inventorMergeList.removeIf(inventor -> inventor.getName().equals(name));
  1224. }
  1225. //添加子文档
  1226. Patent newPatent = new Patent();
  1227. PatentJoin patentJoin = new PatentJoin();
  1228. patentJoin.setParent(id);
  1229. patentJoin.setName("merge_inventor");
  1230. newPatent.setPatentJoin(patentJoin);
  1231. newPatent.setMergeInventor(inventorMergeList);
  1232. String child = this.addChild(newPatent, id);
  1233. if (StringUtils.isEmpty(child)) {
  1234. throw new XiaoShiException("删除失败");
  1235. } else {
  1236. //删除原子文档
  1237. this.delete(Arrays.asList(inventorId));
  1238. }
  1239. }
  1240. }
  1241. }
  1242. }
  1243. } else {
  1244. throw new XiaoShiException("删除失败");
  1245. }
  1246. }
  1247. }
  1248. return vo.getId();
  1249. }
  1250. public Map<String, Patent> getDelPatent(Integer type, Integer projectId, String name) throws IOException {
  1251. SearchRequest.Builder builder = new SearchRequest.Builder();
  1252. //设置查询索引
  1253. builder.index("patent");
  1254. List<Query> queries = new ArrayList<>();
  1255. if (type == 0) {
  1256. //合并申请人
  1257. Query idQ1 = QueryBuilders.term(i -> i.field("merge_applicant.project_id").value(projectId));
  1258. Query q1 = QueryBuilders.term(i -> i.field("merge_applicant.name.raw").value(name));
  1259. Query bool = QueryBuilders.bool(i -> i.must(idQ1, q1));
  1260. Query query1 = QueryBuilders.nested(i -> i.path("merge_applicant").query(bool));
  1261. Query childQ1 = QueryBuilders.hasChild(i -> i.type("merge_applicat").query(query1));
  1262. //合并权利人
  1263. Query idQ2 = QueryBuilders.term(i -> i.field("merge_right_holder.project_id").value(projectId));
  1264. Query q2 = QueryBuilders.term(i -> i.field("merge_right_holder.name.raw").value(name));
  1265. Query bool1 = QueryBuilders.bool(i -> i.must(idQ2, q2));
  1266. Query query2 = QueryBuilders.nested(i -> i.path("merge_right_holder").query(bool1));
  1267. Query childQ2 = QueryBuilders.hasChild(i -> i.type("merge_right_holder").query(query2));
  1268. queries.add(childQ1);
  1269. queries.add(childQ2);
  1270. } else {
  1271. Query idQ = QueryBuilders.term(i -> i.field("merge_inventor.project_id").value(projectId));
  1272. Query q = QueryBuilders.term(i -> i.field("merge_inventor.name.raw").value(name));
  1273. Query bool = QueryBuilders.bool(i -> i.must(idQ, q));
  1274. Query query = QueryBuilders.nested(i -> i.path("merge_inventor").query(bool));
  1275. Query childQ = QueryBuilders.hasChild(i -> i.type("merge_inventor").query(query));
  1276. queries.add(childQ);
  1277. }
  1278. Query query = QueryBuilders.bool(i -> i.should(queries));
  1279. builder.query(query);
  1280. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  1281. List<Hit<Patent>> hits = response.hits().hits();
  1282. Map<String, Patent> map = new HashMap<>();
  1283. for (Hit<Patent> hit : hits) {
  1284. String id = hit.id();
  1285. map.put(id, hit.source());
  1286. }
  1287. return map;
  1288. }
  1289. /**
  1290. * 获取所有国家列表查询
  1291. *
  1292. * @return
  1293. */
  1294. public List<GetAllCountryDTO> getAllCountry() {
  1295. List<GetAllCountryDTO> list = new ArrayList<>();
  1296. List<SystemDict> countries = systemDictMapper.selectList(new LambdaQueryWrapper<SystemDict>()
  1297. .eq(SystemDict::getType, "COUNTRIES"));
  1298. if (!CollectionUtils.isEmpty(countries)) {
  1299. countries.forEach(county -> {
  1300. GetAllCountryDTO dto = new GetAllCountryDTO();
  1301. dto.setLabel(county.getLabel());
  1302. dto.setValue(county.getValue());
  1303. dto.setType(county.getType());
  1304. list.add(dto);
  1305. });
  1306. }
  1307. return list;
  1308. }
  1309. /**
  1310. * 获取国内省份列表查询
  1311. *
  1312. * @return
  1313. */
  1314. public List<GetAllCountryDTO> getAllProvince() {
  1315. List<GetAllCountryDTO> list = new ArrayList<>();
  1316. List<SystemDict> countries = systemDictMapper.selectList(new LambdaQueryWrapper<SystemDict>()
  1317. .eq(SystemDict::getType, "PROVINCE"));
  1318. if (!CollectionUtils.isEmpty(countries)) {
  1319. countries.forEach(county -> {
  1320. GetAllCountryDTO dto = new GetAllCountryDTO();
  1321. dto.setLabel(county.getLabel());
  1322. dto.setValue(county.getValue());
  1323. dto.setType(county.getType());
  1324. list.add(dto);
  1325. });
  1326. }
  1327. return list;
  1328. }
  1329. /**
  1330. * @param patent
  1331. * @throws Exception
  1332. */
  1333. public String addChild(Patent patent, String id) throws Exception {
  1334. IndexResponse indexResponse = client.index(i -> i
  1335. .index("patent")
  1336. .routing(id)
  1337. //传入user对象
  1338. .document(patent)
  1339. );
  1340. return indexResponse.id();
  1341. }
  1342. //更新patent
  1343. public Integer updateChild(Patent patent, String id, String routing) {
  1344. UpdateRequest<Patent, Patent> req;
  1345. req = UpdateRequest.of(
  1346. b -> b.index("patent")
  1347. .id(id)
  1348. .routing(id)
  1349. .doc(patent)
  1350. );
  1351. try {
  1352. client.update(req, Patent.class);
  1353. return 1;
  1354. } catch (IOException e) {
  1355. return -1;
  1356. }
  1357. }
  1358. public Integer delete(List<String> ids) {
  1359. Query query = QueryBuilders.ids(n -> n.values(ids));
  1360. DeleteByQueryRequest request = DeleteByQueryRequest.of(i -> i.index("patent").query(query));
  1361. try {
  1362. client.deleteByQuery(request);
  1363. return 1;
  1364. } catch (IOException e) {
  1365. throw new XiaoShiException("删除失败");
  1366. }
  1367. }
  1368. /**
  1369. * 删除申请人/权利人/发明人合并名称
  1370. *
  1371. * @param id
  1372. * @return
  1373. */
  1374. public Integer delSingleMerge(String id, Integer type, String name) {
  1375. String source = "";
  1376. if (type == 0) {
  1377. source = "if (ctx._source.merge_applicant != null) { ctx._source.merge_applicant.removeIf(item -> item.name == params.name); } if (ctx._source.merge_right_holder != null) { ctx._source.merge_right_holder .removeIf(item -> item.name == params.name); }";
  1378. } else {
  1379. source = "if (ctx._source.merge_inventor != null) { ctx._source.merge_inventor.removeIf(item -> item.name == params.name); }";
  1380. }
  1381. String finalSource = source;
  1382. InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").params("name", JsonData.of(name)).source(finalSource));
  1383. Script script = Script.of(i -> i.inline(inlineScript));
  1384. Query query = QueryBuilders.term(i -> i.field("_id").value(id));
  1385. UpdateByQueryRequest request = UpdateByQueryRequest.of(i -> i.index("patent").script(script).query(query));
  1386. try {
  1387. client.updateByQuery(request);
  1388. return 1;
  1389. } catch (IOException e) {
  1390. return -1;
  1391. }
  1392. }
  1393. public Integer add(String id, Integer type, List<PatentMergePerson> mergePersonList) {
  1394. Integer flag = null;
  1395. for (PatentMergePerson mergePerson : mergePersonList) {
  1396. Map<String, JsonData> map = new HashMap<>();
  1397. map.put("name", JsonData.of(mergePerson.getName()));
  1398. map.put("project_id", JsonData.of(mergePerson.getProjectId()));
  1399. map.put("type", JsonData.of(mergePerson.getType()));
  1400. map.put("order", JsonData.of(mergePerson.getOrder()));
  1401. String source = "";
  1402. if (type == 0) {
  1403. source = "if (ctx._source.merge_applicant != null) {ctx._source.merge_applicant.add(params.data)} else { List list = new ArrayList();list.add(params.data);ctx._source.merge_applicant = list;}" +
  1404. "if (ctx._source.merge_right_holder != null) { ctx._source.merge_right_holder.add(params.data)} else { List list = new ArrayList();list.add(params.data);ctx._source.merge_right_holder = list;}";
  1405. } else {
  1406. source = "if (ctx._source.merge_inventor != null) {ctx._source.merge_inventor.add(params.data)} else { List list = new ArrayList();list.add(params.data);ctx._source.merge_inventor = list;}";
  1407. }
  1408. String finalSource = source;
  1409. InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").params("data", JsonData.of(map)).source(finalSource));
  1410. Script script = Script.of(i -> i.inline(inlineScript));
  1411. Query query = QueryBuilders.term(i -> i.field("_id").value(id));
  1412. UpdateByQueryRequest request = UpdateByQueryRequest.of(i -> i.index("patent").script(script).query(query));
  1413. try {
  1414. client.updateByQuery(request);
  1415. flag += 1;
  1416. } catch (IOException e) {
  1417. flag += -1;
  1418. }
  1419. }
  1420. return flag;
  1421. }
  1422. public Integer edit(String id, Integer type, String oldName, String newName) {
  1423. String source = "";
  1424. if (type == 0) {
  1425. source = "if (ctx._source.merge_applicant != null) {for (item in ctx._source.merge_applicant) {if(item['name']==params.oldName){item['name']=params.newName}}}" +
  1426. "if (ctx._source.merge_right_holder != null) {for (item in ctx._source.merge_right_holder) {if(item['name']==params.oldName){item['name']=params.newName}}}";
  1427. } else {
  1428. source = "if (ctx._source.merge_inventor != null) {for (item in ctx._source.merge_inventor) {if(item['name']==params.oldName){item['name']=params.newName}}}";
  1429. }
  1430. String finalSource = source;
  1431. Map<String, JsonData> map = new HashMap<>();
  1432. map.put("oldName", JsonData.of(oldName));
  1433. map.put("newName", JsonData.of(newName));
  1434. InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").params(map).source(finalSource));
  1435. Script script = Script.of(i -> i.inline(inlineScript));
  1436. Query query = QueryBuilders.term(i -> i.field("_id").value(id));
  1437. UpdateByQueryRequest request = UpdateByQueryRequest.of(i -> i.index("patent").script(script).query(query));
  1438. try {
  1439. client.updateByQuery(request);
  1440. return 1;
  1441. } catch (IOException e) {
  1442. return -1;
  1443. }
  1444. }
  1445. }