package com.bonus.boot.manager.manager.service.impl;

import java.util.List;

import com.bonus.boot.manager.basic.entity.PersonnelLibraryBean;
import com.bonus.boot.manager.manager.dao.UtilDao;
import com.bonus.boot.manager.manager.entity.UnifyBean;
import com.bonus.boot.manager.manager.utils.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.bonus.boot.manager.manager.dao.UserDao;
import com.bonus.boot.manager.manager.entity.UserDto;
import com.bonus.boot.manager.manager.model.SysUser;
import com.bonus.boot.manager.manager.model.SysUser.Status;
import com.bonus.boot.manager.manager.service.UserService;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {

	private static final Logger log = LoggerFactory.getLogger("adminLogger");
	@Resource
	private UtilDao utilDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private BCryptPasswordEncoder passwordEncoder;

	@Override
	@Transactional
	public SysUser saveUser(UserDto userDto) {
		SysUser user = userDto;
		user.setPassword(passwordEncoder.encode(user.getPassword()));
		user.setStatus(Status.VALID);
		userDao.save(user);
		saveUserRoles(user.getId(), userDto.getRoleIds());

		log.debug("新增用户{}", user.getUsername());
		return user;
	}

	private void saveUserRoles(Long userId, List<Long> roleIds) {
		if (roleIds != null) {
			userDao.deleteUserRole(userId);
			if (!CollectionUtils.isEmpty(roleIds)) {
				userDao.saveUserRoles(userId, roleIds);
			}
		}
	}

	@Override
	public SysUser getUser(String username) {
		return userDao.getUser(username);
	}

	@Override
	public void changePassword(String username, String oldPassword, String newPassword) {
		SysUser u = userDao.getUser(username);
		if (u == null) {
			throw new IllegalArgumentException("用户不存在");
		}

		if (!passwordEncoder.matches(oldPassword, u.getPassword())) {
			throw new IllegalArgumentException("旧密码错误");
		}

		userDao.changePassword(u.getId(), passwordEncoder.encode(newPassword));

		log.debug("修改{}的密码", username);
	}

	@Override
    @Transactional
	public SysUser updateUser(UserDto userDto) {
		userDao.update(userDto);
		saveUserRoles(userDto.getId(), userDto.getRoleIds());

		return userDto;
	}
/**----------------------------------------------------------------------------------------------*/

	@Override
	public List<SysUser> getMsgList(SysUser o) {
		return userDao.getMsgList(o);
	}

	@Override
	public int delById(SysUser bean) {
		return userDao.delById(bean);
	}

	@Override
	public int resetPassword(SysUser bean) {
		bean.setPassword(passwordEncoder.encode(bean.getPassword()));
		return userDao.resetPassword(bean);
	}

	@Override
	public int addInfo(SysUser bean) {
		int num = utilDao.determineIfExists("sys_user","id_card",bean.getIdCard(),"");
		if(num > 0){
			num = 3;
		}else {
			int phone = utilDao.personIfExists("sys_user","phone",bean.getPhone());
			if(phone >0){
				return 5;
			}
			bean.setPassword(passwordEncoder.encode("YNsbd@123456"));
			num = userDao.addInfo(bean);
			//返回主键  插入sys_role_user表中去
			int team = userDao.insertroleUser(bean);
		}
		return num;
	}

	@Override
	public int updateInfo(SysUser bean) {
		int num =0;
	/*	int num = utilDao.determineIfExists("sys_user","id_card",bean.getIdCard(),bean.getId()+"");
		if(num > 0){
			num = 3;
		}else {*/
			/*int phone = utilDao.personIfExists("sys_user","phone",bean.getPhone());
			if(phone >0){
				return 5;
			}*/
			//bean.setAge(StringHelper.countAge(bean.getIdCard()));
			/*if (bean.getAge() < 18 || bean.getAge() > 99){
				return 4;
			}*/
		//	bean.setAge(StringHelper.countAge(bean.getIdCard()));
	/*		if ("男".equals(StringHelper.judgeGender(bean.getIdCard()))){
				bean.setSex("1");
			}else{
				bean.setSex("0");
			}*/
			num = userDao.updateInfo(bean);
		int team = userDao.updateRoleUser(bean);

		//}
		return num;
	}

	@Override
	public SysUser getListById(String id) {
		return userDao.getListById(id);
	}

	@Override
	public int resetPasswordForOne(SysUser bean) {
		bean.setPassword(passwordEncoder.encode(bean.getPassword()));
		return userDao.resetPasswordForOne(bean);
	}

	@Override
	public void updateUnify(Long id) {
		userDao.updateUnify(id);
	}

	/**
	 * @param userVo
	 * @return
	 */
	@Override
	public String getPermissionQuery(UnifyBean userVo) {
		return userDao.getPermissionQuery(userVo);
	}

	/**
	 * @param userVo
	 * @return
	 */
	@Override
	public String getPermissionQuerys(UnifyBean userVo) {
		return userDao.getPermissionQuerys(userVo);
	}

}
