package com.ruoyi.app.pay;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.InfoUser;
import com.ruoyi.system.utils.Auth;
import com.ruoyi.system.utils.JwtUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BankCard;
import com.ruoyi.system.service.IBankCardService;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * BankCardController
 * 
 * @author ruoyi
 * @date 2023-08-06
 */
@RestController
@RequestMapping("/system/card")
public class BankCardController extends BaseController
{
    @Autowired
    private IBankCardService bankCardService;


    //删除银行卡
    @Anonymous
    @Auth
    @GetMapping("/delebankcard")
    public AjaxResult delebankcard(@RequestParam Long id)
    {
        int org = bankCardService.deleteBankCardById(id);
        if(org==1){
            return success(MessageUtils.message("no.delete.success"));
        }else {
            return error(MessageUtils.message("no.delete.fail"));
        }
    }
    //用户银行卡列表
    @Anonymous
    @Auth
    @GetMapping("/getbankcard")
    public AjaxResult getbankcard(@RequestHeader String token)
    {
        JwtUtil jwtUtil = new JwtUtil();
        String userid = jwtUtil.getusid(token);
        BankCard bankCard = new BankCard();
        bankCard.setUserId(Long.valueOf(userid));
        List<BankCard> list = bankCardService.selectBankCardList(bankCard);
        return success(MessageUtils.message("no.load.success"), list);
    }
    //添加银行卡
    @Anonymous
    @Auth
    @PostMapping("/addbankcard")
    public AjaxResult addbankcard(@RequestHeader String token, @RequestBody BankCard bankCard)
    {
        JwtUtil jwtUtil = new JwtUtil();
        bankCard.setUserId(Long.valueOf(jwtUtil.getusid(token)));
        return toAjax(bankCardService.insertBankCard(bankCard));
    }
    /**
     * 查询BankCard列表
     */
    @PreAuthorize("@ss.hasPermi('system:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(BankCard bankCard)
    {
        startPage();
        List<BankCard> list = bankCardService.selectBankCardList(bankCard);
        return getDataTable(list);
    }

    /**
     * 导出BankCard列表
     */
    @PreAuthorize("@ss.hasPermi('system:card:export')")
    @Log(title = "BankCard", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BankCard bankCard)
    {
        List<BankCard> list = bankCardService.selectBankCardList(bankCard);
        ExcelUtil<BankCard> util = new ExcelUtil<BankCard>(BankCard.class);
        util.exportExcel(response, list, MessageUtils.message("no.export.excel.bankcard"));
    }

    /**
     * 获取BankCard详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:card:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bankCardService.selectBankCardById(id));
    }

    /**
     * 新增BankCard
     */
    @PreAuthorize("@ss.hasPermi('system:card:add')")
    @Log(title = "BankCard", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BankCard bankCard)
    {
        return toAjax(bankCardService.insertBankCard(bankCard));
    }

    /**
     * 修改BankCard
     */
    @PreAuthorize("@ss.hasPermi('system:card:edit')")
    @Log(title = "BankCard", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BankCard bankCard)
    {
        return toAjax(bankCardService.updateBankCard(bankCard));
    }

    /**
     * 删除BankCard
     */
    @PreAuthorize("@ss.hasPermi('system:card:remove')")
    @Log(title = "BankCard", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bankCardService.deleteBankCardByIds(ids));
    }
}
