PtlInsAttributionTemplateServiceImpl.java 9.5 KB

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