package com.palmnest.webapp.controller;

import com.palmnest.application.CodeConstants;
import com.palmnest.application.Constants;
import com.palmnest.application.ElementConstant;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.bean.Page;
import com.palmnest.application.bean.RoleFloorTypeInfo;
import com.palmnest.application.bean.RoleInfoDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.DateUtils;
import com.palmnest.application.dto.CommonDto;
import com.palmnest.application.dto.OrgDto;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.input.UserFormUpdateDto;
import com.palmnest.application.dto.output.ElementDto;
import com.palmnest.application.dto.output.UserInfoOutPutDto;
import com.palmnest.model.*;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.model.repair.RepairConfig;
import com.palmnest.model.repair.UserRepairType;
import com.palmnest.service.*;
import com.palmnest.service.estateserve.FloorInfoManager;
import com.palmnest.service.org.OrganizationService;
import com.palmnest.service.repair.UserRepairTypeManager;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 用户管理前端控制器
 *
 * @author yubing
 * @since 2014-09-24
 */
@Slf4j
@Controller
@RequestMapping("/admin/users*")
public class UserController extends BaseFormController {

	private UserManager           userManager;
	private RoleManager           roleManager;
	private OrganizationManager   organizationManager;
	@Autowired
	private ElementGroupManager   elementGroupManager;
	@Autowired
	private UserRepairTypeManager userRepairTypeManager;
	@Autowired
	private FloorInfoManager      floorInfoManager;
	@Autowired
	private AppUserService        appUserService;
	@Autowired
	private OrganizationService   organizationService;

	@Autowired
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	@Autowired
	public void setRoleManager(RoleManager roleManager) {
		this.roleManager = roleManager;
	}

	@Autowired
	public void setOrganizationManager(OrganizationManager organizationManager) {
		this.organizationManager = organizationManager;
	}

	/**
	 * 跳转用户列表页面
	 */
	@RequestMapping(value = "/list")
	public ModelAndView handleRequest(HttpServletRequest request,
									  HttpServletResponse response) throws Exception {
		initFunctionOperate(request);       //初始化功能操作权限
		User  user  = (User) request.getSession().getAttribute("UserCache");//----------联调时需放开注释
		Model model = new ExtendedModelMap();

		//页面项目列表
		List<OrgDto> dtos = appUserService.getUserOrgs(user.getId());

		//构建项目id字符串
		String orgStr = "";
		if (!user.isAdmin()) {
			//如果当前登录用户不为admin或者系统管理员,则需要按项目查询
			if (dtos != null && dtos.size() > 0) {
				for (OrgDto org : dtos) {
					orgStr += "," + org.getId();
				}
				orgStr = orgStr.substring(1);
			}
		}

		model.addAttribute("roleList", roleManager.getRoleListByOrg(orgStr));
		model.addAttribute("orgs", dtos);
		return new ModelAndView("admin/userList", model.asMap());
	}

	/**
	 * 系统用户列表条件查询
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("userList")
	@ResponseBody
	public WebPageResultDto getUserList(HttpServletRequest request, HttpServletResponse response) {

		User   user     = (User) request.getSession().getAttribute("UserCache");//----------联调时需放开注释
		String username = request.getParameter("username");//条件查询用户名过滤
		String roleId   = request.getParameter("roleId");//条件查询角色过滤
		String orgId    = request.getParameter("orgId");//条件查询项目过滤id
		String pageNo   = request.getParameter("pageNo");//页码
		String pageSize = request.getParameter("pageSize");//每页显示数

		if (StringUtils.isEmpty(pageNo)) {
			pageNo = "1";
		}

		if (StringUtils.isEmpty(pageSize)) {
			pageSize = "10";
		}

		String orgStr = "";
		if (!user.isAdmin()) {
			//如果当前登录用户不为admin或者系统管理员,则需要按项目查询
			orgStr = getUserManageOrgIds(user);
		}

		//如果项目过滤条件不为空
		if (StringUtils.isNotBlank(orgId)) {
			orgStr = orgId;
		}

		try {
			Page             pagenation = userManager.queryUser(Integer.parseInt(pageNo), Integer.parseInt(pageSize), username, user, orgStr, roleId);
			WebPageResultDto dto        = new WebPageResultDto("200", "成功", pagenation.getItems());
			dto.setCount(pagenation.getTotalCount());
			return dto;
		} catch (Exception e) {
			log.error("查询用户列表失败 getUserList ===>" + e.getMessage());
		}

		return new WebPageResultDto("100", "查询用户列表失败", null);
	}


	/**
	 * 跳转 用户详情/新增用户 页面
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/jumpUserInfo")
	public ModelAndView jumpUserInfo(HttpServletRequest request) {

		Model           model       = new ExtendedModelMap();
		User            user        = (User) request.getSession().getAttribute("UserCache");
		List<TbCompany> companyList = new ArrayList<TbCompany>();
		//如果当前登录用户为admin或者系统管理员,加载所有运营中心列表;否则只加载他所属的运营中心
		if (!user.isAdmin()) {
			Set<TbCompany> companyList1 = user.getCompanys();
			companyList.addAll(companyList1);
		} else {
			companyList = organizationManager.getCompanyList(ElementConstant.YESORNO_1);
		}

		model.addAttribute("user", user);//当前登录操作用户
		model.addAttribute("id", request.getParameter("userid"));//被修改用户的id
		model.addAttribute("funcid", request.getParameter("funcid"));//功能id
		model.addAttribute("companyList", companyList);
		model.addAttribute("actType", request.getParameter("actType"));
		return new ModelAndView("admin/userForm", model.asMap());
	}


	/**
	 * 系统用户编辑保存
	 */
	@RequestMapping(value = "/form")
	@ResponseBody
	public AppResultDto formRequest(@ModelAttribute UserFormUpdateDto userFormUpdateDto, HttpServletRequest request) {

		Map<String, Object> map     = new HashMap<String, Object>();
		String              actType = request.getParameter("actType");
		String              userid  = userFormUpdateDto.getId();

		User user = new User();

		if (StringUtils.isNotBlank(userid)) {//userid不为空说明是修改操作
			user = userManager.get(Long.parseLong(userid));//根据id获取用户信息

			for (TbCompany company : user.getCompanys()) {//用户所属运营中心
				map.put("companyid", company.getId());
				break;
			}
			for (TbDept dept : user.getDepts()) {//用户所属部门
				map.put("deptid", dept.getId());
				break;
			}
		}

		//用户名唯一校验
		if (StringUtils.isNotBlank(userFormUpdateDto.getUsername())) {
			//校验用户名是否已存在
			String username  = userFormUpdateDto.getUsername();
			String searchSql = "from User where username='" + username + "' and ISENABLED='1' ";
			if (null != user.getId()) {
				searchSql += " and id <>" + user.getId();
			}
			List<User> listUsers = userManager.queryUserBySql(searchSql);

			//校验用户名是否已存在
			if (listUsers != null && listUsers.size() >= 1) {
				return AppResultUtil.createErrorDto("100", "用户名已存在");
			}
		}

		//用户手机号唯一校验
		if (StringUtils.isNotEmpty(userFormUpdateDto.getTelephone())) {
			String telephone = userFormUpdateDto.getTelephone();
			String searchSql = "from User where telephone=" + telephone + " and ISENABLED='1' ";
			if (null != user.getId()) {
				searchSql += " and id <>" + user.getId();
			}
			List<User> listUsers = userManager.queryUserBySql(searchSql);

			if (listUsers.size() >= 1) {
				map.put("funcid", request.getParameter("funcid"));
				String retCode = "4";
				map.put("errormsg", "手机号已经存在,请修改");
				map.put("id", user.getId());
				map.put("userid", user.getId());
				map.put("retCode", retCode);
				return AppResultUtil.createCommonDto("100", "手机号已经存在,请修改", map);
			}
		}


		if ("save".equals(actType)) {//当actType为save时,表示该操作为新增或者修改;否则为跳转界面操作;
			String empDate  = userFormUpdateDto.getEmpdate();
			String sex      = userFormUpdateDto.getSexGroup();
			String position = userFormUpdateDto.getPosition();

			if (StringUtils.isBlank(userid)) {
				//新增操作
				user.setAccountEnabled(1);
				user.setAccountExpired(0);
				user.setAccountLocked(0);
				user.setCredentialsExpired(1);
				user.setCreatedTime(new Date());
				user.setIsenabled(ElementConstant.YESORNO_1);
			}

			String       orgnizationIds_ = userFormUpdateDto.getOrgnizationIds();//用户所管辖项目
			List<String> orgnizations    = new ArrayList<String>();
			if (StringUtils.isNotEmpty(orgnizationIds_)) {
				String[] companyIds = orgnizationIds_.split(";");
				for (String c : companyIds) {
					orgnizations.add(c);
				}
			}

			//属性设置
			//---- 有用属性 ----
			user.setUsername(userFormUpdateDto.getUsername());//用户名
			user.setFullName(userFormUpdateDto.getFullName());//姓名
			user.setPassword(userFormUpdateDto.getPassword());//密码
			user.setTelephone(userFormUpdateDto.getTelephone());//手机
			user.setUserType(userFormUpdateDto.getUserType());//用户类型
			user.setCompanys(getCompanySet(request));//所属运营中心
			user.setRoles(getRolesSet(request));//设置用户角色
			user.setLastUpdatedTime(new Date());//最后更新时间
			//---- 目前没用到的属性 ----
			if (StringUtils.isNotEmpty(empDate)) {
				user.setEmpDate(DateUtils.StrToDate(empDate, "yyyy-MM-dd"));
			}
			user.setSex(sex);//性别
			user.setPosition(position);//职位
			user.setDepts(getDeptSet(request));//所属部门
			user.setLastUpdatedBy(request.getRemoteUser());//最后修改人
			user.setUserCode(userFormUpdateDto.getWorkNo());//用户工号


			//一天用户不能存在多个会议室审批权限
          	/*
          	String roles = userFormUpdateDto.getRoleIds();
            Role meeting_role = userManager.findRoleByFunctionAndUser(CodeConstants.MEETING_AUDIT, user.getId());
            if(null!=meeting_role){
            	 return AppResultUtil.createCommonDto("100", "当前用户已经存在会议室审批权限:"+meeting_role.getName(), map);
            }else{}
            */

			String   roles   = userFormUpdateDto.getRoleIds();
			String   roleIds = "'#'";
			String[] strRole = roles.split(";");
			for (String s : strRole) {
				roleIds = roleIds + ",'" + s + "'";
			}
			List<Role> listRoles = userManager.findRoleBySystemCodeAndRoleIds(CodeConstants.MEETING_AUDIT, roleIds);
			if (listRoles.size() > 1) {

				String rName = "";
				if (listRoles.size() > 1) {
					for (Role r : listRoles) {
						rName = rName + r.getName() + "、";
					}
				}
				rName = rName.replaceAll("[、]$", "");
				return AppResultUtil.createCommonDto("100", "不能存在多个会议室审批权限:" + rName, map);
			}
			//一天用户不能存在多个会议室审批权限

			userManager.saveOrUpdateUser(user, orgnizations);

			//如果新增用户为项目管理员,则把项目权限赋给管理员
			if (user.getUserType() == 1 || user.getUserType() == 2 || user.getUserType() == 3) {
				organizationService.updateUserRole(orgnizations, user.getId());
			}

			//处理用户角色权限关系??????????????
			JSONObject                     jasonObject = JSONObject.fromObject(userFormUpdateDto.getRoleMap());
			Map<String, RoleFloorTypeInfo> roleMap     = jasonObject;
			if (roleMap != null) {
				userManager.setUserRoleInfo(user, roleMap, roles, request.getRemoteUser());
			}

			map.put("id", user.getId());
			map.put("userid", user.getId());
			map.put("funcid", request.getParameter("funcid"));
			return AppResultUtil.createSuccesDto(map);
		} else {
			if (StringUtils.isNotEmpty(userid) && !"null".equals(userid)) {
				map.put("edit", 1);  //编辑状态
			} else {
				map.put("edit", 0);  //新增状态
			}
			String retCode = request.getParameter("retCode");
			if ("1".equals(retCode)) {
				map.put("errormsg", "新增用户信息成功");
			} else if ("2".equals(retCode)) {
				map.put("errormsg", "修改用户信息成功");
			} else if ("3".equals(retCode)) {
				map.put("errormsg", "用户名已存在,请重新添加");
			} else if ("4".equals(retCode)) {
				map.put("errormsg", "手机号已经存在,请修改");
			}

			List<TbCompany> companyList   = organizationManager.getCompanyList(ElementConstant.YESORNO_1);
			List<Element>   positionGroup = this.getElementGroupManager().getElementsByGroupName("staffPostGroup");
			map.put("funcid", request.getParameter("funcid"));
			map.put("companyList", companyList);
			map.put("positionGroup", positionGroup);
			map.put("id", user.getId());
			map.put("userid", user.getId());
			if (StringUtils.isEmpty(retCode)) {
				return AppResultUtil.createCommonDto("200", "", map);
			}
			return AppResultUtil.createCommonDto("100", map.get("errormsg").toString(), map);
		}
	}

	/**
	 * 修改登录密码
	 */
	@RequestMapping(value = "/modifyPwd")
	public ModelAndView modifyPwd(HttpServletRequest request,
								  HttpServletResponse response) throws Exception {
		Model  model   = new ExtendedModelMap();
		String actType = request.getParameter("actType");
		if ("save".equals(actType)) {
			String oldPwd = request.getParameter("oldPwd");
			String newPwd = request.getParameter("newPwd");

			boolean retFlag = userManager.updateUserPwd(request.getRemoteUser(), oldPwd, newPwd);
			if (retFlag) {
				model.addAttribute("errormsg", "密码修改成功");
			} else {
				model.addAttribute("errormsg", "密码修改失败，原始密码错误，请检查.");
			}
		}
		return new ModelAndView("admin/userModifyPwd", model.asMap());
	}

	//获取角色集合
	public Set<Role> getRolesSet(HttpServletRequest request) {
		String    roleIds = request.getParameter("roleIds");
		Set<Role> roleSet = new HashSet<Role>();
		if (StringUtils.isNotEmpty(roleIds)) {
			String[] ids = roleIds.split(";");
			for (String id : ids) {
				roleSet.add(roleManager.get(Long.parseLong(id)));
			}
		}
		return roleSet;
	}

	//获取角色集合
	public Set<Element> getRepairTypeSet(HttpServletRequest request) {
		String       repairTypeIds = request.getParameter("repairTypeIds");
		Set<Element> typeSet       = new HashSet<Element>();
		if (StringUtils.isNotEmpty(repairTypeIds)) {
			String[] ids = repairTypeIds.split(";");
			for (String id : ids) {
				typeSet.add(elementGroupManager.getElementId((Long.parseLong(id))));
			}
		}
		return typeSet;
	}

	//获取公司集合
	public Set<TbCompany> getCompanySet(HttpServletRequest request) {
		String         companyid_ = request.getParameter("companyid");
		Set<TbCompany> companySet = new HashSet<TbCompany>();
		if (StringUtils.isNotEmpty(companyid_)) {
			companySet.add(organizationManager.getCompanyById(Integer.parseInt(companyid_)));
		}
		return companySet;
	}

	//获取部门集合
	public Set<TbDept> getDeptSet(HttpServletRequest request) {
		String      deptid_ = request.getParameter("deptid");
		Set<TbDept> deptSet = new HashSet<TbDept>();
		if (StringUtils.isNotEmpty(deptid_)) {
			deptSet.add(organizationManager.getDept(Integer.parseInt(deptid_)));
		}
		return deptSet;
	}

	//获取楼宇集合
	public Set<TbOrganization> getOrganizationSet(HttpServletRequest request) {
		String              organizationid_ = request.getParameter("organizationid");
		Set<TbOrganization> organizationSet = new HashSet<TbOrganization>();
		if (StringUtils.isNotEmpty(organizationid_)) {
			organizationSet.add(organizationManager.getOrganizationById(Integer.parseInt(organizationid_)));
		}
		return organizationSet;
	}

	//获取楼层集合
	public Set<FloorInfo> getFloorSet(HttpServletRequest request) {
		String         floorid_ = request.getParameter("floorid");
		Set<FloorInfo> floorSet = new HashSet<FloorInfo>();
		if (StringUtils.isNotEmpty(floorid_)) {
			floorSet.add(organizationManager.getFloor(Integer.parseInt(floorid_)));
		}
		return floorSet;
	}


	/**
	 * 更新
	 *
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/update/{id}")
	public String update(@PathVariable long id, Model model) {
		model.addAttribute(userManager.get(id));
		model.addAttribute("edit", 1);
		return "admin/userForm";
	}

	/**
	 * 删除角色
	 *
	 * @param id 角色id
	 * @return
	 */
	@RequestMapping(value = "/delete/{id}&{funcid}")
	public String delete(@PathVariable long id, @PathVariable long funcid) {
		User user = userManager.get(id);
		user.setIsenabled(ElementConstant.YESORNO_0);
		userManager.updateUser(user);
		return "redirect:/admin/users/list?funcid=" + funcid;
	}

	@RequestMapping(value = "/info/{id}&{funcid}")
	public String redirect(@PathVariable long id, @PathVariable long funcid, Model model) {
		model.addAttribute(userManager.get(id));
		model.addAttribute("funcid", funcid);
		return "admin/userInfo";
	}

	/**
	 * 获取角色下的楼层或者获取角色下的维修类型
	 *
	 * @return
	 */
	@RequestMapping(value = "/getRoleFloorOrRepair")
	@ResponseBody
	public AppResultDto getRoleFloorOrRepair(HttpServletRequest request, int type, String orgId) {
		User user = (User) request.getSession().getAttribute("UserCache");
		if (StringUtils.isEmpty(orgId)) {
			orgId = getUserManageOrgIds(user);
		}
		log.info("UserController.getRoleFloorOrRepair type:"+type+",orgId:"+orgId+",usersOrgId:"+orgId);

		List<CommonDto> list = new ArrayList<CommonDto>();
		if (type == 1) {//会议室楼层
			List<FloorInfo> floorList = floorInfoManager.getFloorHaveMeeting(orgId);
			if (floorList != null && floorList.size() > 0) {
				for (int i = 0; i < floorList.size(); i++) {
					FloorInfo floorInfo = floorList.get(i);
					list.add(new CommonDto(Long.parseLong(floorInfo.getId().toString()), floorInfo.getName()));
				}
			}
		} else if (type == 2) {//全楼层
			List<FloorInfo> floorList = floorInfoManager.getALLFloor(orgId);
			if (floorList != null && floorList.size() > 0) {
				for (int i = 0; i < floorList.size(); i++) {
					FloorInfo floorInfo = floorList.get(i);
					list.add(new CommonDto(Long.parseLong(floorInfo.getId().toString()), floorInfo.getName()));
				}
			}
		} else if (type == 4) { //维修类型

			if(null != orgId){
				List<ElementDto> elementDtos = elementGroupManager.getOrgElementGroupInfoV2(orgId, Constants.REPAIR_TYPE_CODE);
				if (elementDtos != null && elementDtos.size() > 0) {
					for (int i = 0; i < elementDtos.size(); i++) {
						ElementDto e = elementDtos.get(i);
						list.add(new CommonDto(e.getId(), e.getItemname()));
					}
				}
			}else {
				List<Element> repairTypeGroup = elementGroupManager.getElementsByGroupName("repairTypeGroup");
				if (repairTypeGroup != null && repairTypeGroup.size() > 0) {
					for (int i = 0; i < repairTypeGroup.size(); i++) {
						Element e = repairTypeGroup.get(i);
						list.add(new CommonDto(e.getId(), e.getItemname()));
					}
				}
			}
		}
		return AppResultUtil.createSuccesDto(list);

	}

	/**
	 * 根据项目id获取角色
	 *
	 * @return
	 */
	@RequestMapping(value = "/getRoleList")
	@ResponseBody
	public AppResultDto getRoleList(HttpServletRequest request) {

		String orgStr = request.getParameter("orgId");
		User   user   = (User) request.getSession().getAttribute("UserCache");

		List<Map<String, Object>> list     = new ArrayList<Map<String, Object>>();
		List<Role>                roleList = roleManager.getRoleListByOrg(orgStr);
		if (roleList != null && roleList.size() > 0) {
			for (int i = 0; i < roleList.size(); i++) {
				Role                role = roleList.get(i);
				Map<String, Object> map  = new HashMap<String, Object>();
				map.put("id", role.getId());
				map.put("name", role.getName());
				map.put("dataType", role.getFLOOR_REPAIR());
				list.add(map);
			}
		}
		return AppResultUtil.createSuccesDto(list);

	}


	/**
	 * 获取用户管理详情
	 *
	 * @return
	 */
	@RequestMapping(value = "/getUserInfo")
	@ResponseBody
	public AppResultDto getUserInfo(String userId) {
		if (StringUtils.isEmpty(userId)) {
			return AppResultUtil.createErrorDto("100", "用户Id为空");
		}
		UserInfoOutPutDto dto = new UserInfoOutPutDto();
		if (userManager.exists(Long.parseLong(userId))) {
			User user = userManager.get(Long.parseLong(userId));
			if (user != null) {
				dto.setId(user.getId());
				dto.setUsername(user.getUsername());
				dto.setFullName(user.getFullName());
//				dto.setPassword(user.getPassword());
				dto.setWorkNo(user.getUserCode());
				dto.setPosition(user.getPosition());
				dto.setTelephone(user.getTelephone());
				dto.setUserType(user.getUserType());

				for (TbCompany company : user.getCompanys()) {
					dto.setCompany(company.getId());
					break;
				}
				for (TbDept dept : user.getDepts()) {
					dto.setDept(dept.getId());
					break;
				}

				List<RoleInfoDto> list = new ArrayList<RoleInfoDto>();
				if (user.getRoles() != null) {
					Set<Role> roleSet = user.getRoles();
					for (Role role : roleSet) {
						RoleInfoDto roleDto = new RoleInfoDto();
						roleDto.setId(role.getId());
						roleDto.setName(role.getName());
						roleDto.setDataType(role.getFLOOR_REPAIR());
						List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
						if (role.getFLOOR_REPAIR() != null) {
							if (role.getFLOOR_REPAIR() == 1) {//楼层
								List<AppUserFloorRelation> userFloorList = userManager.getAppUserFloorByUserId(Integer.parseInt(userId), Integer.parseInt(role.getId().toString()));
								if (userFloorList != null && userFloorList.size() > 0) {
									for (int i = 0; i < userFloorList.size(); i++) {
										Map<String, Object>  map       = new HashMap<String, Object>();
										AppUserFloorRelation userFloor = userFloorList.get(i);
										map.put("id", userFloor.getFloor_id());
										if (floorInfoManager.exists(userFloor.getFloor_id())) {
											FloorInfo floorInfo = floorInfoManager.get(userFloor.getFloor_id());
											map.put("name", floorInfo.getName());
										}
										mapList.add(map);
									}
								}
							} else if (role.getFLOOR_REPAIR() == 4) {//维修类型
								List<UserRepairType> userRepairList = userRepairTypeManager.findByUserAndRole(Long.parseLong(userId), role.getId());
								if (userRepairList != null && userRepairList.size() > 0) {
									for (int i = 0; i < userRepairList.size(); i++) {
										Map<String, Object> map        = new HashMap<String, Object>();
										UserRepairType      userRepair = userRepairList.get(i);
										map.put("id", userRepair.getRepairTypeId());
										Element e = elementGroupManager.getElementId(userRepair.getRepairTypeId());
										if (e != null) {
											map.put("name", e.getItemname());
										}
										mapList.add(map);
									}
								}
							}
						}

						roleDto.setList(mapList);
						list.add(roleDto);
					}
				}
				dto.setList(list);
			}
		}
		return AppResultUtil.createSuccesDto(dto);

	}


	/**
	 * 获取所有公司职位
	 *
	 * @return
	 */
	@RequestMapping(value = "/getPosition")
	@ResponseBody
	public AppResultDto getPosition() {
		List<CommonDto> list          = new ArrayList<CommonDto>();
		List<Element>   positionGroup = this.getElementGroupManager().getElementsByGroupName("staffPostGroup");
		if (positionGroup != null && positionGroup.size() > 0) {
			for (int i = 0; i < positionGroup.size(); i++) {
				Element e = positionGroup.get(i);
//				list.add(new CommonDto(e.getId(),e.getItemname()));
				list.add(new CommonDto(Long.parseLong(e.getItemcode()), e.getItemname()));
			}
		}
		return AppResultUtil.createSuccesDto(list);
	}

}
