package com.hotent.uc.controller;

import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotent.base.enums.ResponseErrorEnums;
import com.hotent.base.query.*;
import com.hotent.base.util.UniqueIdUtil;
import com.hotent.orgext.model.OrgAuthExt;
import com.hotent.uc.api.model.IGroup;
import com.hotent.uc.api.model.IUser;
import com.hotent.uc.dao.OrgDao;
import com.hotent.uc.exception.BaseException;
import com.hotent.uc.exception.HotentHttpStatus;
import com.hotent.uc.manager.*;
import com.hotent.uc.params.orgRole.RoleOrgVo;
import com.hotent.uc.vo.OrgTreeVo;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.model.CommonResult;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.uc.dao.OrgUserDao;
import com.hotent.uc.model.Org;
import com.hotent.uc.model.OrgAuth;
import com.hotent.uc.model.OrgJob;
import com.hotent.uc.model.OrgParams;
import com.hotent.uc.model.OrgPost;
import com.hotent.uc.model.OrgRole;
import com.hotent.uc.model.OrgTree;
import com.hotent.uc.model.OrgUser;
import com.hotent.uc.model.Role;
import com.hotent.uc.model.User;
import com.hotent.uc.model.UserUnder;
import com.hotent.uc.params.common.OrgExportObject;
import com.hotent.uc.params.group.GroupIdentity;
import com.hotent.uc.params.org.OaAsyncObject;
import com.hotent.uc.params.org.OrgAuthVo;
import com.hotent.uc.params.org.OrgPostVo;
import com.hotent.uc.params.org.OrgUserVo;
import com.hotent.uc.params.org.OrgVo;
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.web.multipart.MultipartHttpServletRequest;

/**
 * 组织模块接口
 * 
 * @author liangqf
 *
 */
@RestController
@RequestMapping("/api/org/v1/")
@Api(tags = "组织管理")
@ApiGroup(group= {ApiGroupConsts.GROUP_UC})
public class OrgController extends BaseController<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;
    @Autowired
	DingApiManager dingApiManager;
    @Autowired
	OrgExternalSettingManager orgExternalSettingManager;
    @Autowired
	ExtOrgLogManager extOrgLogManager;
    @Autowired
	OrgDao orgDao;
	@Autowired
	OrgPostManager orgPostService;
    /**
     * 根据组织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 {
        return orgUserService.deleteOrgById(orgId,userId);
    }

	@RequestMapping(value = "orgs/getOrgPage", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取组织列表（带分页信息）", httpMethod = "POST", notes = "获取组织列表")
	public PageList<Org> getOrgPage(
			@ApiParam(name = "queryFilter", value = "通用查询对象") @RequestBody QueryFilter<Org> queryFilter,
			@ApiParam(name = "loadChildOrg", value = "加载子组织", required = false) @RequestParam(required = false) String loadChildOrg,
			@ApiParam(name = "loadSuperiorInfo", value = "加载所属组织和部门", required = false) @RequestParam(required = false) String loadSuperiorInfo) throws Exception {
		Map<String,String> vo = queryFilter.getVo();
		if(vo!=null && vo.size() > 0 && "true".equals(vo.get(QueryFilter.VO_AUTHFILTER))) {
			Boolean isAdmin = ContextUtil.getCurrentUser().isAdmin();
			if(!isAdmin) {
				String demId = vo.get("demId");
				if(StringUtil.isEmpty(demId)) {
					return null;
				}
				List<Org> orgList = orgAuthService.getAuthOrgInfoByCurrentUser(demId);
				if(orgList==null || orgList.size()<=0) {
					return null;
				}
				QueryOP op = "true".equals(loadChildOrg) ? QueryOP.RIGHT_LIKE : QueryOP.EQUAL;
				for (int i = 0; i < orgList.size(); i++) {
					Org org = orgList.get(i);
					if(StringUtil.isEmpty(loadChildOrg) || "true".equals(loadChildOrg)) {
						QueryField field = new QueryField("PATH_", org.getPath(), QueryOP.RIGHT_LIKE, FieldRelation.OR,"main");
						queryFilter.getQuerys().add(field);
					} else if("false".equals(loadChildOrg)){
						QueryField field = new QueryField("PATH_=CONCAT('"+org.getPath()+"', ID_, '.')", "", QueryOP.APPLY, FieldRelation.OR,"main");
						queryFilter.getQuerys().add(field);
					}
				}
			}
		}
		PageList<Org> list = orgService.query(queryFilter);
		//加载所属组织信息
		if("true".equals(loadSuperiorInfo)) {
			for (int i = 0; i < list.getRows().size(); i++) {
				Org o = list.getRows().get(i);
				Org superiorOrg = orgService.getById(o.getSuperiorOrgId());
				o.setSuperiorOrg(superiorOrg);
				Org superiorDep = orgService.getById(o.getSuperiorDepId());
				o.setSuperiorDep(superiorDep);
			}
		}

		return 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> rtn = null;
		try {
			String id= UniqueIdUtil.getSuid();
			orgVo.setId(id);//主动设置id
			rtn = orgService.addOrg(orgVo);
            //创建钉钉部门操作
			if(orgVo.getOrgType().equals(Org.ORG_TYPE_DEP)){
				Org org=orgService.get(id);
				List<CommonResult<String>> resultList=new ArrayList<>();
				String pathName=org.getPathName();
				List<Org> orgList=orgService.getByPathName("/"+pathName.split("/")[1]);
				orgList.stream().forEach(item->{
					resultList.add(dingApiManager.releSystemToDingdept(item.getId(),id));
				});
				//记录同步日志
				QueryWrapper<Org> wrapper =new QueryWrapper<>();
				wrapper.eq("PATH_NAME_","/"+pathName.split("/")[1]);
				List<Org> listresult=orgDao.selectOrgByWrapper(wrapper);
				Org orgIndex=listresult.get(0);
				String pushType=orgIndex.getJoinExtSystemType();//=1为钉钉
				String pushDir=orgIndex.getJoinExtSystemSyndir();//=0=>钉钉
				extOrgLogManager.saveSynExtOrgLog(resultList,orgIndex.getId(),pushType,pushDir,"0");
			}




		} catch (Exception e) {
			e.printStackTrace();
			rtn = new CommonResult<String>(false,e.getMessage(),"保存失败");
		}
		return rtn;
	}

	/**
	 * 根据组织编码删除组织
	 * 
	 * @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,
			@ApiParam(name = "verifyChildOrg", value = "验证是否包含子组织和用户", required = false) @RequestParam(required = false) String verifyChildOrg) throws Exception {
		CommonResult<String> rtn = null;
		try {
			if(StringUtil.isNotEmpty(verifyChildOrg) && "true".equals(verifyChildOrg)) {
				String[] codeArr = codes.split(",");
				String message = "";
				for (String code : codeArr) {
					Org o = orgService.getByCode(code);
					if (BeanUtils.isEmpty(o)) {
						message += "编码为【" + code + "】的组织不存在；";
						continue;
					}
					List<Org> orgList = orgService.getByParentId(o.getId());
					if(orgList!=null && orgList.size()>0) {
						message += "名称为【" + o.getName() + "】的组织下包含子组织，请先删除子组织；";
						continue;
					}
					List<User> userList = userService.getUserListByOrgId(o.getId());
					if(userList!=null && userList.size()>0) {
						message += "名称为【" + o.getName() + "】的组织下包含用户，请先删除用户；";
						continue;
					}
				}
				if(StringUtil.isNotEmpty(message)){
					return new CommonResult<String>(false, message, "");
				}
			}
			//删除钉钉部门优先系统中删除
			String[] codeArr = codes.split(",");
			for (String code : codeArr) {
				Org o = orgService.getByCode(code);
				dingApiManager.deleteDingDept(o);

				List<CommonResult<String>> resultList=new ArrayList<>();
				CommonResult<String> result = new CommonResult<String>(true,o.getName()+"部门删除成功",o.getName()+"部门删除成功");
				resultList.add(result);
				QueryWrapper<Org> wrapper =new QueryWrapper<>();
				wrapper.eq("PATH_NAME_","/"+o.getPathName().split("/")[1]);
				List<Org> listresult=orgDao.selectOrgByWrapper(wrapper);
				Org orgIndex=listresult.get(0);
				String pushType=orgIndex.getJoinExtSystemType();//=1为钉钉
				String pushDir=orgIndex.getJoinExtSystemSyndir();//=0=>钉钉
				extOrgLogManager.saveSynExtOrgLog(resultList,orgIndex.getId(),pushType,pushDir,"0");
			}

			rtn = orgService.deleteOrg(codes);

		} catch (Exception e) {
			e.printStackTrace();
			rtn = new CommonResult<String>(false,e.getMessage(),"保存失败");
		}
		return rtn;
	}

	/**
	 * 修改组织
	 * 
	 * @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> result=orgService.updateOrg(orgVo);
		//创建钉钉部门操作
		if(orgVo.getOrgType().equals(Org.ORG_TYPE_DEP)){
			Org org=orgService.get(orgVo.getId());
			List<CommonResult<String>> resultList=new ArrayList<>();
			String pathName=org.getPathName();
			List<Org> orgList=orgService.getByPathName("/"+pathName.split("/")[1]);
			orgList.stream().forEach(item->{
				resultList.add(dingApiManager.releSystemToDingdept(item.getId(),orgVo.getId()));
			});
			//记录同步日志
			QueryWrapper<Org> wrapper =new QueryWrapper<>();
			wrapper.eq("PATH_NAME_","/"+pathName.split("/")[1]);
			List<Org> listresult=orgDao.selectOrgByWrapper(wrapper);
			Org orgIndex=listresult.get(0);
			String pushType=orgIndex.getJoinExtSystemType();//=1为钉钉
			String pushDir=orgIndex.getJoinExtSystemSyndir();//=0=>钉钉
			extOrgLogManager.saveSynExtOrgLog(resultList,orgIndex.getId(),pushType,pushDir,"0");
		}
		return result;
	}

	/**
	 * 根据组织编码获取组织
	 * 
	 * @param code
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "org/getOrg", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据组织编码获取组织", httpMethod = "GET", notes = "根据组织编码获取组织")
	public Org getOrg(@ApiParam(name = "code", value = "组织编码", required = true) @RequestParam String code)
			throws Exception {
		Org org = orgService.getOrg(code);
		Map<String, Object> map = new HashMap<>();
		map.put("orgCode", code);
		map.put("group", "true");
		List<Map<String, Object>> list = orgUserDao.getUserNumByOrgCode(map);
		org.setNowNum(list.size());
		return 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 PageList<Org> getOrgLimitByCodes(@ApiParam(name = "codes", value = "组织编码", required = true) @RequestParam String codes)
			throws Exception {
		PageList<Org> res=orgService.getOrgLimitByCodes(codes);
		return res;
	}
	
	/**
	 * 根据组织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 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 org;
	}
	/**
	 * 根据组织id获取组织
	 * 
	 * @param code
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "org/getOrgById", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据组织id获取组织", httpMethod = "GET", notes = "根据组织id获取组织")
	public Org getOrgById(@ApiParam(name = "id", value = "组织id", required = true) @RequestParam String id)throws Exception {
		return orgService.get(id);
	}

	/**
	 * 根据组织id（多个）获取组织信息
	 * @param ids
	 */
	@RequestMapping(value = "org/getOrgByIds", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据组织id获取组织信息", httpMethod = "GET", notes = "根据组织id获取组织信息")
	public List<Org> getOrgByIds(@ApiParam(name = "ids", value = "组织ids(逗号分隔)", required = true) @RequestParam String ids)throws Exception {
		List<String> orgIds = new ArrayList<>(Arrays.asList(ids.split(",")));
		List<Org> orgList = orgService.getOrgByIds(orgIds);
		return orgList;
	}

	@RequestMapping(value = "org/getOrgByIdsAndCurOrgId", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据组织id获取组织信息", httpMethod = "GET", notes = "根据组织id获取组织信息")
	public List<Map<String, Object>> getOrgByIdsAndCurOrgId(@ApiParam(name = "ids", value = "组织ids(逗号分隔)", required = true) @RequestParam String ids,
											@ApiParam(name="orgId",value="组织id", required = true) @RequestParam String orgId)throws Exception {
		Org org = orgService.get(orgId);
		String orgPath = org.getPath();
		List<String> orgIds = new ArrayList<>(Arrays.asList(ids.split(",")));
		List<Map<String, Object>> orgList = orgService.getOrgByIdsAndCurOrgId(orgIds, orgPath);
		return orgList;
	}
	
	/**
	 * 获取当前用户主组织
	 * 
	 * @param code
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "org/getOrgMaster", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据当前用户主组织", httpMethod = "GET", notes = "根据当前用户主组织")
	public Org getOrgMaster()
			throws Exception {
		return orgService.getOrgMaster(ContextUtil.getCurrentUser().getAccount());
		
	}

	/**
	 * 保存组织参数
	 * 
	 * @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> rtn = null;
		try {
			rtn = orgService.saveOrgParams(orgCode, params);
		} catch (Exception e) {
			rtn = new CommonResult<String>(false,"保存失败,请将参数填写完整",e.getMessage());
		}
		return rtn;
	}

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

	/**
	 * 获取指定组织参数
	 * 
	 * @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 {
		return orgService.getParamByAlias(orgCode, alias);
	}

	/**
	 * 加入用户
	 * 
	 * @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 {
		return orgService.addOrgUser(orgUserVo);
	}

    /**
     * 新增用户是给用户设置组织
     *
     * @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(",");

        //TODO 当给用户添加第二个组织信息时，校验用户是否有岗位信息
		QueryFilter filter = QueryFilter.build()
				.withPage(new PageBean(1, PageBean.WITHOUT_PAGE))
				.withQuery(new QueryField("account", account, QueryOP.EQUAL));
		IPage<HashMap<String,Object>> p = orgUserService.getUserOrgPage(filter);
		List<HashMap<String,Object>> records = p.getRecords();
		if(records!=null && records.size()>1) {
			for (int i = 0; i < records.size(); i++) {
				HashMap<String,Object> map = records.get(i);
				String orgName = (String) map.get("orgName");
				String userName = (String) map.get("userName");
				String posId = (String) map.get("posId");
				if (StringUtil.isEmpty(posId)) {
					throw new RuntimeException("请先对【"+userName+"】所在的【"+orgName+"】组织或部门！设置岗位信息！");
				}
			}
		} else if(records!=null && records.size()==1){
			throw new RuntimeException("当前用户【"+account+"】已有主组织和岗位！【一人多岗】请设置岗位信息！");
		}

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

        return new CommonResult<String>(true, "设置成功", "");
    }

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

    /**
     * 用户取消加入组织
     *
     * @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 {
        return orgService.delOrgUserByUserIds(ids);
    }

	/**
	 * 判断用户在某维度下是否有主组织
	 * 
	 * @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 {
		return orgService.getUserIsMaster(account, demCode);
	}

	/**
	 * 设置人员（取消）主岗位
	 * 
	 * @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 {
		return orgService.setMaster(account, postCode);
	}

	/**
	 * 获取组织树
	 * 
	 * @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 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 {
		return orgService.getTreeDataByDem(demCode, pOrgCode);
	}

	/**
	 * 设置（取消）（主）负责人
	 * 
	 * @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 {
		return orgService.setOrgCharge(account, orgCode, isCharge);
	}

	/**
	 * 组织人员添加下属
	 * 
	 * @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 {
		return orgService.addUserUnders(userUnder);
	}

	/**
	 * 组织人员删除下属
	 * 
	 * @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 {
		return orgService.delUserUnders(account, orgCode);
	}

	/**
	 * 获取用户在某组织下的下属
	 * 
	 * @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 List<UserVo> getUserUnders(
			@ApiParam(name = "account", value = "用户帐号", required = true) @RequestParam String account,
			@ApiParam(name = "orgCode", value = "组织编码", required = true) @RequestParam String orgCode)
			throws Exception {
		return orgService.getUserUnders(account, orgCode);
	}
	
	/**
	 * 获取用户在某组织下的下属(含分页)
	 * @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 PageList<UserUnder> getUserUndersPage(
			@ApiParam(name = "filter", value = "查询对象", required = true) @RequestBody QueryFilter filter)
			throws Exception {
		Page<UserUnder> list = (Page<UserUnder>)userUnderService.getUserUnder(filter);
		return new PageList<UserUnder>(list);
	}
	
	@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 new CommonResult<String>(true,"删除成功！","");
	}

	/**
	 * 添加分级管理
	 * 
	 * @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> rtn = null;
		try {
			rtn = orgService.saveOrgAuth(orgAuthVo);
		} catch (Exception e) {
			rtn = new CommonResult<String>(false,e.getMessage(),"保存失败");
		}
		return rtn;
	}

	/**
	 * 删除分级管理
	 * 
	 * @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 {
		return orgService.deleteOrgAuth(id);
	}

	/**
	 * 获取分级管理列表
	 * 
	 * @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 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);
		return new PageList<OrgAuth>(list);
	}

	/**
	 * 获取分级管理
	 * 
	 * @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 OrgAuth getOrgAuth(@ApiParam(name = "id", value = "分级管理id", required = true) @RequestParam String id)
			throws Exception {
		return orgService.getOrgAuth(id);
	}

	/**
	 * 根据角色 和 用户id 获取信息获取 分级授权按钮信息
	 * @author: 梁宏锦
	 * @date: 2024/04/10
	 * @param roleAlias: 角色别名 xgly,xtgly
	 * @param userId: 用户id
	 * @param menuAlias: 菜单别名 orgManager/userListManager/postManager/roleManager
	 * @return: com.hotent.base.model.CommonResult<java.util.List<java.lang.String>>
	 **/
	@RequestMapping(value = "orgAuth/getOrgAuthBtnByMenuAlias", method = RequestMethod.GET, produces = {"application/json; charset=utf-8" })
	@ApiOperation(value = "根据角色/用户id/菜单别名 获取信息获取 分级授权按钮信息", httpMethod = "GET", notes = "根据角色/用户id/菜单别名 获取信息获取 分级授权按钮信息")
	public CommonResult<List<String>> getOrgAuthBtnByMenuAlias(
			@ApiParam(name="roleAlias",value="角色别名") @RequestParam(required = false) String roleAlias,
			@ApiParam(name="userId",value="用户id") @RequestParam(required = false) String userId,
			@ApiParam(name="menuAlias",value="菜单别名") @RequestParam("menuAlias") String menuAlias) throws Exception {
		String colPerms = "";
		if("orgManager".equals(menuAlias)) {
			colPerms = "ORG_PERMS_"; //组织管理
		} else if("userListManager".equals(menuAlias)) {
			colPerms = "USER_PERMS_"; //用户中心
		} else if("postManager".equals(menuAlias)) {
			colPerms = "POS_PERMS_"; //岗位管理
		} else if("roleManager".equals(menuAlias)) {
			colPerms = "ROLE_PERMS_"; //角色授权
		}

		List<String> alias = null;
		if(StringUtil.isNotEmpty(roleAlias)) {
			alias = new ArrayList<>();
			alias = Arrays.asList(roleAlias.split(","));
		}
		List<Map<String, Object>> list = orgAuthService.getOrgAuthBtnByRoleAliasOrUserId(colPerms, alias, userId);

		List<String> resList = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = list.get(i);
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				String tmp = (String)entry.getValue();
				String[] arr = tmp.split(",");
				for (int j = 0; j < arr.length; j++) {
					String str = arr[j];
					if(StringUtil.isNotEmpty(str) && !resList.contains(str)){
						resList.add(str);
					}
				}
			}
		}
		CommonResult<List<String>> result = new CommonResult<List<String>>(true,"查询成功");
		result.setValue(resList);
		return result;
	}

	/**
	 * 根据角色/用户id/菜单别名  获取信息获取 分级用户授权按钮信息
	 * @author: 梁宏锦
	 * @date: 2024/04/10
	 * @param roleAlias: 角色别名 xgly,xtgly
	 * @param userId: 用户id
	 * @param menuAlias: 菜单别名 orgManager/userListManager/postManager/roleManager
	 * @return: com.hotent.base.query.PageList<com.hotent.uc.model.OrgAuth>
	 **/
	@RequestMapping(value = "orgAuth/getAuthBtnByMenuAlias", method = RequestMethod.GET, produces = {"application/json; charset=utf-8" })
	@ApiOperation(value = "根据角色/用户id/菜单别名  获取信息获取 分级用户授权按钮信息", httpMethod = "GET", notes = "根据角色/用户id/菜单别名  获取信息获取 分级用户授权按钮信息")
	public CommonResult<Map<String, Boolean>> getAuthBtnByMenuAlias(
			@ApiParam(name="roleAlias",value="角色别名") @RequestParam(required = false) String roleAlias,
			@ApiParam(name="userId",value="用户id") @RequestParam(required = false) String userId,
			@ApiParam(name="menuAlias",value="菜单别名") @RequestParam("menuAlias") String menuAlias) throws Exception {
		List<String> alias = null;
		if(StringUtil.isNotEmpty(roleAlias)) {
			alias = new ArrayList<>();
			alias = Arrays.asList(roleAlias.split(","));
		}

		//"orgPerms": "add,edit,delete",
		//"userPerms": "add,edit,delete,setMaster,setPos",
		// "posPerms": "add,edit,delete",
		// "orgauthPerms": "add,edit,delete",
		// "rolePerms": "add,delete",
		Map<String, Map<String, Boolean>> map = new HashedMap();
		Map<String, Boolean> p_org = new HashedMap();
		p_org.put("add", false);
		p_org.put("edit", false);
		p_org.put("delete", false);
		Map<String, Boolean> p_user = new HashedMap();
		p_user.put("add", false);
		p_user.put("edit", false);
		p_user.put("delete", false);
		p_user.put("setMaster", false);
		p_user.put("setPos", false);
		Map<String, Boolean> p_pos = new HashedMap();
		p_pos.put("add", false);
		p_pos.put("edit", false);
		p_pos.put("delete", false);
		Map<String, Boolean> p_orgauth = new HashedMap();
		p_orgauth.put("add", false);
		p_orgauth.put("edit", false);
		p_orgauth.put("delete", false);
		Map<String, Boolean> p_role = new HashedMap();
		p_role.put("add", false);
		p_role.put("delete", false);
		//map.put("orgauthPerms", p_orgauth);
		if("orgManager".equals(menuAlias)) {
			map.put("orgPerms", p_org); //组织管理
		} else if("userListManager".equals(menuAlias)) {
			map.put("userPerms", p_user); //用户中心
		} else if("postManager".equals(menuAlias)) {
			map.put("posPerms", p_pos); //岗位管理
		} else if("roleManager".equals(menuAlias)) {
			map.put("rolePerms", p_orgauth); //角色授权
		}
		List<OrgAuth> list = orgAuthService.getOrgAuthBtnByMenuAlias(alias, userId);

		for (int i = 0; i < list.size() ; i++) {
			OrgAuth auth = list.get(i);
			String orgPerms = auth.getOrgPerms();
			String userPerms = auth.getUserPerms();
			String posPerms = auth.getPosPerms();
			String rolePerms = auth.getRolePerms();
			String orgauthPerms = auth.getOrgauthPerms();

			//this.checkPerms(map, "orgauthPerms", orgauthPerms);
			if("orgManager".equals(menuAlias)) {
				this.checkPerms(map, "orgPerms", orgPerms); //组织管理
			} else if("userListManager".equals(menuAlias)) {
				this.checkPerms(map, "userPerms", userPerms); //用户中心
			} else if("postManager".equals(menuAlias)) {
				this.checkPerms(map, "posPerms", posPerms); //岗位管理
			} else if("roleManager".equals(menuAlias)) {
				this.checkPerms(map, "rolePerms", rolePerms); //角色授权
			}
		}

		CommonResult<Map<String, Boolean>> result = new CommonResult(ResponseErrorEnums.SUCCESS_OPTION);
		if("orgManager".equals(menuAlias)) {
			result.setValue(map.get("orgPerms"));
		} else if("userListManager".equals(menuAlias)) {
			result.setValue(map.get("userPerms"));
		} else if("postManager".equals(menuAlias)) {
			result.setValue(map.get("posPerms"));
		} else if("roleManager".equals(menuAlias)) {
			result.setValue(map.get("rolePerms"));
		} else {
			result.setValue(null);
		}
		return result;
	}

	private void checkPerms(Map<String, Map<String, Boolean>> map, String type, String perms) {
		Map<String, Boolean> m = new HashedMap();
		if("orgPerms".equals(type)) {
			m = map.get("orgPerms");
		} else if("userPerms".equals(type)) {
			m = map.get("userPerms");
		} else if("posPerms".equals(type)) {
			m = map.get("posPerms");
		} else if("orgauthPerms".equals(type)) {
			m = map.get("orgauthPerms");
		} else if("rolePerms".equals(type)) {
			m = map.get("rolePerms");
		}

		String[] str = perms.split(",");
		for (int i = 0; i < str.length; i++) {
			String key = str[i];
			Boolean flag = m.get(key);
			if(flag!=null && flag.equals(false)) {
				m.put(key, true);
			}
		}
	}

	/**
	 * 根据角色/用户id/菜单别名/按钮Code  获取信息获取 分级用户授权信息
	 * @author: 梁宏锦
	 * @date: 2024/04/10
	 * @param roleAlias: 角色别名 xgly,xtgly
	 * @param userId: 用户id
	 * @param menuAlias: 菜单别名 orgManager/userListManager/postManager/roleManager
	 * @param btnCode: 按钮Code add/edit/delete
	 * @param page: 页号
	 * @param pageSize: 分页大小
	 * @return: com.hotent.base.query.PageList<com.hotent.uc.model.OrgAuth>
	 **/
	@RequestMapping(value = "orgAuth/getOrgAuthListByMenuAlias", method = RequestMethod.GET, produces = {"application/json; charset=utf-8" })
	@ApiOperation(value = "根据角色/用户id/菜单别名/按钮Code  获取信息获取 分级用户授权信息", httpMethod = "GET", notes = "根据角色/用户id/菜单别名/按钮Code  获取信息获取 分级用户授权信息")
	public CommonResult<PageList<OrgAuth>> getOrgAuthListByMenuAlias(
			@ApiParam(name="roleAlias",value="角色别名") @RequestParam(required = false) String roleAlias,
			@ApiParam(name="userId",value="用户id") @RequestParam(required = false) String userId,
			@ApiParam(name="menuAlias",value="菜单别名") @RequestParam("menuAlias") String menuAlias,
			@ApiParam(name="btnCode",value="按钮Code") @RequestParam("btnCode") String btnCode,
			@ApiParam(name = "page", value = "页号",required = false) @RequestParam(required = false) Integer page,
			@ApiParam(name = "pageSize", value = "分页大小",required = false) @RequestParam(required = false) Integer pageSize) throws Exception {
		String colPerms = "";
		if("orgManager".equals(menuAlias)) {
			colPerms = "ORG_PERMS_"; //组织管理
		} else if("userListManager".equals(menuAlias)) {
			colPerms = "USER_PERMS_"; //用户中心
		} else if("postManager".equals(menuAlias)) {
			colPerms = "POS_PERMS_"; //岗位管理
		} else if("roleManager".equals(menuAlias)) {
			colPerms = "ROLE_PERMS_"; //角色授权
		}

		List<String> alias = null;
		if(StringUtil.isNotEmpty(roleAlias)) {
			alias = new ArrayList<>();
			alias = Arrays.asList(roleAlias.split(","));
		}
		IPage<OrgAuth> npage = new Page<OrgAuth>(page, pageSize);
		PageList<OrgAuth> list = orgAuthService.getOrgAuthListByMenuAlias(npage, alias, userId, colPerms, btnCode);

		CommonResult<PageList<OrgAuth>> result = new CommonResult(ResponseErrorEnums.SUCCESS_OPTION);
		result.setValue(list);
		return result;
	}

	/**
	 * 获取子组织
	 * 
	 * @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 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+"】没有找到对应的组织");
		}
		return orgService.getByParentId(parentId);
	}
	/**
	 * 获取子组织(只获取底下一层子组织)
	 *
	 * @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 List<Org> getOrgsByparentId(@ApiParam(name = "parentId", value = "父组织id", required = true) @RequestParam String parentId)
					throws Exception {
		return orgService.getOrgsByparentId(parentId);
	}

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

	/**
	 * 获取组织下的人员
	 * 
	 * @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 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 {
		return orgService.getUsersByOrgCodes(orgCodes, isMain);
	}
	
	/**
	 * 根据组织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 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 {
		return orgService.getChargesByOrgId(orgId,isMain,demCode);
	}
	

	/**
	 * 获取组织下的岗位
	 * 
	 * @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 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 {
		return orgService.getPostsByOrgCodes(orgCodes, isMain);
	}
	
	/**
	 * 获取岗位列表（分页）
	 * @param queryFilter
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "orgPosts/getOrgPostPage", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取岗位列表（分页）", httpMethod = "POST", notes = "获取岗位列表（分页）")
	public 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);
		}
		return postService.getOrgPost(queryFilter);
	}

	/**
	 * 根据级别获取组织
	 * 
	 * @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 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 {
		return orgService.getByLevel(level, demCode);
	}

	/**
	 * 用户加入到岗位
	 * 
	 * @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 {
		if (StringUtil.isEmpty(accounts)) {
			throw new com.hotent.base.exception.BaseException(HotentHttpStatus.REUIRED.description() + "用户帐号accounts必填！");
		}
		String[] accountArr = accounts.split(",");
		for (int i = 0; i <accountArr.length ; i++) {
			this.validateUserPost(accountArr[i], postCode);
		}
		return orgService.saveUserPost(accounts, postCode);
	}
	
	/**
	 * 用户加入到岗位
	 * 
	 * @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 {
		if (StringUtil.isEmpty(postCodes)) {
			throw new com.hotent.base.exception.BaseException(HotentHttpStatus.REUIRED.description() + "岗位代码postCode必填！");
		}
		String[] postCodesArr = postCodes.split(",");
		for (int i = 0; i <postCodesArr.length ; i++) {
			this.validateUserPost(account, postCodesArr[i]);
		}
		return orgService.saveUserPosts(account, postCodes);
	}

	/**
	 * 当给用户添加第二个组织信息时，校验用户是否有岗位信息
	 * @param account 账号信息
	 * @param postCode 岗位信息
	 */
	private void validateUserPost(String account, String postCode) {
		//TODO 当给用户添加第二个组织信息时，校验用户是否有岗位信息
		OrgPost post = orgPostService.getByCode(postCode); //只针对单选时组织且空岗位时，跳过验证
		if (BeanUtils.isEmpty(post)) {
			throw new com.hotent.base.exception.BaseException("根据输入岗位代码没有找到对应的岗位信息！");
		}
		String curOrgIdByPost = post.getOrgId();

		QueryFilter filter = QueryFilter.build()
				.withPage(new PageBean(1, PageBean.WITHOUT_PAGE))
				.withQuery(new QueryField("account", account, QueryOP.EQUAL));
		IPage<HashMap<String,Object>> p = orgUserService.getUserOrgPage(filter);
		List<HashMap<String,Object>> records = p.getRecords();
		if(records!=null && records.size()>=1) {
			for (int i = 0; i < records.size(); i++) {
				HashMap<String,Object> map = records.get(i);
				String orgId = (String) map.get("orgId");
				String orgName = (String) map.get("orgName");
				String userName = (String) map.get("userName");
				String posId = (String) map.get("posId");
				if (StringUtil.isEmpty(posId) && !curOrgIdByPost.equals(orgId)) {
					throw new RuntimeException("请先对【"+userName+"】所在的【"+orgName+"】组织或部门！设置岗位信息！");
				}
			}
		}
	}

	/**
	 * 用户退出岗位
	 * 
	 * @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 {
		return orgService.delUserPost(accounts, postCode);
	}

	/**
	 * 组织添加岗位
	 * 
	 * @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> rtn = null;
		try {
			rtn = orgService.saveOrgPost(orgPostVo);
		} catch (Exception e) {
			e.printStackTrace();
			rtn = new CommonResult<String>(false,e.getMessage(),"保存失败");
		}
		return rtn;
	}
	
	/**
	 * 更新岗位
	 * @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> rtn = null;
		try {
			rtn = orgService.updateOrgPost(orgPostVo);
		} catch (Exception e) {
			e.printStackTrace();
			rtn = new CommonResult<String>(false,e.getMessage(),"保存失败");
		}
		return rtn;
	}
	
	/**
	 * 根据岗位编码获取岗位信息
	 * @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 new CommonResult<OrgPost>(true, "获取岗位成功", 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 List<OrgPost> getOrgPostByUserAccount(@ApiParam(name="account",value="用户账号",required=true) @RequestParam String account) throws Exception {
		List<OrgPost> list = postService.getListByAccount(account, null);
		return 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 {
		return orgService.deleteOrgPost(postCodes);
	}

	/**
	 * 设置组织（取消）责任岗位
	 * 
	 * @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 {
		return orgService.setPostMaster(postCode, isMain);
	}

	/**
	 * 设置人员岗位有效期
	 * 
	 * @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 {
		return orgService.setUserPostDueTime(postDueVo);
	}

	/**
	 * 校验所有用户岗位是否有效
	 * 
	 * @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 {
		return orgService.validOrgUser();
	}
	
	/**
	 * 组织添加角色
	 * @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 new CommonResult<>(true,"添加成功","");
	}
	
	/**
	 * 删除组织指定角色
	 * @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 new CommonResult<String>(true, "删除成功！", "");
	}
	
	/**
	 * 删除组织所拥有的角色
	 * @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 new CommonResult<String>(true, "删除成功！", "");
	}
	
	/**
	 * 获取组织所拥有的角色
	 * 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 List<Role> getOrgRoleByCode(
			@ApiParam(name = "orgCode", value = "组织编码", required = true) @RequestParam String orgCode)
			throws Exception {
		return orgService.getOrgRoleByCode(orgCode);
	}
	
	/**
	 * 物理删除所有逻辑删除了的组织数据
	 * @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();
		return OrgUtil.getRemovePhysiMsg(num);
	}
	
	/**
	 * 物理删除所有逻辑删除了的岗位数据
	 * @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();
		return OrgUtil.getRemovePhysiMsg(num);
	}
	
	/**
	 * 物理删除所有逻辑删除了的用户组织关系数据
	 * @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();
		return OrgUtil.getRemovePhysiMsg(num);
	}
	
	/**
	 * 根据维度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 List<OrgTree> getByParentAndDem(
			@ApiParam(name = "params", value = "参数值") @RequestBody Map<String,String> params) throws Exception {
		return orgService.getOrgTreeByParentAndDem(params);
	}

	/**
	 * 根据当前用户分管组织 获取对应维度下的组织
	 * name 部门名称
	 * orgType 0,2,3 组织类型(0:组织 1:部门 2:关联组织 3:虚拟组织)
	 */
	@RequestMapping(value = "orgs/getByParentAndDemByOrgAuth", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "根据当前用户分管组织 获取对应维度下的组织", httpMethod = "POST", notes = "根据当前用户分管组织 获取对应维度下的组织，参数{\"demId\":\"1\",\"parentId\":\"1\",\"orgType\":\"0,1,2,3\"}")
	public List<OrgTree> getByParentAndDemByOrgAuth(@ApiParam(name = "params", value = "参数值") @RequestBody Map<String,String> params) throws Exception {
		String demId = params.get("demId");
		String parentId = params.get("parentId");
		String name = params.get("name");
		String orgType = params.get("orgType");
		if (BeanUtils.isEmpty(demId)) {
			throw new RuntimeException("缺少维度信息！");
		}
		try {
			List<String> orgList = new ArrayList<String>();
			//加载部门数据
			List<OrgTree> listTree = new ArrayList<OrgTree>();
			if (BeanUtils.isEmpty(parentId) || parentId.equals("0")) {
				//获取当前用户 授权部门
				orgList.addAll(orgAuthService.getAuthOrgByCurrentUser(demId));

				for (int i = 0; i < orgList.size(); i++) {
					String orgId = orgList.get(i);
					if(StringUtil.isEmpty(orgId)) {
						continue;
					}
					Map param = new HashMap();
					param.put("demId", demId);
					param.put("id", orgId);
					param.put("orgType", orgType);

					List<OrgTree> tree = orgService.getByParentAndDemToTree(param);
					for (int j = 0; j < tree.size(); j++) {
						OrgTree t = tree.get(j);
						//判断是否自动添加的根节点
						if(t.getId().equals("0")){
							continue;
						}
						if (orgList.contains(t.getId())) {
							t.setParentId("0");
						}
						listTree.add(t);
					}
				}
				//需要进行组织或部门的名称 过滤
				if(StringUtil.isNotEmpty(name)) {
					List<OrgTree> nameListTree = new ArrayList<OrgTree>();
					for (int i = 0; i < listTree.size(); i++) {
						OrgTree org = listTree.get(i);
						Map param = new HashMap();
						param.put("demId", demId);
						param.put("path", org.getPath());
						param.put("name", name);
						param.put("orgType", orgType);

						List<OrgTree> tree = orgService.getByParentAndDemToTree(param);
						nameListTree.addAll(tree);
					}
					return nameListTree;
				} else {
					return listTree;
				}
			} else {
				orgList.add(parentId);
				for (int i = 0; i < orgList.size(); i++) {
					String orgId = orgList.get(i);
					Map param = new HashMap();
					param.put("demId", demId);
					param.put("parentId", orgId);
					param.put("orgType", orgType);

					List<OrgTree> tree = orgService.getByParentAndDemToTree(param);
					for (int j = 0; j < tree.size(); j++) {
						OrgTree t = tree.get(j);
						if (orgList.contains(t.getId())) {
							t.setParentId("0");
						}
						listTree.add(t);
					}
				}
				return listTree;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	@GetMapping(value = "orgs/children/{orgId}", produces = {"application/json; charset=utf-8" })
	@ApiOperation(value = "获取orgId的下级组织", httpMethod = "GET", notes = "获取orgId的下级组织")
	public 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 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 List<OrgTree> getByParentAndDemToTree(
			@ApiParam(name = "params", value = "参数值") @RequestBody Map<String,String> params) throws Exception {
		List<OrgTree> orgTreeList = orgService.getByParentAndDemToTree(params);
		return 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 PageList<HashMap<String,Object>> getOrgUserPage(
			@ApiParam(name = "queryFilter", value = "通用查询对象") @RequestBody QueryFilter queryFilter) throws Exception {
		PageList<HashMap<String, Object>> pageList = orgService.getOrgUserPage(queryFilter);
		return 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 {
		try {
			orgUserService.setMaster(id);
			return  new CommonResult<String>(true, "操作成功！", "");
		} catch (Exception e) {
			e.printStackTrace();
			return  new CommonResult<String>(false, "操作失败！", "");
		}
	}

	/**
	 * 通过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 {
		try {
			orgUserService.setMasterByIds(ids);
			return  new CommonResult<String>(true, "操作成功！", "");
		} catch (Exception e) {
			e.printStackTrace();
			return  new CommonResult<String>(false, "操作失败！", "");
		}
	}
	
	/**
	 * 用户批量添加下属
	 * 
	 * @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 {
		return orgService.setUnderUsers(orgId, account, underAccounts);
	}
	
	/**
	 * 组织批量加入用户
	 * 
	 * @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> rtn = null;
		try {
			rtn = orgService.addUsersForOrg(orgCode, accounts);
		} catch (Exception e) {
			rtn = new CommonResult<String>(false,e.getMessage(),"保存失败");
		}
		return rtn;
	}
	
	/**
	 * 根据时间获取组织数据（数据同步）
	 * @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 List<Org> getOrgByTime(@ApiParam(name="exportObject",value="获取数据参数类",required=true) @RequestBody OrgExportObject exportObject) throws Exception{
		return orgService.getOrgByTime(exportObject);
	}
	
	/**
	 * 根据时间获取组织参数数据（数据同步）
	 * @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 List<OrgParams> getOrgParamByTime(@ApiParam(name="exportObject",value="获取数据参数类",required=true) @RequestBody OrgExportObject exportObject) throws Exception{
		return orgService.getOrgParamByTime(exportObject);
	}
	
	/**
	 * 根据时间获取组织岗位数据（数据同步）
	 * @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 List<OrgPost> getOrgPostByTime(@ApiParam(name="exportObject",value="获取数据参数类",required=true) @RequestBody OrgExportObject exportObject) throws Exception{
		return orgService.getOrgPostByTime(exportObject);
	}
	
	/**
	 * 根据时间获取组织角色数据（数据同步）
	 * @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 List<OrgRole> getOrgRoleByTime(@ApiParam(name="exportObject",value="获取数据参数类",required=true) @RequestBody OrgExportObject exportObject) throws Exception{
		return orgService.getOrgRoleByTime(exportObject);
	}
	
	/**
	 * 根据时间获取用户组织关系数据（数据同步）
	 * @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 List<OrgUser> getOrgUserByTime(@ApiParam(name="exportObject",value="获取数据参数类",required=true) @RequestBody OrgExportObject exportObject) throws Exception{
		return orgService.getOrgUserByTime(exportObject);
	}
	
	/**
	 * 根据时间获取组织中下属数据（数据同步）
	 * @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 List<UserUnder> getUserUnderByTime(@ApiParam(name="exportObject",value="获取数据参数类",required=true) @RequestBody OrgExportObject exportObject) throws Exception{
		return orgService.getUserUnderByTime(exportObject);
	}
	
	@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{
		return orgService.isCodeExist(code);
	}
	
	@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{
		return orgService.isPostCodeExist(code);
	}
	
	
	/**
	 * 同步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);
		return userImportService.syncSoap(oaAsyncObject,ip);
	}
	
	@RequestMapping(value = "orgRoles/getOrgRoleList", method = RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "获取组织角色列表（带分页信息）", httpMethod = "POST", notes = "获取组织角色列表（带分页信息）")
	public PageList<OrgRole> getOrgRoleList(@ApiParam(name = "queryFilter", value = "通用查询对象") @RequestBody QueryFilter queryFilter) throws Exception {
		//queryFilter.setClazz(OrgRole.class);
		PageList<OrgRole> list = orgRoleService.query(queryFilter);
		return list;
	}

	@RequestMapping(value = "orgRoles/getRoleOrgs", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
	@ApiOperation(value = "获取角色下的组织", httpMethod = "POST", notes = "获取角色下的所有组织")
	public PageList<Org> getRoleOrgs(@ApiParam(name = "roleId", value = "角色ID", required = true) @RequestParam String roleId,
									 @ApiParam(name = "queryFilter", value = "通用查询对象", required = true) @RequestBody QueryFilter<Org> queryFilter) {
		return orgRoleService.getRoleOrgs(roleId, queryFilter);
	}

	@RequestMapping(value = "orgRoles/removeRoleOrgs", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
	@ApiOperation(value = "删除角色下的组织", httpMethod = "POST", notes = "删除角色下的组织")
	public CommonResult<String> removeRoleOrgs(@ApiParam(name = "RoleOrgVo", value = "vo") @RequestBody RoleOrgVo vo) throws Exception {
		orgRoleService.removeRoleOrgs(vo);
		return new CommonResult<>(true, "删除成功");
	}
	
	@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 {
		return orgService.addOrgRole(vo);
	}

	@RequestMapping(value = "orgRoles/saveRoleOrg", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "保存角色组织信息", httpMethod = "POST", notes = "保存角色组织信息")
	public CommonResult<String> saveRoleOrg(@ApiParam(name = "vo") @RequestBody RoleOrgVo vo) throws Exception {
		orgService.saveRoleOrg(vo);
		return new CommonResult<>(true, "保存成功");
	}
	
	@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 {
		return orgService.delOrgRoleById(id);
	}

	@RequestMapping(value = "orgRoles/updateInheritStatus", method = RequestMethod.POST, produces = {
			"application/json; charset=utf-8"})
	@ApiOperation(value = "更新组织角色继承状态", httpMethod = "POST", notes = "更新组织角色继承状态")
	public CommonResult<String> updateInheritStatus(@ApiParam(name = "id", value = "主键") @RequestBody RoleOrgVo orgVo) {
		if (BeanUtils.isEmpty(orgVo.getOrgIds())) {
			throw new BaseException("组织ID不能为空");
		}
		orgRoleService.updateInheritStatus(orgVo);
		return new CommonResult<>(true, "更新成功");
	}
	
	
	/**
	 * 更新组织所在树的位置
	 * @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 {
		return orgService.updateOrgPos(orgId, parentId);
	}
	
	
	@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{
		orgService.exportUsers(orgCode,isChildre);
	}
	
	@RequestMapping(value = "org/getMainGroup", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户主组织", httpMethod = "GET", notes = "获取用户主组织")
	public 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 {
		return orgService.getMainGroup(userId, demId.orElse(""));
	}

	@RequestMapping(value = "org/getParentMainOrg", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户主组织（包含父级组织）", httpMethod = "GET", notes = "获取用户主组织（包含父级组织）")
	public Map<String,Org> getParentMainOrg(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId)throws Exception {
		return orgService.getParentMainOrg(userId);
	}
	
	@RequestMapping(value = "orgJobs/getJobsByUserId", method = RequestMethod.GET, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户所有职务", httpMethod = "GET", notes = "获取用户所有职务")
	public List<OrgJob> getJobsByUserId(
		@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId)throws Exception {
		return orgJobManager.getListByUserId(userId);
	}

	@RequestMapping(value = "org/isSupOrgByCurrMain", method = RequestMethod.GET, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "判断当前用户主部门是否有上级 ", httpMethod = "GET", notes = "判断当前用户主部门是否有上级 ")
	public 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 {
		return orgService.isSupOrgByCurrMain(userId,demId,level);
	}
	
	@RequestMapping(value = "org/getSupOrgByCurrMain", method = RequestMethod.GET, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户的主岗位组织关系", httpMethod = "GET", notes = "获取用户的主岗位组织关系")
	public 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)) {
        	return orgUsers.get(0);
        }
		return null;
	}
	
	@RequestMapping(value = "org/getMainPostOrOrgByUserId", method = RequestMethod.GET, produces = {"application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户的主岗位、主组织(优先获取默认维度的，没有时获取其他维度的)", httpMethod = "GET", notes = "获取用户的主岗位、主组织(优先获取默认维度的，没有时获取其他维度的)")
	public OrgUser getMainPostOrOrgByUserId(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId) throws Exception{
		return orgUserService.getMainPostOrOrgByUserId(userId);
	}
	
	/**
	 * 获取指定上级级别的组织的负责人
	 * @param userId
	 * @param level
	 * @param isMainCharge
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/orgusers/getCustomLevelCharge", method = RequestMethod.GET)
	public 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<User> res=orgService.getCustomLevelCharge(userId,level,isMainCharge);
		return res;
	}
	
	/**
	 * 获取指定上级级别的组织 中指定岗位的用户
	 * @param userId
	 * @param level
	 * @param postCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/orgusers/getCustomLevelPost", method = RequestMethod.GET)
	public  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 {
		Set<GroupIdentity> res=orgService.getCustomLevelPost(userId,level,postCode);
		return res;
	}
	
	/**
	 * 获取指定上级级别的组织 中指定职务的用户
	 * @param userId
	 * @param level
	 * @param postCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/orgusers/getCustomLevelJob", method = RequestMethod.GET)
	public  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 {
		Set<GroupIdentity> res=orgService.getCustomLevelJob(userId,level,jobCode);
		return res;
	}
	
	@RequestMapping(value = "/orgusers/getStartOrgParam", method = RequestMethod.GET)
	public 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 paramValue;
		}
		Org org = userOrg.get(0);
		OrgParams byOrgIdAndAlias = orgParamsService.getByOrgIdAndAlias(org.getId(), param);
		return byOrgIdAndAlias.getValue();
	}

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

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

    @RequestMapping(value = "orgpost/getUserByUserId", method = RequestMethod.GET, produces = {
            "application/json; charset=utf-8" })
    @ApiOperation(value = "根据用户ID查询用户组织岗位角色信息", httpMethod = "GET", notes = "根据用户ID查询用户组织岗位角色信息")
    public List<Map<String, Object>> getUserByUserId(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId)throws Exception {
        return postService.getUserByUserId(userId);
    }
    
	@RequestMapping(value = "org/getPathNames", method = RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "根据用户id组获取组织全路径", httpMethod = "POST", notes = "根据用户id获得主岗位的岗位组")
	public List<Map<String,String>> getPathNames(@ApiParam(name = "userIds", value = "用户id组", required = false) @RequestParam List<String> userIds)throws Exception {
		return orgService.getPathNames(userIds);
	}
	
	@RequestMapping(value = "org/getChildrenIds", method = RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "获取子组织ID（包含自己）", httpMethod = "POST", notes = "获取子组织ID（包含自己）")
	public Map<String, Set<String>> getChildrenIds(@ApiParam(name = "ids", value = "组织id", required = true) @RequestParam Map<String,String> ids)throws Exception {
		return orgService.getChildrenIds(ids);
	}
	
	@RequestMapping(value = "org/getOrgListByDemId", method = RequestMethod.GET, produces = {"application/json; charset=utf-8" })
	@ApiOperation(value = "获取组织列表", httpMethod = "GET", notes = "获取组织列表")
	public List<Org> getOrgListByDemId(@ApiParam(name = "demId", value = "维度id", required = true) @RequestParam String demId) throws Exception{
		return orgService.getOrgListByDemId(demId);
	}

    @RequestMapping(value = "orgusers/getUserOrgNowNumByOrgId", method = RequestMethod.GET, produces = {
            "application/json; charset=utf-8" })
    @ApiOperation(value = "根据组织id查询组织下面的人员数量", httpMethod = "GET", notes = "根据组织id查询组织下面的人员数量")
    public 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);
	    return list.size();
    }

    @RequestMapping(value = "orgusers/getUserOrgNowNumByOrgIds", method = RequestMethod.GET, produces = {
            "application/json; charset=utf-8" })
    @ApiOperation(value = "根据组织id集合查询组织下面的人员数量", httpMethod = "GET", notes = "根据组织id查询组织下面的人员数量")
    public List<Map<String, Object>> getUserOrgNowNumByOrgIds(@ApiParam(name = "orgIds", value = "组织id集合", required = true) @RequestParam String orgIds)throws Exception {
		List<Map<String, Object>> res=orgService.getUserOrgNowNumByOrgIds(orgIds);
		return res;
    }
    /**
	 * 从第三方获取组织数据添加到本系统
	 *
	 * @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 {
		try {
			return orgService.addOrgFromExterUni(orgVo);
		} catch (Exception e) {
			e.printStackTrace();
			return new CommonResult<>(false, e.getMessage());
		}
	}
	/**
	 * <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 Map<String,Object> getSuperiorPost(@RequestParam(value="userId",required=true) String userId) {
		return postService.getSuperiorPost(userId);
	}

	@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 = false) @RequestParam(required = false) Optional<String> demId,
			@ApiParam(name = "grade", value = "组织级别", required = false) @RequestParam(required = false)  Optional<String> grade) throws Exception {
		return orgService.getFillOrg(demId.orElse(""),grade.orElse("2"));
	}

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

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

	@RequestMapping(value = "orgParam/hasOrgParamKey", method = RequestMethod.GET, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "判断用户所在的组织是否有该参数", httpMethod = "GET", notes = "判断用户所在的组织是否有该参数")
	public 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 {
		return orgService.hasOrgParamKey(grade,userId,alias);
	}

	@RequestMapping(value="org/getOrgsByIds",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据多个组织id获取组织(以逗号隔开)", httpMethod = "GET", notes = "根据多个组织id获取组织(以逗号隔开)")
	public 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 orgs;
	}

	@RequestMapping(value="org/getOrgsByCodes",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据多个组织code获取组织(以逗号隔开)", httpMethod = "GET", notes = "根据多个组织code获取组织(以逗号隔开)")
	public 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 orgs;
	}

	@RequestMapping(value="orgUser/operatingUserOrgPost",method=RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "转移、复制组织岗位给用户（多个组织岗位，多个用户）", httpMethod = "POST", notes = "转移、复制组织岗位给用户（多个组织岗位，多个用户）")
	public CommonResult<String> operatingUserOrgPost(@ApiParam(name="map",value="参数", required = true) @RequestBody Map<String,Object> map) throws Exception{
		return orgService.operatingUserOrgPost(map);
	}

	/**
	 * 首页门户 获取组织部门数量统计
	 * @param demId 维度Id
	 */
	@GetMapping(value="/orgs/portal/orgnum")
	@ApiOperation(value = "首页门户 获取组织部门数量统计", httpMethod = "GET", notes = "首页门户 获取组织部门数量统计")
	public JSONObject getPortalOrgNum(@ApiParam(name="demId",value="维度Id")@RequestParam(required=false)  String demId) throws Exception{
		LambdaQueryWrapper<Org> wrapper = new LambdaQueryWrapper<Org>();
		if(StringUtil.isNotEmpty(demId)) {
			wrapper.eq(Org::getDemId, demId);
		}
		Integer num = orgService.count(wrapper);
		JSONObject obj = new JSONObject();
		obj.set("orgnum", num);
		return obj;
	}

	/**
	 * 首页门户 获取用户数量统计
	 */
	@GetMapping(value="/orgUsers/portal/usernum")
	@ApiOperation(value = "首页门户 获取用户数量统计", httpMethod = "GET", notes = "首页门户 获取用户数量统计")
	public JSONObject getPortalUserNum() throws Exception{
		LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>();
		Integer num = userService.count(wrapper);
		JSONObject obj = new JSONObject();
		obj.set("usernum", num);
		return obj;
	}

	@RequestMapping(value="org/importOrg", method=RequestMethod.POST, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "组织导入", httpMethod = "POST", notes = "组织导入")
	public CommonResult<String> importOrg(@ApiParam(name = "request", value = "上传的文件流") MultipartHttpServletRequest request) throws Exception{
		CommonResult res = orgService.importOrg(request);
		return res;
	}

	@RequestMapping(value="org/importDepart", method=RequestMethod.POST, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "组织导入", httpMethod = "POST", notes = "组织导入")
	public CommonResult<String> importDepart(@ApiParam(name = "request", value = "上传的文件流") MultipartHttpServletRequest request) throws Exception{
		CommonResult res = orgService.importDepart(request);
		return res;
	}


	@RequestMapping(value="org/export" ,method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
	@ApiOperation(value = "组织导出", httpMethod = "GET", notes = "组织导出")
	public void export(HttpServletResponse response, @ApiParam(name="ids",value="ids", required = true)@RequestParam String ids) throws 	Exception{
		orgService.export(response,ids);
	}


	@RequestMapping(value="getTreeAllOrgByExtOrgId", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获得树形组织(扩展组织id)", httpMethod = "GET", notes = "获得树形组织(扩展组织id)")
	public List<OrgTreeVo> getTreeAllOrgByExtOrgId(@ApiParam(name="orgId",value="组织id", required = true)@RequestParam(required=true) String orgId) throws Exception {
		IUser user = ContextUtil.getCurrentUser(); //获取当前用户
		Org curOrg = orgService.getById(orgId);
		String orgPath = curOrg.getPath();
		List<OrgTreeVo> list = orgService.getTreeAllOrgByExtOrgId(orgPath);
		list = BeanUtils.listToTree(list);
		return list;
	}

	@RequestMapping(value="getTreeCheckOrgByExtOrgId", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获得树形已选中组织(扩展组织id)", httpMethod = "GET", notes = "获得树形已选中组织(扩展组织id)")
	public List<Map<String,Object>> getTreeCheckOrgByExtOrgId(
			@ApiParam(name="orgId",value="组织id", required = true)@RequestParam(required=true) String orgId,
			@ApiParam(name="pcAppId",value="pc应用id", required = true)@RequestParam(required=false) String pcAppId,
			@ApiParam(name="mobileAppId",value="mobile应用id", required = true)@RequestParam(required=false) String mobileAppId) throws Exception {
		IUser user = ContextUtil.getCurrentUser(); //获取当前用户
		Org curOrg = orgService.getById(orgId);
		String orgPath = curOrg.getPath();
		String appType = "";
		String appId = "";
		if(StringUtil.isNotEmpty(pcAppId)) {
			appType = OrgAuthExt.AUTH_TYPE_PC_APP;
			appId = pcAppId;
		}
		if(StringUtil.isNotEmpty(mobileAppId)) {
			appType = OrgAuthExt.AUTH_TYPE_MOBILE_APP;
			appId = mobileAppId;
		}
		List<Map<String, Object>> result = orgService.getTreeCheckOrgByExtOrgId(orgPath, appType, appId);
		return result;
	}

	@RequestMapping(value="getOrgStatic", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取门户首页组织数据统计", httpMethod = "GET", notes = "获取门户首页组织数据统计")
	public CommonResult<?> getOrgStatic() throws Exception {
		return orgService.getOrgStatic();
	}


	@RequestMapping(value="getUserOrgAndDept", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户的部门和组织", httpMethod = "GET", notes = "获取用户的部门和组织")
	public CommonResult<?> getUserOrgAndDept(@ApiParam(name="userId",value="用户id", required = true)@RequestParam(required=true) String userId) throws Exception {
		return  new CommonResult<>(true,"获取成功",orgService.getUserOrgAndDept(userId));
	}
}
