PatentApplicantService.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package cn.cslg.pas.service;
  2. import cn.cslg.pas.common.model.dto.AreaAddressDTO;
  3. import cn.cslg.pas.common.model.params.PatentApplicantAddressParams;
  4. import cn.cslg.pas.common.model.vo.PatentApplicantVO;
  5. import cn.cslg.pas.common.utils.DateUtils;
  6. import cn.cslg.pas.common.utils.PatentUtils;
  7. import cn.cslg.pas.common.utils.Response;
  8. import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
  9. import cn.cslg.pas.common.utils.StringUtils;
  10. import cn.cslg.pas.domain.PatentApplicant;
  11. import cn.cslg.pas.domain.PatentApplicantLink;
  12. import cn.cslg.pas.domain.PatentApplicantMergeLink;
  13. import cn.cslg.pas.domain.SystemDict;
  14. import cn.cslg.pas.mapper.PatentApplicantMapper;
  15. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  16. import com.baomidou.mybatisplus.core.metadata.IPage;
  17. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  18. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  19. import lombok.RequiredArgsConstructor;
  20. import org.springframework.beans.BeanUtils;
  21. import org.springframework.context.annotation.Lazy;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import java.util.*;
  25. import java.util.stream.Collectors;
  26. /**
  27. * <p>
  28. * 专利信息申请人表 服务类
  29. * </p>
  30. *
  31. * @author 王岩
  32. * @since 2022-02-24
  33. */
  34. @Service
  35. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  36. public class PatentApplicantService extends ServiceImpl<PatentApplicantMapper, PatentApplicant> {
  37. private final PatentApplicantLinkService patentApplicantLinkService;
  38. private final PatentApplicantMergeLinkService patentApplicantMergeLinkService;
  39. private final AreaService areaService;
  40. private final SystemDictService systemDictService;
  41. private final LoginUtils loginUtils;
  42. public List<PatentApplicant> getPatentApplicantByIds(List<Integer> ids) {
  43. if (ids == null || ids.size() == 0) {
  44. return new ArrayList<>();
  45. }
  46. LambdaQueryWrapper<PatentApplicant> queryWrapper = new LambdaQueryWrapper<>();
  47. queryWrapper.in(PatentApplicant::getId, ids);
  48. return this.list(queryWrapper);
  49. }
  50. public List<PatentApplicant> getPatentApplicantByPatentIds(List<Integer> patentIds) {
  51. List<PatentApplicantLink> patentApplicantLinkList = patentApplicantLinkService.getApplicantAttributesListByPatentIds(patentIds);
  52. List<Integer> applicantIds = patentApplicantLinkList.stream().map(PatentApplicantLink::getApplicantId).distinct().collect(Collectors.toList());
  53. List<PatentApplicant> patentApplicantList = this.getPatentApplicantByIds(applicantIds);
  54. return this.setPatentApplicantDataList(patentApplicantLinkList, patentApplicantList);
  55. }
  56. public List<PatentApplicant> getPatentApplicantByPatentId(Integer patentId) {
  57. List<PatentApplicantLink> patentApplicantLinkList = patentApplicantLinkService.getPatentApplicantAttributesByPatentId(patentId);
  58. List<Integer> applicantIds = patentApplicantLinkList.stream().map(PatentApplicantLink::getApplicantId).distinct().collect(Collectors.toList());
  59. List<PatentApplicant> patentApplicantList = this.getPatentApplicantByIds(applicantIds);
  60. return this.setPatentApplicantDataList(patentApplicantLinkList, patentApplicantList);
  61. }
  62. private List<PatentApplicant> setPatentApplicantDataList(List<PatentApplicantLink> patentApplicantLinkList, List<PatentApplicant> patentApplicantList) {
  63. List<PatentApplicant> dataList = new ArrayList<>();
  64. patentApplicantLinkList.forEach(attribute -> {
  65. PatentApplicant temp = patentApplicantList.stream().filter(applicant -> applicant.getId().equals(attribute.getApplicantId())).findFirst().orElse(null);
  66. if (temp != null) {
  67. PatentApplicant patentApplicant = new PatentApplicant();
  68. patentApplicant.setType(temp.getType());
  69. patentApplicant.setId(temp.getId());
  70. patentApplicant.setPatentId(attribute.getPatentId());
  71. patentApplicant.setName(temp.getName());
  72. patentApplicant.setShortName(temp.getShortName());
  73. patentApplicant.setCountry(temp.getCountry());
  74. patentApplicant.setProvinceId(temp.getProvinceId());
  75. patentApplicant.setCityId(temp.getCityId());
  76. patentApplicant.setAreaId(temp.getAreaId());
  77. patentApplicant.setAddressStr(temp.getAddressStr());
  78. patentApplicant.setOrder(attribute.getOrder());
  79. patentApplicant.setDataType(attribute.getType());
  80. dataList.add(patentApplicant);
  81. }
  82. });
  83. return dataList;
  84. }
  85. public IPage<PatentApplicant> getPageList(PatentApplicantVO params) {
  86. IPage<PatentApplicant> pageList = baseMapper.getPageList(new Page<>(params.getCurrent(), params.getSize()), params);
  87. List<SystemDict> systemDictList = systemDictService.getSystemDictListByType(Collections.singletonList("COUNTRIES"));
  88. pageList.getRecords().forEach(item -> item.setCountryName(systemDictList.stream().filter(systemDict -> systemDict.getValue().equals(item.getCountry())).findFirst().orElse(new SystemDict()).getLabel()));
  89. return pageList;
  90. }
  91. public IPage<PatentApplicant> getMergeList(PatentApplicantVO params) {
  92. LambdaQueryWrapper<PatentApplicant> queryWrapper = new LambdaQueryWrapper<>();
  93. if (StringUtils.isNotEmpty(params.getName())) {
  94. queryWrapper.like(PatentApplicant::getName, params.getName());
  95. }
  96. if (StringUtils.isNotEmpty(params.getCountry())) {
  97. queryWrapper.eq(PatentApplicant::getCountry, params.getCountry());
  98. }
  99. queryWrapper.eq(PatentApplicant::getProjectId, params.getProjectId());
  100. queryWrapper.eq(PatentApplicant::getMerge, true);
  101. IPage<PatentApplicant> pageList = this.page(new Page<>(params.getCurrent(), params.getSize()), queryWrapper);
  102. List<SystemDict> systemDictList = systemDictService.getSystemDictListByType(Collections.singletonList("COUNTRIES"));
  103. List<PatentApplicantMergeLink> linkList = patentApplicantMergeLinkService.getPatentApplicantMergeLinkListByProjectId(params.getProjectId());
  104. pageList.getRecords().forEach(item -> {
  105. List<PatentApplicantMergeLink> links = linkList.stream().filter(link -> link.getMergeId().equals(item.getId())).collect(Collectors.toList());
  106. item.setApplicantIds(links.stream().map(PatentApplicantMergeLink::getApplicantId).collect(Collectors.toList()));
  107. item.setCountryName(systemDictList.stream().filter(systemDict -> systemDict.getValue().equals(item.getCountry())).findFirst().orElse(new SystemDict()).getLabel());
  108. });
  109. return pageList;
  110. }
  111. public List<PatentApplicant> getApplicantListByNameList(List<String> nameList) {
  112. if (nameList == null || nameList.size() == 0) {
  113. return new ArrayList<>();
  114. }
  115. LambdaQueryWrapper<PatentApplicant> queryWrapper = new LambdaQueryWrapper<>();
  116. queryWrapper.in(PatentApplicant::getName, nameList);
  117. return this.list(queryWrapper);
  118. }
  119. public PatentApplicant getPatentApplicantByName(String name, Integer projectId) {
  120. LambdaQueryWrapper<PatentApplicant> queryWrapper = new LambdaQueryWrapper<>();
  121. queryWrapper.eq(PatentApplicant::getName, name);
  122. queryWrapper.eq(PatentApplicant::getProjectId, projectId);
  123. return this.getOne(queryWrapper);
  124. }
  125. public PatentApplicant add(String name, String shortName) {
  126. PatentApplicant applicant = new PatentApplicant();
  127. applicant.setName(name);
  128. applicant.setShortName(shortName);
  129. applicant.setCreateBy(0);
  130. applicant.setMerge(false);
  131. applicant.setType(PatentUtils.getIntKeyByOrganType(name));
  132. applicant.setUpdateTime(DateUtils.getDateTime());
  133. applicant.insert();
  134. return applicant;
  135. }
  136. private void setMergeData(Integer id, Integer projectId, List<Integer> applicantIds) {
  137. Set<Integer> ids = new HashSet<>();
  138. List<PatentApplicant> tempList = this.getPatentApplicantByIds(applicantIds);
  139. List<PatentApplicant> mergeList = tempList.stream().filter(PatentApplicant::getMerge).collect(Collectors.toList());
  140. List<PatentApplicant> commonList = tempList.stream().filter(item -> !item.getMerge()).collect(Collectors.toList());
  141. List<PatentApplicantMergeLink> tempListMerge = patentApplicantMergeLinkService.getPatentApplicantMergeLinkListByMergeId(id);
  142. ids.addAll(tempListMerge.stream().map(PatentApplicantMergeLink::getApplicantId).collect(Collectors.toList()));
  143. ids.addAll(commonList.stream().map(PatentApplicant::getId).collect(Collectors.toList()));
  144. mergeList.forEach(item -> {
  145. List<PatentApplicantMergeLink> patentApplicantMergeLinkList = patentApplicantMergeLinkService.getPatentApplicantMergeLinkListByMergeId(item.getId());
  146. this.delete(item.getId());
  147. ids.addAll(patentApplicantMergeLinkList.stream().map(PatentApplicantMergeLink::getApplicantId).collect(Collectors.toList()));
  148. });
  149. patentApplicantMergeLinkService.updatePatentApplicantMergeLink(id, projectId, new ArrayList<>(ids));
  150. }
  151. @Transactional
  152. public String add(PatentApplicant patentApplicant) {
  153. PatentApplicant temp = this.getPatentApplicantByName(patentApplicant.getName(), patentApplicant.getProjectId());
  154. if (temp != null) {
  155. return Response.error("名称已存在");
  156. }
  157. patentApplicant.setCreateBy(loginUtils.getId());
  158. patentApplicant.setUpdateTime(DateUtils.getDateTime());
  159. patentApplicant.insert();
  160. return Response.success(true);
  161. }
  162. @Transactional
  163. public String edit(PatentApplicant patentApplicant) {
  164. PatentApplicant temp = this.getPatentApplicantByName(patentApplicant.getName(), patentApplicant.getProjectId());
  165. if (temp != null && !temp.getId().equals(patentApplicant.getId())) {
  166. return Response.error("名称已存在");
  167. }
  168. patentApplicant.setUpdateTime(DateUtils.getDateTime());
  169. patentApplicant.updateById();
  170. if (patentApplicant.getMerge() && patentApplicant.getApplicantIds() != null && patentApplicant.getApplicantIds().size() != 0) {
  171. this.setMergeData(patentApplicant.getId(), patentApplicant.getProjectId(), patentApplicant.getApplicantIds());
  172. }
  173. return Response.success(true);
  174. }
  175. @Transactional
  176. public String delete(Integer id) {
  177. this.removeById(id);
  178. patentApplicantMergeLinkService.deleteByMergeId(id);
  179. return Response.success(true);
  180. }
  181. public String deleteMerge(Integer id, Integer mergeId) {
  182. patentApplicantMergeLinkService.deleteByMergeIdAndApplicantId(id, mergeId);
  183. return Response.success(true);
  184. }
  185. public void importPatentApplicant(Integer projectId, Integer localPatentId, Integer importPatentId, List<PatentApplicant> importPatentApplicantList, List<PatentApplicantLink> importPatentApplicantLinkList) {
  186. patentApplicantLinkService.deleteByPatentId(localPatentId);
  187. List<PatentApplicantLink> importPatentApplicantLinks = importPatentApplicantLinkList.stream().filter(item -> item.getPatentId().equals(importPatentId)).collect(Collectors.toList());
  188. List<Integer> importPatentApplicantIds = importPatentApplicantLinks.stream().map(PatentApplicantLink::getApplicantId).collect(Collectors.toList());
  189. List<PatentApplicant> importPatentApplicants = importPatentApplicantList.stream().filter(item -> importPatentApplicantIds.contains(item.getId())).collect(Collectors.toList());
  190. List<PatentApplicant> localPatentApplicantLists = this.getApplicantListByNameList(importPatentApplicants.stream().map(PatentApplicant::getName).collect(Collectors.toList()));
  191. for (PatentApplicant importPatentApplicant : importPatentApplicants) {
  192. PatentApplicant localPatentApplicant = localPatentApplicantLists.stream().filter(item -> item.getName().equals(importPatentApplicant.getName())).findFirst().orElse(null);
  193. Integer patentApplicantId;
  194. if (localPatentApplicant == null) {
  195. localPatentApplicant = new PatentApplicant();
  196. patentApplicantId = null;
  197. } else {
  198. patentApplicantId = localPatentApplicant.getId();
  199. }
  200. BeanUtils.copyProperties(importPatentApplicant, localPatentApplicant);
  201. localPatentApplicant.setId(patentApplicantId);
  202. localPatentApplicant.insertOrUpdate();
  203. List<PatentApplicantLink> patentApplicantLinkList = importPatentApplicantLinks.stream().filter(item -> item.getApplicantId().equals(importPatentApplicant.getId())).collect(Collectors.toList());
  204. for (PatentApplicantLink patentApplicantLink : patentApplicantLinkList) {
  205. patentApplicantLink.setId(null);
  206. patentApplicantLink.setApplicantId(localPatentApplicant.getId());
  207. patentApplicantLink.setPatentId(localPatentId);
  208. }
  209. patentApplicantLinkService.saveOrUpdateBatch(patentApplicantLinkList);
  210. }
  211. }
  212. /**
  213. * @param name 权利人 通过分割符 | 分割后的List
  214. * @param shortName 【标】权利人 通过分割符 | 分割后的List
  215. */
  216. public List<Integer> updatePatentApplicant(List<String> name, List<String> shortName,List<PatentApplicant> patentApplicantList) {
  217. //生成一个存放ID的List
  218. List<Integer> ids = new ArrayList<>();
  219. //判断当前名称是否为空
  220. if (name != null) {
  221. for (int i = 0; i < name.size(); i++) {
  222. String s = i < shortName.size() ? shortName.get(i) : null;
  223. String nam = name.get(i);
  224. PatentApplicant temp = patentApplicantList.stream().filter(item->item.getMerge().equals(false)&&item.getName().equals(nam)).findFirst().orElse(null);
  225. // LambdaQueryWrapper<PatentApplicant> queryWrapper = new LambdaQueryWrapper<>();
  226. // queryWrapper.eq(PatentApplicant::getName, name.get(i));
  227. // queryWrapper.eq(PatentApplicant::getMerge, 0);
  228. // PatentApplicant temp = this.getOne(queryWrapper);
  229. if (temp == null) {
  230. temp = this.add(name.get(i), s);
  231. } else if (s != null && !s.equals(temp.getShortName())) {
  232. temp.setShortName(s);
  233. temp.updateById();
  234. }
  235. if(!ids.contains(temp.getId()))
  236. { ids.add(temp.getId());}
  237. }
  238. } else {
  239. for (String s : shortName) {
  240. List<PatentApplicant> temp = patentApplicantList.stream().filter(item->item.getMerge().equals(0)&&item.getName().equals(s)).collect(Collectors.toList());
  241. // LambdaQueryWrapper<PatentApplicant> queryWrapper = new LambdaQueryWrapper<>();
  242. // queryWrapper.eq(PatentApplicant::getShortName, s);
  243. // queryWrapper.eq(PatentApplicant::getMerge, 0);
  244. // List<PatentApplicant> temp = this.list(queryWrapper);
  245. PatentApplicant patentApplicant = new PatentApplicant();
  246. if (temp.size() == 0) {
  247. patentApplicant = this.add(s, s);
  248. } else {
  249. if (temp.size() == 1) {
  250. patentApplicant = temp.get(0);
  251. } else {
  252. for (PatentApplicant pa : temp) {
  253. if (pa.getShortName().equals(pa.getName())) {
  254. patentApplicant = pa;
  255. break;
  256. }
  257. }
  258. }
  259. }
  260. if(!ids.contains(patentApplicant.getId())){ids.add(patentApplicant.getId());}
  261. }
  262. }
  263. return ids;
  264. }
  265. public void updatePatentApplicantAddress(PatentApplicantAddressParams params) {
  266. //用专利ID获取数据 申请人ID 申请人类型 申请人顺序
  267. List<PatentApplicantLink> patentApplicantLinkList = patentApplicantLinkService.getPatentApplicantAttributesByPatentId(params.getPatentId());
  268. //获取属于权利人的ID List 并且是第一位
  269. List<Integer> currentIds = patentApplicantLinkList.stream().filter(item -> item.getType().equals(1) && !item.getOrder().equals(0)).map(PatentApplicantLink::getApplicantId).collect(Collectors.toList());
  270. Integer firstCurrentId = patentApplicantLinkList.stream().filter(item -> item.getType().equals(1) && item.getOrder().equals(0)).map(PatentApplicantLink::getApplicantId).findFirst().orElse(null);
  271. List<Integer> originalIds = patentApplicantLinkList.stream().filter(item -> item.getType().equals(2) && item.getOrder().equals(0)).map(PatentApplicantLink::getApplicantId).collect(Collectors.toList());
  272. List<PatentApplicant> currentList = this.getPatentApplicantByIds(currentIds);
  273. PatentApplicant first = this.getById(firstCurrentId);
  274. List<PatentApplicant> originalList = this.getPatentApplicantByIds(originalIds);
  275. if (params.getCurrentAddress() != null) {
  276. for (int i = 0; i < params.getCurrentAddress().size(); i++) {
  277. int finalI = i;
  278. currentList.forEach(item -> {
  279. AreaAddressDTO dto = areaService.getAreaAddressDTO(params.getCurrentAddress().get(finalI));
  280. item.setAddressStr(params.getCurrentAddress().get(finalI));
  281. item.setProvinceId(dto.getProvinceId());
  282. item.setCityId(dto.getCityId());
  283. item.setAreaId(dto.getAreaId());
  284. item.updateById();
  285. });
  286. }
  287. }
  288. if (params.getCurrentCountry() != null) {
  289. for (int i = 0; i < params.getCurrentCountry().size(); i++) {
  290. int finalI = i;
  291. currentList.forEach(item -> {
  292. item.setCountry(params.getCurrentCountry().get(finalI));
  293. item.updateById();
  294. });
  295. }
  296. }
  297. if (params.getOriginalAddress() != null) {
  298. for (int i = 0; i < params.getOriginalAddress().size(); i++) {
  299. int finalI = i;
  300. originalList.forEach(item -> {
  301. AreaAddressDTO dto = areaService.getAreaAddressDTO(params.getOriginalAddress().get(finalI));
  302. item.setAddressStr(params.getOriginalAddress().get(finalI));
  303. item.setProvinceId(dto.getProvinceId());
  304. item.setCityId(dto.getCityId());
  305. item.setAreaId(dto.getAreaId());
  306. item.updateById();
  307. });
  308. }
  309. }
  310. if (params.getOriginalCountry() != null) {
  311. for (int i = 0; i < params.getOriginalCountry().size(); i++) {
  312. int finalI = i;
  313. originalList.forEach(item -> {
  314. item.setCountry(params.getOriginalCountry().get(finalI));
  315. item.updateById();
  316. });
  317. }
  318. }
  319. if (StringUtils.isNotEmpty(params.getFirstCurrentAddress()) && first != null) {
  320. AreaAddressDTO dto = areaService.getAreaAddressDTO(params.getFirstCurrentAddress());
  321. first.setAddressStr(params.getFirstCurrentAddress());
  322. first.setProvinceId(dto.getProvinceId());
  323. first.setCityId(dto.getCityId());
  324. first.setAreaId(dto.getAreaId());
  325. first.updateById();
  326. }
  327. }
  328. }