
package com.ruoyi.dk.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.RandomUtil;
import com.ruoyi.dk.domain.HomeSetting;
import com.ruoyi.dk.domain.LoansSetting;
import com.ruoyi.dk.dto.app.req.CalLoanReq;
import com.ruoyi.dk.dto.app.resp.CalLoanResp;
import com.ruoyi.dk.dto.app.resp.DefaultLocalSetting;
import com.ruoyi.dk.dto.app.resp.LoanUser;
import com.ruoyi.dk.kit.CalLoanManager;
import com.ruoyi.dk.service.HomeSettingService;
import com.ruoyi.dk.service.LoansSettingService;
import com.ruoyi.dk.util.Loan;
import com.ruoyi.dk.util.PhoneRandomUtil;
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.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Random;

@RestController
@RequestMapping("/app/home/loans")
@Tag(name = "首页开放")
@SaIgnore
public class AppHomeController {

    @Autowired
    private CalLoanManager calLoanManager;
    @Autowired
    private LoansSettingService loansSettingService;
    @Autowired
    private HomeSettingService homeSettingService;

    @GetMapping("/defaultLocal")
    @Operation(summary = "获取默认语言")
    public R<DefaultLocalSetting> defaultLocal(){
        HomeSetting homeSetting = homeSettingService.getHomeSetting();
        String defaultLocal = homeSetting.getDefaultLocal();
        if(StringUtils.isEmpty(defaultLocal)){
            defaultLocal = "zh_CN";
        }
        DefaultLocalSetting defaultLocalSetting = new DefaultLocalSetting();
        defaultLocalSetting.setDefaultLocal(defaultLocal);
        defaultLocalSetting.setDefaultCoinUnit(homeSetting.getDefaultCoinUnit());
        defaultLocalSetting.setOpenWithdrawCode(homeSetting.getOpenWithdrawCode());
        return R.ok(defaultLocalSetting);
    }

    @PostMapping("/calLoan")
    @Operation(summary = "计算每月还款")
    public R<CalLoanResp> calLoan(@RequestBody CalLoanReq calLoanReq) {
        if(calLoanReq.getTotalLoanMoney() == null || calLoanReq.getTotalMonth() == null){
            LoansSetting loansSetting = loansSettingService.getLoansSetting();
            if(calLoanReq.getTotalLoanMoney() == null){
                calLoanReq.setTotalLoanMoney(loansSetting.getLoansInitAccount());
            }
            if(calLoanReq.getTotalMonth() == null){
                calLoanReq.setTotalMonth(Integer.valueOf(loansSetting.getLoansInitMonth()));
            }
        }
        Loan loan = calLoanManager.calLoan(calLoanReq);
        CalLoanResp calLoanResp = BeanConvertUtil.convertTo(loan, CalLoanResp::new);
        calLoanResp.setDefaultCoinUnit(homeSettingService.getHomeSetting().getDefaultCoinUnit());
        return R.ok(calLoanResp);
    }


    @GetMapping("/loansUser")
    @Operation(summary = "获取随机贷款用户")
    public R<LoanUser> loansUser() {
        Random random = new Random();
        HomeSetting homeSetting = homeSettingService.getHomeSetting();
        LoanUser loanUser = new LoanUser();
        loanUser.setPhone(PhoneRandomUtil.gen(homeSetting.getRandomPhoneVersion()));
        loanUser.setAmount(RandomUtil.randomInt(10,100)+"000");
        loanUser.setDefaultCoinUnit(homeSetting.getDefaultCoinUnit());
        loanUser.setTime(LocalDate.now().plusDays(-random.nextInt(7)).format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
        return R.ok(loanUser);
    }

}
