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

import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.commons.uuid.UUIDGenerator;
import com.saye.hospitalgd.mapper.system.DepartMapper;
import com.saye.hospitalgd.mapper.system.MenuRoleMapper;
import com.saye.hospitalgd.mapper.system.RoleMapper;
import com.saye.hospitalgd.model.Role;
import com.saye.hospitalgd.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private MenuRoleMapper menuRoleMapper;

	@Autowired
	private DepartMapper departMapper;

	@Override
	public List<Role> findRoleList() {
		// TODO Auto-generated method stub
		return roleMapper.findRoleList();
	}

	@Override
	public List<Role> searchRole(HashMap<String, String> map) {
		// TODO Auto-generated method stub
		return roleMapper.searchRole(map);
	}

	//删除角色，通过角色id删除角色，同时删除角色权限表的信息
	@Override
	@Transactional
	public int deleteRole(String roleId) {
		//通过roleId 删除role_menu表的记录
		int b=menuRoleMapper.deleteMenuRole(roleId);
		//删除角色表数据
		int a=roleMapper.deleteRole(roleId);
		return a;
	}

	//修改用户角色的权限
	@Override
	@Transactional
	public int modifyRoleResource(HashMap<String, String> map) {

		this.roleMapper.updateRole(map);

		String roleId=map.get("roleId");
		if(roleId==null || roleId ==""){
			return 0;
		}
		// 先删除menu_role表中的记录根据roleId
		int a=menuRoleMapper.deleteMenuRole(roleId);
		//插入menu_role 表中数据

		String menuId= map.get("menuId");
		String[] menuIdArr = menuId.split(",");
		int menuIdLen = menuIdArr.length;
		int b=0;
		for (int i = 0; i < menuIdLen; i++) {
			String med = menuIdArr[i];
			b+=menuRoleMapper.insertMenuRole(roleId, med);
		}
		if(menuIdLen!=b){
			return 0;
		}
		return b;
	}

	/**
	 * @description 添加角色
	 * @author thuang       
	 * @created 2019年11月11日 下午4:46:12
	 */
	@Override
	@Transactional
	public String addRole(HashMap<String, String> map) {
		//先创建角色
		if ("".equals(StringDUtil.changeNullToEmpty(map.get("roleName")))) {
			return "角色名不能为空";
		}
		String  menuIdStr= StringDUtil.changeNullToEmpty(map.get("menuId"));
		
		String uuid = UUIDGenerator.getUUID();
		map.put("roleId", uuid);
		roleMapper.addRole(map);
		
		if (!"".equals(menuIdStr)) {
			String[] menuIdList = menuIdStr.split(",");
			//再添加权限
			for (int i = 0; i < menuIdList.length; i++) {
				String menuId=menuIdList[i];
				
				menuRoleMapper.insertMenuRole(uuid, menuId);
			}
		}
		
		return "true";
	}

	/**
	 * 查询用户是否在管理员角色权限下
	 */
	@Override
	public List<HashMap<Object, Object>> findUserRoleListByParam(HashMap<Object, Object> map) throws Exception {
		
		return this.roleMapper.findUserRoleListByParam(map);
	}

	@Override
	public List<HashMap<Object, Object>> findUserRoleListByUserId(HashMap<Object, Object> map) throws Exception {
		List<HashMap<Object, Object>> list = this.roleMapper.findUserRoleListByUserId(map);

		List<HashMap<Object, Object>> departList = new ArrayList<>();

		if (list.size() > 0) {
			HashSet<String> depart_idHS=new HashSet<>();
			for (int i=0;i<list.size();i++){
				HashMap<Object, Object> roleMap = list.get(i);

				String depart_id = StringDUtil.changeNullToEmpty(roleMap.get("DEPART_ID"));

				String[] split = depart_id.split(",");

				for (int j=0;j<split.length;j++){
					String s = split[j];
					if (!"".equals(s)){
						depart_idHS.add(s);
					}
				}
			}


			departList = departMapper.findDepartById(depart_idHS);
		}


		return departList;
	}

}
