package com.jeeplus.modules.API.maillist;


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.web.BaseController;
import com.jeeplus.modules.iim.entity.ChatHistory;
import com.jeeplus.modules.iim.service.ChatHistoryService;
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/contacts")

public class ContactsController extends BaseController{	
	@Autowired
	private ChatHistoryService chatHistoryService;
	
	@ResponseBody
	@RequestMapping(value = "getMailListContactsList", method=RequestMethod.GET)
	public AjaxJson getContactsList (String id,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes)
	{  
		AjaxJson j=new AjaxJson();	
		User u = UserUtils.get(id);
		ChatHistory chat = new ChatHistory();
		chat.setUserid1(u.getLoginName());
		List<ChatHistory> chatList=chatHistoryService.findList(chat);
		List<Map<String,Object>> mapList = Lists.newArrayList();
		for(int i = 0; i < chatList.size(); i++){
			ChatHistory ch = chatList.get(i);
			User user = UserUtils.getByLoginName(ch.getUserid2());
			Map<String,Object> map = Maps.newHashMap();
			map.put("id", user.getId());
			map.put("name", user.getName());
			map.put("mobile", user.getMobile());
			map.put("photo", user.getPhoto());	
			mapList.add(map);
		}
		j.put("data",mapList);
		j.setErrorCode(ErrorCode.code_1004);
		return j;
	}
}
