package com.jwipc.finance.logic.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.jwipc.finance.convert.PaymentAdvanceCodConvert;
import com.jwipc.finance.dto.CreatePcaDetailDto;
import com.jwipc.finance.logic.PaymentAdvanceCodDetailLogic;
import com.jwipc.finance.model.po.CreatePcaDetailPO;
import com.jwipc.finance.service.IPaymentCodApplyDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *
 * </p>
 *
 * @author wt
 * @version 1.0
 * @since 2024/3/8
 */
@Service
@RequiredArgsConstructor
public class PaymentAdvanceCodDetailLogicImpl implements PaymentAdvanceCodDetailLogic {

    private final IPaymentCodApplyDetailService paymentCodApplyDetailService;
    private final PaymentAdvanceCodConvert paymentAdvanceCodConvert;
    @Override
    public List<CreatePcaDetailDto> findItemDateById(Long applyId) {
        List<CreatePcaDetailPO> poList = paymentCodApplyDetailService.findItemDateById(applyId);
        //查找已经付款的明细
        //根据已获取的过账数据筛选已经货到付款的数据
        List<CreatePcaDetailPO> alreadyPoData = paymentCodApplyDetailService.findByApplyCodeAndItem(poList,applyId);

        //数据转换
        List<CreatePcaDetailDto> list = paymentAdvanceCodConvert.POToDtoDetailList(poList);
        List<CreatePcaDetailDto> alreadyData = paymentAdvanceCodConvert.POToDtoDetailList(alreadyPoData);
        //处理数据
        if(CollectionUtils.isNotEmpty(list)){
            for(CreatePcaDetailDto detailDto:list){
                if("Z003".equals(detailDto.getAmountType())){
                    if(CollectionUtils.isNotEmpty(alreadyData)){
                        for(CreatePcaDetailDto dto:alreadyData){
                            if(detailDto.getDataKey().equals(dto.getDataKey())){
                                //已申请金额
                                detailDto.setAlreadyApplyAmount(detailDto.getAlreadyApplyAmount().add(dto.getBudOutMoney()).add(dto.getBudInnerMoney()));
                                //已付款金额
                                if(dto.getStatus() == 5){
                                    detailDto.setAlreadyPaymentAmount(detailDto.getAlreadyPaymentAmount().add(dto.getBudOutMoney()).add(dto.getBudInnerMoney()));
                                }
                                break;
                            }
                        }
                    }
                    detailDto.setApplyAmount(detailDto.getItAmount().subtract(detailDto.getAlreadyApplyAmount() == null? BigDecimal.ZERO:detailDto.getAlreadyApplyAmount()));
//                    detailDto.setCurrApAmount(detailDto.getApplyAmount());
                }else{
                    detailDto.setAlreadyApplyAmount(detailDto.getBudInnerMoney().add(detailDto.getBudOutMoney()));
                    detailDto.setAlreadyPaymentAmount(detailDto.getBudInnerMoney().add(detailDto.getBudOutMoney()));
                }
            }
        }
        return list;
    }

    @Override
    public List<CreatePcaDetailDto> findPaymentDetailByPoCodeList(List<String> poCodeList) {
        List<CreatePcaDetailPO> poList = paymentCodApplyDetailService.findPaymentDetailByPoCodeList(poCodeList);
        return paymentAdvanceCodConvert.POToDtoDetailList(poList);
    }
}
