
package com.tmerclub.cloud.supplier.feign;

import com.tmerclub.cloud.api.supplier.feign.SupplierBankCardFeignClient;
import com.tmerclub.cloud.api.supplier.vo.SupplierBankCardVO;
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.supplier.mapper.SupplierBankCardMapper;
import com.tmerclub.cloud.supplier.model.SupplierBankCard;
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 SupplierBankCardFeignController implements SupplierBankCardFeignClient {

    @Autowired
    private SupplierBankCardMapper supplierBankCardMapper;

    @Override
    public void saveBandCardByAllinpay(String tenantId, SupplierBankCardVO supplierBankCardVO) {
        SupplierBankCard supplierBankCard = new SupplierBankCard();
        BeanUtils.copyProperties(supplierBankCardVO, supplierBankCard);
        supplierBankCard.setSupplierId(Long.valueOf(tenantId.substring(AllinpayConstant.SUPPLIER_LENGTH)));
        supplierBankCard.setStatus(StatusEnum.ENABLE.value());
        supplierBankCardMapper.save(supplierBankCard);
    }

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