package com.test.service.impl;

import com.test.bean.User;
import com.test.mapper.UserMapper;
import com.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    UserMapper userMapper;
    @Override
    public boolean addUser(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.addUser(user);

    }

    @Override
    public User getUserByPhone(String phone) {
        User user = userMapper.getUserByPhone(phone);
        user.setUpdateTime(null);
        user.setCreateTime(null);
        return user;
    }

    @Override
    public User updateUser(User user) {
        Integer userId = user.getUserId();
        user.setUpdateTime(LocalDateTime.now());
        boolean res = userMapper.updateUser(user);
        if (res){
            return userMapper.getUserById(userId);
        }
        return null;
    }

    @Override
    public boolean deleteUser(Integer id) {
        return userMapper.deleteUser(id);
    }

}
