OrgScript.java 17.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
package com.hotent.runtime.script;

import java.io.IOException;
import java.util.*;

import javax.annotation.Resource;

import cn.hutool.core.util.BooleanUtil;
import com.hotent.uc.api.model.Group;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hotent.base.feign.UCFeignService;
import com.hotent.base.groovy.IUserScript;
import com.hotent.base.util.AppUtil;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.JsonUtil;
import com.hotent.base.util.StringUtil;
import com.hotent.bpm.api.cmd.ActionCmd;
import com.hotent.bpm.api.constant.BpmConstants;
import com.hotent.bpm.api.context.ContextThreadUtil;
import com.hotent.bpm.api.model.identity.BpmIdentity;
import com.hotent.bpm.model.identity.DefaultBpmIdentity;
import com.hotent.uc.api.impl.model.Org;
import com.hotent.uc.api.impl.model.UserFacade;
import com.hotent.uc.api.impl.util.ContextUtil;
import com.hotent.uc.api.model.IGroup;
import com.hotent.uc.api.model.IUser;
import com.hotent.uc.api.model.IdentityType;
import com.hotent.uc.api.service.IUserService;



@Component
public class OrgScript implements IUserScript  {
	
	@Resource 
	UCFeignService ucFeignService;
	@Resource
	IUserService userService;

	
	
	
	/**
	 * 将用户列表转换成BpmIdentity列表
	 * @param list
	 * @return
	 */
	private Set<BpmIdentity> convertUserList(List<IUser> list){
		Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
		for (IUser iUser : list) {
			if(BeanUtils.isNotEmpty(iUser) && iUser.getStatus()==1){
				DefaultBpmIdentity bpmIdentity = new DefaultBpmIdentity();
				bpmIdentity.setId(iUser.getUserId());
				bpmIdentity.setName(iUser.getFullname());
				bpmIdentity.setType(BpmIdentity.TYPE_USER);
				identitys.add(bpmIdentity);
			}
		}
		return identitys;
	}
	
	/**
	 * json转iuser
	 * @param objs
	 * @return
	 */
	private List<IUser> usersObjConvertToIusers(List<ObjectNode> objs){
		List<IUser> list = new ArrayList<IUser>();
		if(BeanUtils.isNotEmpty(objs)){
			for (ObjectNode obj : objs) {
				UserFacade user = new UserFacade();
				user.setId(obj.get("id").asText());
				user.setAccount(obj.get("account").asText());
				user.setFullname(obj.get("fullname").asText());
				user.setUserId(obj.get("id").asText());
				user.setStatus(obj.get("status").asInt());
				list.add(user);
			}
		}
		return list;
	}
	
	private IGroup getUserMainGroup(String userId,String demCode){
		UCFeignService service = AppUtil.getBean(UCFeignService.class);
		ObjectNode orgObj = service.getMainGroup(userId,demCode);
		try {
			if(BeanUtils.isNotEmpty(orgObj)){
				boolean isParent = orgObj.get("isParent").asBoolean();
				orgObj.put("isIsParent", isParent?1:0);
				orgObj.remove("isParent");
				IGroup org = JsonUtil.toBean(orgObj, Org.class);
				return org;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 获取当前用户的上级部门的负责人
	 * @param isMainParam 是否主负责人
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	public Set<BpmIdentity> getChargesByPOrg(Object isMainParam,String demCode) throws JsonParseException, JsonMappingException, IOException{
        Boolean isMain = BooleanUtil.toBooleanObject(String.valueOf(isMainParam));
		Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
		IGroup group = getUserMainGroup(ContextUtil.getCurrentUserId(),demCode);
		if(BeanUtils.isNotEmpty(group)){
			List<ObjectNode> users = ucFeignService.getChargesByOrgId(group.getParentId(),isMain);
			if(BeanUtils.isNotEmpty(users)){
				List<IUser> list = usersObjConvertToIusers(users);
				identitys = convertUserList(list);
			}
		}
		return identitys;
	}
	
	/**
	 * 获取发起人的上级部门的(主)负责人
	 * @param isMainParam 是否主负责人
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	public Set<BpmIdentity> getChargesByStartPOrg(Object isMainParam,String demCode) throws JsonParseException, JsonMappingException, IOException{
        Boolean isMain = BooleanUtil.toBooleanObject(String.valueOf(isMainParam));
		Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
		ActionCmd taskCmd = ContextThreadUtil.getActionCmd();
		String userId =  (String) taskCmd.getVariables().get(BpmConstants.START_USER);
		IGroup group = getUserMainGroup(userId,demCode);
		if(BeanUtils.isNotEmpty(group)){
			List<ObjectNode> users = ucFeignService.getChargesByOrgId(group.getParentId(),isMain);
			if(BeanUtils.isNotEmpty(users)){//
				List<IUser> list = usersObjConvertToIusers(users);
				identitys = convertUserList(list);
			}
		}
		return identitys;
	}
	
	/**
	 * 判断发起人是否包含某职务
	 * @param orgReldefCode
	 * @return
	 */
	public boolean isContainsJob(String orgReldefCode){
		if(StringUtil.isEmpty(orgReldefCode)){
			return false;
		}
		ActionCmd taskCmd = ContextThreadUtil.getActionCmd();
		String userId =  (String) taskCmd.getVariables().get(BpmConstants.START_USER);
		ArrayNode reldefs = (ArrayNode) ucFeignService.getJobsByUserId(userId);
		for (JsonNode jsonNode : reldefs) {
			ObjectNode job = (ObjectNode) jsonNode;
			if(orgReldefCode.equals(job.get("code").asText())){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 判断当前用户主部门是否有上级 
	 * @param orgReldefCode
	 * @return
	 */
	public boolean isSupOrgByCurrMain(int level){
        ActionCmd taskCmd = ContextThreadUtil.getActionCmd();
        String userId =  (String) taskCmd.getVariables().get(BpmConstants.START_USER);
        return ucFeignService.isSupOrgByCurrMain(userId,null,level);
    }
	
	public boolean isSupOrgByCurrMain(String demCode) throws IOException{
       IGroup group = getUserMainGroup(ContextUtil.getCurrentUserId(),demCode);
       if(StringUtil.isNotZeroEmpty(group.getParentId())){
           return true;
       }
       return false;
    }
	
	
	/**
	 * 获取当前用户的部门的直接(主)负责人(负责人中可能包含自己)
	 * @param isMainParam 是否主负责人
	 * @return
	 * @throws IOException 
	 */
	public Set<BpmIdentity> getChargesByOrg(Object isMainParam,String demCode) throws IOException{
        Boolean isMain = BooleanUtil.toBooleanObject(String.valueOf(isMainParam));
		Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
		IGroup group = getUserMainGroup(ContextUtil.getCurrentUserId(),demCode);
		if(BeanUtils.isNotEmpty(group)){
			List<ObjectNode> users = ucFeignService.getChargesByOrgId(group.getGroupId(),isMain);
			if(BeanUtils.isNotEmpty(users)){
				List<IUser> list = usersObjConvertToIusers(users);
				identitys = convertUserList(list);
			}
		}
		return identitys;
	}
	
	/**
	 * 获取发起人的部门的直接(主)负责人(负责人中可能包含自己)
	 * @param isMainParam 是否主负责人
	 * @return
	 */
	public Set<BpmIdentity> getChargesByStartOrg(Object isMainParam,String demCode){
        Boolean isMain = BooleanUtil.toBooleanObject(String.valueOf(isMainParam));
		Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
		ActionCmd taskCmd = ContextThreadUtil.getActionCmd();
		String userId =  (String) taskCmd.getVariables().get(BpmConstants.START_USER);
		ObjectNode org =  ucFeignService.getMainGroup(userId,demCode); 
		if(BeanUtils.isNotEmpty(org)){
			List<ObjectNode> users = ucFeignService.getChargesByOrgId(org.get("groupId").asText(),isMain);
			if(BeanUtils.isNotEmpty(users)){
				List<IUser> list = usersObjConvertToIusers(users);
				identitys = convertUserList(list);
			}
		}
		return identitys;
	}
	
	
	
	/**
	 * 根据组织别名获取组织负责人
	 * @param isMain 是否主负责人
	 * @return
	 */
	public Set<BpmIdentity> getChargesByOrgCode(String code,Object isMainParam){
        Boolean isMain = BooleanUtil.toBooleanObject(String.valueOf(isMainParam));
		Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
		ObjectNode org =  ucFeignService.getOrgByIdOrCode(code);
		if(BeanUtils.isNotEmpty(org)){
			List<ObjectNode> users = ucFeignService.getChargesByOrgId(org.get("groupId").asText(),isMain);
			if(BeanUtils.isNotEmpty(users)){
				List<IUser> list = usersObjConvertToIusers(users);
				identitys = convertUserList(list);
			}
		}
		return identitys;
	}
	
	/**
	 * 根据组织编码和职务编码获取人员
	 * @param orgCode 组织编码
	 * @param redDefCode 职务编码
	 * @return
	 */
	public Set<BpmIdentity> getByOrgRelDefCode(String orgCode,String redDefCode){
		Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
		if(StringUtil.isNotEmpty(orgCode)&&StringUtil.isNotEmpty(redDefCode)){
			String[] orgCodeArray = orgCode.split(",");
			List<ObjectNode> users = new ArrayList<ObjectNode>();
			for (String ocode : orgCodeArray) {
				try {
					List<ObjectNode> userList = ucFeignService.getByOrgRelDefCode(redDefCode, ocode);
					if(BeanUtils.isNotEmpty(userList)) {
						users.addAll(userList);
					}
				} catch (Exception e) {}
			}
			if(BeanUtils.isNotEmpty(users)){
				for (ObjectNode obj : users) {
					BpmIdentity bpmIdentity = new DefaultBpmIdentity();
					bpmIdentity.setType(IdentityType.USER);
					bpmIdentity.setId(obj.get("id").asText());
					bpmIdentity.setName(obj.get("name").asText());
					identitys.add(bpmIdentity);
				}
			}
		}
		return identitys;
	}
	
	/**
	 * 根据组织编码和岗位编码获取人员
	 * @param orgCode
	 * @param relCode
	 * @return
	 */
	public Set<BpmIdentity> getByOrgRelCode(String orgCode,String relCode){
		Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
		if(StringUtil.isNotEmpty(orgCode)&&StringUtil.isNotEmpty(orgCode)){
			List<ObjectNode> orgUsers = new ArrayList<ObjectNode>();
			// TODO
			/*StringBuilder sql = new StringBuilder();
			sql.append(" select * from sys_org_user a LEFT JOIN sys_org b on a.org_id_= b.id_ LEFT JOIN sys_org_rel c on a.rel_id_=c.id_ ");
			sql.append(" where b.code_='"+orgCode+"' and c.rel_code_='"+relCode+"' ");*/
			try {
				orgUsers = ucFeignService.getByOrgRelCode( orgCode, relCode);
			} catch (Exception e) {}
			if(BeanUtils.isNotEmpty(orgUsers)){
				List<IUser> list = new ArrayList<IUser>();
				for (ObjectNode obj : orgUsers) {
					IUser iUser = userService.getUserById(obj.get("USER_ID_").asText());
					list.add(iUser);
				}
				identitys = convertUserList(list);
			}
		}
		return identitys;
	}
	
	/**
	 * 获取当前用户的上级部门的负责人
	 * @param isMain 是否主负责人
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	public Set<BpmIdentity> getMasterFromUnder(String demId) throws JsonParseException, JsonMappingException, IOException{
		return getMasterFromUnderByUser(ContextUtil.getCurrentUserId(), demId);
	}
	
	/**
	 * 通过组织下属管理获取发起人在某个组织下的上级
	 * @param orgId 
	 * @param demId
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public Set<BpmIdentity> getMasterFromUnderByStart(String demId) throws JsonParseException, JsonMappingException, IOException{
		Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
		ActionCmd taskCmd = ContextThreadUtil.getActionCmd();
		if(BeanUtils.isNotEmpty(taskCmd)){
			String userId =  (String) taskCmd.getVariables().get(BpmConstants.START_USER);
			identitys =  getMasterFromUnderByUser(userId, demId);
		}
		return identitys;
	}
	
	/**
	 * 通过用户id从组织下属管理中获取上级
	 * @param userId
	 * @param orgId
	 * @param demId
	 * @return
	 */
	public Set<BpmIdentity> getMasterFromUnderByUser(String userId,String demId){
		Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
		ArrayNode arry = ucFeignService.getSuperFromUnder(userId, demId);
		if(BeanUtils.isNotEmpty(arry)){
			List<ObjectNode> list = new ArrayList<ObjectNode>();
			for (JsonNode jsonNode : arry) {
				list.add((ObjectNode)jsonNode);
			}
			List<IUser> users = usersObjConvertToIusers(list);
			identitys = convertUserList(users);
		}
		return identitys;
	}
	
	/**
	   * 获取申请人指定级别组织的负责人 
	 * @return
	 */
	public Set<BpmIdentity> getCustomLevelCharge(String level, Object isMainChargeParam){
        Boolean isMainCharge = BooleanUtil.toBooleanObject(String.valueOf(isMainChargeParam));
		Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
		ActionCmd taskCmd = ContextThreadUtil.getActionCmd();
		String userId = ContextUtil.getCurrentUserId();
		if(BeanUtils.isNotEmpty(taskCmd)){
			userId =  (String) taskCmd.getVariables().get(BpmConstants.START_USER);
		}
		
		ArrayNode arry = ucFeignService.getCustomLevelCharge(userId, level, isMainCharge);
		if(BeanUtils.isNotEmpty(arry)){
			List<ObjectNode> list = new ArrayList<ObjectNode>();
			for (JsonNode jsonNode : arry) {
				list.add((ObjectNode)jsonNode);
			}
			List<IUser> users = usersObjConvertToIusers(list);
			identitys = convertUserList(users);
		}
		
		return identitys;
	}
	
	/**
	   * 获取申请人指定级别的组织的指定岗位人员
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	public Set<BpmIdentity> getCustomLevelPost(String level, String postCode) throws JsonParseException, JsonMappingException, IOException{
		Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
		ActionCmd taskCmd = ContextThreadUtil.getActionCmd();
		String userId = ContextUtil.getCurrentUserId();
		if(BeanUtils.isNotEmpty(taskCmd)){
			userId =  (String) taskCmd.getVariables().get(BpmConstants.START_USER);
		}
		
		Set<ObjectNode> arry = ucFeignService.getCustomLevelPost(userId, level, postCode);
		if(BeanUtils.isNotEmpty(arry)){
			List<IUser> users = new ArrayList<IUser>();
			for (JsonNode jsonNode : arry) {
				UserFacade user = new UserFacade();
				user.setId(jsonNode.get("id").asText());
				user.setFullname(jsonNode.get("name").asText());
				user.setStatus(1);
				users.add(user);
			}
			identitys = convertUserList(users);
		}
		
		return identitys;
	}
	
	/**
	   * 获取申请人指定级别的组织的指定职务人员
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	public Set<BpmIdentity> getCustomLevelJob(String level, String jobCode) throws JsonParseException, JsonMappingException, IOException{
		Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
		ActionCmd taskCmd = ContextThreadUtil.getActionCmd();
		String userId = ContextUtil.getCurrentUserId();
		if(BeanUtils.isNotEmpty(taskCmd)){
			userId =  (String) taskCmd.getVariables().get(BpmConstants.START_USER);
		}
		
		Set<ObjectNode> arry = ucFeignService.getCustomLevelJob(userId, level, jobCode);
		if(BeanUtils.isNotEmpty(arry)){
			List<IUser> users = new ArrayList<IUser>();
			for (JsonNode jsonNode : arry) {
				UserFacade user = new UserFacade();
				user.setId(jsonNode.get("id").asText());
				user.setFullname(jsonNode.get("name").asText());
				user.setStatus(1);
				users.add(user);
			}
			identitys = convertUserList(users);
		}
		
		return identitys;
	}
	
	/**
	   * 获取申请人组织的指定扩展参数值
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	public String getStartOrgParam(String param) throws JsonParseException, JsonMappingException, IOException{
		ActionCmd taskCmd = ContextThreadUtil.getActionCmd();
		String userId = ContextUtil.getCurrentUserId();
		if(BeanUtils.isNotEmpty(taskCmd)){
			userId =  (String) taskCmd.getVariables().get(BpmConstants.START_USER);
		}
		
		String value  = ucFeignService.getStartOrgParam(userId,param);
		return value;
	}

	/**
	 * 根据级别获取当前用户组织
	 * */
	public IGroup getParentMainOrg(String grade) throws IOException {
		UCFeignService service = AppUtil.getBean(UCFeignService.class);
		IGroup org=new Group();
		Map<String,ObjectNode> map = service.getParentMainOrg(ContextUtil.getCurrentUserId());
		if(map.containsKey(grade)){
			org = JsonUtil.toBean(JsonUtil.toJson(map.get(grade)), Org.class);
		}
		return org;
	}

	public String getParamMainOrgId(String grade) throws IOException{
		if(StringUtils.isEmpty(grade)){
			return Strings.EMPTY;
		}
		String groupId = getParentMainOrg(grade).getGroupId();
		return StringUtils.isEmpty(groupId) ? Strings.EMPTY : groupId;
	}

	public String getParamKey(String orgId,String alias) throws IOException {
		if(StringUtils.isEmpty(orgId) || StringUtils.isEmpty(alias)){
			return Strings.EMPTY;
		}
		String result = ucFeignService.getParamKey(orgId,alias);
		return StringUtils.isEmpty(result) ? Strings.EMPTY : result;
		//return ucFeignService.hasOrgParamKey(grade,ContextUtil.getCurrentUserId(),alias);
	}
	public boolean isOrgHasParamKey(String orgId,String alias) throws Exception{
		if(StringUtils.isEmpty(orgId) || StringUtils.isEmpty(alias)){
			return false;
		}
		return ucFeignService.isOrgHasParamKey(orgId,alias);
	}
	public boolean hasParamKey(String grade,String alias) throws IOException {
		if(StringUtils.isEmpty(grade) || StringUtils.isEmpty(alias)){
			return false;
		}
		return ucFeignService.hasOrgParamKey(grade,ContextUtil.getCurrentUserId(),alias);
	}
}