package com.sunxung.factoring.service.financing.payment.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.exception.ValidatorException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.entity.financing.payment.PaymentApplyHis;
import com.sunxung.factoring.entity.financing.payment.PaymentConditionFileRefHis;
import com.sunxung.factoring.entity.financing.payment.PaymentConditionHis;
import com.sunxung.factoring.entity.financing.payment.PaymentInfo;
import com.sunxung.factoring.entity.financing.review.ReviewFinancingInfo;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.financing.payment.PaymentApplyHisMapper;
import com.sunxung.factoring.service.financing.payment.IPaymentApplyHisService;
import com.sunxung.factoring.service.financing.payment.IPaymentConditionFileRefHisService;
import com.sunxung.factoring.service.financing.payment.IPaymentConditionHisService;
import com.sunxung.factoring.service.financing.payment.IPaymentInfoService;
import com.sunxung.factoring.service.financing.review.ReviewFinancingInfoService;
import com.sunxung.factoring.service.sys.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Description :
 * @Author : L.Jia
 * @Date: 2024-02-28 15:17
 */
@Service("payment_applyService")
public class PaymentApplyHisServiceImpl extends ServiceImpl<PaymentApplyHisMapper, PaymentApplyHis>
        implements IPaymentApplyHisService {

    @Autowired
    private IPaymentInfoService paymentInfoService;
    @Autowired
    private IPaymentConditionHisService paymentConditionHisService;
    @Autowired
    private IPaymentConditionFileRefHisService paymentConditionFileRefHisService;
    @Autowired
    private ReviewFinancingInfoService reviewFinancingInfoService;
    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public PaymentApplyHis getView(Long id) {

        PaymentApplyHis paymentApplyHis = getById(id);
        if (paymentApplyHis == null || paymentApplyHis.getPaymentInfoId() == null) {
            throw new ValidatorException(CodeUtil.FAIL, "查询不到对应的付款申请历史信息！");
        }
        PaymentInfo paymentInfo = paymentInfoService.getById(paymentApplyHis.getPaymentInfoId());
        if (paymentInfo == null) {
            throw new ValidatorException(CodeUtil.FAIL, "查询不到对应的付款信息！");
        }

        ReviewFinancingInfo reviewFinancingInfo = reviewFinancingInfoService.getOne(new QueryWrapper<ReviewFinancingInfo>()
                .eq("review_quota_disburse_id", paymentInfo.getQuotaDisburseId()));
        if (reviewFinancingInfo != null) {
            paymentApplyHis.setFinancingRateStr(reviewFinancingInfo.getFinancingRate().multiply(new BigDecimal("100")) + "%");
            paymentApplyHis.setManagementFeeRateStr(reviewFinancingInfo.getManagementFeeRatio().multiply(new BigDecimal("100")) + "%");
            paymentApplyHis.setInterestCalculate(reviewFinancingInfo.getInterestCalculate());
            paymentApplyHis.setPayBackMethod(reviewFinancingInfo.getPayBackMethod());
        }

        List<PaymentConditionHis> conditionHisList = paymentConditionHisService.list(new QueryWrapper<PaymentConditionHis>()
                .eq("payment_apply_his_id", paymentApplyHis.getId()));
        if (CollectionUtil.isNotEmpty(conditionHisList)){
            for (PaymentConditionHis conditionHis : conditionHisList) {
                List<PaymentConditionFileRefHis> conditionFileRefs = paymentConditionFileRefHisService.list(new QueryWrapper<PaymentConditionFileRefHis>()
                        .eq("payment_condition_his_id", conditionHis.getId()));
                List<FileStorage> exemptionFile = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(conditionFileRefs)) {
                    for (PaymentConditionFileRefHis conditionFileRef : conditionFileRefs) {
                        FileStorage fileStorage = fileStorageService.get(conditionFileRef.getFileId());
                        exemptionFile.add(fileStorage);
                    }
                }
                conditionHis.setExemptionFile(exemptionFile);
            }
        }
        paymentApplyHis.setConditionList(conditionHisList);
        return paymentApplyHis;
    }
}
