package com.jwipc.finance.logic.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.finance.convert.PaymentAdvanceRefundTransferApplyConvert;
import com.jwipc.finance.dto.PaymentAdvanceRefundTransferApplyAndDetailListDTO;
import com.jwipc.finance.entity.PaymentAdvanceRefundTransferApplyDetail;
import com.jwipc.finance.logic.PaymentAdvanceRefundTransferApplyDetailLogic;
import com.jwipc.finance.model.po.PaymentAdvanceRefundTransferApplyDetailPO;
import com.jwipc.finance.service.IPaymentAdvanceRefundTransferApplyDetailService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

/**
 * @author qzc
 * @date 2025-07-21
 */
@Service
@RequiredArgsConstructor
public class PaymentAdvanceRefundTransferApplyDetailLogicImpl implements PaymentAdvanceRefundTransferApplyDetailLogic {
    private final IPaymentAdvanceRefundTransferApplyDetailService paymentAdvanceRefundTransferApplyDetailService;
    private final PaymentAdvanceRefundTransferApplyConvert paymentAdvanceRefundTransferApplyConvert;

    @Override
    public PageView findRefundTransferApplyDetailByRefundId(Long applyId, Integer dataShowType) {

        List<PaymentAdvanceRefundTransferApplyDetailPO> poList = paymentAdvanceRefundTransferApplyDetailService.findRefundTransferApplyDetailByRefundId(applyId);
        List<PaymentAdvanceRefundTransferApplyAndDetailListDTO> detailDtoList = paymentAdvanceRefundTransferApplyConvert.POToDtoDetailList(poList);
        Page<PaymentAdvanceRefundTransferApplyAndDetailListDTO> toPage = new Page<>(1, detailDtoList.size());
        toPage.setRecords(detailDtoList);
        BigDecimal theRegistTaxAmount = BigDecimal.ZERO, alreadyApplyAmount = BigDecimal.ZERO;
        PageView pageView = PageUtil.getPV(detailDtoList, toPage.getTotal());
        for (PaymentAdvanceRefundTransferApplyAndDetailListDTO dto : toPage.getRecords()) {
            theRegistTaxAmount = theRegistTaxAmount.add(dto.getTheRegistTaxAmount());
            alreadyApplyAmount = alreadyApplyAmount.add(dto.getAlreadyApplyAmount() == null ? BigDecimal.ZERO : dto.getAlreadyApplyAmount());
        }
        //加入千分位保留2位小数
        DecimalFormat df = new DecimalFormat("###,###.##");
        //这里需要增加页面页脚汇总金额
        JSONArray footer = new JSONArray();
        JSONObject obj = new JSONObject();
        obj.put("poCode", "合计");
        obj.put("theRegistTaxAmount", df.format(theRegistTaxAmount));//本次登记含税金额
        obj.put("alreadyApplyAmount", df.format(alreadyApplyAmount));//已冲账含税金额
        footer.add(obj);
        pageView.setFooter(footer);
        return pageView;
    }

    @Override
    public List<PaymentAdvanceRefundTransferApplyAndDetailListDTO> findRefundTransferApplyDetailByDateKey(List<String> dataKeys) {
        List<PaymentAdvanceRefundTransferApplyDetail> refundTransferApplyDetailByDateKey = paymentAdvanceRefundTransferApplyDetailService.findRefundTransferApplyDetailByDateKey(dataKeys);
        return paymentAdvanceRefundTransferApplyConvert.detailToDTOList(refundTransferApplyDetailByDateKey);
    }

    @Override
    public List<PaymentAdvanceRefundTransferApplyAndDetailListDTO> findRefundTransferApplyDataTypeInfoByDateKey(List<String> dataKeys) {
        List<PaymentAdvanceRefundTransferApplyDetail> refundTransferApplyDetailByDateKey = paymentAdvanceRefundTransferApplyDetailService.findRefundTransferApplyDataTypeInfoByDateKey(dataKeys);
        return paymentAdvanceRefundTransferApplyConvert.detailToDTOList(refundTransferApplyDetailByDateKey);
    }

    @Override
    public List<PaymentAdvanceRefundTransferApplyAndDetailListDTO> findRefundTApplyDataByPoCodeAndItemDateKey(List<String> dataKeys) {
        List<PaymentAdvanceRefundTransferApplyDetail> refundTransferApplyDetailByDateKey = paymentAdvanceRefundTransferApplyDetailService.findRefundTApplyDataByPoCodeAndItemDateKey(dataKeys);
        return paymentAdvanceRefundTransferApplyConvert.detailToDTOList(refundTransferApplyDetailByDateKey);
    }

    @Override
    public List<PaymentAdvanceRefundTransferApplyAndDetailListDTO> findRefundTransferApplyDetailByReferOrderKey(List<String> dataKeys) {
        List<PaymentAdvanceRefundTransferApplyDetail> refundTransferApplyDetailByReferOrderKey = paymentAdvanceRefundTransferApplyDetailService.findRefundTransferApplyDetailByReferOrderKey(dataKeys);
        return paymentAdvanceRefundTransferApplyConvert.detailToDTOList(refundTransferApplyDetailByReferOrderKey);

    }

    @Override
    public List<PaymentAdvanceRefundTransferApplyAndDetailListDTO> findRefundTransferApplyDetailSumRefundAmountByDateKey(List<String> prepaymentKeyword) {
        List<PaymentAdvanceRefundTransferApplyDetail> refundTransferApplyDetailSumRefundAmountByDateKey = paymentAdvanceRefundTransferApplyDetailService.findRefundTransferApplyDetailSumRefundAmountByDateKey(prepaymentKeyword);
        return paymentAdvanceRefundTransferApplyConvert.detailToDTOList(refundTransferApplyDetailSumRefundAmountByDateKey);
    }

    @Override
    public List<PaymentAdvanceRefundTransferApplyAndDetailListDTO> findRefundTransferApplyDataTypeInfoByPoCodeAndItem(List<String> poCodeAndItem) {
        List<PaymentAdvanceRefundTransferApplyDetail> refundTransferApplyDetailByDateKey = paymentAdvanceRefundTransferApplyDetailService.findRefundTransferApplyDataTypeInfoByPoCodeAndItem(poCodeAndItem);
        return paymentAdvanceRefundTransferApplyConvert.detailToDTOList(refundTransferApplyDetailByDateKey);
    }

    @Override
    public List<PaymentAdvanceRefundTransferApplyAndDetailListDTO> findRefundTransferApplyDetailDateKey(Integer dataType, List<String> prepaymentKeyword, List<String> dataKeys) {
        List<PaymentAdvanceRefundTransferApplyDetail> refundTransferApplyDetailSumRefundAmountByDateKey = paymentAdvanceRefundTransferApplyDetailService.findRefundTransferApplyDetailDateKey(dataType,prepaymentKeyword,dataKeys);
        return paymentAdvanceRefundTransferApplyConvert.detailToDTOList(refundTransferApplyDetailSumRefundAmountByDateKey);
    }


}
