package com.ruoyi.dk.controller.app;

import cn.hutool.core.util.NumberUtil;
import com.ruoyi.dk.domain.AgreementSetting;
import com.ruoyi.dk.domain.HomeSetting;
import com.ruoyi.dk.domain.LoansSetting;
import com.ruoyi.dk.dto.app.resp.LoansSettingVO;
import com.ruoyi.dk.enums.BankTypeEnums;
import com.ruoyi.dk.service.AgreementSettingService;
import com.ruoyi.dk.service.HomeSettingService;
import com.ruoyi.dk.service.LoansSettingService;
import com.ruoyi.component.core.domain.R;
import com.ruoyi.component.core.util.BeanConvertUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/app/home/setting")
@Tag(name = "设置信息")
public class AppSettingController {

    @Autowired
    private AgreementSettingService agreementSettingService;
    @Autowired
    private HomeSettingService homeSettingService;
    @Autowired
    private LoansSettingService loansSettingService;

    @GetMapping("/agreement")
    @Operation(summary = "协议内容")
    public R<AgreementSetting> agreement() {
        AgreementSetting setting = agreementSettingService.getAgreementSetting();
        return R.ok(setting);
    }

    @GetMapping("/home")
    @Operation(summary = "常规内容")
    public R<HomeSetting> home() {
        HomeSetting setting = homeSettingService.getHomeSetting();
        return R.ok(setting);
    }

    @GetMapping("/bankType")
    @Operation(summary = "获取银行列表")
    public R<List<String>> bankType(){
        List<String> list = Arrays.stream(BankTypeEnums.values()).map(BankTypeEnums::getName).collect(Collectors.toList());
        return R.ok(list);
    }

    @GetMapping("/loans")
    @Operation(summary = "贷款信息")
    public R<LoansSettingVO> loans() {
        LoansSetting setting = loansSettingService.getLoansSetting();
        LoansSettingVO vo = BeanConvertUtil.convertTo(setting, LoansSettingVO::new);
        Double minDayServiceRate = 0D;
        Double minMonthRate = Arrays.stream(setting.getServiceRate().split(","))
                .map(Double::valueOf).min(Double::compareTo).orElse(null);
        vo.setMinMonthRate(minMonthRate);
        if(minMonthRate != null){
            minDayServiceRate = NumberUtil.div(minMonthRate, new Double(30D), 4);
        }
        vo.setMinDayServiceRate(minDayServiceRate);
        return R.ok(vo);
    }

}
