package com.sqx.modules.shop.controller;

import cn.hutool.core.util.ObjectUtil;
import com.sqx.common.exception.SqxException;
import com.sqx.common.utils.Result;
import com.sqx.modules.shop.entity.ShopBankCardInfo;
import com.sqx.modules.shop.service.ShopBankCardInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(tags={"商户管理-商户银行卡管理"})
@RestController
@RequestMapping("/admin/shop/bank")
@RequiredArgsConstructor
public class AdminShopBankCardInfoController {

    private final ShopBankCardInfoService shopBankCardInfoService;

    @ApiOperation("新增银行卡信息")
    @PostMapping
    public Result saveCardInfo(@Valid @RequestBody ShopBankCardInfo shopBankCardInfo) {
        shopBankCardInfoService.saveCardInfo(shopBankCardInfo);
        return Result.success();
    }

    @ApiOperation("修改银行卡信息")
    @PutMapping
    public Result updateCardInfo(@Valid @RequestBody ShopBankCardInfo shopBankCardInfo) {
        if (ObjectUtil.isNull(shopBankCardInfo.getId())) {
            throw new SqxException("id不能为空");
        }
        shopBankCardInfoService.updateCardInfo(shopBankCardInfo);
        return Result.success();
    }

    @ApiOperation("根据id获取银行卡信息")
    @GetMapping("{id}")
    public Result getById(@ApiParam("id") @PathVariable Long id) {
        ShopBankCardInfo cardInfo = shopBankCardInfoService.getById(id);
        if (ObjectUtil.isNull(cardInfo)) {
            throw new SqxException("id对应的记录不存在");
        }
        return Result.success().put("data", cardInfo);
    }

    @ApiOperation("根据店铺id获取银行卡信息")
    @GetMapping("getByShopId/{shopId}")
    public Result getByShopId(@ApiParam("店铺id") @PathVariable Long shopId) {
        ShopBankCardInfo cardInfo = shopBankCardInfoService.getByShopId(shopId);

        return Result.success().put("data", cardInfo);
    }

    @ApiOperation("根据id删除银行卡信息")
    @DeleteMapping("{id}")
    public Result deleteById(@ApiParam("id") @PathVariable Long id) {
        shopBankCardInfoService.removeById(id);

        return Result.success();
    }

    @ApiOperation("根据店铺id删除银行卡信息")
    @DeleteMapping("delete-by-shop-id/{shopId}")
    public Result deleteByShopId(@ApiParam("店铺id") @PathVariable Long shopId) {
        shopBankCardInfoService.deleteByShopId(shopId);

        return Result.success();
    }
}
