package com.winsun.utils;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsun.bean.AuthCode;
import com.winsun.mapper.AuthCodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SaveAuthCodeUtil {
    @Autowired
    private AuthCodeMapper authCodeMapper;

    public void saveAuthCode(String phone, String code){
        AuthCode authCode = new AuthCode();
        authCode.setId(phone);
        authCode.setUserAuthCode(Integer.parseInt(code));
        Wrapper<AuthCode> authCodeWrapper = new EntityWrapper<>();
        authCodeWrapper.eq("id",phone);
        Map<String,Object> map2 = new HashMap<>();
        map2.put("user_authcode",code);
        List<AuthCode> auth = authCodeMapper.queryAuthCode(phone);
        if(auth.size()>0){
            authCodeMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(map2),authCodeWrapper);
        }else{
            authCodeMapper.insert(authCode);
        }
    }

}
