package com.saye.hrs.service.system.impl;

import com.saye.hrs.commons.date.DateDUtil;
import com.saye.hrs.commons.string.StringDUtil;
import com.saye.hrs.commons.uuid.UUIDGenerator;
import com.saye.hrs.mapper.system.UserRoleMapper;
import com.saye.hrs.mapper.system.UsersMapper;
import com.saye.hrs.model.Users;
import com.saye.hrs.service.system.UsersService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class UsersServiceImpl implements UsersService{

	@Autowired
	private UsersMapper usersMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;

	@Override
	public List<Users> searchUsers(HashMap<Object, Object> map) {
		List<Users> usersList = usersMapper.searchUsers(map);
		return usersList;
	}

	/**新增用户逻辑，插入user表该用户的信息
	 * 在插入user_role表该用户角色id
	 * 
	 * */
	@Override
	@Transactional
	public void insertUser(HashMap<Object, Object> map)throws Exception {
		// TODO Auto-generated method stub
		//将密码进行加密
		String password = StringDUtil.changeNullToEmpty(map.get("password"));
		String encryptPassword = new Md5Hash(password,"hrs",2).toString();
		map.put("password", encryptPassword);
		
		String addUserName = StringDUtil.changeNullToEmpty(map.get("userName"));
		
		//先验证是否已有用户名
		List<Users> searchByName = usersMapper.searchByName(addUserName);
		
		if (searchByName!=null && searchByName.size()>0) {
			throw new RuntimeException("用户名已存在");
		}
		
		String uuid = UUIDGenerator.getUUID();
		map.put("userId", uuid);
		map.put("create_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
		map.put("create_date", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd));

		//插入用户表
		usersMapper.insertUser(map);
		//插入用户角色表userRole
		String roleId = StringDUtil.changeNullToEmpty(map.get("roleId"));
		String userId= uuid;
		String[] roleIdArr = roleId.split(",");
		int roleIdLen = roleIdArr.length;
		for (int i = 0; i < roleIdLen; i++) {
			String rId = roleIdArr[i];
			userRoleMapper.insertUserRole(userId, rId);
		}
	}

	//修改用户信息
	@Override
	@Transactional
	public int modifyUsers(HashMap<Object, Object> map)throws Exception {
		// TODO Auto-generated method stub
		//修改用户信息 更新user表信息
		int a=usersMapper.modifyUsers(map);
		//删除user_role表中userId的记录
		String userId=StringDUtil.changeNullToEmpty(map.get("userId"));
		userRoleMapper.deleteUserRole(userId);
		//插入用户角色表userRole中记录
		String roleId = StringDUtil.changeNullToEmpty(map.get("roleId"));
		String[] roleIdArr = roleId.split(",");
		int roleIdLen = roleIdArr.length;
		int b=0;
		for (int i = 0; i < roleIdLen; i++) {
			 String rId = roleIdArr[i];
			 b+=userRoleMapper.insertUserRole(userId, rId);
		}
		return a;
	}

	//user用户密码重置
	@Override
	@Transactional
	public int reSetPassword(HashMap<Object, Object> map)throws Exception {
		// TODO Auto-generated method stub
		return usersMapper.modifyPassword(map);
	}

	//用户修改禁用状态
	@Override
	@Transactional
	public int unableUser(HashMap<Object, Object> map)throws Exception {
		// TODO Auto-generated method stub
		return usersMapper.unableUser(map);
	}

	@Override
	public List<Users> searchByName(String userName) {
		// TODO Auto-generated method stub
		return usersMapper.searchByName(userName);
	}

	/**
	 * @description 修改密码
	 * @author thuang       
	 * @created 2019年11月28日 上午8:40:26
	 */
	@Override
	@Transactional
	public void modifyPassword(String username, String password, String modifyTime) throws Exception {
		
		this.usersMapper.modifyPassword(username,password,modifyTime);
	}

	@Override
	public void modifyLockTime(String username, String lockTime){

		this.usersMapper.modifyLockTime(username, lockTime);
	}

	/**
	 * @description: 导入人员
	 * @author thuang
	 * @date 2021/5/19 14:34
	 * @version 1.0
	 */
	@Override
	public void insertExcelUsers(List<HashMap<Object, Object>> list) {

		this.usersMapper.insertExcelUsers(list);

	}

	@Override
	public List<HashMap<Object, Object>> findUserByDepartIds(List<String> idList) throws Exception {

		return this.usersMapper.findUserByDepartIds(idList);
	}


}
