package com.uas.platform.b2b.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.uas.platform.b2b.dao.UserDao;
import com.uas.platform.b2b.model.User;
import com.uas.platform.b2b.service.UserService;
import com.uas.platform.core.model.Constant;
import com.uas.platform.core.util.encry.Md5Utils;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public boolean isEmailUseable(String email) {
		return userDao.findUserByUserEmail(email).size() == 0;
	}

	@Override
	public User save(User user) {
		user.setEnable(Constant.NO);
		user.setUserPwd(Md5Utils.encode(user.getUserPwd(), user.getUserUU()));
		return userDao.save(user);
	}

	@Override
	public User findUserByUserUU(Long uu) {
		List<User> users = userDao.findUserByUserUU(uu);
		if (users.size() == 0)
			throw new UsernameNotFoundException("没有找到您的账号 " + uu);
		return users.get(0);
	}

	@Override
	public User setEnable(User user) {
		user.setEnable(Constant.YES);
		return userDao.save(user);
	}

	@Override
	public User findUserByUserEmail(String email) {
		List<User> users = userDao.findUserByUserEmail(email);
		if (users.size() == 0)
			throw new UsernameNotFoundException("没有找到您的账号 " + email);
		return users.get(0);
	}

	@Override
	public User findUserByUserTel(String tel) {
		List<User> users = userDao.findUserByUserTel(tel);
		if (users.size() == 0)
			throw new UsernameNotFoundException("没有找到您的账号 " + tel);
		return users.get(0);
	}

	@Override
	public boolean isTelUseable(String tel) {
		return userDao.findUserByUserTel(tel).size() == 0;
	}

}
