package com.jeeplus.modules.API.maillist;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeeplus.modules.utils.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.iim.dao.LayGroupDao;
import com.jeeplus.modules.iim.dao.LayGroupUserDao;
import com.jeeplus.modules.iim.entity.LayGroup;
import com.jeeplus.modules.iim.entity.LayGroupUser;
import com.jeeplus.modules.iim.service.LayGroupService;
import com.jeeplus.modules.sys.dao.UserDao;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
/**
 * 我的群组Controller
 * @author zmy
 * @version 2017-04-17
 */
@Controller
@RequestMapping(value = "${frontPath}/maillist/groups")
public class GroupController extends BaseController{
	@Autowired
	private LayGroupDao layGroupDao;
	@Autowired
	private LayGroupService layGroupService;
	@Autowired
	private UserDao userDao;
	@Autowired
	private LayGroupUserDao layGroupUserDao;
	/**
	 * 我创建的群组
	 */
	@ResponseBody
	@RequestMapping(value = "getMailListMyGroupList", method=RequestMethod.GET)
	public AjaxJson findMyGroups(String id,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes){
		AjaxJson j=new AjaxJson();
		LayGroup laygroup=new LayGroup();
		User user=new User();
		user.setId(id);
		laygroup.setCreateBy(user);
		List<LayGroup> groupList=layGroupDao.findList(laygroup);
		List<Map<String,Object>> mapList = Lists.newArrayList();
		List<Map<String,Object>> mapList2 = Lists.newArrayList();
		Map<String,Object> map2 = Maps.newHashMap();
		map2.put("groupcount", groupList.size());
		for(int i = 0; i < groupList.size(); i++){
			LayGroup g = groupList.get(i);
			Map<String,Object> map = Maps.newHashMap();
			map.put("groupName",g.getGroupname());
			map.put("avatar", g.getAvatar());
			map.put("groupId", g.getId());
			LayGroupUser lgu = new LayGroupUser();
			lgu.setGroup(g);
			List<LayGroupUser> lguList= layGroupUserDao.findList(lgu);
			map.put("userCount", lguList.size());
			mapList.add(map);						
		}
		map2.put("groupInfo", mapList);
		mapList2.add(map2);
		j.put("data", mapList2);
		j.setErrorCode(ErrorCode.code_1004);
		return j;
	}
	/**
	 * 我加入的群组
	 */
	@ResponseBody
	@RequestMapping(value = "getMailListAllGroupList", method=RequestMethod.GET)
	public AjaxJson findAllGroups(String id,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes){
		AjaxJson j=new AjaxJson();
		User user=new User();
		LayGroup laygroup=new LayGroup();
		user.setId(id);	
		laygroup.setCreateBy(user);
		List<LayGroup> list=layGroupDao.findList(laygroup);
		List<LayGroup> joinlist = layGroupService.findGroupList(user);
		List<LayGroup> alllist=new ArrayList<LayGroup>();
		alllist.addAll(list);
		alllist.addAll(joinlist);				
		List<Map<String,Object>> mapList2 = Lists.newArrayList();
		Map<String,Object> map2 = Maps.newHashMap();
		map2.put("groupcount", alllist.size());
		List<Map<String,Object>> mapList = Lists.newArrayList();
		for(int i = 0; i < alllist.size(); i++){
			LayGroup g = alllist.get(i);
			Map<String,Object> map = Maps.newHashMap();
			map.put("groupName",g.getGroupname());
			map.put("avatar", g.getAvatar());
			map.put("groupId", g.getId());
			LayGroupUser lgu = new LayGroupUser();
			lgu.setGroup(g);
			List<LayGroupUser> lguList= layGroupUserDao.findList(lgu);
			map.put("userCount", lguList.size());
			mapList.add(map);
		}		
		map2.put("groupInfo", mapList);
		mapList2.add(map2);
		j.put("data", mapList2);
		j.setErrorCode(ErrorCode.code_1004);
		return j;
	}	
	/**
	 * 添加群组成员
	 */
	@ResponseBody
	@RequestMapping(value="addMailListUser",method=RequestMethod.GET)
	public AjaxJson addMailListUser(String ids,String groupId,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes){
		AjaxJson j=new AjaxJson();
		String idArry[]=ids.split(",");			
		for(String id : idArry){		
		User user=userDao.get(id);
		LayGroup laygroup=layGroupService.get(groupId);
		LayGroupUser lguser = new LayGroupUser();
		lguser.setUser(user);
		lguser.setGroup(laygroup);
		 	if(layGroupUserDao.findList(lguser).size() == 0 && !user.getId().equals(laygroup.getCreateBy().getId())){
		 		lguser.setId(IdGen.uuid());
		 		lguser.preInsert();
		 		layGroupUserDao.insert(lguser);	
		 	}		 	
		}	
		j.setMsg("添加成员成功");
		j.setErrorCode(ErrorCode.code_1004);
		return j;
	}
	/**
	 * 删除群组成员
	 */
	@ResponseBody
	@RequestMapping(value="delMailListUser",method=RequestMethod.GET)
	public AjaxJson delMailListUser(String lguserIds,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes){
		AjaxJson j=new AjaxJson();
		String idArry[]=lguserIds.split(",");
		for(String id : idArry){
			LayGroupUser lguser = new LayGroupUser();	
			lguser.setId(id);
			layGroupUserDao.delete(lguser);
		}
		j.setMsg("删除成员成功");
		j.setErrorCode(ErrorCode.code_1004);
		return j;
	}
	/**
	 * 聊天信息
	 */
	@ResponseBody
	@RequestMapping(value="getMailListMembersList",method=RequestMethod.GET)
	public AjaxJson membersList(String groupId,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes){
		 AjaxJson j=new AjaxJson();
		 List<Map<String,Object>> mapList = Lists.newArrayList();
		 List<Map<String,Object>> mapList1 = Lists.newArrayList();
		 LayGroup layg= new LayGroup();
		 layg.setId(groupId);
		 LayGroupUser lgu = new LayGroupUser();
		 lgu.setGroup(layg);
		 Map<String,Object> map = Maps.newHashMap();
		 List<LayGroupUser> lguList= layGroupUserDao.findList(lgu);
		 for(LayGroupUser lu:lguList){
				User u = userDao.get(lu.getUser());
				Map<String,Object> map1 = Maps.newHashMap();
				map1.put("name", u.getName());
				map1.put("photo",u.getPhoto());	
				mapList1.add(map1);
		 }	
		 map.put("groupMembers", mapList1);
		 map.put("groupName", layGroupDao.get(groupId).getGroupname());
		 mapList.add(map);
		 j.put("data",mapList);
		 j.setErrorCode(ErrorCode.code_1004);
		 return j;
	}	
	/**
	 * 修改群名
	 */
	@ResponseBody
	@RequestMapping(value="saveMailListGroupName",method=RequestMethod.GET)
	public AjaxJson saveGroupName(String groupId,String groupName,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes){
		AjaxJson j=new AjaxJson();
		LayGroup lay = layGroupDao.get(groupId);
		lay.setGroupname(groupName);
		layGroupDao.update(lay);
		j.setMsg("修改成功");
		j.setErrorCode(ErrorCode.code_1004);
		return j;
	}
	/**
	 * 退出群组
	 */
	@ResponseBody
	@RequestMapping(value="delMailListGroup",method=RequestMethod.GET)
	public AjaxJson delGroup(String id,String groupId,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes){
		AjaxJson j=new AjaxJson();
		User user =userDao.get(id);
		LayGroup group = layGroupDao.get(groupId);
		LayGroupUser layGroupUser =new LayGroupUser();
		layGroupUser.setUser(user);
		layGroupUser.setGroup(group);
		layGroupUserDao.delete(layGroupUserDao.findList(layGroupUser).get(0));
		j.setMsg("退出成功");
		j.setErrorCode(ErrorCode.code_1004);
		return j;
	}
}

