package com.ruoyi.dk.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.BorrowStatus;
import com.ruoyi.dk.enums.BankTypeEnums;
import com.ruoyi.dk.service.BorrowStatusService;
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.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

    private final BorrowStatusService borrowStatusService;

    /**
     * 查询借款状态列表
     */
    @SaCheckPermission("dk:BorrowStatus:list")
    @GetMapping("/list")
    public TableDataInfo<BorrowStatus> list(PageQuery pageParams, @Validated BorrowStatus bo) {
        IPage<BorrowStatus> page = borrowStatusService.page(pageParams.build(), Wrappers.query(bo));
        return TableDataInfo.build(page);
    }

    @GetMapping("/bankType")
    public R<List<String>> bankType(){
        List<String> list = Arrays.stream(BankTypeEnums.values()).map(BankTypeEnums::getName).collect(Collectors.toList());
        return R.ok(list);
    }

    @GetMapping("/all")
    public R<List<BorrowStatus>> all(){
        List<BorrowStatus> list = borrowStatusService.list();
        return R.ok(list);
    }

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

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

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

    /**
     * 删除借款状态
     */
    @SaCheckPermission("dk:BorrowStatus: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(borrowStatusService.removeByIds(idList) ? 1 : 0);
    }
}
