package com.jwipc.finance.service.impl;

import cn.hutool.extra.template.engine.wit.WitTemplate;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jwipc.finance.entity.*;
import com.jwipc.finance.mapper.PaymentAdvanceApplyMapper;
import com.jwipc.finance.model.po.*;
import com.jwipc.finance.model.query.AccountStatementQuery;
import com.jwipc.finance.model.query.PaymentApplyHeadQuery;
import com.jwipc.finance.model.query.PaymentApplyItemQuery;
import com.jwipc.finance.model.query.PaymentPlanQuery;
import com.jwipc.finance.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwipc.security.LoginUtil;
import com.jwipc.util.DateUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 预付款申请主表 服务实现类
 * </p>
 *
 * @author WT
 * @since 2024-03-08
 */
@Service
@RequiredArgsConstructor
public class PaymentAdvanceApplyServiceImpl extends ServiceImpl<PaymentAdvanceApplyMapper, PaymentAdvanceApply> implements IPaymentAdvanceApplyService {
    private final IPaymentCodApplyDetailService paymentCodApplyDetailService;
    private final IPaymentAdvanceApplyDetailService paymentAdvanceApplyDetailService;
    private final IPaymentPlanService paymentPlanService;
    private final IPaymentPlanSummaryService paymentPlanSummaryService;
    private final IAccountStatementService accountStatementService;

    @Override
    public List<PaymentAdvanceApply> selectListByPage(Page<PaymentAdvanceApply> dbPage, PaymentApplyHeadQuery query) {
        if(dbPage == null){
            return baseMapper.selectListByPage(query);
        }
        return baseMapper.selectListByPage(dbPage,query);
    }

    @Override
    public void checkAmount(List<PaymentAdvanceApplyDetail> pcaDetailList, List<Long> delItemIdList) throws Exception {
        List<String> poKeyList = Lists.newArrayList();
        List<Long> idList = Optional.ofNullable(delItemIdList).orElse(Lists.newArrayList());
        for (PaymentAdvanceApplyDetail dto : pcaDetailList) {
            poKeyList.add(dto.getDataKey());
            if(Objects.nonNull(dto.getId())){
                idList.add(dto.getId());
            }
        }

        // 新增时检查采购订单行在SRM中使用的情况
        // 获取SRM中预付款申请单明细及金额
        final List<PaymentAdvanceApplyDetailPO> paaDetailList = baseMapper.createGetPoSituationByPoKey(poKeyList);
        List<PaymentAdvanceApplyDetailPO> codDetailList = paymentCodApplyDetailService.findCodApplyByPoKey(poKeyList);
        List<PaymentAdvanceApplyDetailPO> detailList=paaDetailList.stream().filter(e->!idList.contains(e.getId())).collect(Collectors.toList());
        detailList.addAll(codDetailList);
        for (PaymentAdvanceApplyDetail dto : pcaDetailList) {
            String dataKey = dto.getDataKey();
            BigDecimal itAmount = dto.getItAmount();
            BigDecimal budMoney = dto.getBudInnerMoney().add(dto.getBudOutMoney());

            for (PaymentAdvanceApplyDetailPO detail : detailList) {
                if (dataKey.equals(detail.getDataKey())) {
                    itAmount = itAmount.subtract(detail.getBudInnerMoney().add(detail.getBudOutMoney()));
                }
            }
            if (itAmount.compareTo(BigDecimal.ZERO)<0){
                BigDecimal itAmount1 = itAmount.negate();
                if (budMoney.compareTo(BigDecimal.ZERO)>0){
                    throw new Exception("订单"+dataKey+"本次申请金额不可大于0，请检查！");
                }
                BigDecimal budMoney1 = budMoney.negate();
                if (itAmount1.compareTo(budMoney1) < 0) {
                    throw new Exception("订单"+dataKey+"预付申请金额已大于可申请金额，请检查！");
                }
            }else {
                if (itAmount.compareTo(budMoney) < 0) {
                    throw new Exception("订单"+dataKey+"预付申请金额已大于可申请金额，请检查！");
                }
            }

        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createAdvanceApply(PaymentAdvanceApply headData, List<PaymentAdvanceApplyDetail> pcaDetailList, Integer isPushOa, String adviceFjid) throws Exception {
        String message = "";
        //计算预算外超额
        if(headData.getOaBudOutMoney() != null && headData.getBudOutMoney() != null){
            headData.setOaOutExcess(headData.getOaBudOutMoney().add(headData.getBudOutMoney()));
            //计算预算外超出比
            if(headData.getOaQuaeterBud() != null && headData.getOaQuaeterBud().compareTo(BigDecimal.ZERO) > 0){
                headData.setOaExcessScale(headData.getOaOutExcess().divide(headData.getOaQuaeterBud(), 4, RoundingMode.HALF_UP));
            }
        }
        this.save(headData);

        for (PaymentAdvanceApplyDetail acd : pcaDetailList) {
            acd.setAppId(headData.getId());
            acd.setAppNo(headData.getApApplyNo());
        }

        if (CollectionUtils.isNotEmpty(pcaDetailList)) {
            paymentAdvanceApplyDetailService.saveBatch(pcaDetailList);
        }

        // 更新明细行号
        paymentAdvanceApplyDetailService.updateDetailItemNo(headData.getId());
        //付款申请新增时，需要更新付款计划汇总数据 邓京术 2025-08-13
        String backStr = initPaymentPlanSummary(headData,true);
        if (StringUtils.isNotEmpty(backStr) && backStr.equals("F")) {
//            暂时去掉付款计划的约束，方便测试，上生产时，需要恢复，邓京术 2025-09-25
            throw new Exception("本月付款计划没有发布，请先发布付款计划！");
        }
        return message;
    }

    @Override
    public String editAdvanceApply(PaymentAdvanceApply headData, List<PaymentAdvanceApplyDetail> pcaDetailList, List<Long> delItemIdList, Integer isPushOa, String adviceFjid) {
        //计算预算外超额
        if(headData.getOaBudOutMoney() != null && headData.getBudOutMoney() != null){
            headData.setOaOutExcess(headData.getOaBudOutMoney().add(headData.getBudOutMoney()));
            //计算预算外超出比
            if(headData.getOaQuaeterBud() != null && headData.getOaQuaeterBud().compareTo(BigDecimal.ZERO) > 0){
                headData.setOaExcessScale(headData.getOaOutExcess().divide(headData.getOaQuaeterBud(),4, BigDecimal.ROUND_HALF_UP));
            }
        }
        this.updateById(headData);

        if (CollectionUtils.isNotEmpty(pcaDetailList)) {
//            pcaDetailList.stream().forEach(item ->{
//                initPaymentPlanSummary(item, null, headData, 1);
//            });
            paymentAdvanceApplyDetailService.remove(Wrappers.lambdaQuery(PaymentAdvanceApplyDetail.class).eq(PaymentAdvanceApplyDetail::getAppId, headData.getId()));
            paymentAdvanceApplyDetailService.saveBatch(pcaDetailList);
        }

//        if (CollectionUtils.isNotEmpty(delItemIdList)) {
//            List<PaymentAdvanceApplyDetail> detailList = Lists.newArrayList();
//            for (Long id : delItemIdList) {
//                PaymentAdvanceApplyDetail ad = new PaymentAdvanceApplyDetail();
//                ad.setId(id);
//                ad.setDeleteFlag(1);
//                ad.setItemNo("00000");
//                detailList.add(ad);
//            }
//
//            paymentAdvanceApplyDetailService.updateBatchById(detailList);
//        }

        // 更新明细行号
        paymentAdvanceApplyDetailService.updateDetailItemNo(headData.getId());

        //请款明细有金额有更新时，需要更新付款计划汇总，减少付款计划汇总的本月已请款金额， fin_payment_plan_summary applied_payment_amount 2025-07-30 邓京术
        PaymentAdvanceApply advanceApply = this.getById(headData.getId());
        String ss = initPaymentPlanSummary(advanceApply,false);
        return "";
    }

    @Override
    public String initPaymentPlanSummary(PaymentAdvanceApply headData, Boolean flag) {
        String paymentExpireStr = DateUtils.formatYYYYMMDDDash(headData.getCreateTime());
        PaymentPlan paymentPlan = paymentPlanService.getOne(new LambdaQueryWrapper<PaymentPlan>()
                .eq(PaymentPlan::getPaymentPlanYear, paymentExpireStr.split("-")[0])
                .eq(PaymentPlan::getPaymentPlanMonth, paymentExpireStr.split("-")[1]));
        if (Objects.nonNull(paymentPlan) && paymentPlan.getStatus() == 6) {
            PaymentPlanQuery paymentPlanQuery = new PaymentPlanQuery();
            paymentPlanQuery.setPpId(paymentPlan.getId()+"");
            paymentPlanQuery.setSupplierCode(headData.getSupplierCode());
            paymentPlanQuery.setCompanyCode(headData.getCompanyCode());
            paymentPlanQuery.setPaymentType("S");

            List<PaymentPlanSummary> paymentPlanSummaryList = paymentPlanSummaryService.findPaymentPlanSummaryBySupplier(paymentPlanQuery);
            PaymentPlanSummary paymentPlanSummary = (null != paymentPlanSummaryList && paymentPlanSummaryList.size() > 0)?paymentPlanSummaryList.get(0):null;

            AccountStatementQuery accountStatementQuery = new AccountStatementQuery();
            accountStatementQuery.setSupplierCode(headData.getSupplierCode());
            accountStatementQuery.setCompanyCode(headData.getCompanyCode());
            accountStatementQuery.setAccountStatementYear( paymentExpireStr.split("-")[0] +  paymentExpireStr.split("-")[1]);
            List<AccountStatementPO> accountStatementPOS = accountStatementService.findAccountStatementBySupplierCodeS(accountStatementQuery);
            //计算总额
            final BigDecimal[] budOutMoney = {BigDecimal.ZERO};
            accountStatementPOS.stream().forEach(oo -> {
                if (null != oo.getActualUnpaidAmount()) {
                    budOutMoney[0] = budOutMoney[0].add(oo.getActualUnpaidAmount());
                }
            });

            if (Objects.nonNull(paymentPlanSummary)) {
                //本月已请款金额
                paymentPlanSummary.setAppliedPaymentAmount(budOutMoney[0]);
                if (paymentPlanSummary.getActualPayAmount() != null && paymentPlanSummary.getActualPayAmount().floatValue() > 0) {
                    paymentPlanSummary.setDifferenceAmount(paymentPlanSummary.getAppliedPaymentAmount().subtract(paymentPlanSummary.getActualPayAmount()));
                    MathContext mc = new MathContext(4, RoundingMode.HALF_UP);
                    BigDecimal result = paymentPlanSummary.getDifferenceAmount().divide(paymentPlanSummary.getActualPayAmount(), mc);
                    paymentPlanSummary.setDifferencePercent(result);
                } else {
                    paymentPlanSummary.setDifferenceAmount(paymentPlanSummary.getAppliedPaymentAmount());
                    paymentPlanSummary.setDifferencePercent(new BigDecimal(1.00));
                }

                //如果是新增的情况。拼接备注
                if (null != paymentPlanSummary.getNewSign() && "X".equals(paymentPlanSummary.getNewSign()) && flag) {
                    String remark = paymentPlanSummary.getRemark();
                    if (StringUtils.isEmpty(remark)) {
                        paymentPlanSummary.setRemark(headData.getApplyRemark() == null?"":headData.getApplyRemark());
                    } else {
                        paymentPlanSummary.setRemark(paymentPlanSummary.getRemark() + "," + headData.getApplyRemark());
                    }
                }
                paymentPlanSummaryService.saveOrUpdate(paymentPlanSummary);
            } else {
                paymentPlanSummary = new PaymentPlanSummary();
                paymentPlanSummary.setPaymentPlanNo(paymentPlan.getPaymentPlanNo());
                paymentPlanSummary.setPaymentPlanId(paymentPlan.getId());
                paymentPlanSummary.setSupplierCode(headData.getSupplierCode());
                paymentPlanSummary.setSupplierName(headData.getSupplierName());
                paymentPlanSummary.setCurrencyCode(headData.getCurrencyCode());
                paymentPlanSummary.setCompanyCode(headData.getCompanyCode());
                paymentPlanSummary.setCompanyName(headData.getCompanyName());
                //应付金额
                paymentPlanSummary.setActualPayAmount(BigDecimal.ZERO);
                paymentPlanSummary.setAppliedUnpaidAmount(BigDecimal.ZERO);

                paymentPlanSummary.setAppliedPaymentAmount(budOutMoney[0]);
                paymentPlanSummary.setDifferenceAmount(paymentPlanSummary.getAppliedPaymentAmount());
                paymentPlanSummary.setDifferencePercent(new BigDecimal(1.00));

                paymentPlanSummary.setPaymentType("S");
                paymentPlanSummary.setPaidAmountMonth(BigDecimal.ZERO);
                paymentPlanSummary.setCreateUserId(LoginUtil.getUserId());
                paymentPlanSummary.setCreateUserCode(LoginUtil.getAccount());
                paymentPlanSummary.setCreateUserName(LoginUtil.getName());
                paymentPlanSummary.setCreateTime(LocalDateTime.now());
                paymentPlanSummary.setNewSign("X");
                paymentPlanSummary.setRemark(headData.getApplyRemark());
                paymentPlanSummaryService.saveOrUpdate(paymentPlanSummary);
            }
            return "S";
        //如果
        } else if (flag) {
            return "F";
        }
        return null;
    }

    @Override
    public List<PaymentAdvanceApplyDetail> findDataBySupplierAndPoCodeAndAmount(String supplierCode, String poCode, BigDecimal budOutMoney,Long id) {
        return baseMapper.findDataBySupplierAndPoCodeAndAmount(supplierCode, poCode, budOutMoney,id);
    }

    @Override
    public BigDecimal getUnCleared1(String supplierCode) {
        return baseMapper.getUnCleared1(supplierCode);
    }

    @Override
    public BigDecimal getUnCleared2(String supplierCode) {
        return baseMapper.getUnCleared2(supplierCode);
    }

    @Override
    public BigDecimal getUnCleared3(String supplierCode) {
        return baseMapper.getUnCleared3(supplierCode);
    }

    @Override
    public BigDecimal getUnCleared4(String supplierCode) {
        return baseMapper.getUnCleared4(supplierCode);
    }

    @Override
    public BigDecimal getInPaymentAmount(String supplierCode,String companyCode) {
        return baseMapper.getInPaymentAmount(supplierCode,companyCode);
    }

    @Override
    public void initPaymentPlanSummary(PaymentAdvanceApply headData, int action) {
        List<PaymentAdvanceApplyDetail> detailList = paymentAdvanceApplyDetailService.findByPaymentDetail(headData.getId());
        //3 废弃相当于删除操作.
        if (action == 3) {
            detailList.stream().forEach(item -> {
//                initPaymentPlanSummary(null, item, headData, action);
            });
            //付款申请oa审批通过，付款计划，本月已付款金额需要同步修改。
        } else {
            detailList.stream().forEach(item -> {
                initPaymentPlanSummary(null, item, headData, action);
            });
        }
    }

    public void initPaymentPlanSummary(PaymentAdvanceApplyDetail codApplyDetailAdd, PaymentAdvanceApplyDetail codApplyDetailDel, PaymentAdvanceApply headData, int action) {
        //查询对账单的付款到期日
        //请款明细有金额有新增时，需要更新付款计划汇总，增加付款计划汇总的本月已请款金额， fin_payment_plan_summary applied_payment_amount 2025-07-30 邓京术
        //取本次申请金额
        BigDecimal budOutMoney = codApplyDetailAdd == null ? codApplyDetailDel.getBudOutMoney():codApplyDetailAdd.getBudOutMoney();
        //先根据供应商及公司，付款年月 去付款汇总表中查询是否有付款计划汇总，如果没有则要新增一笔付款计划汇总
        Calendar cl = Calendar.getInstance();
        String paymentExpireStr = DateUtils.formatYYYYMMDDDash(cl.getTime());
        PaymentPlan paymentPlan = paymentPlanService.getOne(new LambdaQueryWrapper<PaymentPlan>()
                .eq(PaymentPlan::getPaymentPlanYear, paymentExpireStr.split("-")[0])
                .eq(PaymentPlan::getPaymentPlanMonth, paymentExpireStr.split("-")[1]));
        if (Objects.nonNull(paymentPlan) && paymentPlan.getStatus() == 6) {
            PaymentPlanQuery paymentPlanQuery = new PaymentPlanQuery();
            paymentPlanQuery.setPpId(paymentPlan.getId()+"");
            paymentPlanQuery.setSupplierCode(headData.getSupplierCode());
            paymentPlanQuery.setCompanyCode(headData.getCompanyCode());
            paymentPlanQuery.setPaymentType("S");
            List<PaymentPlanSummary> paymentPlanSummaryList = paymentPlanSummaryService.findPaymentPlanSummaryBySupplier(paymentPlanQuery);
            PaymentPlanSummary paymentPlanSummary = (null != paymentPlanSummaryList && paymentPlanSummaryList.size() > 0)?paymentPlanSummaryList.get(0):null;

            if (Objects.nonNull(paymentPlanSummary)) {
                //删除操作
                if (action == 3) {
                    //设置本月已付款金额(本月已请款金额)
                    paymentPlanSummary.setAppliedPaymentAmount(
                            paymentPlanSummary.getAppliedPaymentAmount() == null ? BigDecimal.ZERO : paymentPlanSummary.getAppliedPaymentAmount().subtract(budOutMoney)
                    );
                    //设置差异比例
                    if (paymentPlanSummary.getActualPayAmount() != null && paymentPlanSummary.getActualPayAmount().floatValue() > 0) {
                        paymentPlanSummary.setDifferenceAmount(paymentPlanSummary.getAppliedPaymentAmount().subtract(paymentPlanSummary.getActualPayAmount()));
                        MathContext mc = new MathContext(4, RoundingMode.HALF_UP);
                        BigDecimal result = paymentPlanSummary.getDifferenceAmount().divide(paymentPlanSummary.getActualPayAmount(), mc);
                        paymentPlanSummary.setDifferencePercent(result);
                    } else {
                        paymentPlanSummary.setDifferenceAmount(paymentPlanSummary.getAppliedPaymentAmount());
                        paymentPlanSummary.setDifferencePercent(new BigDecimal(1.00));
                    }
                } else if (action == 1) {
                    //如果请款金额与之前的保持一致，则不做任何动作
                    if (null != codApplyDetailDel && budOutMoney.floatValue() != codApplyDetailDel.getBudOutMoney().floatValue()) {  //这一操作不需要了.
                        //设置本月已付款金额(本月已请款金额)
//                        paymentPlanSummary.setAppliedPaymentAmount(
//                                paymentPlanSummary.getAppliedPaymentAmount() == null ? budOutMoney : paymentPlanSummary.getAppliedPaymentAmount().add(budOutMoney)
//                        );
//                        //设置差异比例
//                        if (paymentPlanSummary.getActualPayAmount() != null && paymentPlanSummary.getActualPayAmount().floatValue() > 0) {
//                            paymentPlanSummary.setDifferenceAmount(paymentPlanSummary.getAppliedPaymentAmount().subtract(paymentPlanSummary.getActualPayAmount()));
//                            paymentPlanSummary.setDifferencePercent(paymentPlanSummary.getDifferenceAmount().divide(paymentPlanSummary.getActualPayAmount(), 2));
//                        } else {
//                            paymentPlanSummary.setDifferenceAmount(paymentPlanSummary.getAppliedPaymentAmount());
//                            paymentPlanSummary.setDifferencePercent(new BigDecimal(1.00));
//                        }
//                        if (null != paymentPlanSummary.getNewSign() && "X".equals(paymentPlanSummary.getNewSign())) {
//                            paymentPlanSummary.setRemark(paymentPlanSummary.getRemark() + "\n\r" + headData.getApplyRemark());
//                        }
//
//                        //设置本月已付款金额(本月已请款金额)
//                        paymentPlanSummary.setAppliedPaymentAmount(
//                                paymentPlanSummary.getAppliedPaymentAmount() == null ? BigDecimal.ZERO : paymentPlanSummary.getAppliedPaymentAmount().subtract(codApplyDetailDel.getBudOutMoney())
//                        );
//                        //设置差异比例
//                        if (paymentPlanSummary.getActualPayAmount() != null && paymentPlanSummary.getActualPayAmount().floatValue() > 0) {
//                            paymentPlanSummary.setDifferenceAmount(paymentPlanSummary.getAppliedPaymentAmount().subtract(paymentPlanSummary.getActualPayAmount()));
//                            paymentPlanSummary.setDifferencePercent(paymentPlanSummary.getDifferenceAmount().divide(paymentPlanSummary.getActualPayAmount(), 2));
//                        } else {
//                            paymentPlanSummary.setDifferenceAmount(paymentPlanSummary.getAppliedPaymentAmount());
//                            paymentPlanSummary.setDifferencePercent(new BigDecimal(1.00));
//                        }
//                        paymentPlanSummaryService.saveOrUpdate(paymentPlanSummary);
                        //如果没有修改，则只做新增操作
                    } else {
                        //设置本月已付款金额(本月已请款金额) 新增的时候
                        paymentPlanSummary.setAppliedPaymentAmount(
                                paymentPlanSummary.getAppliedPaymentAmount() == null ? budOutMoney : paymentPlanSummary.getAppliedPaymentAmount().add(budOutMoney)
                        );
                        //设置差异比例
                        if (paymentPlanSummary.getActualPayAmount() != null && paymentPlanSummary.getActualPayAmount().floatValue() > 0) {
                            paymentPlanSummary.setDifferenceAmount(paymentPlanSummary.getAppliedPaymentAmount().subtract(paymentPlanSummary.getActualPayAmount()));
                            MathContext mc = new MathContext(4, RoundingMode.HALF_UP);
                            BigDecimal result = paymentPlanSummary.getDifferenceAmount().divide(paymentPlanSummary.getActualPayAmount(), mc);
                            paymentPlanSummary.setDifferencePercent(result);
//                            paymentPlanSummary.setDifferencePercent(paymentPlanSummary.getDifferenceAmount().divide(paymentPlanSummary.getActualPayAmount(), 2));
                        } else {
                            paymentPlanSummary.setDifferenceAmount(paymentPlanSummary.getAppliedPaymentAmount());
                            paymentPlanSummary.setDifferencePercent(new BigDecimal(1.00));
                        }
                        if (null != paymentPlanSummary.getNewSign() && "X".equals(paymentPlanSummary.getNewSign())) {
                            paymentPlanSummary.setRemark(paymentPlanSummary.getRemark() + "\n\r" + headData.getApplyRemark());
                        }
                        paymentPlanSummaryService.saveOrUpdate(paymentPlanSummary);
                    }
                } else if (action == 4) {
                    //更新本月已付款金额
                    paymentPlanSummary.setPaidAmountMonth(paymentPlanSummary.getPaidAmountMonth() == null ? budOutMoney : paymentPlanSummary.getPaidAmountMonth().add(budOutMoney));
                    paymentPlanSummaryService.saveOrUpdate(paymentPlanSummary);
                }
            } else if (action == 1){
                paymentPlanSummary = new PaymentPlanSummary();
                paymentPlanSummary.setPaymentPlanNo(paymentPlan.getPaymentPlanNo());
                paymentPlanSummary.setPaymentPlanId(paymentPlan.getId());
                paymentPlanSummary.setSupplierCode(headData.getSupplierCode());
                paymentPlanSummary.setSupplierName(headData.getSupplierName());
                paymentPlanSummary.setCurrencyCode(headData.getCurrencyCode());
                paymentPlanSummary.setCompanyCode(headData.getCompanyCode());
                paymentPlanSummary.setCompanyName(headData.getCompanyName());
                //应付金额
                paymentPlanSummary.setActualPayAmount(BigDecimal.ZERO);
                paymentPlanSummary.setAppliedUnpaidAmount(BigDecimal.ZERO);

                paymentPlanSummary.setAppliedPaymentAmount(budOutMoney);
                paymentPlanSummary.setDifferenceAmount(paymentPlanSummary.getAppliedPaymentAmount());
                paymentPlanSummary.setDifferencePercent(new BigDecimal(1.00));


                paymentPlanSummary.setPaidAmountMonth(BigDecimal.ZERO);
                paymentPlanSummary.setCreateUserId(LoginUtil.getUserId());
                paymentPlanSummary.setCreateUserCode(LoginUtil.getAccount());
                paymentPlanSummary.setCreateUserName(LoginUtil.getName());
                paymentPlanSummary.setCreateTime(LocalDateTime.now());
                paymentPlanSummary.setNewSign("X");
                paymentPlanSummary.setPaymentType("S");
                paymentPlanSummary.setRemark(headData.getApplyRemark());
                paymentPlanSummaryService.saveOrUpdate(paymentPlanSummary);
            }
        }
    }

    @Override
    public PaymentAdvanceApplyPO selectById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public List<PaymentAdvanceApply> findDataStatusByIdList(List<Long> applyIdList) {
        return baseMapper.findDataStatusByIdList(applyIdList);
    }

    @Override
    public List<CreatePcaDetailPO> findByQuery(PaymentApplyItemQuery query) {
        return baseMapper.findByQuery(query);
    }

    @Override
    public List<PaymentAdvanceRefundTransferApplyPO> paaOrderListByPage(Page<PaymentAdvanceRefundTransferApplyPO> dbPage, PaymentApplyHeadQuery query) {
        if(dbPage == null){
            return baseMapper.selectPaaOrderListByPage(query);
        }
        return baseMapper.selectPaaOrderListByPage(dbPage,query);
    }
}
