package com.ydtech.modules.admin.service.impl; import cn.hutool.core.collection.CollUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.google.gson.Gson; import com.ydtech.constants.InsuranceEnum; import com.ydtech.exception.SystemException; import com.ydtech.modules.admin.constants.EnabledEnum; import com.ydtech.modules.admin.constants.UserConstants; import com.ydtech.modules.admin.dao.SysChannelGroupMapper; import com.ydtech.modules.admin.model.SysUser; import com.ydtech.modules.admin.model.po.*; import com.ydtech.modules.admin.model.vo.AddSysChannelGroupVo; import com.ydtech.modules.admin.model.vo.CostSysChannelGroupVo; import com.ydtech.modules.admin.model.vo.SysChannelGroupPageVo; import com.ydtech.modules.admin.model.vo.SysChannelGroupVo; import com.ydtech.modules.admin.service.*; import com.ydtech.modules.esm.dao.EsmInsCompanyMapper; import com.ydtech.modules.esm.model.EsmInsCompany; import com.ydtech.modules.order.entity.InsAreaCompany; import com.ydtech.modules.order.entity.InsOrders; import com.ydtech.modules.order.entity.api.dajia.response.NVHLODR1001Response; import com.ydtech.modules.order.entity.api.zhongmei.response.SubmitResponse; import com.ydtech.modules.order.entity.crawler.response.CrawlerVerifyPaymentResponse; import com.ydtech.modules.order.entity.dto.DrivingInsuranceDto; import com.ydtech.modules.protocol.entity.constants.PtlApiType; import com.ydtech.modules.protocol.entity.po.PtlAgreement; import com.ydtech.modules.protocol.utils.AssertionUtils; import com.ydtech.modules.protocols.entity.po.InsFeeOrderNew; import com.ydtech.modules.protocols.entity.po.PtlAgreementProductCostsNew; import com.ydtech.modules.protocols.service.PtlAgreementProductCostsNewService; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; /** * @author dongxin * @description 针对表【sys_channel_group(渠道分组)】的数据库操作Service实现 * @createDate 2024-09-09 18:55:22 */ @Service public class SysChannelGroupServiceImpl extends ServiceImpl implements SysChannelGroupService { @Autowired private PtlAgreementProductCostsNewService ptlAgreementProductCostsNewService; @Autowired private SysChannelCostRelationService sysChannelCostRelationService; @Autowired private SysChannelGroupInfoService sysChannelGroupInfoService; @Autowired private SysChannelGroupService sysChannelGroupService; @Autowired private SysChannelCostService sysChannelCostService; @Autowired private SysCostOperateLogService sysCostOperateLogService; @Autowired private SysChannelGradeService sysChannelGradeService; @Autowired private EsmInsCompanyMapper esmInsCompanyMapper; @Override public Page queryPage(Page page, SysChannelGroupVo sysChannelGroupVo) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(!ObjectUtils.isEmpty(sysChannelGroupVo.getName()), SysChannelGroup::getName, sysChannelGroupVo.getName()) .eq(!ObjectUtils.isEmpty(sysChannelGroupVo.getId()), SysChannelGroup::getId, sysChannelGroupVo.getId()) .eq(!ObjectUtils.isEmpty(sysChannelGroupVo.getEnabled()), SysChannelGroup::getEnabled, sysChannelGroupVo.getEnabled()); Page channelGroupPage = page(page, wrapper); return channelGroupPage; } @Override public String checkNameUnique(Long id, AddSysChannelGroupVo sysChannelGroup) { Long groupId = ObjectUtils.isEmpty(id) ? -1L: id; QueryWrapper qw = new QueryWrapper<>(); qw.eq("name", sysChannelGroup.getName()); SysChannelGroup info = this.baseMapper.selectOne(qw); if (!Objects.isNull(info) && info.getId() != groupId) { return UserConstants.NOT_UNIQUE; } return UserConstants.UNIQUE; } @Override @Transactional public void insert(String userName, AddSysChannelGroupVo addChannelGroupVo) { SysChannelGroup sysChannelGroup = new SysChannelGroup(); BeanUtils.copyProperties(addChannelGroupVo, sysChannelGroup); sysChannelGroup.setCreateTime(LocalDateTime.now()); sysChannelGroup.setCreateBy(userName); sysChannelGroupService.save(sysChannelGroup); List infos = addChannelGroupVo.getInfos(); if(CollUtil.isNotEmpty(infos)){ for (SysChannelGroupInfo info : infos) { info.setChannelId(sysChannelGroup.getId()); info.setCreateTime(LocalDateTime.now()); info.setCreateBy(userName); } sysChannelGroupInfoService.saveBatch(infos); } } @Override @Transactional public void edit(Long id, String userName, AddSysChannelGroupVo addChannelGroupVo) { SysChannelGroup sysChannelGroup = new SysChannelGroup(); BeanUtils.copyProperties(addChannelGroupVo, sysChannelGroup); sysChannelGroup.setId(id); sysChannelGroup.setUpdateTime(LocalDateTime.now()); sysChannelGroup.setUpdateBy(userName); boolean modify = sysChannelGroupService.saveOrUpdate(sysChannelGroup); // 删除之前的比例 QueryWrapper wrapper2 = new QueryWrapper<>(); wrapper2.eq("channel_id", id); sysChannelGroupInfoService.remove(wrapper2); List infos = addChannelGroupVo.getInfos(); if(CollUtil.isNotEmpty(infos)){ for (SysChannelGroupInfo info : infos) { if(!StringUtils.isBlank(info.getJqCostsProportion())){ BigDecimal jq = new BigDecimal(info.getJqCostsProportion()); if(jq.compareTo(new BigDecimal("1")) > 0){ throw new SystemException("交强加投比例不能大于1"); } } if(!StringUtils.isBlank(info.getSyCostsProportion())){ BigDecimal sy = new BigDecimal(info.getSyCostsProportion()); if(sy.compareTo(new BigDecimal("1")) > 0){ throw new SystemException("商业加投比例不能大于1"); } } if(!StringUtils.isBlank(info.getNoCostsProportion())){ BigDecimal no = new BigDecimal(info.getNoCostsProportion()); if(no.compareTo(new BigDecimal("1")) > 0){ throw new SystemException("非车加投比例不能大于1"); } } info.setChannelId(sysChannelGroup.getId()); info.setUpdateTime(LocalDateTime.now()); info.setUpdateBy(userName); } sysChannelGroupInfoService.saveBatch(infos); } } @Override @Transactional public void delete(Long id) { // 删除所有关联费用 QueryWrapper wrapper1 = new QueryWrapper<>(); wrapper1.eq("channel_id", id); sysChannelCostRelationService.remove(wrapper1); // 删除渠道比例 QueryWrapper wrapper2 = new QueryWrapper<>(); wrapper2.eq("channel_id", id); sysChannelGroupInfoService.remove(wrapper2); // 删除渠道分组 sysChannelGroupService.removeById(id); } @Override @Transactional public void costSet(CostSysChannelGroupVo costSysChannelGroupVo) { // 删除之前所有的 LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(SysChannelCostRelation::getChannelId, costSysChannelGroupVo.getId()); sysChannelCostRelationService.remove(wrapper); List costIdList = costSysChannelGroupVo.getCostIdList(); if(CollUtil.isNotEmpty(costIdList)){ List relations = new ArrayList<>(); for (Long costId : costIdList) { // 获取协议id PtlAgreementProductCostsNew productCostsNew = ptlAgreementProductCostsNewService.getById(costId); if (productCostsNew == null) {continue;} SysChannelCostRelation sysChannelCostRelation = new SysChannelCostRelation(); sysChannelCostRelation.setChannelId(costSysChannelGroupVo.getId()); sysChannelCostRelation.setAgreementId(Long.parseLong(productCostsNew.getAgreementId())); sysChannelCostRelation.setCostId(costId); relations.add(sysChannelCostRelation); } sysChannelCostRelationService.saveBatch(relations); } } @Override public void costCompute(InsFeeOrderNew insFeeOrderNew, PtlAgreement ptlAgreement, PtlAgreementProductCostsNew ptlAgreementProductCostsNew, SysUser sysUser, InsAreaCompany insAreaCompany, InsOrders insOrders) { /** 1.根据机构获取到分组级别*/ /** 2.根据费用获取渠道分组*/ SysChannelGroupInfo info = sysChannelGroupInfoService.queryProportion( ptlAgreementProductCostsNew.getId(),sysUser.getDeptId()); EsmInsCompany insuranceCompany = new EsmInsCompany(); if(!StringUtils.isBlank(ptlAgreement.getPartnerCompaniesId())){ insuranceCompany = esmInsCompanyMapper.selectById(ptlAgreement.getPartnerCompaniesId()); } if(ObjectUtils.isEmpty(info)){ log.error("未查询到关联费用比例"); return; } // 时间判断有没有失效 有没有禁用 SysChannelGroup sysChannelGroup = sysChannelGroupService.getById(info.getChannelId()); if(sysChannelGroup.getStartTime().isAfter(LocalDateTime.now()) || sysChannelGroup.getEndTime().isBefore(LocalDateTime.now()) || sysChannelGroup.getEnabled().equals(EnabledEnum.EE_0.getCode()) ){ log.error("费用分组【"+sysChannelGroup.getName()+"】禁用或者失效!"); return; } if(StringUtils.isBlank(info.getJqCostsProportion()) && StringUtils.isBlank(info.getSyCostsProportion()) && StringUtils.isBlank(info.getNoCostsProportion())){ log.error("费用分组【"+sysChannelGroup.getName()+"】未设置加投费用!"); return; } /** 3.分组级别匹配,计算加投金额 */ BigDecimal jqpremium = insAreaCompany.getJqpremium(); // 交强保费 BigDecimal sypremium = insAreaCompany.getSypremium(); // 商业保费 BigDecimal jypremium = insAreaCompany.getJypremium(); // 非车保费 // 计算加投金额 BigDecimal jq = jqpremium.multiply(new BigDecimal(StringUtils.isBlank(info.getJqCostsProportion())? "0" : info.getJqCostsProportion())) .setScale(2, BigDecimal.ROUND_HALF_UP); BigDecimal sy = sypremium.multiply(new BigDecimal(StringUtils.isBlank(info.getSyCostsProportion())? "0": info.getSyCostsProportion())) .setScale(2, BigDecimal.ROUND_HALF_UP); BigDecimal no = jypremium.multiply(new BigDecimal(StringUtils.isBlank(info.getNoCostsProportion())? "0": info.getNoCostsProportion())) .setScale(2, BigDecimal.ROUND_HALF_UP); SysChannelCost cost = new SysChannelCost(); cost.setOrderNo(insAreaCompany.getId()); cost.setUserId(insOrders.getUserid()); cost.setUserName(insOrders.getUsername()); cost.setLeaderName(insOrders.getLeaderName()); cost.setStatus(0); cost.setSettleStatus(0); cost.setLicenseNo(insOrders.getLicenseno()); // 获取被保险人姓名 json 获取 JSONObject insure = insOrders.getInsureinfo(); cost.setInsuredName(insure.getString("name")); cost.setCompanyId(insAreaCompany.getCompanyId()); cost.setInscompany(insAreaCompany.getInscompany()); cost.setPartnerCompaniesId(ptlAgreement.getPartnerCompaniesId()); cost.setPartnerCompaniesName(insuranceCompany.getNamesimple()); cost.setProductId(ptlAgreementProductCostsNew.getProductId()); cost.setProductName(ptlAgreementProductCostsNew.getProductName()); cost.setDockingPerson(ptlAgreement.getDockingPerson()); cost.setJqCostsProportion(info.getJqCostsProportion()); cost.setSyCostsProportion(info.getSyCostsProportion()); cost.setNoCostsProportion(info.getNoCostsProportion()); cost.setSigningTime(insAreaCompany.getSigningTime()); cost.setPayDate(insAreaCompany.getPayDate()); cost.setJqPremium(jqpremium); cost.setSyPremium(sypremium); cost.setJyPremium(jypremium); cost.setCreateTime(LocalDateTime.now()); cost.setCreateBy(insOrders.getUsername()); cost.setJqPolicyNo(insAreaCompany.getJqpolicyno()); String noPolicyno = this.setNoPolicyno(insAreaCompany); cost.setNoPolicyNo(noPolicyno); cost.setSyPolicyNo(insAreaCompany.getSypolicyno()); cost.setTaxAmount(insAreaCompany.getTaxamount()); cost.setJqJtPremium(jq); cost.setSyJtPremium(sy); cost.setJyJtPremium(no); cost.setJqCostsExportProportion(insFeeOrderNew.getJqCostsExportProportion().add(insFeeOrderNew.getJqOtherCostsProportion())); cost.setSyCostsExportProportion(insFeeOrderNew.getSyCostsExportProportion().add(insFeeOrderNew.getSyOtherCostsProportion())); cost.setJqCostsExportProportion(insFeeOrderNew.getNoCostsExportProportion().add(insFeeOrderNew.getNoOtherCostsProportion())); cost.setTotalPremium(jq.add(sy).add(no)); // 交强手续费出口 加 商业手续费出口 加 非车手续费出口 + 交强跟单费出口 加 商业跟单费出口 加 非车跟单费出口 BigDecimal settlementAmount = insFeeOrderNew.getJqExportSuperviseCostsPremiums() .add(insFeeOrderNew.getSyExportSuperviseCostsPremiums()) .add(insFeeOrderNew.getNoExportSuperviseCostsPremiums()) .add(insFeeOrderNew.getJqExportOtherCostsPremiums()) .add(insFeeOrderNew.getSyExportOtherCostsPremiums()) .add(insFeeOrderNew.getNoExportOtherCostsPremiums()); cost.setSettlementAmount(settlementAmount); cost.setDeleted(0); sysChannelCostService.save(cost); // 日志新增 SysChannelCost logEntity = new SysChannelCost(); logEntity.setId(cost.getId()); logEntity.setJqCostsProportion(cost.getJqCostsProportion()); logEntity.setSyCostsProportion(cost.getSyCostsProportion()); logEntity.setNoCostsProportion(cost.getNoCostsProportion()); logEntity.setJqJtPremium(cost.getJqJtPremium()); logEntity.setSyJtPremium(cost.getSyJtPremium()); logEntity.setJyJtPremium(cost.getJyJtPremium()); SysCostOperateLog sysCostOperateLog = new SysCostOperateLog(); sysCostOperateLog.setOptId(cost.getId()); sysCostOperateLog.setOptType(UserConstants.OPT_TYPE_0); sysCostOperateLog.setOptContent(JSON.toJSONString(logEntity)); sysCostOperateLog.setCreateBy(cost.getCreateBy()); sysCostOperateLog.setCreateTime(cost.getCreateTime()); sysCostOperateLogService.save(sysCostOperateLog); } /** * 获取非车保单号 * @param insAreaCompany * @return */ private String setNoPolicyno(InsAreaCompany insAreaCompany) { if (insAreaCompany.getJypremium() == null || insAreaCompany.getJypremium().compareTo(BigDecimal.ZERO) == 0) { // 非车跟单费 return null; } JSONArray crossInsurance = insAreaCompany.getCrossInsurance(); if(ObjectUtils.isEmpty(crossInsurance)) { return null; } if (crossInsurance.isEmpty()) { return null; } if (ObjectUtils.isEmpty(insAreaCompany.getApiType()) || PtlApiType.PTL_API_2.getCode() == insAreaCompany.getApiType()) { List drivingInsurancesDTOS = crossInsurance.toJavaList(CrawlerVerifyPaymentResponse.DrivingInsurancesDTO.class); return drivingInsurancesDTOS.get(0).getApplication(); } // 永诚 if(insAreaCompany.getCompanyId().contains(InsuranceEnum.AICS.getCode())){ List javaList = crossInsurance.toJavaList(DrivingInsuranceDto.class); return javaList.get(0).getPolicyNumber(); } // 大家 if(insAreaCompany.getCompanyId().contains(InsuranceEnum.DJPC.getCode())){ List javaList = crossInsurance.toJavaList(NVHLODR1001Response.policyInfosDTO.class); return javaList.get(0).getPlyNo(); } // 国任 if(insAreaCompany.getCompanyId().contains(InsuranceEnum.XDCX.getCode())){ List javaList = crossInsurance.toJavaList(DrivingInsuranceDto.class); return javaList.get(0).getPolicyNumber(); } // 中煤 if(insAreaCompany.getCompanyId().contains(InsuranceEnum.ZMBX.getCode())){ List javaList = crossInsurance.toJavaList(SubmitResponse.AccidentNo.class); return javaList.get(0).getRideProposalNo(); } // 紫金 if(insAreaCompany.getCompanyId().contains(InsuranceEnum.ZKIC.getCode())){ List javaList = crossInsurance.toJavaList(DrivingInsuranceDto.class); return javaList.get(0).getPolicyNumber(); } return null; } @Override public SysChannelGroup queryById(Long id) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(SysChannelGroup::getId, id); SysChannelGroup group = getOne(wrapper); // 查询费用关联数据 LambdaQueryWrapper wrapper2 = new LambdaQueryWrapper<>(); wrapper2.eq(!ObjectUtils.isEmpty(id),SysChannelCostRelation::getChannelId,id); List costRelations = sysChannelCostRelationService.list(wrapper2); group.setSysChannelCostRelations(costRelations); // 获取所有级别 List grades = sysChannelGradeService.list(); // 查询分组明细费用比例 LambdaQueryWrapper wrapper3 = new LambdaQueryWrapper<>(); wrapper3.eq(!ObjectUtils.isEmpty(id),SysChannelGroupInfo::getChannelId,id); List groupInfos = sysChannelGroupInfoService.list(wrapper3); Map gradeMap = new HashMap(); if (CollUtil.isNotEmpty(groupInfos)) { gradeMap = groupInfos.stream().collect(Collectors.toMap( SysChannelGroupInfo::getGradeId, SysChannelGroupInfo::getGradeName)); } if(CollUtil.isEmpty(grades)){ group.setSysChannelGroupInfos(groupInfos); return group; } for (SysChannelGrade grade : grades) { if (!gradeMap.containsKey(grade.getId())){ SysChannelGroupInfo sysChannelGroupInfo = new SysChannelGroupInfo(); sysChannelGroupInfo.setGradeId(grade.getId()); sysChannelGroupInfo.setGradeName(grade.getName()); groupInfos.add(sysChannelGroupInfo); } } group.setSysChannelGroupInfos(groupInfos); return group; } @Override public void openOne(Long id) { SysChannelGroup sysChannelGroup = getByIdExist(id, "查询配置信息不存在"); Integer enabled = sysChannelGroup.getEnabled(); Integer i = Objects.equals(EnabledEnum.EE_1.getCode(), enabled) ? EnabledEnum.EE_0.getCode() : EnabledEnum.EE_1.getCode(); boolean b = updateEnabled(i); sysChannelGroup.setEnabled(i); boolean b1 = updateById(sysChannelGroup); AssertionUtils.isSucceed(b1 && b, "成功"); } @Override public void queryCostList(List costsNewList, SysChannelGroupPageVo sysChannelGroupPageVo) { if(CollUtil.isNotEmpty(costsNewList)){ // 查询费用关联数据 LambdaQueryWrapper wrapper= new LambdaQueryWrapper<>(); wrapper.eq(!ObjectUtils.isEmpty(sysChannelGroupPageVo.getId()),SysChannelCostRelation::getChannelId,sysChannelGroupPageVo.getId()); List costRelations = sysChannelCostRelationService.list(wrapper); List costIds = costRelations.stream().map(SysChannelCostRelation::getCostId).collect(Collectors.toList()); for (PtlAgreementProductCostsNew productCostsNew : costsNewList) { if (costIds.contains(Long.parseLong(productCostsNew.getId()))){ productCostsNew.setCheck(true); }else{ productCostsNew.setCheck(false); } } } } @Override public List getProductList(Long id) { List groupList = this.baseMapper.getProductList(id); return groupList; } public static void main(String[] args) { // SysChannelCost logEntity = new SysChannelCost(); // logEntity.setId(1l); // logEntity.setJqCostsProportion("1"); // logEntity.setSyCostsProportion("1"); // logEntity.setNoCostsProportion("1"); // logEntity.setJqJtPremium(new BigDecimal(1)); // logEntity.setSyJtPremium(new BigDecimal(1)); // logEntity.setJyJtPremium(new BigDecimal(1)); // System.out.println(JSON.toJSON(logEntity)); } }