PtlInsAttributionTemplateServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package com.ydtech.modules.protocol.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.ydtech.core.page.HttpResult;
  6. import com.ydtech.exception.SystemException;
  7. import com.ydtech.modules.esm.model.EsmInsCompany;
  8. import com.ydtech.modules.esm.service.EsmInsCompanyService;
  9. import com.ydtech.modules.order.entity.BasePageResult;
  10. import com.ydtech.modules.protocol.dao.PtlInsAttributionTemplateMapper;
  11. import com.ydtech.modules.protocol.entity.constants.PtlApiType;
  12. import com.ydtech.modules.protocol.entity.dto.PtlInsAttributionTemplateQueryDto;
  13. import com.ydtech.modules.protocol.entity.po.PtlAgreementAttribution;
  14. import com.ydtech.modules.protocol.entity.po.PtlCrawlerApi;
  15. import com.ydtech.modules.protocol.entity.po.PtlInsAttributionTemplate;
  16. import com.ydtech.modules.protocol.entity.vo.PtlCrawlerApiSaveVo;
  17. import com.ydtech.modules.protocol.entity.vo.PtlInsAttributionTemplateQueryVo;
  18. import com.ydtech.modules.protocol.entity.vo.PtlInsAttributionTemplateSaveVo;
  19. import com.ydtech.modules.protocol.entity.vo.PtlInsAttributionTemplateVo;
  20. import com.ydtech.modules.protocol.service.PtlAgreementAttributionService;
  21. import com.ydtech.modules.protocol.service.PtlCrawlerApiService;
  22. import com.ydtech.modules.protocol.service.PtlInsAttributionTemplateService;
  23. import com.ydtech.modules.protocol.utils.AssertionUtils;
  24. import com.ydtech.utils.StringUtils;
  25. import org.springframework.beans.BeanUtils;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.context.annotation.Lazy;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import org.springframework.util.ObjectUtils;
  31. import java.util.List;
  32. import java.util.Objects;
  33. /**
  34. * @author wenks
  35. * @description 针对表【ptl_ins_attribution_template(保险公司归属信息模板配置)】的数据库操作Service实现
  36. * @createDate 2023-07-03 09:31:16
  37. */
  38. @Service
  39. public class PtlInsAttributionTemplateServiceImpl extends ServiceImpl<PtlInsAttributionTemplateMapper, PtlInsAttributionTemplate>
  40. implements PtlInsAttributionTemplateService {
  41. private final EsmInsCompanyService esmInsCompanyService;
  42. private final PtlCrawlerApiService ptlCrawlerApiService;
  43. private final PtlInsAttributionTemplateService proxyService;
  44. private final PtlAgreementAttributionService ptlAgreementAttributionService;
  45. public PtlInsAttributionTemplateServiceImpl(EsmInsCompanyService esmInsCompanyService, PtlCrawlerApiService ptlCrawlerApiService,
  46. @Lazy PtlInsAttributionTemplateService proxyService,PtlAgreementAttributionService ptlAgreementAttributionService) {
  47. this.esmInsCompanyService = esmInsCompanyService;
  48. this.ptlCrawlerApiService = ptlCrawlerApiService;
  49. this.proxyService = proxyService;
  50. this.ptlAgreementAttributionService = ptlAgreementAttributionService;
  51. }
  52. @Override
  53. public PtlInsAttributionTemplate getByApiTypeAndInsCompanyId(PtlInsAttributionTemplateSaveVo ptlInsAttributionTemplateSaveVo) {
  54. // 判断保险公司是否存在
  55. EsmInsCompany esmInsCompany = esmInsCompanyService.isExists(ptlInsAttributionTemplateSaveVo.getInsCompanyId());
  56. // 查询一个保险公司,一种api方式是否只有一种配置
  57. LambdaQueryWrapper<PtlInsAttributionTemplate> wrapper = byInsCompanyIdAndApiType(ptlInsAttributionTemplateSaveVo.getInsCompanyId(), ptlInsAttributionTemplateSaveVo.getApiType());
  58. List<PtlInsAttributionTemplate> list = list(wrapper);
  59. if (!list.isEmpty()) {
  60. throw new SystemException("一个保险公司,一种api方式只能配置一种");
  61. }
  62. return new PtlInsAttributionTemplate(ptlInsAttributionTemplateSaveVo, esmInsCompany.getNamesimple());
  63. }
  64. @Override
  65. @Transactional
  66. public HttpResult<Object> submit(PtlInsAttributionTemplateSaveVo ptlInsAttributionTemplateSaveVo) {
  67. // 一个保险公司,一种api方式只能配置一种
  68. PtlInsAttributionTemplate ptlInsAttributionTemplate = proxyService.getByApiTypeAndInsCompanyId(ptlInsAttributionTemplateSaveVo);
  69. boolean save = save(ptlInsAttributionTemplate);
  70. AssertionUtils.isSucceed(save, "保险公司归属信息模板添加失败");
  71. // 保存请求链接
  72. if (PtlApiType.PTL_API_2.getCode() == ptlInsAttributionTemplateSaveVo.getApiType()) {
  73. List<PtlCrawlerApi> ptlCrawlerApiList = PtlCrawlerApi.toPtlCrawlerApiList(ptlInsAttributionTemplate.getId(), ptlInsAttributionTemplateSaveVo.getApi());
  74. boolean b = ptlCrawlerApiService.saveBatch(ptlCrawlerApiList);
  75. AssertionUtils.isSucceed(b, "保险公司api请求插入失败");
  76. }
  77. return HttpResult.ok("添加成功");
  78. }
  79. @Override
  80. public HttpResult<PtlInsAttributionTemplateQueryDto> getByIdObtain(String id) {
  81. PtlInsAttributionTemplate ptlInsAttributionTemplate = getById(id);
  82. PtlInsAttributionTemplateQueryDto ptlInsAttributionTemplateQueryDto = new PtlInsAttributionTemplateQueryDto();
  83. BeanUtils.copyProperties(ptlInsAttributionTemplate, ptlInsAttributionTemplateQueryDto);
  84. if (PtlApiType.PTL_API_2.getCode() == ptlInsAttributionTemplate.getApiType()) {
  85. List<PtlCrawlerApi> ptlCrawlerApiList = ptlCrawlerApiService.getByTemplateIdList(id);
  86. ptlInsAttributionTemplateQueryDto.setApi(ptlCrawlerApiList);
  87. }
  88. return HttpResult.ok(ptlInsAttributionTemplateQueryDto);
  89. }
  90. @Override
  91. public HttpResult<BasePageResult<PtlInsAttributionTemplate>> getAll(PtlInsAttributionTemplateQueryVo queryVo) {
  92. Page<PtlInsAttributionTemplate> pageQuery = new Page<>(queryVo.getPageNum(), queryVo.getPageSize());
  93. LambdaQueryWrapper<PtlInsAttributionTemplate> queryWrapper = new LambdaQueryWrapper<PtlInsAttributionTemplate>()
  94. .eq(!StringUtils.isNullOrEmpty(queryVo.getInsCompanyId()), PtlInsAttributionTemplate::getInsCompanyId, queryVo.getInsCompanyId())
  95. .eq(!StringUtils.isNullOrEmpty(queryVo.getApiType()), PtlInsAttributionTemplate::getApiType, queryVo.getApiType());
  96. Page<PtlInsAttributionTemplate> page = page(pageQuery, queryWrapper);
  97. BasePageResult<PtlInsAttributionTemplate> pageResult = new BasePageResult<>(queryVo.getPageNum(), page.getSize(), page.getTotal(), page.getPages(), page.getRecords());
  98. return HttpResult.ok("succeed", pageResult);
  99. }
  100. @Override
  101. public HttpResult<Object> deleteById(String id) {
  102. boolean b = removeById(id);
  103. LambdaQueryWrapper<PtlCrawlerApi> wrapper = new LambdaQueryWrapper<>();
  104. wrapper.eq(PtlCrawlerApi::getTemplateId, id);
  105. ptlCrawlerApiService.remove(wrapper);
  106. AssertionUtils.isSucceed(b, "保险公司归属信息模板删除失败");
  107. return HttpResult.ok("删除成功");
  108. }
  109. @Override
  110. public HttpResult<Object> revise(String id, PtlInsAttributionTemplateSaveVo ptlInsAttributionTemplateSaveVo) {
  111. // 判断保险公司是否存在
  112. EsmInsCompany esmInsCompany = esmInsCompanyService.isExists(ptlInsAttributionTemplateSaveVo.getInsCompanyId());
  113. PtlInsAttributionTemplate ptlInsAttributionTemplate = new PtlInsAttributionTemplate(ptlInsAttributionTemplateSaveVo, esmInsCompany.getNamesimple());
  114. ptlInsAttributionTemplate.setId(id);
  115. LambdaQueryWrapper<PtlInsAttributionTemplate> wrapper = byInsCompanyIdAndApiType(ptlInsAttributionTemplateSaveVo.getInsCompanyId(), ptlInsAttributionTemplateSaveVo.getApiType());
  116. PtlInsAttributionTemplate ptlInsAttributionTemplateOne = getOne(wrapper);
  117. if (!ObjectUtils.isEmpty(ptlInsAttributionTemplateOne) && !Objects.equals(ptlInsAttributionTemplateOne.getId(), id)) {
  118. throw new SystemException("一个保险公司,一种api方式只能配置一种");
  119. }
  120. if (PtlApiType.PTL_API_2.getCode() == ptlInsAttributionTemplateSaveVo.getApiType()) {
  121. LambdaQueryWrapper<PtlCrawlerApi> crawlerApiWrapper = new LambdaQueryWrapper<>();
  122. crawlerApiWrapper.eq(PtlCrawlerApi::getTemplateId, ptlInsAttributionTemplate.getId());
  123. ptlCrawlerApiService.remove(crawlerApiWrapper);
  124. // 保存请求链接
  125. List<PtlCrawlerApi> ptlCrawlerApiList = PtlCrawlerApi.toPtlCrawlerApiList(ptlInsAttributionTemplate.getId(), ptlInsAttributionTemplateSaveVo.getApi());
  126. boolean b = ptlCrawlerApiService.saveBatch(ptlCrawlerApiList);
  127. AssertionUtils.isSucceed(b, "保险公司api请求插入失败");
  128. }
  129. boolean b = updateById(ptlInsAttributionTemplate);
  130. AssertionUtils.isSucceed(b, "保险公司归属信息模板修改失败");
  131. return HttpResult.ok("修改成功");
  132. }
  133. @Override
  134. public HttpResult<PtlAgreementAttribution> getPtlInsAttributionTemplate(PtlInsAttributionTemplateVo ptlInsAttributionTemplateVo) {
  135. String code = "000000";
  136. String substring = ptlInsAttributionTemplateVo.getInsCompanyId().substring(0, 5);
  137. String companyId = substring + code;
  138. LambdaQueryWrapper<PtlAgreementAttribution> wrapper = byInsCompanyIdAndApiTypeAndAgreementId(ptlInsAttributionTemplateVo.getAgreementId(),companyId, ptlInsAttributionTemplateVo.getApiType());
  139. PtlAgreementAttribution ptlAgreementAttribution = ptlAgreementAttributionService.getOne(wrapper);
  140. if(ObjectUtils.isEmpty(ptlAgreementAttribution)) {
  141. PtlInsAttributionTemplate ptlInsAttributionTemplate = lambdaQuery().eq(PtlInsAttributionTemplate::getInsCompanyId, companyId)
  142. .eq(PtlInsAttributionTemplate::getApiType, ptlInsAttributionTemplateVo.getApiType())
  143. .one();
  144. AssertionUtils.isEmpty(ptlInsAttributionTemplate, "保险公司归属信息模板查询失败");
  145. ptlAgreementAttribution = new PtlAgreementAttribution( ptlInsAttributionTemplate);
  146. }
  147. return HttpResult.ok(ptlAgreementAttribution);
  148. }
  149. private LambdaQueryWrapper<PtlInsAttributionTemplate> byInsCompanyIdAndApiType(String insCompanyId, Integer apiType) {
  150. LambdaQueryWrapper<PtlInsAttributionTemplate> wrapper = new LambdaQueryWrapper<>();
  151. wrapper.eq(PtlInsAttributionTemplate::getInsCompanyId, insCompanyId)
  152. .eq(PtlInsAttributionTemplate::getApiType, apiType);
  153. return wrapper;
  154. }
  155. private LambdaQueryWrapper<PtlAgreementAttribution> byInsCompanyIdAndApiTypeAndAgreementId(String agreementId, String insCompanyId, Integer apiType) {
  156. LambdaQueryWrapper<PtlAgreementAttribution> wrapper = new LambdaQueryWrapper<>();
  157. wrapper.eq(PtlAgreementAttribution::getId, agreementId)
  158. .eq(PtlAgreementAttribution::getInsCompanyId, insCompanyId)
  159. .eq(PtlAgreementAttribution::getApiType, apiType);
  160. return wrapper;
  161. }
  162. }