package com.kxs.user.biz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.core.util.R;
import com.kxs.common.security.util.SecurityUtils;
import com.kxs.user.api.dto.kxsapp.user.UserUpdateBankInfoDTO;
import com.kxs.user.api.model.KxsUser;
import com.kxs.user.api.model.KxsUserReal;
import com.kxs.user.api.vo.kxsapp.userAccount.BindBankInfoVO;
import com.kxs.user.biz.constant.enums.UserErrorTypeEnum;
import com.kxs.user.biz.mapper.KxsUserBankMapper;
import com.kxs.user.api.model.KxsUserBank;
import com.kxs.user.biz.mapper.KxsUserMapper;
import com.kxs.user.biz.mapper.KxsUserRealMapper;
import com.kxs.user.biz.service.KxsUserBankService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 用户银行卡表(KxsUserBank)表服务实现类
 *
 * @author 系统
 * @since 2024-01-03 09:39:55
 */
@Service("kxsUserBankService")
@RequiredArgsConstructor
public class KxsUserBankServiceImpl extends ServiceImpl<KxsUserBankMapper, KxsUserBank> implements KxsUserBankService {

    private final KxsUserRealMapper kxsUserRealMapper;

    @Override
    public R updateBankInfo(UserUpdateBankInfoDTO param) {

        KxsUserBank userBank = this.getOne(Wrappers.<KxsUserBank>lambdaQuery().eq(KxsUserBank::getUserId, SecurityUtils.getUser().getId()));
        KxsUserReal userReal = kxsUserRealMapper.selectOne(Wrappers.<KxsUserReal>lambdaQuery().eq(KxsUserReal::getUserId, SecurityUtils.getUser().getId()));

        if(userBank == null){
            return R.failed(UserErrorTypeEnum.NOT_REAL.getDescription());
        }
        //TODO 提交调用三方验证实名信息

        userBank.setBankCode(param.getBankCode());
        userBank.setBankName(param.getBankName());
        userBank.setBankPhone(param.getBankPhone());
        userBank.setBankOpen(param.getBankOpen());
        userBank.setBankPic(param.getBankPic());
        baseMapper.updateById(userBank);

        return R.ok();
    }

    @Override
    public BindBankInfoVO bindBankInfo() {

        KxsUserBank userBank = this.getOne(Wrappers.<KxsUserBank>lambdaQuery().eq(KxsUserBank::getUserId, SecurityUtils.getUser().getId()));

        BindBankInfoVO bindBankInfoVO = new BindBankInfoVO();
        BeanUtils.copyProperties(userBank, bindBankInfoVO);

        return bindBankInfoVO;
    }




}

