package com.sqx.modules.datacentre.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.exception.SqxException;
import com.sqx.common.utils.Result;
import com.sqx.modules.datacentre.dao.RiderBankCardInfoDao;
import com.sqx.modules.datacentre.entity.RiderBankCardInfo;
import com.sqx.modules.datacentre.service.RiderBankCardInfoService;
import org.springframework.stereotype.Service;

@Service
public class RiderBankCardInfoServiceImpl extends ServiceImpl<RiderBankCardInfoDao, RiderBankCardInfo> implements RiderBankCardInfoService {

    @Override
    public Result saveCardInfo(RiderBankCardInfo riderBankCardInfo) {
        Long userId = riderBankCardInfo.getUserId();

        LambdaQueryWrapper<RiderBankCardInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(RiderBankCardInfo::getUserId,userId);
        RiderBankCardInfo cardInfo = this.getOne(wrapper);
        if (ObjectUtils.isNotEmpty(cardInfo)) {
            return Result.error("该银行卡信息已存在");
        }

        this.save(riderBankCardInfo);

        return Result.success();
    }

    @Override
    public Result getUserIdCardInfo(Long userId) {
        LambdaQueryWrapper<RiderBankCardInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(RiderBankCardInfo::getUserId,userId);
        RiderBankCardInfo riderBankCardInfo = this.getOne(wrapper);
        return Result.success().put("data",riderBankCardInfo);
    }

    @Override
    public RiderBankCardInfo getByUserId(Long userId) {
        LambdaQueryWrapper<RiderBankCardInfo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RiderBankCardInfo::getUserId, userId);

        RiderBankCardInfo bankCardInfo = this.getOne(queryWrapper);

        if (ObjectUtil.isNull(bankCardInfo)) {
            throw new SqxException("银行卡信息不存在！");
        }

        return bankCardInfo;
    }
}
