package cn.iocoder.yudao.module.system.controller.admin.bank;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.bank.vo.UserBankPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.bank.vo.UserBankRespVO;
import cn.iocoder.yudao.module.system.controller.admin.bank.vo.UserBankSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.bank.BankDO;
import cn.iocoder.yudao.module.system.service.bank.UserBankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 用户银行卡信息")
@RestController
@RequestMapping("/system/user-bank")
@Validated
public class UserBankController {

    @Resource
    private UserBankService userBankService;

    @PostMapping("/create")
    @Operation(summary = "创建银行卡信息")
    @PreAuthorize("@ss.hasPermission('system:user-bank:create')")
    public CommonResult<Long> createBank(@Valid @RequestBody UserBankSaveReqVO createReqVO) {

        return success(userBankService.createBank(createReqVO));
    }

    @PostMapping("/create-bank")
    @Operation(summary = "创建银行卡信息")
    @PreAuthorize("@ss.hasPermission('system:user-bank:create')")
    public CommonResult<Boolean> createPayBank(@Valid @RequestBody UserBankSaveReqVO createReqVO) {

        userBankService.createPayBank(createReqVO);
        return success(true);
    }

    @PostMapping("/create-user")
    @Operation(summary = "创建用户和银行卡关联信息")
    @PreAuthorize("@ss.hasPermission('system:user-bank:create')")
    public CommonResult<Boolean> createUserBank(@RequestBody List<Long> bankId) {

        userBankService.createUserBank(bankId);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新银行卡信息")
    @PreAuthorize("@ss.hasPermission('system:user-bank:update')")
    public CommonResult<Boolean> updateBank(@Valid @RequestBody UserBankSaveReqVO updateReqVO) {
        userBankService.updateBank(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户和银行卡关联信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:user-bank:delete')")
    public CommonResult<Boolean> deleteUserBank(@RequestParam("id") Long id) {
        userBankService.deleteUserBank(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得银行卡信息")
    @Parameter(name = "bankId", description = "银行卡信息编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:user-bank:query')")
    public CommonResult<UserBankRespVO> getBank(@RequestParam("bankId") Long bankId) {

        BankDO userBank = userBankService.getBank(bankId);
        return success(BeanUtils.toBean(userBank, UserBankRespVO.class));
    }

    @GetMapping("/page-bank")
    @Operation(summary = "获得银行卡信息分页列表")
    @PreAuthorize("@ss.hasPermission('system:user-bank:query')")
    public CommonResult<PageResult<UserBankRespVO>> getBankPage(@Valid UserBankPageReqVO pageReqVO) {

        PageResult<BankDO> pageResult = new PageResult<BankDO>();
        List<Long> bankIds = userBankService.getBankIds(getLoginUserId());
        if (bankIds.isEmpty()) {

            pageResult = userBankService.getBankPageIn(pageReqVO, null);
        }else {
            pageResult = userBankService.getBankPageNotIn(pageReqVO, bankIds);
        }


        return success(BeanUtils.toBean(pageResult, UserBankRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户和银行卡关联信息分页")
    @PreAuthorize("@ss.hasPermission('system:user-bank:query')")
    public CommonResult<PageResult<UserBankRespVO>> getUserBankPage(@Valid UserBankPageReqVO pageReqVO) {

        PageResult<BankDO> pageResult = new PageResult<BankDO>();
        List<Long> bankIds = userBankService.getBankIds(getLoginUserId());
        if (!bankIds.isEmpty()) {

            pageResult = userBankService.getBankPageIn(pageReqVO, bankIds);
        }


        return success(BeanUtils.toBean(pageResult, UserBankRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户和银行卡关联信息列表")
    @PreAuthorize("@ss.hasPermission('system:user-bank:query')")
    public CommonResult<List<UserBankRespVO>> getUserBankList() {

        List<BankDO> bankDOS = userBankService.getBankListByUserId(getLoginUserId());

        return success(BeanUtils.toBean(bankDOS, UserBankRespVO.class));
    }
}