package com.jeeplus.modules.API.userinfo;

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.sys.utils.UserUtils;
import com.jeeplus.modules.sysuseroffice.entity.Useroffice;
import com.jeeplus.modules.sysuseroffice.service.UserofficeService;
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.sys.dao.UserDao;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;

/**
 * 查找用户Controller
 * 
 * @author zmy
 * @version 2017-04-17
 */
@Controller
@RequestMapping(value = "${frontPath}/userInfo/search")
public class SearchController extends BaseController {
	@Autowired
	private UserDao userDao;
    @Autowired
    private UserofficeService userofficeService;

    @ResponseBody
    @RequestMapping(value = "getUserInfoSearchList", method=RequestMethod.GET)
    public AjaxJson findCompanyUser(String nameOrMobile, HttpServletRequest request, HttpServletResponse response,
                                RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        User user = UserUtils.getUser();
        Useroffice useroffice = new Useroffice();
        List<Office> companyList = UserUtils.getAllCompany(user);
        List companyIds = new ArrayList();
        for (Office office:companyList){
            companyIds.add(office.getId());
        }
        useroffice.setCompanyIds(companyIds);
        useroffice.setName(nameOrMobile);
        useroffice.setDelFlag("0");
        List<Useroffice> list = userofficeService.searchUsersGroupByUserId(useroffice);
        if (list==null || list.size()==0) {
            useroffice.setMobile(nameOrMobile);
            useroffice.setName(null);
            list = userofficeService.searchUsersGroupByUserId(useroffice);
        }
        List<Map<String,Object>> mapList = Lists.newArrayList();
        for(int i = 0; i < list.size(); i++){
            Map<String,Object> map = Maps.newHashMap();
            User u = list.get(i).getUser();
            map.put("photo", u.getPhoto());
            map.put("name", u.getName());
            map.put("office", u.getOfficeName());
            map.put("officeId", u.getOffice().getId());
            map.put("companyId", u.getCompany().getId());
            map.put("id", u.getId());
            mapList.add(map);
        }
        j.put("data", mapList);
        j.setErrorCode(ErrorCode.code_1004);
        return j;
    }

	@ResponseBody
	@RequestMapping(value = "getUserInfoAllList", method=RequestMethod.GET)
	public AjaxJson findAllUser(String nameOrMobile, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		User user = new User();
		List<User> list = new ArrayList<User>();
		user.setName(nameOrMobile);
		if (userDao.searchUsers(user) != null) {
			list = userDao.searchUsers(user);
		}
		if (list.size()==0) {
			user.setMobile(nameOrMobile);
			user.setName(null);
			list = userDao.searchUsers(user);
		}
		List<Map<String,Object>> mapList = Lists.newArrayList();
		for(int i = 0; i < list.size(); i++){
			Map<String,Object> map = Maps.newHashMap();
			User u = list.get(i);
			map.put("photo", u.getPhoto());
			map.put("name", u.getName());
			map.put("office", u.getOffice().getName());	
			map.put("id", u.getId());
			mapList.add(map); 
		}
        logger.info("mapList.size="+mapList.size());
		j.put("data", mapList);
		j.setErrorCode(ErrorCode.code_1004);
		return j;
	}
}
