package com.bonus.gs.sub.evaluate.manager.service.impl;

import java.util.List;

import com.bonus.gs.sub.evaluate.manager.utils.AjaxRes;
import org.apache.commons.lang3.StringUtils;
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.gs.sub.evaluate.manager.dao.UserDao;
import com.bonus.gs.sub.evaluate.manager.entity.UserDto;
import com.bonus.gs.sub.evaluate.manager.model.SysUser;
import com.bonus.gs.sub.evaluate.manager.model.SysUser.Status;
import com.bonus.gs.sub.evaluate.manager.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	private static final Logger log = LoggerFactory.getLogger("adminLogger");

	@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) {
		SysUser user = userDao.getUser(username);
		if(user !=null){
			String roleIds = userDao.getUserRoles(user.getId());
			user.setRoleId(roleIds);
			if (StringUtils.isNotBlank(user.getDeptId())) {
				String isBusinessDivision = userDao.getIsBusinessDivision(user.getId());
				user.setIsBusinessDivision(isBusinessDivision);
			}
		}
		return user;
	}

	@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(rollbackFor = Exception.class)
	public AjaxRes changePassWord(UserDto userDto) {
		AjaxRes ar = new AjaxRes();
		try {
			userDao.changePassword(userDto.getId(), passwordEncoder.encode(userDto.getPassword()));
			userDao.changOldTablePassword(userDto.getPhone(), passwordEncoder.encode(userDto.getPassword()));

			ar.setSucceedMsg("修改成功");
		} catch (Exception e) {
			ar.setFailMsg("修改失败");

		}
		return ar;
	}

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

		return userDto;
	}

	@Override
	public int getUserCount(String username) {
		return userDao.getUserCount(username);
	}
}
