package com.tplink.nbu.cloud.service;
import com.tplink.nbu.cloud.dao.UserDao;
import com.tplink.nbu.cloud.exceptions.ParamsException;
import com.tplink.nbu.cloud.entity.User;
import com.tplink.nbu.cloud.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tplink.nbu.cloud.utils.Md5Util;
import java.io.*;
import java.util.List;
import java.util.Properties;
@Service
public class UserService {
@Autowired
private UserDao userDao;
// private UserCassDao userDao;
public User userLogin(String userEmail,String userPwd) {
// 1. 参数判断,判断用户姓名、用户密码非空
checkLoginParams(userEmail, userPwd);
// 2. 调用数据访问层,通过用户名查询用户记录,返回用户对象
User user = findByEmail(userEmail);
// 3. 判断用户对象是否为空
AssertUtil.isTrue(user == null,"邮箱不存在或已注销");
// 4. 判断密码是否正确,比较客户端传递的用户密码与数据库中查询的用户对象中的用户密码
checkUserPwd(userPwd, user.getPassword());
// 返回构建用户对象
return user;
}
private void checkLoginParams(String userEmail, String userPwd) {
AssertUtil.isTrue(StringUtils.isBlank(userEmail),"邮箱不能为空");
AssertUtil.isTrue(StringUtils.isBlank(userPwd),"用户密码不能为空");
}
private void checkUserPwd(String userPwd, String userPwdTrue) {
AssertUtil.isTrue(!(StringUtils.equals( Md5Util.encode(userPwd),userPwdTrue)),"密码错误");
}
public User userSignin(String userName, String userPwd, String userEmail, String userAddr) {
User user = new User(userName, Md5Util.encode(userPwd) , userEmail, userAddr);
// 1. 参数判断,判断用户姓名、用户密码、邮箱、地址非空
checkSigninParams(userName, userPwd, userEmail, userAddr);
String basePath = "./src/main/resources/config.properties";
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(new File(basePath)));
} catch (FileNotFoundException e) {
e.printStackTrace();
return user;
}
Properties prop = new Properties();
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
return user;
}
String totalSigninNumber = prop.getProperty("TotalSigninNumber");
System.out.println(totalSigninNumber);
int signinNumber = Integer.parseInt(totalSigninNumber);
System.out.println(signinNumber);
long count = Count();
if (signinNumber > count){
// 2. 调用数据访问层,增加用户数据
User user_ = save(user);
} else{
throw new ParamsException("超过注册上限");
}
// 返回构建用户对象
return user;
}
public User userEdit(String userName, String userPwd, String userEmail, String userAddr) {
checkSigninParams(userName, userPwd, userEmail, userAddr);
User user_ = findByEmail(userEmail);
user_.setAddr(userAddr);
user_.setUsername(userName);
// 1. 参数判断,判断用户姓名、用户密码、邮箱、地址非空
User userReturn = edit(user_);
// 返回构建用户对象
return userReturn;
}
public boolean userDelete(String userPwd, String userEmail) {
User user_ = findByEmail(userEmail);
boolean res = false;
String userPassword = Md5Util.encode(userPwd);
if (userPassword.equals(user_.getPassword())){
int id = user_.getId();
res = deleteById(id);
}
// 返回构建用户对象
return res;
}
private void checkSigninParams(String userName, String userPwd, String userEmail, String userAddr) {
AssertUtil.isTrue(StringUtils.isBlank(userName),"用户名不能为空");
AssertUtil.isTrue(StringUtils.isBlank(userPwd),"用户密码不能为空");
AssertUtil.isTrue(StringUtils.isBlank(userEmail),"邮箱不能为空");
AssertUtil.isTrue(StringUtils.isBlank(userAddr),"用户地址不能为空");
}
/**
* 根据用户ID,查询用户信息
*/
public User findById(int userId)
{
return userDao.findById(userId).get();
}
/**
* 根据用户Email,查询用户信息
*/
public User findByEmail(String email)
{
return userDao.findUserByEmail(email);
}
/**
* 查询用户列表
*/
public long Count()
{
return userDao.count();
}
/**
* 计算 count
*/
public List<User> findAll()
{
return userDao.findAll();
}
/**
* 新增用户
*/
public User save(User user)
{
return userDao.save(user);
}
/**
* 修改用户
*/
public User edit(User user)
{
return userDao.save(user);
}
/**
* 删除用户
*/
public boolean deleteById(int userId)
{
boolean result = true;
try
{
userDao.deleteById(userId);
}
catch(Exception ex)
{
result = false;
}
return result;
}
}
评论0