package com.jeeplus.modules.API.maillist;

import java.util.List;
import java.util.Map;
import java.util.Set;

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

import com.jeeplus.common.utils.CacheUtils;
import com.jeeplus.modules.sys.service.SystemService;
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.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.oa.dao.LeaveDao;
import com.jeeplus.modules.oa.entity.Leave;
import com.jeeplus.modules.sys.dao.AreaDao;
import com.jeeplus.modules.sys.dao.OfficeDao;
import com.jeeplus.modules.sys.dao.RoleDao;
import com.jeeplus.modules.sys.dao.UserDao;
import com.jeeplus.modules.sys.entity.Area;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.Role;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workovertimeform.entity.WorkOvertimeForm;



/**
 * 人员Controller
 * @author zmy
 * @version 2017-04-17
 */

@Controller
@RequestMapping(value = "${frontPath}/maillist/colleague")

public class ColleagueController extends BaseController{

	@Autowired
	private UserDao userDao;
	@Autowired
	private OfficeDao officeDao;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private AreaDao areaDao;
	@Autowired
	private SystemService systemService;
	
	/**
	 * 公司人员列表
	 */
	    @ResponseBody
		@RequestMapping(value = "getMailListColleagueList", method=RequestMethod.GET)
		public AjaxJson findAllColleague (String companyId,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes)
		{  
		 AjaxJson j=new AjaxJson();
		 List<User> userList = systemService.findUserByCompanyId(companyId);
         List<Map<String,Object>> mapList = Lists.newArrayList();
         for(User u :userList){
        	 Map<String,Object> map = Maps.newHashMap();
        	 Office office = officeDao.get(u.getOffice());

        	 map.put("id", u.getId());
        	 map.put("photo", u.getPhoto());
        	 map.put("name", u.getName());
        	 map.put("mobile", u.getMobile());
			 map.put("isgly", 0);
             if(office!=null){
                 map.put("officeName", office.getName());
             }else{
                 map.put("officeName", "");
             }
        	 if(u.getName() != null && !u.getName().trim().equalsIgnoreCase("")) {
        		 char[] strChar = u.getName().trim().toCharArray();         		 
        		 if(StringUtils.isChinese(strChar[0])){
        			 String enName =  StringUtils.getPinYinHeadChar(u.getName().trim(),1);
        			// System.out.println(enName.charAt(0));
        			 map.put("initial",enName.charAt(0));
        		 }else{
        			// System.out.println(strChar[0]);
        			 map.put("initial",strChar[0]);
        		 }
        	 }else{
        		 map.put("initial","");
        	 }
        	 Role role1 = new Role();
     		 role1.setUser(u);
			 role1.setCompany(new Office(companyId));
     		 List<Role> roleList=roleDao.findList(role1);
     		 if (roleList!=null && roleList.size()!=0){
				 for(Role role : roleList){
					 if(org.apache.commons.lang3.StringUtils.isNotBlank(role.getEnname()) && role.getEnname().endsWith("gly")||role.getEnname().equals("system")){
						 map.put("isgly", 1);
					 }
				 }
			 }else {
				 map.put("isgly", 0);
			 }

        	 mapList.add(map); 
         }         
         j.put("data",mapList);
		 j.setMsg("获取公司人员列表成功");
		 j.setErrorCode(ErrorCode.code_1004);
		 return j;
		}
	 /**
	  * 获取成员信息
	  */
	 @ResponseBody
	 @RequestMapping(value = "getMailListColleagueInfoMap", method=RequestMethod.GET)
	 public AjaxJson findColleagueInfo (String id,String officeId,String companyId,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes)
	 {
		 AjaxJson j=new AjaxJson();
		 User user=	UserUtils.get(id);
		 if(user!=null){
			 Office office = new Office();
			 Office company = new Office();
			 if (StringUtils.isNotBlank(officeId) && StringUtils.isNotBlank(companyId)){
				 company = officeDao.get(officeId);
				 office = officeDao.get(companyId);
				 user.setOffice(company);
				 user.setCompany(office);
				 user.setCompanyName(company.getName());
				 user.setOfficeName(office.getName());
			 }else {
				 office = officeDao.get(user.getOffice());
				 company = officeDao.get(user.getCompany());
			 }
			 Leave leave = new Leave();
			 leave.setCreateBy(user);
			 WorkOvertimeForm over = new WorkOvertimeForm();
			 over.setCreateBy(user);

			 User user1=new User();
			 user1.setId(id);
			 Role role1 = new Role();
			 role1.setUser(user1);
			 List<Role> roleList = systemService.getRoleList(user.getId(),company.getId());
			 if(roleList!=null && roleList.size()!=0){
			 	for(Role r :roleList){
			 		if(r.getCompany().getId().equals(company.getId())){
						user.setRoleName(r.getName());
					}
				}
			 }
			 user.setOfficeName(office.getName());
			 user.setCompanyName(company.getName());

			 Map<String,Object> map = Maps.newHashMap();
			 map.put("logo", company==null? "":company.getLogo());
			 map.put("status", "0");
			 map.put("userInfo", user);
			 map.put("leaveStartTime", leave.getStartTime());
			 map.put("leaveEndTime", leave.getEndTime());
			 map.put("leaveDays", leave.getLeaveDays());
			 map.put("workOverTimeStartTime", over.getStartTime());
			 map.put("workOverTimeEndTime", over.getEndTime());
			 map.put("workOverTimeLongTime", over.getLongTime());
			 if(office.getArea()!=null && office.getArea().getId()!=null){
				 Area area = areaDao.get(office.getArea());
				 if(area!=null){
					 map.put("aeraName", area.getName());
				 }else{
					 map.put("aeraName", "");
				 }
			 }else{
				 map.put("aeraName", "");
			 }
			 j.put("data",map);
		 }else{
			 j.setSuccess(false);
			 j.setMsg("获取用户个人信息失败");
			 j.setErrorCode(ErrorCode.code_2005);
			 j.put("data","");
		 }

		 return j;
	 }
}
