/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.multishop.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.model.ShopBankCard;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.ShopBankCardService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;


/**
 * 商家提现申请信息
 *
 * @author YXF
 *  * @date 2020-04-07 14:22:08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/shopBankCard" )
public class ShopBankCardController {

    private final ShopBankCardService shopBankCardService;



    @GetMapping("/getShopBankCardList")
    public ResponseEntity< List<ShopBankCard>> getShopInfo() {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        List<ShopBankCard> list = shopBankCardService.list(new LambdaQueryWrapper<ShopBankCard>().eq(ShopBankCard::getShopId, shopId));
        return ResponseEntity.ok(list);
    }

    /**
     * 保存
     */
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody @Valid ShopBankCard shopBankCard) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        shopBankCard.setShopId(shopId);
        shopBankCardService.insert(shopBankCard);
        return ResponseEntity.ok().build();
    }

    /**
     * 设置默认银行卡
     */
    @PutMapping("/setDefault")
    public ResponseEntity<Void> setDefault(@RequestBody @Valid ShopBankCard shopBankCard) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        shopBankCardService.setDefault(shopBankCard.getShopBankCardId(),shopId);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{shopBankCardId}")
    public ResponseEntity<Void> removeById(@PathVariable("shopBankCardId") Long shopBankCardId) {
        ShopBankCard shopBankCard = shopBankCardService.getById(shopBankCardId);
        if (Objects.equals(shopBankCard.getIsDefault(),1)){
            throw new GlobalException("不能删除默认银行卡");
        }
        shopBankCardService.removeById(shopBankCardId);
        return ResponseEntity.ok().build();
    }
}
