sinake
4 天以前 ee089a8ac4a9d99ef0a0491bbf1d80a2d4fbc393
ruoyi-project/src/main/java/com/ruoyi/project/service/impl/ServiceFunddetailServiceImpl.java
@@ -1,6 +1,7 @@
package com.ruoyi.project.service.impl;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
@@ -12,10 +13,12 @@
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.tax.PerformanceTaxtUtils;
import com.ruoyi.common.tax.TaxtUtils;
import com.ruoyi.common.tax.VatTaxUtils;
import com.ruoyi.common.utils.bean.DtoConversionUtils;
import com.ruoyi.project.domain.*;
import com.ruoyi.project.domain.vo.*;
import com.ruoyi.project.mapper.ServiceFundMapper;
import com.ruoyi.project.mapper.SpStatBonusMapper;
import lombok.extern.flogger.Flogger;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
@@ -46,6 +49,9 @@
    @Autowired
    ServiceFundMapper serviceFundMapper;
    @Autowired
    SpStatBonusMapper spStatBonusMapper;
    /**
@@ -131,21 +137,30 @@
     */
    @Override
    public Double getTaxByBeneFiciaryNo(TaxMoneyVO taxMoneyVo) {
        Double sum = 0.00;
        Double totalScore = 0.00;
        // 金额累加必须用 BigDecimal:double 累加会引入浮点尾差,导致算出的个税出现分以下误差
        BigDecimal sum = BigDecimal.ZERO;
        BigDecimal totalScore = BigDecimal.ZERO;
        List<ServiceFunddetail> taxationByBeneFiciaryNo = serviceFunddetailMapper.getTaxBeforeByBeneFiciaryNo(taxMoneyVo);
        if (CollectionUtils.isNotEmpty(taxationByBeneFiciaryNo)) {
            //计算申请金额总和,不包括当前传进来的
            sum = taxationByBeneFiciaryNo.stream().mapToDouble(ServiceFunddetail::getAmount).sum();
            sum = taxationByBeneFiciaryNo.stream()
                    .map(ServiceFunddetail::getAmount)
                    .filter(java.util.Objects::nonNull)
                    .map(BigDecimal::valueOf)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            //不加本次薪酬的个税
            totalScore = taxationByBeneFiciaryNo.stream().mapToDouble(ServiceFunddetail::getTaxamount).sum();
            totalScore = taxationByBeneFiciaryNo.stream()
                    .map(ServiceFunddetail::getTaxamount)
                    .filter(java.util.Objects::nonNull)
                    .map(BigDecimal::valueOf)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        sum = sum + new Double(taxMoneyVo.getMoney());
        sum = sum.add(new BigDecimal(taxMoneyVo.getMoney()));
        //加上本次薪酬的总个税
        String taxation = TaxtUtils.getTaxation(new BigDecimal(sum));
        String taxation = TaxtUtils.getTaxation(sum);
        Double tax = Double.valueOf(taxation) - totalScore;
        Double tax = new BigDecimal(taxation).subtract(totalScore).doubleValue();
        return tax;
    }
@@ -158,21 +173,30 @@
    @Override
    public TaxedMoneyVO getTaxBeforeByAfterMoney(TaxMoneyVO taxMoneyVo) {
        TaxedMoneyVO taxedMoneyVO = new TaxedMoneyVO();
        Double sum = 0.00;
        Double totalScore = 0.00;
        // 金额累加必须用 BigDecimal,避免浮点尾差影响税前/税额结果
        BigDecimal sum = BigDecimal.ZERO;
        BigDecimal totalScore = BigDecimal.ZERO;
        List<ServiceFunddetail> taxationByBeneFiciaryNo = serviceFunddetailMapper.getTaxBeforeByBeneFiciaryNo(taxMoneyVo);
        if (CollectionUtils.isNotEmpty(taxationByBeneFiciaryNo)) {
            //计算出税后总金额(不包含本次)
            sum = taxationByBeneFiciaryNo.stream().mapToDouble(ServiceFunddetail::getTaxedamount).sum();
            sum = taxationByBeneFiciaryNo.stream()
                    .map(ServiceFunddetail::getTaxedamount)
                    .filter(java.util.Objects::nonNull)
                    .map(BigDecimal::valueOf)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            //求出之前的总的个税
            totalScore = taxationByBeneFiciaryNo.stream().mapToDouble(ServiceFunddetail::getTaxamount).sum();
            totalScore = taxationByBeneFiciaryNo.stream()
                    .map(ServiceFunddetail::getTaxamount)
                    .filter(java.util.Objects::nonNull)
                    .map(BigDecimal::valueOf)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        //计算出总的税后薪酬(包含本次)
        sum = sum + Double.valueOf(taxMoneyVo.getMoney());
        sum = sum.add(new BigDecimal(taxMoneyVo.getMoney()));
        //税前总金额
        String taxationBefore = TaxtUtils.getTaxationBefore(new BigDecimal(sum));
        String taxationBefore = TaxtUtils.getTaxationBefore(sum);
        //总个税
        BigDecimal tax = new BigDecimal(taxationBefore).subtract(new BigDecimal(sum));
        BigDecimal tax = new BigDecimal(taxationBefore).subtract(sum);
        //本次的税前金额
        String nowTaxMoney = TaxtUtils.getTaxationBefore(new BigDecimal(taxMoneyVo.getMoney()));
@@ -230,15 +254,22 @@
        Long fundid = serviceFunddetail.getFundid();
        ServiceFund serviceFund = serviceFundMapper.selectById(fundid);
        if (ObjectUtils.isNotEmpty(serviceFund)) {
            Double pretax = serviceFund.getPretaxcost();
            Double aftertax = serviceFund.getTaxedcost();
            pretax = pretax - serviceFunddetail.getAmount();
            aftertax = aftertax - serviceFunddetail.getTaxedamount();
            Double amount = pretax + aftertax;
            // 金额一律用 BigDecimal 累加/扣减,避免浮点尾差
            BigDecimal pretax = toMoney(serviceFund.getPretaxcost()).subtract(toMoney(serviceFunddetail.getAmount()));
            BigDecimal aftertax = toMoney(serviceFund.getTaxedcost()).subtract(toMoney(serviceFunddetail.getTaxedamount()));
            BigDecimal vat = toMoney(serviceFund.getVatCost()).subtract(toMoney(serviceFunddetail.getVatAmount()));
            BigDecimal cityTax = toMoney(serviceFund.getCityTaxCost()).subtract(toMoney(serviceFunddetail.getCityTaxAmount()));
            BigDecimal laborPretax = toMoney(serviceFund.getLaborPretaxCost()).subtract(toMoney(serviceFunddetail.getLaborPretaxAmount()));
            BigDecimal tax = toMoney(serviceFund.getTaxcost()).subtract(toMoney(serviceFunddetail.getTaxamount()));
            BigDecimal amount = pretax.add(aftertax);
            serviceFund.setPretaxcost(pretax);
            serviceFund.setTaxedcost(aftertax);
            serviceFund.setAmountrequested(amount);
            serviceFund.setPretaxcost(pretax.doubleValue());
            serviceFund.setTaxedcost(aftertax.doubleValue());
            serviceFund.setVatCost(vat.doubleValue());
            serviceFund.setCityTaxCost(cityTax.doubleValue());
            serviceFund.setLaborPretaxCost(laborPretax.doubleValue());
            serviceFund.setTaxcost(tax.doubleValue());
            serviceFund.setAmountrequested(amount.doubleValue());
            int updateAmount = serviceFundMapper.updateById(serviceFund);
        }
        return serviceFunddetailMapper.deleteById(id);
@@ -262,159 +293,248 @@
    }
    /**
     * 根据条件算税
     * 批量算税主方法(劳务个税 + 增值税 + 城建税,v2 口径)
     * <p>
     * 金额模型:
     * <pre>
     * 税前录入:V = ceil(P × 1%)、C = ceil(V × 3.5%)、L = P - V - C、
     *          T = 劳务个税(当月累计 L) - 原累计 T、N = L - T
     * 税后录入:N累计 = 原累计 N + N、L累计 = getTaxationBefore(N累计)、
     *          L = L累计 - 原累计 L、T = L - N、P/V/C = solveGross(L)
     * 无身份证:V = C = T = 0,L = N = P
     * </pre>
     *
     * @param serviceFunddetails
     * @param serviceFunddetails 费用明细列表
     * @param tempTaxSumMap      本月累计金额缓存(按身份证号)
     * @return 是否算税成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean calculateTax(List<ServiceFunddetail> serviceFunddetails) {
    public Boolean calculateTax(List<ServiceFunddetail> serviceFunddetails, Map<String, TaxMoneySumEO> tempTaxSumMap) {
        return calculateTax(serviceFunddetails, tempTaxSumMap, new Date());
    }
    /**
     * 批量算税(指定本批算税时间,保证同批明细 tax_time 一致)
     *
     * @param serviceFunddetails 费用明细列表
     * @param tempTaxSumMap      本月累计金额缓存(按身份证号)
     * @param taxTime            本次算税时间
     * @return 是否算税成功
     */
    @Override
    public Boolean calculateTax(List<ServiceFunddetail> serviceFunddetails, Map<String, TaxMoneySumEO> tempTaxSumMap, Date taxTime) {
        // 1. 校验参数
        if (CollectionUtils.isEmpty(serviceFunddetails)) {
            throw new BaseException("算税条件为空,请检查后,再进行计算");
        }
        //这个是为了获取fund_tax_time
        // 2. 排序:税时间为空的排后面,再按 id 保证同批顺序稳定
        serviceFunddetails.sort(Comparator
                .comparing((ServiceFunddetail obj) -> ObjectUtils.isEmpty(obj.getTaxTime()) ? 1 : 0)
                .thenComparing(obj -> obj.getId() == null ? Long.MAX_VALUE : obj.getId()));
        // 3. 获取本批次 fundid 及主表信息
        Long fundid = serviceFunddetails.get(0).getFundid();
        log.info("算税的fundid的值为:{}", fundid);
        ServiceFund serviceFund1 = serviceFundMapper.selectById(fundid);
        log.info("fund表的fundtextime值为:{}", serviceFund1.getFundtaxtime());
        if (serviceFund1 == null) {
            throw new BaseException("未找到费用申请单,ID=" + fundid);
        }
        // 明细 tax_time 统一使用本次算税时间
        if (taxTime == null) {
            taxTime = new Date();
        }
        //用于临时保存已经算好的"费用详情数据"
        List<ServiceFunddetail> temporarySave = new ArrayList<>();
        //保存总税前金额(用于更新fund表里的)
        BigDecimal pretaxcost = BigDecimal.valueOf(0.00);
        for (ServiceFunddetail serviceFunddetail : serviceFunddetails) {
            if (StringUtils.isEmpty(serviceFunddetail.getIdcardno())) {
                logger.info("serviceFunddetail数据入参 : {}", serviceFunddetail);
                //如果身份证号为空,就不用查了,可能是医院的记录
                if (StringUtils.isEmpty(serviceFunddetail.getServicesscopename()) || !serviceFunddetail.getServicesscopename().contains("税后")) {
                    serviceFunddetail.setTaxedamount(serviceFunddetail.getAmount());
                } else {
                    serviceFunddetail.setAmount(serviceFunddetail.getTaxedamount());
                }
                //公司的申请金额也要加进去
                pretaxcost = pretaxcost.add(BigDecimal.valueOf(serviceFunddetail.getAmount()));
        // 4. 本批次合计(全部用 BigDecimal,禁止 double 累加)
        BigDecimal pretaxTotal = BigDecimal.ZERO;
        BigDecimal vatTotal = BigDecimal.ZERO;
        BigDecimal cityTaxTotal = BigDecimal.ZERO;
        BigDecimal laborPretaxTotal = BigDecimal.ZERO;
        BigDecimal taxTotal = BigDecimal.ZERO;
        BigDecimal taxedTotal = BigDecimal.ZERO;
                serviceFunddetail.setTaxamount(0.0);
                logger.info("serviceFunddetail更新后的数据 : {}", serviceFunddetail);
                boolean b = this.updateById(serviceFunddetail);
                logger.info("serviceFunddetail修改结果 : {}", b);
        List<ServiceFunddetail> updateList = new ArrayList<>();
        // 5. 遍历每条明细
        for (ServiceFunddetail detail : serviceFunddetails) {
            if (detail.getAmount() == null) {
                detail.setAmount(0.0);
            }
            if (detail.getTaxedamount() == null) {
                detail.setTaxedamount(0.0);
            }
            // 服务说明为空按“非税后”处理
            String scopeName = detail.getServicesscopename() == null ? "" : detail.getServicesscopename().trim();
            boolean afterTax = scopeName.contains("税后");
            // 5.1 身份证号为空:不扣增值税/城建税/劳务个税
            if (StringUtils.isEmpty(detail.getIdcardno())) {
                BigDecimal p = afterTax ? toMoney(detail.getTaxedamount()) : toMoney(detail.getAmount());
                detail.setAmount(p.doubleValue());
                detail.setVatAmount(0.0);
                detail.setCityTaxAmount(0.0);
                detail.setLaborPretaxAmount(p.doubleValue());
                detail.setTaxamount(0.0);
                detail.setTaxedamount(p.doubleValue());
                detail.setTaxVersion(VatTaxUtils.TAX_VERSION_V2);
                detail.setTaxTime(taxTime);
                updateList.add(detail);
                pretaxTotal = pretaxTotal.add(p);
                laborPretaxTotal = laborPretaxTotal.add(p);
                taxedTotal = taxedTotal.add(p);
                continue;
            }
            //获取当月的第一天
            Date firstDay = Date.from(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay(ZoneId.systemDefault()).toInstant());
//            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String firstDatStr = dateFormat.format(firstDay);
            TaxMoneyByItemEO taxMoneyVO = new TaxMoneyByItemEO();
            if (serviceFund1.getFundtaxtime() != null) {
                taxMoneyVO.setTaxTime(serviceFund1.getFundtaxtime());
            } else {
                taxMoneyVO.setTaxTime(new Date());
            }
            taxMoneyVO.setFirstDay(firstDay);
            taxMoneyVO.setFundID(serviceFunddetail.getFundid());
            taxMoneyVO.setIDCard(serviceFunddetail.getIdcardno());
            //查出税前、税、税后的总额(不包含本次)
            TaxMoneySumEO taxSum = serviceFunddetailMapper.getTaxSum(taxMoneyVO);
            logger.info("查询的taxSum的值 : {}", taxSum);
            if (ObjectUtils.isEmpty(taxSum)) {
            // 5.2 身份证号不为空:本月累计 + 分档算税
            String idcard = detail.getIdcardno().trim();
            // 5.2.1 取本月累计(缓存避免重复查库)
            TaxMoneySumEO taxSum = tempTaxSumMap.get(idcard);
            if (taxSum == null) {
                taxSum = new TaxMoneySumEO();
                taxSum.setXh(0L);
                tempTaxSumMap.put(idcard, taxSum);
            }
            long xh = (taxSum.getXh() == null ? 0L : taxSum.getXh()) + 1;
            taxSum.setXh(xh);
            detail.setXh(xh);
            // 累计税前金额(不含本笔),用于页面展示
            detail.setAllAmount(taxSum.getAmounts());
            //判断临时集合temporarySave中,有没有已经算过的数据
            if (CollectionUtils.isNotEmpty(temporarySave)) {
                for (ServiceFunddetail serviceFunddetail1 : temporarySave) {
                    //如果本次的”人员类型“、”身份证号“在临时集合中存在,则将集合数据中的该受益人的,税前、税金、税后,和总的相加,并将老的覆盖
                    // if (serviceFunddetail1.getApplytype().equals(serviceFunddetail.getApplytype()) && serviceFunddetail1.getBeneficiaryno().equals(serviceFunddetail.getBeneficiaryno())) {
                    if (serviceFunddetail1.getIdcardno().trim().equals(serviceFunddetail.getIdcardno().trim())) {
                        //将集合数据中的该受益人的,税前、税金、税后,和总的相加
                        BigDecimal amounts = BigDecimal.valueOf(taxSum.getAmounts()).add(new BigDecimal(String.valueOf(serviceFunddetail1.getAmount())));
                        BigDecimal taxAmounts = BigDecimal.valueOf(taxSum.getTaxAmounts()).add(new BigDecimal(String.valueOf(serviceFunddetail1.getTaxamount())));
                        BigDecimal TaxedAmounts = BigDecimal.valueOf(taxSum.getTaxedAmounts()).add(new BigDecimal(String.valueOf(serviceFunddetail1.getTaxedamount())));
                        //将老的覆盖
                        taxSum.setAmounts(amounts.doubleValue());
                        taxSum.setTaxAmounts(taxAmounts.doubleValue());
                        taxSum.setTaxedAmounts(TaxedAmounts.doubleValue());
            BigDecimal prevAmount = toMoney(taxSum.getAmounts());
            BigDecimal prevLaborPretax = toMoney(taxSum.getLaborPretaxAmounts());
            BigDecimal prevTax = toMoney(taxSum.getTaxAmounts());
            BigDecimal prevTaxed = toMoney(taxSum.getTaxedAmounts());
            BigDecimal prevVat = toMoney(taxSum.getVatAmounts());
            BigDecimal prevCityTax = toMoney(taxSum.getCityTaxAmounts());
            BigDecimal pretax;
            BigDecimal vat;
            BigDecimal cityTax;
            BigDecimal laborPretax;
            BigDecimal taxAmount;
            BigDecimal taxedAmount;
            if (afterTax) {
                // 5.2.2 税后录入:先反算劳务税前,再循环反解税前/增值税/城建税
                taxedAmount = toMoney(detail.getTaxedamount());
                BigDecimal sumTaxed = prevTaxed.add(taxedAmount);
                BigDecimal sumLaborPretax = new BigDecimal(TaxtUtils.getTaxationBefore(sumTaxed));
                laborPretax = VatTaxUtils.roundMoney(sumLaborPretax.subtract(prevLaborPretax));
                if (laborPretax.signum() < 0) {
                    // 逐笔向上取整可能带来几分钱的反算误差,容差内按 0 处理;超出容差说明数据异常
                    if (laborPretax.abs().compareTo(new BigDecimal("0.05")) <= 0) {
                        log.warn("税后金额反算劳务税前出现负尾差,已按 0 处理。人员:{},反算劳务税前={},原累计劳务税前={}",
                                safeName(detail), sumLaborPretax, prevLaborPretax);
                        laborPretax = BigDecimal.ZERO;
                    } else {
                        throw new BaseException("税后金额反算劳务税前失败,人员:" + safeName(detail));
                    }
                    continue;
                }
                taxAmount = VatTaxUtils.roundMoney(laborPretax.subtract(taxedAmount));
                if (taxAmount.signum() < 0) {
                    taxAmount = BigDecimal.ZERO;
                }
                VatTaxUtils.GrossAmount gross = VatTaxUtils.solveGross(laborPretax);
                pretax = gross.getPretax();
                vat = gross.getVat();
                cityTax = gross.getCityTax();
            } else {
                // 5.2.3 税前录入:先算增值税/城建税/劳务税前,再按累计劳务税前算劳务个税
                pretax = toMoney(detail.getAmount());
                vat = VatTaxUtils.calcVat(pretax);
                cityTax = VatTaxUtils.calcCityTax(vat);
                laborPretax = VatTaxUtils.roundMoney(pretax.subtract(vat).subtract(cityTax));
                BigDecimal sumLaborPretax = prevLaborPretax.add(laborPretax);
                BigDecimal sumTax = new BigDecimal(TaxtUtils.getTaxation(sumLaborPretax));
                taxAmount = VatTaxUtils.roundMoney(sumTax.subtract(prevTax));
                if (taxAmount.signum() < 0) {
                    taxAmount = BigDecimal.ZERO;
                }
                taxedAmount = VatTaxUtils.roundMoney(laborPretax.subtract(taxAmount));
            }
            if (StringUtils.isEmpty(serviceFunddetail.getServicesscopename()) || !serviceFunddetail.getServicesscopename().contains("税后")) {
                logger.info("计算税前信息开始:{}", serviceFunddetail);
                logger.info("taxSum入参数据:{}", taxSum);
                // 如何税前金额不为空,用之前的”总税前金额“,加上当前”税前金额“
                BigDecimal newAmounts = BigDecimal.valueOf(taxSum.getAmounts()).add(BigDecimal.valueOf(serviceFunddetail.getAmount()));
            detail.setAmount(pretax.doubleValue());
            detail.setVatAmount(vat.doubleValue());
            detail.setCityTaxAmount(cityTax.doubleValue());
            detail.setLaborPretaxAmount(laborPretax.doubleValue());
            detail.setTaxamount(taxAmount.doubleValue());
            detail.setTaxedamount(taxedAmount.doubleValue());
            detail.setTaxVersion(VatTaxUtils.TAX_VERSION_V2);
            detail.setTaxTime(taxTime);
            updateList.add(detail);
                // 计算总税金(包含本次)
                String taxation = TaxtUtils.getTaxation(newAmounts);
                //计算本次税金(总税金(包含本次) - 总税金(不包含本次))
                BigDecimal taxAmountNow = new BigDecimal(taxation).subtract(new BigDecimal(String.valueOf(taxSum.getTaxAmounts())));
            // 5.2.4 更新本月累计
            taxSum.setAmounts(prevAmount.add(pretax).doubleValue());
            taxSum.setVatAmounts(prevVat.add(vat).doubleValue());
            taxSum.setCityTaxAmounts(prevCityTax.add(cityTax).doubleValue());
            taxSum.setLaborPretaxAmounts(prevLaborPretax.add(laborPretax).doubleValue());
            taxSum.setTaxAmounts(prevTax.add(taxAmount).doubleValue());
            taxSum.setTaxedAmounts(prevTaxed.add(taxedAmount).doubleValue());
                //计算本次的税后金额(本次的税前金额 - 税金)
                BigDecimal texdAfterNow = BigDecimal.valueOf(serviceFunddetail.getAmount()).subtract(taxAmountNow);
                logger.info("打印newAmounts:{},taxation:{},taxAmountNow:{},texdAfterNow:{}", newAmounts, taxation, taxAmountNow, texdAfterNow);
                //将循环的数据重新放到一个新对象中
                ServiceFunddetail serviceFunddetail3 = DtoConversionUtils.sourceToTarget(serviceFunddetail, ServiceFunddetail.class);
                serviceFunddetail3.setTaxamount(taxAmountNow.doubleValue());
                serviceFunddetail3.setTaxedamount(texdAfterNow.doubleValue());
                if (ObjectUtils.isEmpty(serviceFund1.getFundtaxtime())) {
                    serviceFunddetail3.setTaxTime(new Date());
                } else {
                    serviceFunddetail3.setTaxTime(serviceFund1.getFundtaxtime());
                }
                logger.info("打印税前serviceFunddetail3数据 :{}", serviceFunddetail3);
                // 将该条数据更新
                boolean b = this.updateById(serviceFunddetail3);
                pretaxcost = pretaxcost.add(BigDecimal.valueOf(serviceFunddetail.getAmount()));
                // 把该数据,放到临时的集合中
                temporarySave.add(serviceFunddetail3);
            } else if (serviceFunddetail.getServicesscopename().contains("税后")) {
                logger.info("计算税后信息开始:{}", serviceFunddetail);
                logger.info("taxSum入参数据:{}", taxSum);
                //税后不为空, 用之前的”总税后金额“,加上当前”税后金额“
                BigDecimal newTaxedAmounts = BigDecimal.valueOf(taxSum.getTaxedAmounts()).add(BigDecimal.valueOf(serviceFunddetail.getTaxedamount()));
                //通过总税后(包含本次)算出总税前
                String taxationBefore = TaxtUtils.getTaxationBefore(newTaxedAmounts);
                //算出本次税前(总税前(包含本次) - 总税前(不包含本次))
                BigDecimal amountNow = new BigDecimal(taxationBefore).subtract(BigDecimal.valueOf(taxSum.getAmounts()));
                //算出本次税金(本次税前 - 本次税后)
                BigDecimal tax = amountNow.subtract(BigDecimal.valueOf(serviceFunddetail.getTaxedamount()));
                logger.info("打印newTaxedAmounts:{},taxationBefore:{},amountNow:{},tax:{}", newTaxedAmounts, taxationBefore, amountNow, tax);
                //将循环的数据重新放到一个新对象中
                ServiceFunddetail serviceFunddetail3 = DtoConversionUtils.sourceToTarget(serviceFunddetail, ServiceFunddetail.class);
                serviceFunddetail3.setAmount(amountNow.doubleValue());
                serviceFunddetail3.setTaxamount(tax.doubleValue());
                if (ObjectUtils.isEmpty(serviceFund1.getFundtaxtime())) {
                    serviceFunddetail3.setTaxTime(new Date());
                } else {
                    serviceFunddetail3.setTaxTime(serviceFund1.getFundtaxtime());
                }
                logger.info("打印serviceFunddetail3:{}", serviceFunddetail3);
                // 将该条数据更新
                updateById(serviceFunddetail3);
                pretaxcost = pretaxcost.add(BigDecimal.valueOf(serviceFunddetail3.getAmount()));
                // 把该数据,放到临时的集合中
                temporarySave.add(serviceFunddetail3);
            }
            pretaxTotal = pretaxTotal.add(pretax);
            vatTotal = vatTotal.add(vat);
            cityTaxTotal = cityTaxTotal.add(cityTax);
            laborPretaxTotal = laborPretaxTotal.add(laborPretax);
            taxTotal = taxTotal.add(taxAmount);
            taxedTotal = taxedTotal.add(taxedAmount);
        }
        // 6. 批量更新所有明细
        for (ServiceFunddetail d : updateList) {
            this.updateById(d);
        }
        // 7. 更新主表 service_fund 的 6 个金额、算税时间、状态、版本
        ServiceFund serviceFund = new ServiceFund();
        serviceFund.setPretaxcost(pretaxcost.doubleValue());
        serviceFund.setId(serviceFunddetails.get(0).getFundid());
        if (ObjectUtils.isEmpty(serviceFund1.getFundtaxtime())) {
            serviceFund.setFundtaxtime(new Date());
        }
        // 1 已算税
        serviceFund.setId(fundid);
        serviceFund.setPretaxcost(pretaxTotal.doubleValue());
        serviceFund.setVatCost(vatTotal.doubleValue());
        serviceFund.setCityTaxCost(cityTaxTotal.doubleValue());
        serviceFund.setLaborPretaxCost(laborPretaxTotal.doubleValue());
        serviceFund.setTaxcost(taxTotal.doubleValue());
        serviceFund.setTaxedcost(taxedTotal.doubleValue());
        serviceFund.setTaxVersion(VatTaxUtils.TAX_VERSION_V2);
        serviceFund.setFundtaxtime(taxTime);
        serviceFund.setIstax(1);
        serviceFundMapper.updateById(serviceFund);
        return true;
    }
    /**
     * null 安全的金额转换
     */
    private static BigDecimal toMoney(Double value) {
        return value == null ? BigDecimal.ZERO : BigDecimal.valueOf(value);
    }
    /**
     * 报错时能定位到具体人员
     */
    private static String safeName(ServiceFunddetail detail) {
        String name = detail.getBeneficiaryname() == null ? "" : detail.getBeneficiaryname();
        String idcard = detail.getIdcardno() == null ? "" : detail.getIdcardno();
        return name + "(" + idcard + ")";
    }
//    @Override
//    public Boolean calculateTax2(List<ServiceFunddetail> serviceFunddetails) {
//        //用于缓存每个身份证号(idcardno)本月累计的税前、税金、税后金额(TaxMoneySumEO对象)
//        Map<String, TaxMoneySumEO> tempTaxSumMap = new HashMap<>();
//
//        TaxMoneyByItemEO taxMoneyVO = new TaxMoneyByItemEO();
//        Date firstDay = Date.from(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay(ZoneId.systemDefault()).toInstant());
//        taxMoneyVO.setFirstDay(firstDay);
//        taxMoneyVO.setTaxTime(new Date());
//
//        List<TaxMoneySumEO> taxSum = serviceFunddetailMapper.getTaxSum(taxMoneyVO);
//        for (TaxMoneySumEO taxMoneySumEO : taxSum) {
//            tempTaxSumMap.put(taxMoneySumEO.getIDCardNo(), taxMoneySumEO);
//        }
//
//        return calculateTax(serviceFunddetails, tempTaxSumMap);
//    }
    @Override
    public Map<String, Double> performance(ServiceFunddetail serviceFunddetail) {
@@ -464,6 +584,10 @@
        //将计算出来的数据放到serviceFunddetail中
        serviceFunddetail.setTaxamount(nowSingleTax.doubleValue());
        serviceFunddetail.setTaxedamount(taxAfter.doubleValue());
        // 绩效不属于专家费:不计算增值税/城建税,劳务税前 = 税前金额
        serviceFunddetail.setVatAmount(0.0);
        serviceFunddetail.setCityTaxAmount(0.0);
        serviceFunddetail.setLaborPretaxAmount(serviceFunddetail.getAmount());
        serviceFunddetail.setJxrq(nowMonth);
        save(serviceFunddetail);
@@ -473,4 +597,9 @@
        return map;
    }
    @Override
    public List<SpStatBonus> getListBySpStatBonus(SpStatBonusReq spStatBonusReq) {
        return spStatBonusMapper.getListBySpStatBonus(spStatBonusReq.getPabegtime(), spStatBonusReq.getPaendtime(), spStatBonusReq.getPadeptno(), spStatBonusReq.getPausername());
    }
}