package com.szxgl.nft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.szxgl.nft.entity.UserDO;
import com.szxgl.nft.mapper.UserMapper;
import com.szxgl.nft.service.UserService;
import com.szxgl.nft.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public UserDO getUserDo(String openid) {
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public void addUser(UserDO userDO) {
        userMapper.insert(userDO);
    }

    @Override
    public R addPhone(String seed, String openid) {
        //手机号是否已被用户绑定
        UserDO user = getPhone(seed);
        if(user != null ){
            return R.error("手机号已被用户绑定!");
        }
        UserDO userDo = getUserDo(openid);
        UpdateWrapper<UserDO> wrapper = new UpdateWrapper<>();
        wrapper.set("phone",seed);
        wrapper.eq("openid",openid);
        userMapper.update(userDo,wrapper);
        return R.ok();
    }

    @Override
    public UserDO getPhone(String seed) {
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",seed);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public void addUserNftPhoto(String openid, String nickname,String walletAddr) {
        UserDO userDo = getUserDo(openid);
        UpdateWrapper<UserDO> wrapper = new UpdateWrapper<>();
        wrapper.set("nickname",nickname);
        wrapper.set("wallet_addr",walletAddr);
        wrapper.eq("openid",openid);
        userMapper.update(userDo,wrapper);
    }
}
