package com.ruoyi.dk.controller.app;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.component.mybatis.core.page.PageQuery;
import com.ruoyi.component.mybatis.core.page.TableDataInfo;
import com.ruoyi.dk.domain.Borrow;
import com.ruoyi.dk.domain.HomeSetting;
import com.ruoyi.dk.dto.admin.resp.BorrowAdminVO;
import com.ruoyi.dk.dto.app.req.BorrowStartReq;
import com.ruoyi.dk.dto.app.resp.BorrowInfo;
import com.ruoyi.dk.dto.app.resp.LoanProcessResp;
import com.ruoyi.dk.service.BorrowService;
import com.ruoyi.dk.service.HomeSettingService;
import com.ruoyi.component.core.domain.R;
import com.ruoyi.component.satoken.utils.LoginHelper;
import com.ruoyi.component.core.util.BeanConvertUtil;
import com.ruoyi.component.core.util.MessageUtils;
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.*;

import java.util.List;

@RestController
@RequestMapping("/app/borrow")
@Tag(name = "贷款相关的接口")
public class AppBorrowController {

    @Autowired
    private BorrowService borrowService;
    @Autowired
    private HomeSettingService homeSettingService;

    @PostMapping("/start")
    @Operation(summary = "发起贷款")
    public R<Borrow> start(@RequestBody BorrowStartReq req){
        Long userId = LoginHelper.getUserId();
        req.setCustomerId(userId);
        Borrow borrow = borrowService.borrow(req);
        return R.ok(borrow);
    }

    @GetMapping("/withdraw")
    @Operation(summary = "提现")
    public R<Void> withdraw(Double withdrawAmount,String withdrawCode){
        Long customerId = LoginHelper.getUserId();
        HomeSetting homeSetting = homeSettingService.getHomeSetting();
        if(homeSetting.getOpenWithdrawCode()){
            if(StringUtils.isBlank(withdrawCode)){
                return R.fail(MessageUtils.message("dk.withdraw.code.null"));
            }
            if(!homeSetting.getWithdrawCode().equals(withdrawCode)){
                return R.fail(MessageUtils.message("dk.withdraw.code.error"));
            }
        }
        borrowService.withdraw(withdrawAmount,customerId);
        return R.ok();
    }


    @GetMapping("/getStepBorrow")
    @Operation(summary = "获取贷款进度")
    public R<LoanProcessResp> getStepBorrow(){
        Long customerId = LoginHelper.getUserId();
        LoanProcessResp stepBorrow = borrowService.getStepBorrow(customerId);
        return R.ok(stepBorrow);
    }


    @GetMapping("/info")
    @Operation(summary = "查看贷款详情")
    public R<BorrowInfo> info(String tradeNo){
        Borrow borrow = borrowService.getByTradeNo(tradeNo);
        BorrowInfo borrowInfo = BeanConvertUtil.convertTo(borrow, BorrowInfo::new);
        LoanProcessResp stepBorrow = borrowService.parseStepBorrow(borrow);
        borrowInfo.setLoanProcessResp(stepBorrow);
        borrowInfo.setDefaultCoinUnit(homeSettingService.getHomeSetting().getDefaultCoinUnit());
        return R.ok(borrowInfo);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询贷款我的贷款")
    public TableDataInfo<BorrowAdminVO> page(PageQuery pageParams){
        Long customerId = LoginHelper.getUserId();
        LambdaQueryWrapper<Borrow> query = Wrappers.lambdaQuery(Borrow.class)
                .eq(Borrow::getCustomerId,customerId)
                .orderByDesc(Borrow::getCreateTime);
        IPage<Borrow> page = borrowService.page(pageParams.build(), query);
        TableDataInfo<BorrowAdminVO> info = TableDataInfo.build(page, BorrowAdminVO::new);
        List<BorrowAdminVO> rows = info.getRows();
        for (BorrowAdminVO row : rows) {
            row.setDefaultCoinUnit(homeSettingService.getHomeSetting().getDefaultCoinUnit());
        }
        return info;
    }


    @GetMapping("/getContract")
    public R<String> getContract(String tradeNo){
        String contract = borrowService.getContract(tradeNo);
        R<String> success = R.ok();
        success.setData(contract);
        return success;
    }
}
