package com.ydtech.modules.protocol.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ydtech.constants.sys.SystemConstant; import com.ydtech.modules.admin.constants.RoleConstants; import com.ydtech.modules.admin.model.SysUserRole; import com.ydtech.modules.admin.service.SysUserRoleService; import com.ydtech.modules.esm.model.EsmInsCompany; import com.ydtech.modules.esm.service.EsmInsCompanyService; import com.ydtech.modules.order.service.InsOrdersService; import com.ydtech.modules.protocol.constants.TaxSourceStatusConstants; import com.ydtech.modules.protocol.dao.TaxSourceMapper; import com.ydtech.modules.protocol.entity.dto.PtlAgreementDto; import com.ydtech.modules.protocol.entity.dto.PtlAgreementInsCompanyDto; import com.ydtech.modules.protocol.entity.dto.TaxSourceDto; import com.ydtech.modules.protocol.entity.dto.TaxSourceExcludeDto; import com.ydtech.modules.protocol.entity.po.PtlAgreement; import com.ydtech.modules.protocol.entity.vo.TaxSourceVo; import com.ydtech.modules.protocol.service.ITaxSourceExcludeService; import com.ydtech.modules.protocol.service.ITaxSourceService; import com.ydtech.modules.protocol.service.PtlAgreementService; import com.ydtech.utils.idgen.IdGenerate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @Service public class TaxSourceServiceImpl extends ServiceImpl implements ITaxSourceService { @Autowired PtlAgreementService ptlAgreementService; @Autowired ITaxSourceExcludeService taxSourceExcludeService; @Autowired InsOrdersService insOrdersService; @Autowired SysUserRoleService sysUserRoleService; @Autowired EsmInsCompanyService esmInsCompanyService; @Override public IPage getInsureCompany(Page page) { return baseMapper.getInsureCompany(page); } @Override public List getAgreementByCompanyId(String companyId, String deptId, String userId) { List agreementInsCompany = this.getAgreementInsCompany(deptId, userId); List agreementIds = new ArrayList<>(); for (PtlAgreementInsCompanyDto item : agreementInsCompany) { if (item.getId().equals(companyId)) { for (PtlAgreementDto agreementDto : item.getAgreement()) { agreementIds.add(agreementDto.getId()); } } } QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.in("id", agreementIds); List list = ptlAgreementService.list(queryWrapper); return list; } @Override @Transactional public boolean saveTaxSource(TaxSourceVo taxSourceVo) { //删除排除的信息 taxSourceExcludeService.remove(new LambdaQueryWrapper().eq(TaxSourceExcludeDto::getInsCompanyId, taxSourceVo.getInsCompanyId())); //删除税源配置信息 baseMapper.delete(new LambdaQueryWrapper().eq(TaxSourceDto::getInsCompanyId, taxSourceVo.getInsCompanyId())); //组装税源数据 List dtos = new ArrayList<>(); List excludeDtos = new ArrayList<>(); taxSourceVo.getAgreementTaxList().forEach(agreementTax -> { TaxSourceDto dto = new TaxSourceDto(); dto.setId(IdGenerate.nextId()); dto.setInsCompanyId(taxSourceVo.getInsCompanyId()); dto.setInsCompanyName(taxSourceVo.getInsCompanyName()); dto.setAgreementId(agreementTax.getAgreementId()); dto.setIsTaxSource(agreementTax.getIsTaxSource()); dto.setLicense(agreementTax.getLicense()); dto.setInsuranceRepertoire(agreementTax.getInsuranceRepertoire()); dtos.add(dto); }); //组装排除的部门信息 for (String agreementTaxExclude : taxSourceVo.getAgreementTaxExcludeDeptList()) { TaxSourceExcludeDto dto = new TaxSourceExcludeDto(); dto.setId(IdGenerate.nextId()); dto.setType(Integer.parseInt(TaxSourceStatusConstants.EXCLUDE_TYPE_DEPT)); dto.setInsCompanyId(taxSourceVo.getInsCompanyId()); dto.setExcludeInfo(agreementTaxExclude); excludeDtos.add(dto); } // taxSourceVo.getAgreementTaxExcludeDeptList().forEach(agreementTaxExclude -> { // TaxSourceExcludeDto dto = new TaxSourceExcludeDto(); // dto.setId(IdGenerate.nextId()); // dto.setType(Integer.parseInt(TaxSourceStatusConstants.EXCLUDE_TYPE_DEPT)); // dto.setInsCompanyId(taxSourceVo.getInsCompanyId()); // dto.setExcludeInfo(agreementTaxExclude.getExcludeInfo()); // excludeDtos.add(dto); // }); //组装排除的车牌号信息 for (String agreementTaxExclude : taxSourceVo.getAgreementTaxExcludeLicenseList()) { TaxSourceExcludeDto dto = new TaxSourceExcludeDto(); dto.setId(IdGenerate.nextId()); dto.setType(Integer.parseInt(TaxSourceStatusConstants.EXCLUDE_TYPE_LICENSE)); dto.setInsCompanyId(taxSourceVo.getInsCompanyId()); dto.setExcludeInfo(agreementTaxExclude); excludeDtos.add(dto); } // taxSourceVo.getAgreementTaxExcludeLicenseList().forEach(agreementTaxExclude -> { // TaxSourceExcludeDto dto = new TaxSourceExcludeDto(); // dto.setId(IdGenerate.nextId()); // dto.setType(Integer.parseInt(TaxSourceStatusConstants.EXCLUDE_TYPE_LICENSE)); // dto.setInsCompanyId(taxSourceVo.getInsCompanyId()); // dto.setExcludeInfo(agreementTaxExclude.getExcludeInfo()); // excludeDtos.add(dto); // }); this.saveBatch(dtos); taxSourceExcludeService.saveBatch(excludeDtos); return true; } @Override public TaxSourceVo getTaxSource(String companyId) { TaxSourceVo taxSourceVo = new TaxSourceVo(); //税源信息 List taxSourceDto = baseMapper.selectList(new LambdaQueryWrapper().eq(TaxSourceDto::getInsCompanyId, companyId)); //排除的部门信息 List taxSourceExcludeDepts = taxSourceExcludeService.list(new LambdaQueryWrapper().eq(TaxSourceExcludeDto::getInsCompanyId, companyId).eq(TaxSourceExcludeDto::getType, TaxSourceStatusConstants.EXCLUDE_TYPE_DEPT)); //排除的车牌号信息 List taxSourceExcludeLicense = taxSourceExcludeService.list(new LambdaQueryWrapper().eq(TaxSourceExcludeDto::getInsCompanyId, companyId).eq(TaxSourceExcludeDto::getType, TaxSourceStatusConstants.EXCLUDE_TYPE_LICENSE)); taxSourceVo.setInsCompanyId(companyId); //税源信息转换 List agreementTaxList = new ArrayList<>(); taxSourceDto.forEach(item -> { TaxSourceVo.AgreementTax agreementTax = new TaxSourceVo.AgreementTax(); agreementTax.setAgreementId(item.getAgreementId()); agreementTax.setIsTaxSource(item.getIsTaxSource()); agreementTax.setLicense(item.getLicense()); agreementTax.setInsuranceRepertoire(item.getInsuranceRepertoire()); agreementTaxList.add(agreementTax); }); taxSourceVo.setAgreementTaxList(agreementTaxList); //部门信息转换 List agreementTaxExcludeDepts = new ArrayList<>(); taxSourceExcludeDepts.forEach(item -> { agreementTaxExcludeDepts.add(item.getExcludeInfo()); }); taxSourceVo.setAgreementTaxExcludeDeptList(agreementTaxExcludeDepts.toArray(new String[agreementTaxExcludeDepts.size()])); //车牌信息转换 List agreementTaxExcludeLicense = new ArrayList<>(); taxSourceExcludeLicense.forEach(item -> { agreementTaxExcludeLicense.add(item.getExcludeInfo()); }); taxSourceVo.setAgreementTaxExcludeLicenseList(agreementTaxExcludeLicense.toArray(new String[agreementTaxExcludeLicense.size()])); return taxSourceVo; } public List getAgreementInsCompany(String deptId, String userId) { // 领导标志 SysUserRole userRole = sysUserRoleService.selectByUserId(userId); if (Arrays.asList(SystemConstant.getSysRoleTeamLeader()).contains(userRole.getRoleId().toString()) && !RoleConstants.R21.getCode().equals(userRole.getRoleId().toString())) { //团队长可查询下面所有人员订单 deptId = null; } List agreementCompany = ptlAgreementService.getAgreementCompany(deptId); return agreementCompany; } public PtlAgreementInsCompanyDto getAgreementInsCompany(String deptId, String userId, String companyId) { // 领导标志 SysUserRole userRole = sysUserRoleService.selectByUserId(userId); if (Arrays.asList(SystemConstant.getSysRoleTeamLeader()).contains(userRole.getRoleId().toString()) && !RoleConstants.R21.getCode().equals(userRole.getRoleId().toString())) { //团队长可查询下面所有人员订单 deptId = null; } PtlAgreementInsCompanyDto agreementCompany = ptlAgreementService.getAgreementCompany(deptId, companyId); return agreementCompany; } // @Override // public List getAgreementInsCompany(String deptId, String userId, String license, // String volume) { // //查出所有协议 // List agreementInsCompany = this.getAgreementInsCompany(deptId, userId); // if(license != null && volume != null) { // //排除的部门信息 // List excludeDepts = taxSourceExcludeService.list(new // LambdaQueryWrapper().eq // (TaxSourceExcludeDto::getExcludeInfo, deptId) // .eq(TaxSourceExcludeDto::getType, TaxSourceStatusConstants.EXCLUDE_TYPE_DEPT)); // // agreementInsCompany.forEach(item -> // excludeDepts.stream() // .filter(taxSourceDto -> item.getId().equals(taxSourceDto.getInsCompanyId()) && // taxSourceDto.getExcludeInfo // ().equals(deptId)) // .findFirst() // .ifPresent(taxSourceDto -> item.setIsExclude(TaxSourceStatusConstants.TAX_SOURCE_START)) // ); // //查找排除的车牌号 // List licenseExclude = taxSourceExcludeService.list(new // LambdaQueryWrapper().eq // (TaxSourceExcludeDto::getExcludeInfo, license) // .eq(TaxSourceExcludeDto::getType, TaxSourceStatusConstants.EXCLUDE_TYPE_LICENSE)); // agreementInsCompany.forEach(item -> { // licenseExclude.forEach(taxSourceDto -> { // if (item.getId().equals(taxSourceDto.getInsCompanyId())) { // if (license.equals(taxSourceDto.getExcludeInfo())) { // //排除 // item.setIsExclude(TaxSourceStatusConstants.TAX_SOURCE_START); // } // } // }); // }); // // //查找配置的默认项 // List taxSourceDefaultDtos = baseMapper.selectList(new LambdaQueryWrapper() // .eq(TaxSourceDto::getLicense, license)); // //设置协议的默认项 // agreementInsCompany.forEach(item -> { // taxSourceDefaultDtos.forEach(taxSourceDto -> { // if (item.getId().equals(taxSourceDto.getInsCompanyId())) { // item.getAgreement().forEach(agreement -> { // if (agreement.getId().equals(taxSourceDto.getAgreementId())) { // agreement.setIsAllocation(TaxSourceStatusConstants.TAX_SOURCE_START); // } // }); // } // }); // }); // //判断汽车排量是否大于1升 // if (Float.parseFloat(volume) >= 1) { // //查找是否配置税源 // List taxSourceDtos = baseMapper.selectList(new LambdaQueryWrapper().eq // (TaxSourceDto::getIsTaxSource, "1")); // //设置税源参数 // agreementInsCompany.forEach(item -> { // if (!TaxSourceStatusConstants.TAX_SOURCE_START.equals(item.getIsExclude())) { // taxSourceDtos.forEach(taxSourceDto -> { // if (item.getId().equals(taxSourceDto.getInsCompanyId())) { // item.getAgreement().forEach(agreement -> { // if (agreement.getId().equals(taxSourceDto.getAgreementId())) { // agreement.setIsTaxSource(TaxSourceStatusConstants.TAX_SOURCE_START); // item.setIsTaxSource(TaxSourceStatusConstants.TAX_SOURCE_START); // item.setTaxSourceAgreementId(taxSourceDto.getAgreementId()); // } // }); // } // }); // } // }); // } // } // return agreementInsCompany; // } /** * 返回是否是税源 * * @param deptId * @param userId * @return */ @Override public List getAllAgreement(String deptId, String userId) { //查出所有协议 List agreementInsCompany = this.getAgreementInsCompany(deptId, userId); //查找是否配置税源 List taxSourceDtos = baseMapper.selectList(new LambdaQueryWrapper().eq(TaxSourceDto::getIsTaxSource, "1")); //设置税源参数 agreementInsCompany.forEach(item -> { if (!TaxSourceStatusConstants.TAX_SOURCE_START.equals(item.getIsExclude())) { taxSourceDtos.forEach(taxSourceDto -> { if (item.getId().equals(taxSourceDto.getInsCompanyId())) { item.getAgreement().forEach(agreement -> { if (agreement.getId().equals(taxSourceDto.getAgreementId())) { agreement.setIsTaxSource(TaxSourceStatusConstants.TAX_SOURCE_START); item.setIsTaxSource(TaxSourceStatusConstants.TAX_SOURCE_START); item.setTaxSourceAgreementId(taxSourceDto.getAgreementId()); } }); } }); } }); return agreementInsCompany; } /** * 根据税源获取协议id * * @param deptId * @param userId * @param companyId * @param license * @param volume * @return */ @Override public String getAgreementId(String deptId, String userId, String companyId, String license, String volume, String productid) { // 车牌前两位 String firstTwoPlates = license.substring(0, 2); String resultId = ""; //查找配置的默认项 List taxSourceDtoList = baseMapper.selectList(new LambdaQueryWrapper().eq(TaxSourceDto::getInsCompanyId, companyId)); List taxSource = taxSourceDtoList.stream().filter(x -> x.getLicense().equals(firstTwoPlates)).collect(Collectors.toList()); if (!taxSource.isEmpty()) { resultId = taxSourceDtoList.get(0).getAgreementId(); } boolean isExclude = false; //排除的部门信息 List excludeDepts = taxSourceExcludeService.list(new LambdaQueryWrapper().eq(TaxSourceExcludeDto::getExcludeInfo, deptId).eq(TaxSourceExcludeDto::getInsCompanyId, companyId).eq(TaxSourceExcludeDto::getType, TaxSourceStatusConstants.EXCLUDE_TYPE_DEPT)); //部门排除 if (excludeDepts.stream().anyMatch(taxSourceDto -> taxSourceDto.getExcludeInfo().equals(deptId))) { isExclude = true; } //查找排除的车牌号 List licenseExclude = taxSourceExcludeService.list(new LambdaQueryWrapper().eq(TaxSourceExcludeDto::getExcludeInfo, firstTwoPlates).eq(TaxSourceExcludeDto::getType, TaxSourceStatusConstants.EXCLUDE_TYPE_LICENSE)); //车牌排除 if (licenseExclude.stream().anyMatch(taxSourceDto -> taxSourceDto.getExcludeInfo().equals(firstTwoPlates))) { isExclude = true; } // 税源 TaxSourceDto taxSourceDtos = taxSourceDtoList.stream().filter(x -> x.getIsTaxSource().equals("1")).collect(Collectors.toList()).get(0); if (!isExclude && (Float.parseFloat(volume) >= 1) && (taxSourceDtos != null)) { resultId = taxSourceDtos.getAgreementId(); } // 不包含险种 随机其他保险公司 if (taxSourceDtos != null && taxSourceDtos.getAgreementId().equals(resultId) && taxSourceDtos.getInsuranceRepertoire().contains(productid)) { resultId = taxSourceDtos.getBanishAgreementId(); } return resultId; } }