package com.ydtech.modules.protocol.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ydtech.core.page.HttpResult; import com.ydtech.exception.SystemException; import com.ydtech.modules.esm.model.EsmInsCompany; import com.ydtech.modules.esm.service.EsmInsCompanyService; import com.ydtech.modules.order.entity.BasePageResult; import com.ydtech.modules.protocol.dao.PtlInsAttributionTemplateMapper; import com.ydtech.modules.protocol.entity.constants.PtlApiType; import com.ydtech.modules.protocol.entity.dto.PtlInsAttributionTemplateQueryDto; import com.ydtech.modules.protocol.entity.po.PtlCrawlerApi; import com.ydtech.modules.protocol.entity.po.PtlInsAttributionTemplate; import com.ydtech.modules.protocol.entity.vo.PtlCrawlerApiSaveVo; import com.ydtech.modules.protocol.entity.vo.PtlInsAttributionTemplateQueryVo; import com.ydtech.modules.protocol.entity.vo.PtlInsAttributionTemplateSaveVo; import com.ydtech.modules.protocol.entity.vo.PtlInsAttributionTemplateVo; import com.ydtech.modules.protocol.service.PtlCrawlerApiService; import com.ydtech.modules.protocol.service.PtlInsAttributionTemplateService; import com.ydtech.modules.protocol.utils.AssertionUtils; import com.ydtech.utils.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.ObjectUtils; import java.util.List; import java.util.Objects; /** * @author wenks * @description 针对表【ptl_ins_attribution_template(保险公司归属信息模板配置)】的数据库操作Service实现 * @createDate 2023-07-03 09:31:16 */ @Service public class PtlInsAttributionTemplateServiceImpl extends ServiceImpl implements PtlInsAttributionTemplateService { private final EsmInsCompanyService esmInsCompanyService; private final PtlCrawlerApiService ptlCrawlerApiService; private final PtlInsAttributionTemplateService proxyService; public PtlInsAttributionTemplateServiceImpl(EsmInsCompanyService esmInsCompanyService, PtlCrawlerApiService ptlCrawlerApiService, @Lazy PtlInsAttributionTemplateService proxyService) { this.esmInsCompanyService = esmInsCompanyService; this.ptlCrawlerApiService = ptlCrawlerApiService; this.proxyService = proxyService; } @Override public PtlInsAttributionTemplate getByApiTypeAndInsCompanyId(PtlInsAttributionTemplateSaveVo ptlInsAttributionTemplateSaveVo) { // 判断保险公司是否存在 EsmInsCompany esmInsCompany = esmInsCompanyService.isExists(ptlInsAttributionTemplateSaveVo.getInsCompanyId()); // 查询一个保险公司,一种api方式是否只有一种配置 LambdaQueryWrapper wrapper = byInsCompanyIdAndApiType(ptlInsAttributionTemplateSaveVo.getInsCompanyId(), ptlInsAttributionTemplateSaveVo.getApiType()); List list = list(wrapper); if (!list.isEmpty()) { throw new SystemException("一个保险公司,一种api方式只能配置一种"); } return new PtlInsAttributionTemplate(ptlInsAttributionTemplateSaveVo, esmInsCompany.getNamesimple()); } @Override @Transactional public HttpResult submit(PtlInsAttributionTemplateSaveVo ptlInsAttributionTemplateSaveVo) { // 一个保险公司,一种api方式只能配置一种 PtlInsAttributionTemplate ptlInsAttributionTemplate = proxyService.getByApiTypeAndInsCompanyId(ptlInsAttributionTemplateSaveVo); boolean save = save(ptlInsAttributionTemplate); AssertionUtils.isSucceed(save, "保险公司归属信息模板添加失败"); // 保存请求链接 if (PtlApiType.PTL_API_2.getCode() == ptlInsAttributionTemplateSaveVo.getApiType()) { List ptlCrawlerApiList = PtlCrawlerApi.toPtlCrawlerApiList(ptlInsAttributionTemplate.getId(), ptlInsAttributionTemplateSaveVo.getApi()); boolean b = ptlCrawlerApiService.saveBatch(ptlCrawlerApiList); AssertionUtils.isSucceed(b, "保险公司api请求插入失败"); } return HttpResult.ok("添加成功"); } @Override public HttpResult getByIdObtain(String id) { PtlInsAttributionTemplate ptlInsAttributionTemplate = getById(id); PtlInsAttributionTemplateQueryDto ptlInsAttributionTemplateQueryDto = new PtlInsAttributionTemplateQueryDto(); BeanUtils.copyProperties(ptlInsAttributionTemplate, ptlInsAttributionTemplateQueryDto); if (PtlApiType.PTL_API_2.getCode() == ptlInsAttributionTemplate.getApiType()) { List ptlCrawlerApiList = ptlCrawlerApiService.getByTemplateIdList(id); ptlInsAttributionTemplateQueryDto.setApi(ptlCrawlerApiList); } return HttpResult.ok(ptlInsAttributionTemplateQueryDto); } @Override public HttpResult> getAll(PtlInsAttributionTemplateQueryVo queryVo) { Page pageQuery = new Page<>(queryVo.getPageNum(), queryVo.getPageSize()); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper() .eq(!StringUtils.isNullOrEmpty(queryVo.getInsCompanyId()), PtlInsAttributionTemplate::getInsCompanyId, queryVo.getInsCompanyId()) .eq(!StringUtils.isNullOrEmpty(queryVo.getApiType()), PtlInsAttributionTemplate::getApiType, queryVo.getApiType()); Page page = page(pageQuery, queryWrapper); BasePageResult pageResult = new BasePageResult<>(queryVo.getPageNum(), page.getSize(), page.getTotal(), page.getPages(), page.getRecords()); return HttpResult.ok("succeed", pageResult); } @Override public HttpResult deleteById(String id) { boolean b = removeById(id); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(PtlCrawlerApi::getTemplateId, id); ptlCrawlerApiService.remove(wrapper); AssertionUtils.isSucceed(b, "保险公司归属信息模板删除失败"); return HttpResult.ok("删除成功"); } @Override public HttpResult revise(String id, PtlInsAttributionTemplateSaveVo ptlInsAttributionTemplateSaveVo) { // 判断保险公司是否存在 EsmInsCompany esmInsCompany = esmInsCompanyService.isExists(ptlInsAttributionTemplateSaveVo.getInsCompanyId()); PtlInsAttributionTemplate ptlInsAttributionTemplate = new PtlInsAttributionTemplate(ptlInsAttributionTemplateSaveVo, esmInsCompany.getNamesimple()); ptlInsAttributionTemplate.setId(id); LambdaQueryWrapper wrapper = byInsCompanyIdAndApiType(ptlInsAttributionTemplateSaveVo.getInsCompanyId(), ptlInsAttributionTemplateSaveVo.getApiType()); PtlInsAttributionTemplate ptlInsAttributionTemplateOne = getOne(wrapper); if (!ObjectUtils.isEmpty(ptlInsAttributionTemplateOne) && !Objects.equals(ptlInsAttributionTemplateOne.getId(), id)) { throw new SystemException("一个保险公司,一种api方式只能配置一种"); } if (PtlApiType.PTL_API_2.getCode() == ptlInsAttributionTemplateSaveVo.getApiType()) { LambdaQueryWrapper crawlerApiWrapper = new LambdaQueryWrapper<>(); crawlerApiWrapper.eq(PtlCrawlerApi::getTemplateId, ptlInsAttributionTemplate.getId()); ptlCrawlerApiService.remove(crawlerApiWrapper); // 保存请求链接 List ptlCrawlerApiList = PtlCrawlerApi.toPtlCrawlerApiList(ptlInsAttributionTemplate.getId(), ptlInsAttributionTemplateSaveVo.getApi()); boolean b = ptlCrawlerApiService.saveBatch(ptlCrawlerApiList); AssertionUtils.isSucceed(b, "保险公司api请求插入失败"); } boolean b = updateById(ptlInsAttributionTemplate); AssertionUtils.isSucceed(b, "保险公司归属信息模板修改失败"); return HttpResult.ok("修改成功"); } @Override public HttpResult getPtlInsAttributionTemplate(PtlInsAttributionTemplateVo ptlInsAttributionTemplateVo) { String code = "000000"; String substring = ptlInsAttributionTemplateVo.getInsCompanyId().substring(0, 5); String companyId = substring + code; LambdaQueryWrapper wrapper = byInsCompanyIdAndApiType(companyId, ptlInsAttributionTemplateVo.getApiType()); PtlInsAttributionTemplate ptlInsAttributionTemplate = getOne(wrapper); AssertionUtils.isEmpty(ptlInsAttributionTemplate, "保险公司归属信息模板查询失败"); return HttpResult.ok(ptlInsAttributionTemplate); } private LambdaQueryWrapper byInsCompanyIdAndApiType(String insCompanyId, Integer apiType) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(PtlInsAttributionTemplate::getInsCompanyId, insCompanyId) .eq(PtlInsAttributionTemplate::getApiType, apiType); return wrapper; } }