package com.palmnest.service.impl;

import cn.jiguang.common.utils.StringUtils;
import com.palmnest.application.dto.NameMapDto;
import com.palmnest.application.dto.VirtDeptUserDto;
import com.palmnest.service.MeetingService;
import com.palmnest.service.estateserve.AppUserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Service("meetingService")
public class MeetingServiceImpl implements MeetingService {

	@Autowired
	private AppUserManager appUserManager;

	@Override
	public Map<String, Object> getVirtDeptUser(Integer virtId, String name) {
		String                    virtName = "";
		Map<String, Object>       virtMap  = new HashMap<String, Object>();
		List<VirtDeptUserDto>     userList = new ArrayList<VirtDeptUserDto>();
		List<Map<String, Object>> list     = appUserManager.getVirtDeptUser(virtId, name);
		if (!CollectionUtils.isEmpty(list)) {
			Map<String, NameMapDto> deptMap = new HashMap<String, NameMapDto>();
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				if (null == map) {
					continue;
				}
				String deptId   = "";
				String deptName = "";
				String userId   = "";
				String openId   = "";
				String userName = "";
				String mobile   = "";
				if (map.containsKey("virName") && null != map.get("virName")) {
					virtName = map.get("virName").toString();
				}
				if (map.containsKey("departmentId") && null != map.get("departmentId")) {
					deptId = map.get("departmentId").toString();
				}
				if (map.containsKey("deptName") && null != map.get("deptName")) {
					deptName = map.get("deptName").toString();
				}
				if (map.containsKey("id") && null != map.get("id")) {
					userId = map.get("id").toString();
				}
				if (map.containsKey("userName") && null != map.get("userName")) {
					userName = map.get("userName").toString();
				}
				if (map.containsKey("userMobile") && null != map.get("userMobile")) {
					mobile = map.get("userMobile").toString();
				}
				if (map.containsKey("openId") && null != map.get("openId")) {
					openId = map.get("openId").toString();
				}

				if (StringUtils.isNotEmpty(deptId)) {
					if (deptMap.containsKey(deptId)) {
						//如果存在部门中了，就直接加进去
						NameMapDto            nameMapDto = deptMap.get(deptId);
						List<VirtDeptUserDto> userVo     = nameMapDto.getUserVoList();
						VirtDeptUserDto       dto        = getDto(virtId, virtName, Integer.parseInt(deptId), deptName, Integer.parseInt(userId), userName, mobile, openId);
						userVo.add(dto);
					} else {
						//如果不存在部门中，就新增一个
						List<VirtDeptUserDto> userVo = new ArrayList<VirtDeptUserDto>();
						VirtDeptUserDto       dto    = getDto(virtId, virtName, Integer.parseInt(deptId), deptName, Integer.parseInt(userId), userName, mobile, openId);
						userVo.add(dto);
						deptMap.put(deptId, new NameMapDto(deptName, userVo));
					}
				} else {
					VirtDeptUserDto dto = getDto(virtId, virtName, null, deptName, Integer.parseInt(userId), userName, mobile, openId);
					userList.add(dto);
				}
			}
			virtMap.put("virtId", virtId);
			virtMap.put("virtName", virtName);
			virtMap.put("deptList", deptMap);
			virtMap.put("userList", userList);
		}
		return virtMap;
	}

	//获取指定权限组下的用户列表new
	@Override
	public Map<Integer, Object> getGroupUser(Integer groupId, Integer orgId, String name, String type) {
		String                    virtName = "";
		Map<String, Object>       virtMap  = new HashMap<String, Object>();
		Map<Integer, Object>      orgMap   = new HashMap<Integer, Object>();
		List<VirtDeptUserDto>     userList = new ArrayList<VirtDeptUserDto>();
		List<Map<String, Object>> list     = null;
		if (type.equals("1")) {
			list = appUserManager.getGroupUser(groupId, orgId, name);
		} else if (type.equals("2")) {
			list = appUserManager.getNotGroupUser(groupId, orgId, name);
		}

		if (!CollectionUtils.isEmpty(list)) {
			Map<String, NameMapDto> deptMap = new HashMap<String, NameMapDto>();
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				if (null == map) {
					continue;
				}
				String  deptId   = "";
				String  deptName = "";
				String  userId   = "";
				String  openId   = "";
				String  userName = "";
				String  mobile   = "";
				Integer virtId   = null;
				if (map.containsKey("virName") && null != map.get("virName")) {
					virtName = map.get("virName").toString();
				}
				if (map.containsKey("departmentId") && null != map.get("departmentId")) {
					deptId = map.get("departmentId").toString();
				}
				if (map.containsKey("deptName") && null != map.get("deptName")) {
					deptName = map.get("deptName").toString();
				}
				if (map.containsKey("id") && null != map.get("id")) {
					userId = map.get("id").toString();
				}
				if (map.containsKey("userName") && null != map.get("userName")) {
					userName = map.get("userName").toString();
				}
				if (map.containsKey("userMobile") && null != map.get("userMobile")) {
					mobile = map.get("userMobile").toString();
				}
				if (map.containsKey("openId") && null != map.get("openId")) {
					openId = map.get("openId").toString();
				}
				if (map.containsKey("virtualArchId") && null != map.get("virtualArchId")) {
					virtId = Integer.valueOf(map.get("virtualArchId").toString());
				}

				if (null != virtId) {


					if (orgMap.containsKey(virtId)) {
						/*	Map<String, Object> virtMap = (Map<String, Object>)orgMap.get(virtId);
						Map<String, NameMapDto> deptExitMap =(Map<String, NameMapDto>)virtExitMap.get("deptList");
						List<VirtDeptUserDto> userExitList = (List<VirtDeptUserDto>)virtExitMap.get("userList");*/

						virtMap = (Map<String, Object>) orgMap.get(virtId);
						deptMap = (Map<String, NameMapDto>) virtMap.get("deptList");
						userList = (List<VirtDeptUserDto>) virtMap.get("userList");
					} else {
						virtMap = new HashMap<String, Object>();
						deptMap = new HashMap<String, NameMapDto>();
						userList = new ArrayList<VirtDeptUserDto>();
					}

					//部门人员操作
					if (StringUtils.isNotEmpty(deptId)) {
						if (deptMap.containsKey(deptId)) {
							//如果存在部门中了，就直接加进去
							NameMapDto            nameMapDto = deptMap.get(deptId);
							List<VirtDeptUserDto> userVo     = nameMapDto.getUserVoList();
							VirtDeptUserDto       dto        = getDto(virtId, virtName, Integer.parseInt(deptId), deptName, Integer.parseInt(userId), userName, mobile, openId);
							userVo.add(dto);
						} else {
							//如果不存在部门中，就新增一个
							List<VirtDeptUserDto> userVo = new ArrayList<VirtDeptUserDto>();
							VirtDeptUserDto       dto    = getDto(virtId, virtName, Integer.parseInt(deptId), deptName, Integer.parseInt(userId), userName, mobile, openId);
							userVo.add(dto);
							deptMap.put(deptId, new NameMapDto(deptName, userVo));
						}
					} else {
						VirtDeptUserDto dto = getDto(virtId, virtName, null, deptName, Integer.parseInt(userId), userName, mobile, openId);
						userList.add(dto);
					}
					//部门人员操作					

					virtMap.put("virtId", virtId);
					virtMap.put("virtName", virtName);
					virtMap.put("deptList", deptMap);
					virtMap.put("userList", userList);

					orgMap.put(virtId, virtMap);

				}
			}

		}
		return orgMap;
	}


	private VirtDeptUserDto getDto(Integer virtId, String virtName, Integer deptId, String deptName, Integer userId, String userName, String mobile, String openId) {
		VirtDeptUserDto dto = new VirtDeptUserDto();
		dto.setVirtId(virtId);
		dto.setVirtName(virtName);
		dto.setDeptId(deptId);
		dto.setDeptName(deptName);
		dto.setUserId(userId);
		dto.setName(userName);
		dto.setMobile(mobile);
		dto.setOpenId(openId);
		return dto;
	}

}
