package com.ruoyi.dk.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.component.idempotent.annotation.RepeatSubmit;
import com.ruoyi.component.log.annotation.Log;
import com.ruoyi.component.log.enums.BusinessType;
import com.ruoyi.component.mybatis.core.page.PageQuery;
import com.ruoyi.component.mybatis.core.page.TableDataInfo;
import com.ruoyi.component.web.core.BaseController;
import com.ruoyi.dk.domain.Borrow;
import com.ruoyi.dk.dto.admin.req.BorrowUpdateStatusReq;
import com.ruoyi.dk.dto.admin.resp.BorrowResp;
import com.ruoyi.dk.service.BorrowService;
import com.ruoyi.component.core.domain.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/dk/borrow")
public class DkBorrowController extends BaseController {

    private final BorrowService borrowService;

    /**
     * 查询借款计划列表
     */
    @SaCheckPermission("dk:borrow:list")
    @GetMapping("/list")
    public TableDataInfo<BorrowResp> list(PageQuery pageQuery, @Validated Borrow bo) {
        IPage<BorrowResp> page = borrowService.pageAdmin(pageQuery, bo);
        return TableDataInfo.build(page);
    }


    /**
     * 获取借款计划详细信息
     */
    @SaCheckPermission("dk:borrow:query")
    @GetMapping("/{id}")
    public R<Borrow> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.ok(borrowService.getById(id));
    }

    /**
     * 新增借款计划
     */
    @SaCheckPermission("dk:borrow:add")
    @Log(title = "借款计划", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public R<Void> add(@Validated @RequestBody Borrow bo) {
        return toAjax(borrowService.save(bo) ? 1 : 0);
    }

    /**\
     *
     * 修改借款计划
     */
    @SaCheckPermission("dk:borrow:edit")
    @Log(title = "借款计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public R<Void> edit(@Validated @RequestBody Borrow bo) {
        return toAjax(borrowService.updateById(bo) ? 1 : 0);
    }

    @Log(title = "修改接口银行卡", businessType = BusinessType.UPDATE)
    @PostMapping("updateBank")
    public R<Void> updateBank(@RequestBody Borrow bo) {
        return toAjax(borrowService.updateBank(bo));
    }

    @SaCheckPermission("dk:borrow:edit")
    @Log(title = "修改借款", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PostMapping("/updateLoan")
    public R<Void> updateLoan(@Validated @RequestBody Borrow bo) {
        return toAjax(borrowService.updateLoan(bo));
    }


    @SaCheckPermission("dk:borrow:edit")
    @Log(title = "修改借款状态", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PostMapping("/updateStatus")
    public R<Void> updateStatus(@Validated @RequestBody BorrowUpdateStatusReq bo) {
        return toAjax(borrowService.updateStatus(bo));
    }


    /**
     * 删除借款计划
     */
    @SaCheckPermission("dk:borrow:remove")
    @Log(title = "借款计划" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable String ids) {
        List<String> idList = Stream.of(ids.split(",")).collect(Collectors.toList());
        return toAjax(borrowService.removeByIds(idList) ? 1 : 0);
    }

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