package com.ruoyi.dk.kit;

import com.ruoyi.dk.domain.LoansSetting;
import com.ruoyi.dk.dto.app.req.CalLoanReq;
import com.ruoyi.dk.service.LoansSettingService;
import com.ruoyi.dk.util.ACPIMLoanCalculator;
import com.ruoyi.dk.util.Loan;
import com.ruoyi.dk.util.LoanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class CalLoanManager {
    @Autowired
    private LoansSettingService loansSettingService;

    public Loan calLoan(BigDecimal totalLoanMoney,Integer totalMonth, Double loanRate){
        ACPIMLoanCalculator calculator = new ACPIMLoanCalculator();
        return calculator.calLoan(totalLoanMoney, totalMonth, loanRate, LoanUtil.RATE_TYPE_MONTH);
    }

    public Loan calLoan(CalLoanReq calLoanReq){
        Double loanRate = getLoanRate(calLoanReq.getTotalMonth());
        return this.calLoan(calLoanReq.getTotalLoanMoney(), calLoanReq.getTotalMonth(), loanRate);
    }

    public Double getLoanRate(Integer mouth){
        LoansSetting loansSetting = loansSettingService.getLoansSetting();
        String serviceRate = loansSetting.getServiceRate();
        String[] split = serviceRate.split(",");
        if(split.length < mouth){
            return Double.valueOf(split[split.length-1]);
        }else{
            return Double.valueOf(split[mouth-1]);
        }
    }

    public static void main(String[] args) {
        ACPIMLoanCalculator calculator = new ACPIMLoanCalculator();
        Loan loan = calculator.calLoan(BigDecimal.valueOf(20000), 3, 0.5, LoanUtil.RATE_TYPE_MONTH);
        System.out.println(loan);
    }

}
