package service.impl;

import exception.UserNotFindException;
import mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.User;
import pojo.UserExample;
import service.UserService;

import java.util.List;

/**
 * 实现用户登录
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User userLogin(User user) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        //根据用户名和密码判断
        criteria.andUsernameEqualTo(user.getUsername());
        criteria.andUserpasswordEqualTo(user.getUserpassword());
        List<User> list = this.userMapper.selectByExample(userExample);
        if (list == null || list.size() <= 0){
            throw new UserNotFindException("用户名或者密码错误");
        }
        return list.get(0);
    }
}
