
package com.tmerclub.cloud.multishop.feign;

import com.tmerclub.cloud.api.multishop.feign.ShopBankCardFeignClient;
import com.tmerclub.cloud.api.multishop.vo.ShopBankCardVO;
import com.tmerclub.cloud.common.constant.AllinpayConstant;
import com.tmerclub.cloud.common.constant.StatusEnum;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.multishop.mapper.ShopBankCardMapper;
import com.tmerclub.cloud.multishop.model.ShopBankCard;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * @author track
 */
@DubboService
public class ShopBankCardFeignController implements ShopBankCardFeignClient {

    @Autowired
    private ShopBankCardMapper shopBankCardMapper;

    @Override
    public void saveBandCardByAllinpay(String tenantId, ShopBankCardVO shopBankCardVO) {
        ShopBankCard shopBankCard = new ShopBankCard();
        BeanUtils.copyProperties(shopBankCardVO, shopBankCard);
        shopBankCard.setShopId(Long.valueOf(tenantId.substring(AllinpayConstant.SHOP_LENGTH)));
        shopBankCard.setStatus(StatusEnum.ENABLE.value());
        shopBankCardMapper.save(shopBankCard);
    }

    @Override
    public ServerResponseEntity<Void> deleteLegalBankCard(Long shopBankCardId) {
        ShopBankCardVO shopBankCardVO = shopBankCardMapper.getByShopBankCardId(shopBankCardId);
        if (Objects.nonNull(shopBankCardVO) && Objects.equals(shopBankCardVO.getBankCardPro(), 1)) {
            throw new LuckException("企业对公户不支持解绑");
        }
        shopBankCardMapper.deleteById(shopBankCardId);
        return ServerResponseEntity.success();
    }
}
