package com.hotent.uc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.controller.BaseController;
import com.hotent.base.controller.BaseController2;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.*;
import com.hotent.base.util.*;
import com.hotent.uc.dao.OrgUserDao;
import com.hotent.uc.manager.*;
import com.hotent.uc.model.*;
import com.hotent.uc.params.common.OrgExportObject;
import com.hotent.uc.params.group.GroupIdentity;
import com.hotent.uc.params.org.*;
import com.hotent.uc.params.orgRole.OrgRoleVo;
import com.hotent.uc.params.post.PostDueVo;
import com.hotent.uc.params.user.UserUnderVo;
import com.hotent.uc.params.user.UserVo;
import com.hotent.uc.util.AuthFilterUtil;
import com.hotent.uc.util.ContextUtil;
import com.hotent.uc.util.OrgUtil;
import com.hotent.uc.util.UpdateMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 组织模块接口
 * 
 * @author liangqf
 *
 */
@RestController
@RequestMapping("/api/org/v2/")
@Api(tags = "组织管理2")
@ApiGroup(group= {ApiGroupConsts.GROUP_UC})
public class OrgController2 extends BaseController2<OrgManager, Org> {

	@Autowired
	OrgManager orgService;
	@Autowired
	DemensionManager demService;
	@Autowired
	OrgPostManager postService;
	@Autowired
	OrgUserManager orgUserService;
	@Autowired
	UserUnderManager userUnderService;
	@Autowired
	UserImportManager userImportService;
	@Resource
	OrgRoleManager orgRoleService;
	@Autowired
	OrgAuthManager orgAuthService;
	@Autowired
	OrgJobManager orgJobManager;
	@Autowired
	OrgUserManager orgUserManager;
	@Autowired
	UserManager userService;
	@Resource
	OrgParamsManager orgParamsService;
	@Resource
    OrgUserDao orgUserDao;

    /**
     * 根据组织id和用户id删除组织下的用户，并岗位id为空
     * @param orgId,userId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "orgUser/deleteOrgById", method = RequestMethod.DELETE, produces = {
            "application/json; charset=utf-8" })
    @ApiOperation(value = "根据组织id和用户id删除组织下的用户，并岗位id为空", httpMethod = "DELETE", notes = "根据组织id和用户id删除组织下的用户，并岗位id为空")
    public CommonResult<String> deleteOrgById(
            @ApiParam(name = "orgId", value = "组织id", required = true) @RequestParam
                    String orgId,
            @ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId) throws Exception {
		CommonResult<String> commonResult = orgUserService.deleteOrgById(orgId, userId);
		return CommonResultUtil.<String>getResult(commonResult);
    }

	@RequestMapping(value = "orgs/getOrgPage", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取组织列表（带分页信息）", httpMethod = "POST", notes = "获取组织列表")
	public CommonResult<PageList<Org>> getOrgPage(
			@ApiParam(name = "queryFilter", value = "通用查询对象") @RequestBody QueryFilter<Org> queryFilter) throws Exception {
		PageList<Org> list = orgService.query(queryFilter);
		return CommonResult.<PageList<Org>>ok().value(list);
	}

	/**
	 * 添加组织
	 * 
	 * @param orgVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "org/addOrg", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "添加组织", httpMethod = "POST", notes = "添加组织")
	public CommonResult<String> addOrg(
			@ApiParam(name = "orgVo", value = "组织视图", required = true) @RequestBody OrgVo orgVo) throws Exception {
		CommonResult<String> commonResult = orgService.addOrg(orgVo);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 根据组织编码删除组织
	 * 
	 * @param code
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "org/deleteOrg", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "根据组织编码删除组织", httpMethod = "POST", notes = "根据组织编码删除组织（多个用,号隔开），连同其子组织、组织参数、岗位、组织人员关系及对应下属一起删除")
	public CommonResult<String> deleteOrg(
			@ApiParam(name = "codes", value = "组织编码", required = true) @RequestBody String codes) throws Exception {
		CommonResult<String> commonResult = orgService.deleteOrg(codes);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 修改组织
	 * 
	 * @param orgVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "org/updateOrg", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "修改组织", httpMethod = "POST", notes = "修改组织")
	@UpdateMethod(type=OrgVo.class)
	public CommonResult<String> updateOrg(
			@ApiParam(name = "orgVo", value = "组织视图", required = true) @RequestBody OrgVo orgVo) throws Exception {
		CommonResult<String> commonResult = orgService.updateOrg(orgVo);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 根据组织编码获取组织
	 * 
	 * @param code
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "org/getOrg", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据组织编码获取组织", httpMethod = "GET", notes = "根据组织编码获取组织")
	public CommonResult<Org> getOrg(@ApiParam(name = "code", value = "组织编码", required = true) @RequestParam String code)
			throws Exception {
		Map<String, Object> map = new HashMap<>();
		map.put("orgCode", code);
		map.put("group", "true");
		List<Map<String, Object>> list = orgUserDao.getUserNumByOrgCode(map);
		Org org = orgService.getOrg(code);
		org.setNowNum(list.size());
		return CommonResult.<Org>ok().value(org);
	}
	
	/**
	 * 根据组织编码获取组织限编
	 * @param codes
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "org/getOrgLimitByCodes", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据组织编码获取组织限编", httpMethod = "GET", notes = "根据组织编码获取组织限编")
	public CommonResult<PageList<Org>> getOrgLimitByCodes(@ApiParam(name = "codes", value = "组织编码", required = true) @RequestParam String codes)
			throws Exception {
		QueryFilter queryFilter = QueryFilter.build().withPage(new PageBean(1, PageBean.WITHOUT_PAGE));
		queryFilter.addFilter("code", codes, QueryOP.IN,FieldRelation.AND,"group_1");
		PageList<Org> query = orgService.query(queryFilter);
		if (BeanUtils.isNotEmpty(query) && BeanUtils.isNotEmpty(query.getRows())) {
			for (Org org : query.getRows()) {
				Map<String, Object> map = new HashMap<>();
				map.put("orgCode", org.getCode());
				map.put("group", "true");
				List<Map<String, Object>> list = orgUserDao.getUserNumByOrgCode(map);
				org.setNowNum(list.size());
			}
		}
		return CommonResult.<PageList<Org>>ok().value(query);
	}
	
	/**
	 * 根据组织id获取组织
	 * 
	 * @param code
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "org/get", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据组织id获取组织", httpMethod = "GET", notes = "根据组织id获取组织")
	public CommonResult<Org> get(@ApiParam(name = "id", value = "组织id", required = true) @RequestParam String id)
			throws Exception {
		Org org = orgService.get(id);
		if(BeanUtils.isEmpty(org)){
			org = orgService.getByCode(id);
		}
		return CommonResult.<Org>ok().value(org);
	}
	
	/**
	 * 获取当前用户主组织
	 * 
	 * @param code
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "org/getOrgMaster", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据当前用户主组织", httpMethod = "GET", notes = "根据当前用户主组织")
	public CommonResult<Org> getOrgMaster()
			throws Exception {
		Org org = orgService.getOrgMaster(ContextUtil.getCurrentUser().getAccount());
		return CommonResult.<Org>ok().value(org);
		
	}

	/**
	 * 保存组织参数
	 * 
	 * @param orgCode
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgParam/saveOrgParams", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "保存组织参数", httpMethod = "POST", notes = "保存组织参数，参数params格式[{\"alias\":\"a1\",\"value\":\"v1\"},{\"alias\":\"a2\",\"value\":\"v2\"}]")
	public CommonResult<String> saveOrgParams(
			@ApiParam(name = "orgCode", value = "组织编码", required = true) @RequestParam String orgCode,
			@ApiParam(name = "params", value = "参数值", required = true) @RequestBody List<ObjectNode> params)
			throws Exception {
		CommonResult<String> commonResult = orgService.saveOrgParams(orgCode, params);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 获取组织参数
	 * 
	 * @param orgCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgParam/getOrgParams", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取组织参数", httpMethod = "GET", notes = "根据组织编码获取组织参数")
	public CommonResult<List<OrgParams>> getOrgParams(
			@ApiParam(name = "orgCode", value = "组织编码", required = true) @RequestParam String orgCode)
			throws Exception {
		List<OrgParams> orgParamsList = orgService.getOrgParams(orgCode);
		return CommonResult.<List<OrgParams>>ok().value(orgParamsList);
	}

	/**
	 * 获取指定组织参数
	 * 
	 * @param orgCode
	 * @param alias
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgParam/getParamByAlias", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取指定组织参数", httpMethod = "GET", notes = "根据组织编码和参数别名获取组织参数")
	public CommonResult<OrgParams> getParamByAlias(
			@ApiParam(name = "orgCode", value = "组织代码", required = true) @RequestParam String orgCode,
			@ApiParam(name = "alias", value = "参数代码", required = true) @RequestParam String alias) throws Exception {
		CommonResult<OrgParams> commonResult = orgService.getParamByAlias(orgCode, alias);
		return CommonResultUtil.<OrgParams>getResult(commonResult);
	}

	/**
	 * 加入用户
	 * 
	 * @param orgUserVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgUser/addOrgUser", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "加入用户", httpMethod = "POST", notes = "向组织中加入系统已有的用户")
	public CommonResult<String> addOrgUser(
			@ApiParam(name = "orgUserVo", value = "组织用户", required = true) @RequestBody OrgUserVo orgUserVo)
			throws Exception {
		CommonResult<String> commonResult = orgService.addOrgUser(orgUserVo);
		return CommonResultUtil.<String>getResult(commonResult);
	}

    /**
     * 新增用户是给用户设置组织
     *
     * @param account,orgCode
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "orgUser/addOrgUserByCode", method = RequestMethod.POST, produces = {
            "application/json; charset=utf-8" })
    @ApiOperation(value = "新增用户是给用户设置组织", httpMethod = "POST", notes = "新增用户是给用户设置组织")
    public CommonResult<String> addOrgUserByCode(
            @ApiParam(name = "account", value = "组织用户", required = true) @RequestParam String account,
            @ApiParam(name = "orgCode", value = "组织编码", required = true) @RequestParam String orgCode)
            throws Exception {
        String[] orgCodes = orgCode.split(",");

        for (String code : orgCodes) {
            OrgUserVo orgUserVo = new OrgUserVo();
            orgUserVo.setAccount(account);
            orgUserVo.setOrgCode(code);
            orgService.addOrgUser(orgUserVo);
        }

		return CommonResult.<String>ok().message("设置成功！");
    }

	/**
	 * 用户取消加入组织
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgUser/delOrgUser", method = RequestMethod.DELETE, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "用户取消加入组织", httpMethod = "DELETE", notes = "用户取消加入组织，ids为用户组织关系id，多个用英文逗号隔开")
	public CommonResult<String> delOrgUser(
			@ApiParam(name = "ids", value = "用户组织关系id字符串", required = true) @RequestParam String ids) throws Exception {
		CommonResult<String> commonResult = orgService.delOrgUser(ids);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 判断用户在某维度下是否有主组织
	 * 
	 * @param account
	 * @param demCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgUser/getUserIsMaster", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "判断用户是否有主组织", httpMethod = "GET", notes = "判断用户是否有主组织")
	public CommonResult<Boolean> getUserIsMaster(
			@ApiParam(name = "account", value = "用户帐号", required = true) @RequestParam String account,
			@ApiParam(name = "demCode", value = "维度编码", required = true) @RequestParam String demCode)
			throws Exception {
		CommonResult<Boolean> commonResult = orgService.getUserIsMaster(account, demCode);
		return CommonResultUtil.<Boolean>getResult(commonResult);
	}

	/**
	 * 设置人员（取消）主岗位
	 * 
	 * @param account
	 * @param postCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgPost/setMaster", method = RequestMethod.PUT, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "设置人员（取消）主岗位", httpMethod = "PUT", notes = "设置人员（取消）主岗位")
	public CommonResult<String> setMaster(
			@ApiParam(name = "account", value = "用户帐号", required = true) @RequestParam String account,
			@ApiParam(name = "postCode", value = "岗位编码", required = true) @RequestParam String postCode)
			throws Exception {
		CommonResult<String> commonResult = orgService.setMaster(account, postCode);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 获取组织树
	 * 
	 * @param demCode
	 * @param pOrgCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgs/getTreeDataByDem", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取组织树", httpMethod = "GET", notes = "获取组织树")
	public CommonResult<List<OrgTree>> getTreeDataByDem(
			@ApiParam(name = "demCode", value = "维度编码", required = true) @RequestParam String demCode,
			@ApiParam(name = "pOrgCode", value = "父组织编码",required=false) @RequestParam(required=false) String pOrgCode) throws Exception {
		List<OrgTree> orgTreeList = orgService.getTreeDataByDem(demCode, pOrgCode);
		return CommonResult.<List<OrgTree>>ok().value(orgTreeList);
	}

	/**
	 * 设置（取消）（主）负责人
	 * 
	 * @param account
	 * @param orgCode
	 * @param isCharge
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgUser/setOrgCharge", method = RequestMethod.PUT, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "设置（取消）（主）负责人", httpMethod = "PUT", notes = "isCharge为true表示设置主负责人，为false时，若此时为负责人，则降为非负责人")
	public CommonResult<String> setOrgCharge(
			@ApiParam(name = "account", value = "用户帐号", required = true) @RequestParam String account,
			@ApiParam(name = "orgCode", value = "组织编码", required = true) @RequestParam String orgCode,
			@ApiParam(name = "isCharge", value = "是否设置主负责人", required = true) @RequestParam Boolean isCharge)
			throws Exception {
		CommonResult<String> commonResult = orgService.setOrgCharge(account, orgCode, isCharge);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 组织人员添加下属
	 * 
	 * @param userUnder
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "userUnder/addUserUnders", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "组织人员添加下属", httpMethod = "POST", notes = "组织人员添加下属")
	public CommonResult<String> addUserUnders(
			@ApiParam(name = "userUnder", value = "下属用户", required = true) @RequestBody UserUnderVo userUnder)
			throws Exception {
		CommonResult<String> commonResult = orgService.addUserUnders(userUnder);
		return commonResult;
	}

	/**
	 * 组织人员删除下属
	 * 
	 * @param account
	 * @param orgCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "userUnder/delUserUnders", method = RequestMethod.DELETE, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "组织人员删除下属", httpMethod = "DELETE", notes = "组织人员删除下属")
	public CommonResult<String> delUserUnders(
			@ApiParam(name = "account", value = "用户帐号", required = true) @RequestParam String account,
			@ApiParam(name = "orgCode", value = "组织编码", required = true) @RequestParam String orgCode)
			throws Exception {
		CommonResult<String> commonResult = orgService.delUserUnders(account, orgCode);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 获取用户在某组织下的下属
	 * 
	 * @param account
	 * @param orgCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "userUnder/getUserUnders", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户在某组织下的下属", httpMethod = "GET", notes = "获取用户在某组织下的下属")
	public CommonResult<List<UserVo>> getUserUnders(
			@ApiParam(name = "account", value = "用户帐号", required = true) @RequestParam String account,
			@ApiParam(name = "orgCode", value = "组织编码", required = true) @RequestParam String orgCode)
			throws Exception {
		List<UserVo> userVoList = orgService.getUserUnders(account, orgCode);
		return CommonResult.<List<UserVo>>ok().value(userVoList);
	}
	
	/**
	 * 获取用户在某组织下的下属(含分页)
	 * @param filter
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "userUnder/getUserUndersPage", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户在某组织下的下属(含分页)", httpMethod = "POST", notes = "获取用户在某组织下的下属(含分页)",hidden=true)
	public CommonResult<PageList<UserUnder>> getUserUndersPage(
			@ApiParam(name = "filter", value = "查询对象", required = true) @RequestBody QueryFilter filter)
			throws Exception {
		Page<UserUnder> list = (Page<UserUnder>)userUnderService.getUserUnder(filter);
		PageList<UserUnder> userUnderPageList = new PageList<>(list);
		return CommonResult.<PageList<UserUnder>>ok().value(userUnderPageList);
	}
	
	@RequestMapping(value = "userUnder/delUnderUserByIds", method = RequestMethod.DELETE, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "根据id删除下属", httpMethod = "DELETE", notes = "根据id删除下属，多个用户逗号隔开",hidden=true)
	public CommonResult<String> delUnderUserByIds(@ApiParam(name="ids",value="记录id字符串",required=true) @RequestParam String ids) throws Exception{
		String[] idArr = ids.split(",");
		userUnderService.removeByIds(idArr);
		return CommonResult.<String>ok().message("删除成功！");
	}

	/**
	 * 添加分级管理
	 * 
	 * @param orgAuthVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgAuth/saveOrgAuth", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "添加分级管理", httpMethod = "POST", notes = "添加分级管理")
	public CommonResult<String> saveOrgAuth(
			@ApiParam(name = "orgAuthVo", value = "分级组织管理", required = true) @RequestBody OrgAuthVo orgAuthVo)
			throws Exception {
		CommonResult<String> commonResult = orgService.saveOrgAuth(orgAuthVo);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 删除分级管理
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgAuth/deleteOrgAuth", method = RequestMethod.DELETE, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "删除分级管理", httpMethod = "DELETE", notes = "删除分级管理")
	public CommonResult<String> deleteOrgAuth(
			@ApiParam(name = "id", value = "分级管理id", required = true) @RequestParam String id) throws Exception {
		CommonResult<String> commonResult = orgService.deleteOrgAuth(id);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 获取分级管理列表
	 * 
	 * @param account
	 * @param orgCode
	 * @param demCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgAuths/getOrgAuthList", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取分级管理列表", httpMethod = "GET", notes = "获取分级管理列表")
	public CommonResult<PageList<OrgAuth>> getOrgAuthList(
			@ApiParam(name = "account", value = "用户帐号", required = true) @RequestParam String account,
			@ApiParam(name = "orgCode", value = "组织编码", required = false) @RequestParam(required=false) String orgCode,
			@ApiParam(name = "demCode", value = "维度编码", required = false) @RequestParam(required=false) String demCode)
			throws Exception {
		Page<OrgAuth> list = orgService.getOrgAuthList(account, orgCode, demCode);
		PageList<OrgAuth> orgAuthPageList = new PageList<>(list);
		return CommonResult.<PageList<OrgAuth>>ok().value(orgAuthPageList);
	}

	/**
	 * 获取分级管理
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgAuth/getOrgAuth", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取分级管理", httpMethod = "GET", notes = "根据id获取分级管理")
	public CommonResult<OrgAuth> getOrgAuth(@ApiParam(name = "id", value = "分级管理id", required = true) @RequestParam String id)
			throws Exception {
		OrgAuth orgAuth = orgService.getOrgAuth(id);
		return CommonResult.<OrgAuth>ok().value(orgAuth);
	}

	/**
	 * 获取子组织
	 * 
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgs/getByParentId", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取子组织", httpMethod = "GET", notes = "根据父组织id获取子组织")
	public CommonResult<List<Org>> getByParentId(
			@ApiParam(name = "parentId", value = "父组织id", required = true) @RequestParam String parentId)
			throws Exception {
		if(BeanUtils.isEmpty(orgService.get(parentId))){
			throw new RuntimeException("根据组织id【"+parentId+"】没有找到对应的组织");
		}
		List<Org> orgList = orgService.getByParentId(parentId);
		return CommonResult.<List<Org>>ok().value(orgList);
	}
	/**
	 * 获取子组织(只获取底下一层子组织)
	 *
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgs/getOrgsByparentId", method = RequestMethod.GET, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "获取子组织(只获取底下一层子组织)", httpMethod = "GET", notes = "根据父组织id获取子组织")
	public CommonResult<List<Org>> getOrgsByparentId(@ApiParam(name = "parentId", value = "父组织id", required = true) @RequestParam String parentId)
					throws Exception {
		List<Org> orgList = orgService.getOrgsByparentId(parentId);
		return CommonResult.<List<Org>>ok().value(orgList);
	}

	/**
	 * 获取用户所属（主）组织
	 * 
	 * @param account
	 * @param demCode
	 * @param isMain
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgs/getUserOrgs", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户所属（主）组织", httpMethod = "GET", notes = "获取用户所属（主）组织")
	public CommonResult<List<Org>> getUserOrgs(
			@ApiParam(name = "account", value = "用户帐号", required = true) @RequestParam String account,
			@ApiParam(name = "demCode", value = "维度编码", required = false) @RequestParam(required=false) String demCode,
			@ApiParam(name = "isMain", value = "是否主组织", required = false) @RequestParam(required=false) Boolean isMain)
			throws Exception {
		List<Org> orgList = orgService.getUserOrgs(account, demCode, isMain);
		return CommonResult.<List<Org>>ok().value(orgList);
	}
	
	@RequestMapping(value = "orgs/getOrgsByAccount", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户所属组织", httpMethod = "POST", notes = "获取用户所属组织")
	public CommonResult<List<Org>> getOrgsByAccount(@ApiParam(name = "account", value = "用户帐号", required = true) @RequestParam String account) throws Exception{
		List<Org> orgList = orgService.getOrgsByAccount(account);
		return CommonResult.<List<Org>>ok().value(orgList);
	}
	
	@RequestMapping(value = "orgs/getOrgListByUserId", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户所属组织", httpMethod = "GET", notes = "获取用户所属组织")
	public CommonResult<List<Org>> getOrgListByUserId(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId) throws Exception{
		List<Org> orgList = orgService.getOrgListByUserId(userId);
		return CommonResult.<List<Org>>ok().value(orgList);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "orgs/getUserOrgPage", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户所属组织", httpMethod = "POST", notes = "获取用户所属组织")
	public CommonResult<PageList<HashMap<String, Object>>> getUserOrgPage(@ApiParam(name = "filter", value = "查询对象", required = true) @RequestBody QueryFilter filter) throws Exception{
		IPage<HashMap<String,Object>> p = orgUserService.getUserOrgPage(filter);
		PageList<HashMap<String, Object>> pageList = new PageList<>(p);
		return CommonResult.<PageList<HashMap<String, Object>>>ok().value(pageList);
	}

	/**
	 * 获取组织下的人员
	 * 
	 * @param orgCodes
	 * @param isMain
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgUsers/getUsersByOrgCodes", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 获取组织下的人员", httpMethod = "GET", notes = " 获取组织下的人员，orgCodes组织编码多个用英文逗号隔开")
	public CommonResult<List<UserVo>> getUsersByOrgCodes(
			@ApiParam(name = "orgCodes", value = "组织编码", required = true) @RequestParam String orgCodes,
			@ApiParam(name = "isMain", value = "是否主岗位", required = false) @RequestParam(required=false) Boolean isMain)
			throws Exception {
		List<UserVo> userVoList = orgService.getUsersByOrgCodes(orgCodes, isMain);
		return CommonResult.<List<UserVo>>ok().value(userVoList);
	}
	
	/**
	 * 根据组织ID获取组织的负责人组织关系
	 * 
	 * @param orgCodes
	 * @param isMain
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgUsers/getChargesByOrgId", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "根据组织ID获取组织的负责人组织关系", httpMethod = "GET", notes = "根据组织ID获取组织的负责人组织关系")
	public CommonResult<List<User>> getChargesByOrgId(
			@ApiParam(name = "orgId", value = "组织id", required = true) @RequestParam String orgId,
			@ApiParam(name = "isMain", value = "是否主组织", required = false) @RequestParam(required=false) Boolean isMain,
			@ApiParam(name = "demCode", value = "维度编码（不传则为默认维度）", required = false) @RequestParam(required=false) Boolean demCode)
			throws Exception {
		List<User> userList = orgService.getChargesByOrgId(orgId, isMain, demCode);
		return CommonResult.<List<User>>ok().value(userList);
	}
	

	/**
	 * 获取组织下的岗位
	 * 
	 * @param orgCodes
	 * @param isMain
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgPosts/getPostsByOrgCodes", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 获取组织下的岗位", httpMethod = "GET", notes = " 获取组织下的岗位，orgCodes组织编码多个用英文逗号隔开")
	public CommonResult<List<OrgPost>> getPostsByOrgCodes(
			@ApiParam(name = "orgCodes", value = "组织编码", required = true) @RequestParam String orgCodes,
			@ApiParam(name = "isMain", value = "是否主岗位", required = false) @RequestParam(required=false) Boolean isMain)
			throws Exception {
		List<OrgPost> orgPostList = orgService.getPostsByOrgCodes(orgCodes, isMain);
		return CommonResult.<List<OrgPost>>ok().value(orgPostList);
	}
	
	/**
	 * 获取岗位列表（分页）
	 * @param queryFilter
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgPosts/getOrgPostPage", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取岗位列表（分页）", httpMethod = "POST", notes = "获取岗位列表（分页）")
	public CommonResult<PageList<OrgPost>> getOrgPostPage(@ApiParam(name = "queryFilter", value = "通用查询对象") @RequestBody QueryFilter queryFilter) throws Exception {
		//queryFilter.setClazz(OrgPost.class);
		//添加权限语句
		String postAuthSql = AuthFilterUtil.getPostAuthSql();
		if(StringUtil.isNotEmpty(postAuthSql)){
			queryFilter.addParams("authSql", postAuthSql);
		}
		PageList<OrgPost> pageList = postService.getOrgPost(queryFilter);
		return CommonResult.<PageList<OrgPost>>ok().value(pageList);
	}

	/**
	 * 根据级别获取组织
	 * 
	 * @param level
	 * @param demCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgs/getByLevel", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 根据级别获取组织", httpMethod = "GET", notes = " 根据级别获取组织")
	public CommonResult<List<Org>> getByLevel(@ApiParam(name = "level", value = "组织级别", required = true) @RequestParam String level,
			@ApiParam(name = "demCode", value = "维度编码",required=false) @RequestParam(required=false) String demCode)
			throws Exception {
		List<Org> orgList = orgService.getByLevel(level, demCode);
		return CommonResult.<List<Org>>ok().value(orgList);
	}

	/**
	 * 用户加入到岗位
	 * 
	 * @param accounts
	 * @param postCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "userPost/saveUserPost", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 用户加入到岗位", httpMethod = "POST", notes = " 用户加入到岗位，accounts用户帐号，多个用英文逗号隔开")
	public CommonResult<String> saveUserPost(
			@ApiParam(name = "accounts", value = "用户帐号", required = true) @RequestParam String accounts,
			@ApiParam(name = "postCode", value = "岗位编码", required = true) @RequestParam String postCode)
			throws Exception {
		CommonResult<String> commonResult = orgService.saveUserPost(accounts, postCode);
		return CommonResultUtil.<String>getResult(commonResult);
	}
	
	/**
	 * 用户加入到岗位
	 * 
	 * @param accounts
	 * @param postCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "userPost/saveUserPosts", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 用户加入到岗位", httpMethod = "POST", notes = " 用户加入到岗位，多个用英文逗号隔开")
	public CommonResult<String> saveUserPosts(
			@ApiParam(name = "account", value = "用户帐号", required = true) @RequestParam String account,
			@ApiParam(name = "postCodes", value = "岗位编码", required = true) @RequestParam String postCodes)
			throws Exception {
		CommonResult<String> commonResult = orgService.saveUserPosts(account, postCodes);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 用户退出岗位
	 * 
	 * @param accounts
	 * @param postCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "userPost/delUserPost", method = RequestMethod.DELETE, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 用户退出岗位", httpMethod = "DELETE", notes = " 用户退出岗位，accounts用户帐号，多个用英文逗号隔开")
	public CommonResult<String> delUserPost(
			@ApiParam(name = "accounts", value = "用户帐号", required = true) @RequestParam String accounts,
			@ApiParam(name = "postCode", value = "岗位编码", required = true) @RequestParam String postCode)
			throws Exception {
		CommonResult<String> commonResult = orgService.delUserPost(accounts, postCode);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 组织添加岗位
	 * 
	 * @param orgPostVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgPost/saveOrgPost", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 组织添加岗位", httpMethod = "POST", notes = " 组织添加岗位")
	public CommonResult<String> saveOrgPost(
			@ApiParam(name = "orgPostVo", value = "组织岗位", required = true) @RequestBody OrgPostVo orgPostVo)
			throws Exception {
		CommonResult<String> commonResult = orgService.saveOrgPost(orgPostVo);
		return CommonResultUtil.<String>getResult(commonResult);
	}
	
	/**
	 * 更新岗位
	 * @param orgPostVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgPost/updateOrgPost", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "更新岗位", httpMethod = "POST", notes = "更新岗位")
	public CommonResult<String> updateOrgPost(
			@ApiParam(name = "orgPostVo", value = "组织岗位", required = true) @RequestBody OrgPostVo orgPostVo)
			throws Exception {
		CommonResult<String> commonResult = orgService.updateOrgPost(orgPostVo);
		return CommonResultUtil.<String>getResult(commonResult);
	}
	
	/**
	 * 根据岗位编码获取岗位信息
	 * @param postCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgPost/getOrgPost", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 根据岗位id或编码获取岗位信息", httpMethod = "GET", notes = " 根据岗位编码获取岗位信息")
	public CommonResult<OrgPost> getOrgPost(@ApiParam(name="postCode",value="岗位代码",required=true) @RequestParam String postCode) throws Exception{
		OrgPost p = postService.getByCode(postCode);
		if(BeanUtils.isEmpty(p)){
			p = postService.get(postCode);
		}
		return CommonResult.<OrgPost>ok().message("获取岗位成功").value(p);
	}
	
	/**
	 * 根据用户账号获取所属岗位列表
	 * @param account
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgPost/getOrgPostByUserAccount", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = " 根据用户账号获取所属岗位", httpMethod = "GET", notes = " 根据用户账号获取所属岗位")
	public CommonResult<List<OrgPost>> getOrgPostByUserAccount(@ApiParam(name="account",value="用户账号",required=true) @RequestParam String account) throws Exception {
		List<OrgPost> list = postService.getListByAccount(account, null);
		return CommonResult.<List<OrgPost>>ok().value(list);
	}

	/**
	 * 删除组织岗位，连同岗位下的人员信息一起删除
	 * 
	 * @param postCodes
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgPost/deleteOrgPost", method = RequestMethod.DELETE, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 删除组织岗位", httpMethod = "DELETE", notes = " 删除组织岗位，连同岗位下的人员信息一起删除，postCodes岗位编码，多个用英文逗号隔开")
	public CommonResult<String> deleteOrgPost(
			@ApiParam(name = "postCodes", value = "岗位编码", required = true) @RequestParam String postCodes)
			throws Exception {
		CommonResult<String> commonResult = orgService.deleteOrgPost(postCodes);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 设置组织（取消）责任岗位
	 * 
	 * @param postCode
	 * @param isMain
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgPost/setPostMaster", method = RequestMethod.PUT, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 设置组织（取消）责任岗位", httpMethod = "PUT", notes = " 设置组织（取消）责任岗位")
	public CommonResult<String> setPostMaster(
			@ApiParam(name = "postCode", value = "岗位编码", required = true) @RequestParam String postCode,
			@ApiParam(name = "isMain", value = "是否责任岗位", required = true) @RequestParam Boolean isMain)
			throws Exception {
		CommonResult<String> commonResult = orgService.setPostMaster(postCode, isMain);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 设置人员岗位有效期
	 * 
	 * @param postDueVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "userPost/setUserPostDueTime", method = RequestMethod.PUT, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 设置人员岗位有效期", httpMethod = "PUT", notes = " 设置人员岗位有效期")
	public CommonResult<String> setUserPostDueTime(
			@ApiParam(name = "postDueVo", value = "人员岗位有效期", required = true) @RequestBody PostDueVo postDueVo)
			throws Exception {
		CommonResult<String> commonResult = orgService.setUserPostDueTime(postDueVo);
		return CommonResultUtil.<String>getResult(commonResult);
	}

	/**
	 * 校验所有用户岗位是否有效
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "userPost/validOrgUser", method = RequestMethod.PUT, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 校验所有用户岗位是否有效", httpMethod = "PUT", notes = " 校验所有用户岗位是否有效")
	public CommonResult<String> validOrgUser() throws Exception {
		CommonResult<String> commonResult = orgService.validOrgUser();
		return CommonResultUtil.<String>getResult(commonResult);
	}
	
	/**
	 * 组织添加角色
	 * @param orgRoleVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgRole/addOrgRole", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 组织添加角色", httpMethod = "POST", notes = " 组织添加角色")
	public CommonResult<String> addOrgRole(@ApiParam(name="orgRoleVo",value="组织角色",required=true) @RequestBody OrgRoleVo orgRoleVo) throws Exception{
		orgService.addOrgRole(orgRoleVo);
		return CommonResult.<String>ok().message("添加成功！");
	}
	
	/**
	 * 删除组织指定角色
	 * @param orgCode
	 * @param roleCodes
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgRole/delOrgRoleByCode", method = RequestMethod.DELETE, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 删除组织指定角色", httpMethod = "DELETE", notes = " 删除组织指定角色，roleCodes角色编码多个用英文逗号隔开")
	public CommonResult<String> delOrgRoleByCode(
			@ApiParam(name = "orgCode", value = "组织编码", required = true) @RequestParam String orgCode,
			@ApiParam(name = "roleCodes", value = "角色编码", required = true) @RequestParam String roleCodes)
			throws Exception {
		orgService.delOrgRoleByCode(orgCode, roleCodes);
		return CommonResult.<String>ok().message("删除成功！");
	}
	
	/**
	 * 删除组织所拥有的角色
	 * @param orgCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgRole/delAllOrgRole", method = RequestMethod.DELETE, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 删除组织所拥有的角色", httpMethod = "DELETE", notes = " 删除组织所拥有的角色")
	public CommonResult<String> delAllOrgRole(
			@ApiParam(name = "orgCode", value = "组织编码", required = true) @RequestParam String orgCode)
			throws Exception {
		orgService.delAllOrgRole(orgCode);
		return CommonResult.<String>ok().message("删除成功！");
	}
	
	/**
	 * 获取组织所拥有的角色
	 * 1.首先查找组织自身的角色，有则返回
	 * 2.若组织自身没有角色，则从下往上找父组织的角色（可继承的），有则返回最近的
	 * @param orgCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgRoles/getOrgRoleByCode", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = " 获取组织所拥有的角色", httpMethod = "GET", notes = " 获取组织所拥有的角色，若自身有角色则返回自身的角色，若自身没有角色，则返回最近的父组织所拥有的可继承的角色")
	public CommonResult<List<Role>> getOrgRoleByCode(
			@ApiParam(name = "orgCode", value = "组织编码", required = true) @RequestParam String orgCode)
			throws Exception {
		List<Role> roleList = orgService.getOrgRoleByCode(orgCode);
		return CommonResult.<List<Role>>ok().value(roleList);
	}
	
	/**
	 * 物理删除所有逻辑删除了的组织数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="org/deleteOrgPhysical",method=RequestMethod.DELETE, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "物理删除所有逻辑删除了的组织数据", httpMethod = "DELETE", notes = "物理删除所有逻辑删除了的组织数据")
	public CommonResult<Integer> deleteOrgPhysical() throws Exception{
		Integer num = orgService.removePhysical();
		CommonResult<Integer> commonResult = OrgUtil.getRemovePhysiMsg(num);
		return CommonResultUtil.<Integer>getResult(commonResult);
	}
	
	/**
	 * 物理删除所有逻辑删除了的岗位数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="org/deletePostPhysical",method=RequestMethod.DELETE, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "物理删除所有逻辑删除了的岗位数据", httpMethod = "DELETE", notes = "物理删除所有逻辑删除了的岗位数据")
	public CommonResult<Integer> deletePostPhysical() throws Exception{
		Integer num = orgService.removePostPhysical();
		CommonResult<Integer> commonResult = OrgUtil.getRemovePhysiMsg(num);
		return CommonResultUtil.<Integer>getResult(commonResult);
	}
	
	/**
	 * 物理删除所有逻辑删除了的用户组织关系数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="org/deleteOrgUserPhysical",method=RequestMethod.DELETE, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "物理删除所有逻辑删除了的用户组织关系数据", httpMethod = "DELETE", notes = "物理删除所有逻辑删除了的用户组织关系数据")
	public CommonResult<Integer> deleteOrgUserPhysical() throws Exception{
		Integer num = orgService.removeOrgUserPhysical();
		CommonResult<Integer> commonResult = OrgUtil.getRemovePhysiMsg(num);
		return CommonResultUtil.<Integer>getResult(commonResult);
	}
	
	/**
	 * 根据维度id  获取对应维度下的组织
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgs/getByParentAndDem", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "根据维度id  获取对应维度下的组织", httpMethod = "POST", notes = "根据维度id获取对应维度下的组织，参数{\"demId\":\"1\",\"parentId\":\"1\"}")
	public CommonResult<List<OrgTree>> getByParentAndDem(
			@ApiParam(name = "params", value = "参数值") @RequestBody Map<String,String> params) throws Exception {
		List<OrgTree> orgTreeList = orgService.getOrgTreeByParentAndDem(params);
		return CommonResult.<List<OrgTree>>ok().value(orgTreeList);
	}

	@GetMapping(value = "orgs/children/{orgId}", produces = {"application/json; charset=utf-8" })
	@ApiOperation(value = "获取orgId的下级组织", httpMethod = "GET", notes = "获取orgId的下级组织")
	public CommonResult<List<Org>> getByParentAndDem(@ApiParam(name = "orgId", value = "组织id") @PathVariable String orgId) throws Exception {
		QueryFilter filter = QueryFilter.build();
		filter.addParams("parentId", " ('" +orgId+"') ");
		List<Org> list = orgService.getByParentAndDem(filter);
		return CommonResult.<List<Org>>ok().value(list);
	}

	/**
	 * 根据维度id  获取对应维度下的组织 专为组织管理树服务
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgs/getByParentAndDemToTree", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "根据维度id  获取对应维度下的组织", httpMethod = "POST", notes = "根据维度id获取对应维度下的组织，参数{\"demId\":\"1\",\"parentId\":\"1\"}")
	public CommonResult<List<OrgTree>> getByParentAndDemToTree(
			@ApiParam(name = "params", value = "参数值") @RequestBody Map<String,String> params) throws Exception {
		List<OrgTree> orgTreeList = orgService.getByParentAndDemToTree(params);
		return CommonResult.<List<OrgTree>>ok().value(orgTreeList);
	}


	/**
	 * 获取组织人员列表
	 * @param queryFilter
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "orgUsers/getOrgUserPage", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取组织人员（带分页信息）", httpMethod = "POST", notes = "获取组织人员列表")
	public CommonResult<PageList<HashMap<String, Object>>> getOrgUserPage(
			@ApiParam(name = "queryFilter", value = "通用查询对象") @RequestBody QueryFilter queryFilter) throws Exception {
		PageList<HashMap<String, Object>> pageList = orgService.getOrgUserPage(queryFilter);
		return CommonResult.<PageList<HashMap<String, Object>>>ok().value(pageList);
	}
	
	/**
	 * 通过id，设置人员（取消）主岗位
	 * 
	 * @param account
	 * @param postCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgPost/setMasterById", method = RequestMethod.PUT, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "设置人员（取消）主岗位", httpMethod = "PUT", notes = "通过id，设置人员（取消）主岗位")
	public CommonResult<String> setMasterById(
			@ApiParam(name = "id", value = "组织人员关系id", required = true) @RequestParam String ... id )throws Exception {
			orgUserService.setMaster(id);
			return CommonResult.<String>ok().message("操作成功！");
	}

	/**
	 * 通过id，设置人员主岗位
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgPost/setMasterByIds", method = RequestMethod.PUT, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "设置人员主岗位", httpMethod = "PUT", notes = "通过id，设置人员主岗位")
	public CommonResult<String> setMasterByIds(
			@ApiParam(name = "ids", value = "组织人员关系ids", required = true) @RequestParam String ... ids )throws Exception {
			orgUserService.setMasterByIds(ids);
		return CommonResult.<String>ok().message("操作成功！");
	}
	
	/**
	 * 用户批量添加下属
	 * 
	 * @param orgId
	 * @param account
	 *            上级用户帐号
	 * @param underAccounts
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgUsers/setUnderUsers", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "用户批量添加下属", httpMethod = "POST", notes = "用户批量添加下属", hidden = true)
	public CommonResult<String> setUnderUsers(
			@ApiParam(name = "orgId", value = "组织id", required = true) @RequestParam String orgId,
			@ApiParam(name = "account", value = "上级用户帐号", required = true) @RequestParam String account,
			@ApiParam(name = "underAccounts", value = "下级用户帐号", required = true) @RequestParam String underAccounts) throws Exception {
		CommonResult<String> commonResult = orgService.setUnderUsers(orgId, account, underAccounts);
		return CommonResultUtil.<String>getResult(commonResult);
	}
	
	/**
	 * 组织批量加入用户
	 * 
	 * @param orgCode
	 * @param accounts
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgUsers/addUsersForOrg", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "组织批量加入用户", httpMethod = "POST", notes = "组织批量加入用户", hidden = true)
	public CommonResult<String> addUsersForOrg(
			@ApiParam(name = "orgCode", value = "组织代码", required = true) @RequestParam String orgCode,
			@ApiParam(name = "accounts", value = "用户帐号", required = true) @RequestParam String accounts) throws Exception {
		CommonResult<String> commonResult = orgService.addUsersForOrg(orgCode, accounts);
		return CommonResultUtil.<String>getResult(commonResult);

	}
	
	/**
	 * 根据时间获取组织数据（数据同步）
	 * @param btime
	 * @param etime
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="orgs/getOrgByTime",method=RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "根据时间获取组织数据（数据同步）", httpMethod = "POST", notes = "根据时间获取组织数据（数据同步）")
	public CommonResult<List<Org>> getOrgByTime(@ApiParam(name="exportObject",value="获取数据参数类",required=true) @RequestBody OrgExportObject exportObject) throws Exception{
		List<Org> orgList = orgService.getOrgByTime(exportObject);
		return CommonResult.<List<Org>>ok().value(orgList);
	}
	
	/**
	 * 根据时间获取组织参数数据（数据同步）
	 * @param btime
	 * @param etime
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="orgParams/getOrgParamByTime",method=RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "根据时间获取组织参数数据（数据同步）", httpMethod = "POST", notes = "根据时间获取组织参数数据（数据同步）")
	public CommonResult<List<OrgParams>> getOrgParamByTime(@ApiParam(name="exportObject",value="获取数据参数类",required=true) @RequestBody OrgExportObject exportObject) throws Exception{
		List<OrgParams> orgParamsList = orgService.getOrgParamByTime(exportObject);
		return CommonResult.<List<OrgParams>>ok().value(orgParamsList);
	}
	
	/**
	 * 根据时间获取组织岗位数据（数据同步）
	 * @param btime
	 * @param etime
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="orgPosts/getOrgPostByTime",method=RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "根据时间获取组织岗位数据（数据同步）", httpMethod = "POST", notes = "根据时间获取组织岗位数据（数据同步）")
	public CommonResult<List<OrgPost>> getOrgPostByTime(@ApiParam(name="exportObject",value="获取数据参数类",required=true) @RequestBody OrgExportObject exportObject) throws Exception{
		List<OrgPost> orgPostList = orgService.getOrgPostByTime(exportObject);
		return CommonResult.<List<OrgPost>>ok().value(orgPostList);
	}
	
	/**
	 * 根据时间获取组织角色数据（数据同步）
	 * @param btime
	 * @param etime
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="orgRoles/getOrgRoleByTime",method=RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "根据时间获取组织角色数据（数据同步）", httpMethod = "POST", notes = "根据时间获取组织角色数据（数据同步）")
	public CommonResult<List<OrgRole>> getOrgRoleByTime(@ApiParam(name="exportObject",value="获取数据参数类",required=true) @RequestBody OrgExportObject exportObject) throws Exception{
		List<OrgRole> orgRoleList = orgService.getOrgRoleByTime(exportObject);
		return CommonResult.<List<OrgRole>>ok().value(orgRoleList);
	}
	
	/**
	 * 根据时间获取用户组织关系数据（数据同步）
	 * @param btime
	 * @param etime
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="orgUsers/getOrgUserByTime",method=RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "根据时间获取用户组织关系数据（数据同步）", httpMethod = "POST", notes = "根据时间获取用户组织关系数据（数据同步）")
	public CommonResult<List<OrgUser>> getOrgUserByTime(@ApiParam(name="exportObject",value="获取数据参数类",required=true) @RequestBody OrgExportObject exportObject) throws Exception{
		List<OrgUser> orgUserList = orgService.getOrgUserByTime(exportObject);
		return CommonResult.<List<OrgUser>>ok().value(orgUserList);
	}
	
	/**
	 * 根据时间获取组织中下属数据（数据同步）
	 * @param btime
	 * @param etime
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="userUnders/getUserUnderByTime",method=RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "根据时间获取组织中下属数据（数据同步）", httpMethod = "POST", notes = "根据时间获取组织中下属数据（数据同步）")
	public CommonResult<List<UserUnder>> getUserUnderByTime(@ApiParam(name="exportObject",value="获取数据参数类",required=true) @RequestBody OrgExportObject exportObject) throws Exception{
		List<UserUnder> userUnderList = orgService.getUserUnderByTime(exportObject);
		return CommonResult.<List<UserUnder>>ok().value(userUnderList);

	}
	
	@RequestMapping(value="org/isCodeExist",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "查询组织编码是否已存在", httpMethod = "GET", notes = "查询组织编码是否已存在")
	public CommonResult<Boolean> isCodeExist(@ApiParam(name="code",value="组织编码")
												@RequestParam(required=true) String code) throws Exception{
		CommonResult<Boolean> commonResult = orgService.isCodeExist(code);
		return CommonResultUtil.<Boolean>getResult(commonResult);
	}
	
	@RequestMapping(value="orgPost/isPostCodeExist",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "查询岗位编码是否已存在", httpMethod = "GET", notes = "查询岗位编码是否已存在")
	public CommonResult<Boolean> isPostCodeExist(@ApiParam(name="code",value="岗位编码")
												@RequestParam(required=true) String code) throws Exception{
		CommonResult<Boolean> commonResult = orgService.isPostCodeExist(code);
		return CommonResultUtil.<Boolean>getResult(commonResult);
	}
	
	
	/**
	 * 同步OA组织岗位数据
	 * @param btime
	 * @param etime
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="orgs/oaAsync",method=RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "同步OA组织岗位数据", httpMethod = "POST", notes = "同步OA组织岗位数据")
	public CommonResult<String> oaAsync(@ApiParam(name="oaAsyncObject",value="同步参数",required=true) @RequestBody OaAsyncObject oaAsyncObject,HttpServletRequest request) throws Exception{
		String ip = OrgUtil.getIpAddress(request);
		CommonResult<String> commonResult = userImportService.syncSoap(oaAsyncObject, ip);
		return CommonResultUtil.<String>getResult(commonResult);
	}
	
	@RequestMapping(value = "orgRoles/getOrgRoleList", method = RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "获取组织角色列表（带分页信息）", httpMethod = "POST", notes = "获取组织角色列表（带分页信息）")
	public CommonResult<PageList<OrgRole>> getOrgRoleList(@ApiParam(name = "queryFilter", value = "通用查询对象") @RequestBody QueryFilter queryFilter) throws Exception {
		//queryFilter.setClazz(OrgRole.class);
		PageList<OrgRole> list = orgRoleService.query(queryFilter);
		return CommonResult.<PageList<OrgRole>>ok().value(list);
	}
	
	@RequestMapping(value = "orgRoles/saveOrgRole", method = RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "保存组织角色信息", httpMethod = "POST", notes = "保存组织角色信息")
	public CommonResult<String> saveOrgRole(@ApiParam(name = "vo", value = "通用查询对象") @RequestBody OrgRoleVo vo) throws Exception {
		CommonResult<String> commonResult = orgService.addOrgRole(vo);
		return CommonResultUtil.<String>getResult(commonResult);
	}
	
	@RequestMapping(value = "orgRoles/removeOrgRole", method = RequestMethod.GET, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "保存组织角色信息", httpMethod = "GET", notes = "保存组织角色信息")
	public CommonResult<String> removeOrgRole(@ApiParam(name="code",value="岗位编码")
	@RequestParam(required=true) String id) throws Exception {
		CommonResult<String> commonResult = orgService.delOrgRoleById(id);
		return CommonResultUtil.<String>getResult(commonResult);
	}
	
	
	/**
	 * 更新组织所在树的位置
	 * @param request
	 * @param response
	 * @throws Exception void
	 * @exception
	 */
	@RequestMapping(value="orgs/updateOrgPos",method=RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "更新组织所在树的位置", httpMethod = "POST", notes = "更新组织所在树的位置（树结构拖动保存）")
	public CommonResult<String> updateOrgPos(@ApiParam(name="orgId",value="移动节点id", required = true) @RequestParam String orgId,@ApiParam(name="parentId",value="移至（目标）节点id", required = true) @RequestParam String parentId) throws Exception {

		CommonResult<String> commonResult = orgService.updateOrgPos(orgId, parentId);
		return CommonResultUtil.<String>getResult(commonResult);
	}
	
	
	@RequestMapping(value="orgs/exportUsers",method=RequestMethod.GET, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "导出用户组织数据", httpMethod = "GET", notes = "导出用户组织数据")
	public void exportUsers(@ApiParam(name="orgCode",value="组织编码",required=true)   @RequestParam String orgCode,@ApiParam(name="isChildre",value="是否包含子组织。默认为true",required=true)   @RequestParam Boolean isChildre) throws Exception{
		HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
		isChildre = BeanUtils.isEmpty(isChildre)?true:isChildre;
		String path = orgService.exportData(orgCode, isChildre);
		if(StringUtil.isNotEmpty(path)){
			String excelPath = path+".xls";
			File file = new File(excelPath);
			if(!file.exists()){
				throw new RuntimeException("生成Excel文件失败！");
			}
			String excelName = file.getName();
			// 导出
			HttpUtil.downLoadFile(response, excelPath, excelName);
			// 删除导出的文件
			FileUtil.deleteFile(file.getParentFile().getPath());
		}else{
			throw new RuntimeException("导出组织用户信息失败！");
		}
	}
	
	@RequestMapping(value = "org/getMainGroup", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户主组织", httpMethod = "GET", notes = "获取用户主组织")
	public CommonResult<Org> getMainGroup(
			@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId,
			@ApiParam(name = "demId", value = "维度id", required = true) @RequestParam  Optional<String> demId)throws Exception {
		Org org = orgService.getMainGroup(userId, demId.orElse(""));
		return CommonResult.<Org>ok().value(org);
	}

	@RequestMapping(value = "org/getParentMainOrg", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户主组织（包含父级组织）", httpMethod = "GET", notes = "获取用户主组织（包含父级组织）")
	public CommonResult<Map<String, Org>> getParentMainOrg(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId)throws Exception {
		Map<String, Org> stringOrgMap = orgService.getParentMainOrg(userId);
		return CommonResult.<Map<String, Org>>ok().value(stringOrgMap);
	}
	
	@RequestMapping(value = "orgJobs/getJobsByUserId", method = RequestMethod.GET, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户所有职务", httpMethod = "GET", notes = "获取用户所有职务")
	public CommonResult<List<OrgJob>> getJobsByUserId(
		@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId)throws Exception {
		List<OrgJob> orgJobList = orgJobManager.getListByUserId(userId);
		return CommonResult.<List<OrgJob>>ok().value(orgJobList);
	}
	
	@RequestMapping(value = "org/isSupOrgByCurrMain", method = RequestMethod.GET, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "判断当前用户主部门是否有上级 ", httpMethod = "GET", notes = "判断当前用户主部门是否有上级 ")
	public CommonResult<Boolean> isSupOrgByCurrMain(
		@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId,
		@ApiParam(name = "demId", value = "维度id", required = true) @RequestParam String demId,
		@ApiParam(name = "level", value = "级别", required = true) @RequestParam Integer level)throws Exception {
		Boolean flag = orgService.isSupOrgByCurrMain(userId, demId, level);
		return CommonResult.<Boolean>ok().value(flag);

	}
	
	@RequestMapping(value = "org/getSupOrgByCurrMain", method = RequestMethod.GET, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户的主岗位组织关系", httpMethod = "GET", notes = "获取用户的主岗位组织关系")
	public CommonResult<OrgUser> getSupOrgByCurrMain(
		@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId,
		@ApiParam(name = "demId", value = "维度id", required = true) @RequestParam String demId)throws Exception {
		List<OrgUser> orgUsers = orgUserService.getOrgUserMaster(userId,demId);
        if(BeanUtils.isNotEmpty(orgUsers)) {
			OrgUser orgUser = orgUsers.get(0);
			return CommonResult.<OrgUser>ok().value(orgUser);
        }
		return CommonResult.<OrgUser>ok().message("该用户的主岗位组织关系暂无！");
	}
	
	@RequestMapping(value = "org/getMainPostOrOrgByUserId", method = RequestMethod.GET, produces = {"application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户的主岗位、主组织(优先获取默认维度的，没有时获取其他维度的)", httpMethod = "GET", notes = "获取用户的主岗位、主组织(优先获取默认维度的，没有时获取其他维度的)")
	public CommonResult<OrgUser> getMainPostOrOrgByUserId(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId) throws Exception{
		OrgUser orgUser = orgUserService.getMainPostOrOrgByUserId(userId);
		return CommonResult.<OrgUser>ok().value(orgUser);
	}
	
	/**
	 * 获取指定上级级别的组织的负责人
	 * @param userId
	 * @param level
	 * @param isMainCharge
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/orgusers/getCustomLevelCharge", method = RequestMethod.GET)
	public CommonResult<List<User>> getCustomLevelCharge(@RequestParam(value="userId",required=true) String userId,@RequestParam(value="level",required=true)  String level,@RequestParam(value="isMainCharge",required=true)  boolean isMainCharge) throws Exception {
		List<Org> userOrg = orgService.getUserOrg(userId, "", true);
		List<User> chargesByOrg = new ArrayList<User>();
		if( BeanUtils.isEmpty(userOrg) ) {
			return CommonResult.<List<User>>ok().value(chargesByOrg);
		}
		Org org = userOrg.get(0);
		while(!"0".equals(org.getParentId())) {
			String orgId = org.getParentId();
			org = orgService.get(orgId);
			if(level.equals(org.getGrade())) {
				chargesByOrg = userService.getChargesByOrg(org.getCode(), isMainCharge);
				break;
			}
		}
		return CommonResult.<List<User>>ok().value(chargesByOrg);
	}
	
	/**
	 * 获取指定上级级别的组织 中指定岗位的用户
	 * @param userId
	 * @param level
	 * @param postCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/orgusers/getCustomLevelPost", method = RequestMethod.GET)
	public CommonResult<Set<GroupIdentity>> getCustomLevelPost(@RequestParam(value="userId",required=true) String userId,@RequestParam(value="level",required=true)  String level,@RequestParam(value="postCode",required=true)  String postCode) throws Exception {
		List<Org> userOrg = orgService.getUserOrg(userId, "", true);
		 Set<GroupIdentity> chargesByOrg = new HashSet<GroupIdentity>();
		if( BeanUtils.isEmpty(userOrg) ) {
			return CommonResult.<Set<GroupIdentity>>ok().value(chargesByOrg);
		}
		Org org = userOrg.get(0);
		while(!"0".equals(org.getParentId())) {
			String orgId = org.getParentId();
			org = orgService.get(orgId);
			if(level.equals(org.getGrade())) {
				chargesByOrg = userService.getByPostCodeAndOrgCode(postCode, org.getCode());
				break;
			}
		}
		return CommonResult.<Set<GroupIdentity>>ok().value(chargesByOrg);
	}
	
	/**
	 * 获取指定上级级别的组织 中指定职务的用户
	 * @param userId
	 * @param level
	 * @param postCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/orgusers/getCustomLevelJob", method = RequestMethod.GET)
	public  CommonResult<Set<GroupIdentity>> getCustomLevelJob(@RequestParam(value="userId",required=true) String userId,@RequestParam(value="level",required=true)  String level,@RequestParam(value="jobCode",required=true)  String jobCode) throws Exception {
		List<Org> userOrg = orgService.getUserOrg(userId, "", true);
		 Set<GroupIdentity> chargesByOrg = new HashSet<GroupIdentity>();
		if( BeanUtils.isEmpty(userOrg) ) {
			return CommonResult.<Set<GroupIdentity>>ok().value(chargesByOrg);
		}
		Org org = userOrg.get(0);
		while(!"0".equals(org.getParentId())) {
			String orgId = org.getParentId();
			org = orgService.get(orgId);
			if(level.equals(org.getGrade())) {
				chargesByOrg =  userService.getByJobCodeAndOrgCode(jobCode,org.getCode());
				break;
			}
		}
		return CommonResult.<Set<GroupIdentity>>ok().value(chargesByOrg);
	}
	
	@RequestMapping(value = "/orgusers/getStartOrgParam", method = RequestMethod.GET)
	public CommonResult<String> getStartOrgParam(@RequestParam(value="userId",required=true) String userId,@RequestParam(value="param",required=true)  String param) {
		List<Org> userOrg = orgService.getUserOrg(userId, "", true);
		String paramValue = "";
		if( BeanUtils.isEmpty(userOrg) ) {
			return CommonResult.<String>ok().value(paramValue);
		}
		Org org = userOrg.get(0);
		OrgParams byOrgIdAndAlias = orgParamsService.getByOrgIdAndAlias(org.getId(), param);
		String value = byOrgIdAndAlias.getValue();
		return CommonResult.<String>ok().value(paramValue);
	}

    @RequestMapping(value = "orgpost/getFullname", method = RequestMethod.GET, produces = {
            "application/json; charset=utf-8" })
    @ApiOperation(value = "根据岗位id查询岗位所在的人员，一般只有一位", httpMethod = "GET", notes = "根据岗位id查询岗位所在的人员，一般只有一位")
    public CommonResult<List<Map<String, Object>>> getFullname(@ApiParam(name = "postId", value = "岗位id", required = true) @RequestParam String postId)throws Exception {
		List<Map<String, Object>> mapList = postService.getFullname(postId);
		return CommonResult.<List<Map<String, Object>>>ok().value(mapList);
    }

    @RequestMapping(value = "orgpost/getPostByJobId", method = RequestMethod.GET, produces = {
            "application/json; charset=utf-8" })
    @ApiOperation(value = "根据职务id查询岗位以及这些岗位所属的组织全路径", httpMethod = "GET", notes = "根据职位id查询岗位以及这些岗位所属的组织全路径")
    public CommonResult<List<Map<String, Object>>> getPostByJobId(@ApiParam(name = "jobId", value = "职务id", required = true) @RequestParam String jobId)throws Exception {
		List<Map<String, Object>> mapList = postService.getPostByJobId(jobId);
		return CommonResult.<List<Map<String, Object>>>ok().value(mapList);
	}

    @RequestMapping(value = "orgpost/getUserByUserId", method = RequestMethod.GET, produces = {
            "application/json; charset=utf-8" })
    @ApiOperation(value = "根据用户ID查询用户组织岗位角色信息", httpMethod = "GET", notes = "根据用户ID查询用户组织岗位角色信息")
    public CommonResult<List<Map<String, Object>>> getUserByUserId(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId)throws Exception {
		List<Map<String, Object>> mapList = postService.getUserByUserId(userId);
		return CommonResult.<List<Map<String, Object>>>ok().value(mapList);

	}
    
	@RequestMapping(value = "org/getPathNames", method = RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "根据用户id组获取组织全路径", httpMethod = "POST", notes = "根据用户id获得主岗位的岗位组")
	public CommonResult<List<Map<String, String>>> getPathNames(@ApiParam(name = "userIds", value = "用户id组", required = false) @RequestParam List<String> userIds)throws Exception {
		List<Map<String, String>> mapList = orgService.getPathNames(userIds);
		return CommonResult.<List<Map<String, String>>>ok().value(mapList);

	}
	
	@RequestMapping(value = "org/getChildrenIds", method = RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "获取子组织ID（包含自己）", httpMethod = "POST", notes = "获取子组织ID（包含自己）")
	public CommonResult<Map<String, Set<String>>> getChildrenIds(@ApiParam(name = "ids", value = "组织id", required = true) @RequestParam Map<String,String> ids)throws Exception {
		Map<String, Set<String>> childrenIds = orgService.getChildrenIds(ids);
		return CommonResult.<Map<String, Set<String>>>ok().value(childrenIds);
	}
	
	@RequestMapping(value = "org/getOrgListByDemId", method = RequestMethod.GET, produces = {"application/json; charset=utf-8" })
	@ApiOperation(value = "获取组织列表", httpMethod = "GET", notes = "获取组织列表")
	public CommonResult<List<Org>> getOrgListByDemId(@ApiParam(name = "demId", value = "维度id", required = true) @RequestParam String demId) throws Exception{
		List<Org> orgList = orgService.getOrgListByDemId(demId);
		return CommonResult.<List<Org>>ok().value(orgList);
	}

    @RequestMapping(value = "orgusers/getUserOrgNowNumByOrgId", method = RequestMethod.GET, produces = {
            "application/json; charset=utf-8" })
    @ApiOperation(value = "根据组织id查询组织下面的人员数量", httpMethod = "GET", notes = "根据组织id查询组织下面的人员数量")
    public CommonResult<Integer> getUserOrgNowNumByOrgId(@ApiParam(name = "orgId", value = "组织id", required = true) @RequestParam String orgId)throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("group", "true");
        List<Map<String, Object>> list = orgUserDao.getUserNumByOrgId(map);
		int size = list.size();
		return CommonResult.<Integer>ok().value(size);

	}

    @RequestMapping(value = "orgusers/getUserOrgNowNumByOrgIds", method = RequestMethod.GET, produces = {
            "application/json; charset=utf-8" })
    @ApiOperation(value = "根据组织id集合查询组织下面的人员数量", httpMethod = "GET", notes = "根据组织id查询组织下面的人员数量")
    public CommonResult<List<Map<String, Object>>> getUserOrgNowNumByOrgIds(@ApiParam(name = "orgIds", value = "组织id集合", required = true) @RequestParam String orgIds)throws Exception {
        List<Map<String, Object>> mapList = new ArrayList<>();
        String[] value = orgIds.split(",");
        for(String orgId :value){
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> numMap = new HashMap<>();
            map.put("orgId", orgId);
            map.put("group", "true");
            List<Map<String, Object>> list = orgUserDao.getUserNumByOrgId(map);
            numMap.put("orgId", orgId);
            numMap.put("num", list.size());
            mapList.add(numMap);
        }
		return CommonResult.<List<Map<String, Object>>>ok().value(mapList);
    }
    /**
	 * 从第三方获取组织数据添加到本系统
	 *
	 * @param orgVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "org/addOrgFromExterUni", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "从第三方获取组织数据添加到本系统", httpMethod = "POST", notes = "从第三方获取组织数据添加到本系统")
	public CommonResult<String> addOrgFromExterUni(
			@ApiParam(name = "org", value = "组织", required = true) @RequestBody Org orgVo) throws Exception {
		CommonResult<String> commonResult = orgService.addOrgFromExterUni(orgVo);
		return CommonResultUtil.<String>getResult(commonResult);
	}
	/**
	 * <pre>
	 * 获取人员的上级岗位
	 * 1、先查找主岗位的上级岗位
	 * 2、无主岗位查其他岗位，其他岗位只有一个时，查找该岗位的上级岗位，
	 * 其他岗位为多个时，抛出异常
	 * </pre>
	 * @param userId
	 * @return
	 */
	//暂时弃用，岗位无上下级关系
	@Deprecated
	@RequestMapping(value = "org/getSuperiorPost", method = RequestMethod.GET, produces = {"application/json; charset=utf-8" })
	@ApiOperation(value = "获取组织列表", httpMethod = "GET", notes = "获取组织列表")
	public CommonResult<Map<String, Object>> getSuperiorPost(@RequestParam(value="userId",required=true) String userId) {
		Map<String, Object> map = postService.getSuperiorPost(userId);
		return CommonResult.<Map<String, Object>>ok().value(map);
	}

	@RequestMapping(value = "org/getFillOrg", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取当前用户填制单位", httpMethod = "GET", notes = "获取当前用户填制单位")
	public CommonResult<Org> getFillOrg(
			@ApiParam(name = "demId", value = "维度Id", required = true) @RequestParam Optional<String> demId,
			@ApiParam(name = "grade", value = "组织级别", required = true) @RequestParam Optional<String> grade) throws Exception {
		CommonResult<Org> commonResult = orgService.getFillOrg(demId.orElse(""), grade.orElse("2"));
		return CommonResultUtil.<Org>getResult(commonResult);
	}

	@RequestMapping(value = "orgParam/getParamById", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取指定组织参数", httpMethod = "GET", notes = "根据组织Id和参数别名获取组织参数")
	public CommonResult<String> getParamById(@ApiParam(name = "orgId", value = "组织代码", required = true) @RequestParam String orgId,
							   @ApiParam(name = "alias", value = "参数代码", required = true) @RequestParam String alias) throws Exception {
		String value = "";
		OrgParams params = orgService.getParamById(orgId,alias);
		if(BeanUtils.isNotEmpty(params)){
			value = params.getValue();
		}
		return CommonResult.<String>ok().value(value);
	}

	@RequestMapping(value = "orgParam/isOrgHasParamKey", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "根据组织ID判断是否有该参数", httpMethod = "GET", notes = "判断用户所在的组织是否有该参数")
	public CommonResult<Boolean> isOrgHasParamKey(@ApiParam(name = "orgId", value = "组织Id", required = true) @RequestParam String orgId,
									@ApiParam(name = "alias", value = "参数代码", required = true) @RequestParam String alias) throws Exception {
		boolean flag = orgService.isOrgHasParamKey(orgId, alias);
		return CommonResult.<Boolean>ok().value(flag);

	}

	@RequestMapping(value = "orgParam/hasOrgParamKey", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "判断用户所在的组织是否有该参数", httpMethod = "GET", notes = "判断用户所在的组织是否有该参数")
	public CommonResult<Boolean> hasOrgParamKey(@ApiParam(name = "grade", value = "组织级别", required = true) @RequestParam String grade,
								  @ApiParam(name = "userId", value = "用户Id", required = true) @RequestParam String userId,
								  @ApiParam(name = "alias", value = "参数代码", required = true) @RequestParam String alias) throws Exception {
		boolean flag = orgService.hasOrgParamKey(grade, userId, alias);
		return CommonResult.<Boolean>ok().value(flag);

	}

	@RequestMapping(value="org/getOrgsByIds",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据多个组织id获取组织(以逗号隔开)", httpMethod = "GET", notes = "根据多个组织id获取组织(以逗号隔开)")
	public CommonResult<List<Org>> getOrgsByIds(@ApiParam(name="ids",value="组织ids") @RequestParam(required=true) String ids) throws Exception{
		QueryFilter queryFilter = QueryFilter.build();
		queryFilter.addFilter("ID_", ids, QueryOP.IN);
		List<Org> orgs = orgService.queryNoPage(queryFilter);
		return CommonResult.<List<Org>>ok().value(orgs);
	}

	@RequestMapping(value="org/getOrgsByCodes",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据多个组织code获取组织(以逗号隔开)", httpMethod = "GET", notes = "根据多个组织code获取组织(以逗号隔开)")
	public CommonResult<List<Org>> getOrgsByCodes(@ApiParam(name="codes",value="组织codes") @RequestParam(required=true) String codes) throws Exception{
		QueryFilter queryFilter = QueryFilter.build();
		queryFilter.addFilter("CODE_", codes, QueryOP.IN);
		List<Org> orgs = orgService.queryNoPage(queryFilter);
		return CommonResult.<List<Org>>ok().value(orgs);
	}
}
