From c007f0ca1785db093d48f4846cda82fe8e955765 Mon Sep 17 00:00:00 2001
From: kimi <kimi42345@gmail.com>
Date: 星期三, 27 五月 2020 09:59:29 +0800
Subject: [PATCH] merage

---
 src/main/java/com/highdatas/mdm/service/act/impl/IdentityServiceImpl.java |  163 +++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 143 insertions(+), 20 deletions(-)

diff --git a/src/main/java/com/highdatas/mdm/service/act/impl/IdentityServiceImpl.java b/src/main/java/com/highdatas/mdm/service/act/impl/IdentityServiceImpl.java
index aa878e3..e2376eb 100644
--- a/src/main/java/com/highdatas/mdm/service/act/impl/IdentityServiceImpl.java
+++ b/src/main/java/com/highdatas/mdm/service/act/impl/IdentityServiceImpl.java
@@ -1,8 +1,11 @@
 package com.highdatas.mdm.service.act.impl;
 
 import com.highdatas.mdm.entity.TUser;
+import com.highdatas.mdm.mapper.TableInfoMapper;
 import com.highdatas.mdm.pojo.CodeMsg;
 import com.highdatas.mdm.pojo.Result;
+import com.highdatas.mdm.util.RedisClient;
+import lombok.extern.slf4j.Slf4j;
 import org.activiti.engine.IdentityService;
 import org.activiti.engine.RepositoryService;
 import org.activiti.engine.RuntimeService;
@@ -14,11 +17,16 @@
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 @Service
+@Slf4j
 public class IdentityServiceImpl extends BaseServiceImpl implements com.highdatas.mdm.service.act.IdentityService{
 	@Autowired
 	IdentityService identityService;
@@ -26,13 +34,32 @@
 	RuntimeService runtimeService;
 	@Autowired
 	RepositoryService repositoryService;
+	@Autowired
+	TableInfoMapper tableInfoMapper;
+	@Autowired
+	RedisClient redisClient;
 
-
+	/**
+	 *
+	 * @description:  鍚姩娴佺▼
+	 * @param businessId 涓氬姟id
+	 * @param key 娴佺▼key
+	 * @return: 瀹炰緥id
+	 *
+	 */
     @Override
 	public String startProcess(String businessId, String key) {
 		return startProcess(businessId, key, null, null);
 	}
-
+	/**
+	 *
+	 * @description:  鍚姩娴佺▼
+	 * @param businessId 涓氬姟id
+	 * @param key 娴佺▼key
+	 * @param variables 棰濆鍙傛暟
+	 * @return: 瀹炰緥id
+	 *
+	 */
     @Override
 	public String startProcess(String businessId, String key, String workflowStall, HashMap<String, Object> variables) {
     	TUser user = getOnlineUser();
@@ -57,19 +84,26 @@
         	variables.put("workflowstall", workflowStallInt);
         	
         	identityService.setAuthenticatedUserId(user.getUserId());
-
+			Date startDate = new Date();
             //get max version processDefinition
             List<Model> modelList = repositoryService.createModelQuery().modelKey(key).orderByModelVersion().desc().list();
+			Date endDate = new Date();
+			log.info("process load model:" + (endDate.getTime() - startDate.getTime()) +"ms");
             ProcessDefinition processDefinition = null;
             
             for (Model model : modelList) {
 				if (processDefinition == null && model.getDeploymentId() != null) {
 					String deploymentId = model.getDeploymentId();
 					processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
+					break;
 				}
 			}
-
+			startDate = new Date();
+			log.info("process load processDef:" + (startDate.getTime() - endDate.getTime()) +"ms");
             processInstance = runtimeService.startProcessInstanceById(processDefinition.getId(), businessId, variables);
+
+			endDate = new Date();
+			log.info("process start process :" + (endDate.getTime() - startDate.getTime()) +"ms");
 
             String processInstanceId = processInstance.getId();
             //TODO db
@@ -86,8 +120,20 @@
     	}
 	}
 
+	/**
+	 *
+	 * @description:  娣诲姞宸ヤ綔娴佺敤鎴�
+	 * @param id 鐢ㄦ埛id
+	 * @return: 娣诲姞缁撴灉
+	 *
+	 */
 	@Override
+	@Transactional(rollbackFor = {RuntimeException.class, Error.class})
 	public Result addUser(String id) {
+		User preUser = identityService.createUserQuery().userId(id).singleResult();
+		if (preUser != null) {
+			return  Result.error(new CodeMsg(6002, "宸叉湁鐩稿悓id鐨勭敤鎴峰瓨鍦�"));
+		}
 		try {
 			User user = identityService.newUser(id);
 			identityService.saveUser(user);
@@ -99,8 +145,20 @@
 
 	}
 
+	/**
+	 *
+	 * @description:  娣诲姞宸ヤ綔娴佽鑹�
+	 * @param id 瑙掕壊id
+	 * @return: 娣诲姞缁撴灉
+	 *
+	 */
 	@Override
+	@Transactional(rollbackFor = {RuntimeException.class, Error.class})
 	public Result addRole(String id) {
+		Group preGroup = identityService.createGroupQuery().groupId(id).singleResult();
+		if (preGroup != null) {
+			return  Result.error(new CodeMsg(6002, "宸叉湁鐩稿悓id鐨勮鑹插瓨鍦�"));
+		}
 		try {
 			Group group = identityService.newGroup(id);
 			identityService.saveGroup(group);
@@ -111,18 +169,28 @@
 		}
 	}
 
+	/**
+	 *
+	 * @description:  娣诲姞宸ヤ綔娴佺敤鎴凤紝瑙掕壊鍏宠仈
+	 * @param roleId 瑙夊緱id
+	 * @param userId 鐢ㄦ埛id
+	 * @return: 娣诲姞缁撴灉
+	 *
+	 */
 	@Override
+	@Transactional(rollbackFor = {RuntimeException.class, Error.class})
 	public Result addUserRole(String roleId, String userId) {
     	try {
 			User user = identityService.createUserQuery().userId(userId).singleResult();
 			if (user == null) {
 				return Result.error(new CodeMsg(3001, "褰撳墠鐢ㄦ埛涓嶅瓨鍦�,璇峰厛鍒涘缓鐢ㄦ埛"));
 			}
-			Group group = identityService.createGroupQuery().groupId(roleId).singleResult();
-			if (user == null) {
-				return Result.error(new CodeMsg(3001, "褰撳墠瑙掕壊涓嶅瓨鍦�,璇峰厛瑙掕壊鐢ㄦ埛"));
-			}
-			identityService.createMembership(userId,roleId);
+
+            Map<String, String> memberShip = tableInfoMapper.selectActMemberShip(userId, roleId);
+			if (memberShip != null) {
+                return Result.success(null);
+            }
+            identityService.createMembership(userId,roleId);
 			return Result.success(null);
 		}
 		catch (Exception e) {
@@ -130,14 +198,31 @@
 			return Result.error(new CodeMsg(3002, e.getMessage()));
 		}
 	}
-
+	/**
+	 *
+	 * @description:  鍒犻櫎宸ヤ綔娴佺敤鎴�
+	 * @param id 鐢ㄦ埛id
+	 * @return: 鍒犻櫎缁撴灉
+	 *
+	 */
 	@Override
+	@Transactional(rollbackFor = {RuntimeException.class, Error.class})
 	public Result deleteUser(String id) {
 		try {
 			User user = identityService.createUserQuery().userId(id).singleResult();
 			if (user == null) {
 				return Result.error(new CodeMsg(3001, "褰撳墠鐢ㄦ埛涓嶅瓨鍦�"));
 			}
+			List<Map<String, Object>> maps = tableInfoMapper.selectActMemberShipByUser(id);
+			for (Map<String, Object> map : maps) {
+				Object group_id_ = map.get("GROUP_ID_");
+				Object user_id_ = map.get("USER_ID_");
+				if (user_id_ == null || group_id_ == null) {
+					continue;
+				}
+				identityService.deleteMembership(user_id_.toString(), group_id_.toString());
+			}
+			redisClient.delByCharacter(id);
 			identityService.deleteUser(id);
 			return Result.success(null);
 		}catch (Exception e){
@@ -146,13 +231,30 @@
 		}
 
 	}
-
+	/**
+	 *
+	 * @description:  鍒犻櫎宸ヤ綔娴佽鑹�
+	 * @param id 瑙掕壊id
+	 * @return: 鍒犻櫎缁撴灉
+	 *
+	 */
 	@Override
+	@Transactional(rollbackFor = {RuntimeException.class, Error.class})
 	public Result deleteRole(String id) {
 		try {
 			Group group = identityService.createGroupQuery().groupId(id).singleResult();
 			if (group == null) {
 				return Result.error(new CodeMsg(3001, "褰撳墠瑙掕壊涓嶅瓨鍦�"));
+			}
+			List<Map<String, Object>> maps = tableInfoMapper.selectActMemberShipByRole(id);
+			for (Map<String, Object> map : maps) {
+				Object group_id_ = map.get("GROUP_ID_");
+				Object user_id_ = map.get("USER_ID_");
+				if (user_id_ == null || group_id_ == null) {
+					continue;
+				}
+				identityService.deleteMembership(user_id_.toString(), group_id_.toString());
+				redisClient.delByCharacter(user_id_.toString());
 			}
 			identityService.deleteGroup(id);
 			return Result.success(null);
@@ -162,18 +264,24 @@
 		}
 	}
 
-
+	/**
+	 *
+	 * @description:  鍒犻櫎宸ヤ綔娴佺敤鎴� 瑙掕壊鍏宠仈
+	 * @param userId 鐢ㄦ埛id
+	 * @param roleId 瑙掕壊id
+	 * @return: 鍒犻櫎缁撴灉
+	 *
+	 */
 	@Override
+	@Transactional(rollbackFor = {RuntimeException.class, Error.class})
 	public Result deleteUserRole(String roleId, String userId) {
 		try {
-			User user = identityService.createUserQuery().userId(userId).singleResult();
-			if (user == null) {
-				return Result.error(new CodeMsg(3001, "褰撳墠鐢ㄦ埛涓嶅瓨鍦�,璇峰厛鍒涘缓鐢ㄦ埛"));
-			}
-			Group group = identityService.createGroupQuery().groupId(roleId).singleResult();
-			if (user == null) {
-				return Result.error(new CodeMsg(3001, "褰撳墠瑙掕壊涓嶅瓨鍦�,璇峰厛瑙掕壊鐢ㄦ埛"));
-			}
+
+            Map<String, String> memberShip = tableInfoMapper.selectActMemberShip(userId, roleId);
+            if (memberShip == null) {
+                return Result.success(null);
+            }
+			redisClient.delByCharacter(userId);
 			identityService.deleteMembership(userId,roleId);
 			return Result.success(null);
 		}
@@ -182,6 +290,21 @@
 			return Result.error(new CodeMsg(3002, e.getMessage()));
 		}
 	}
+	/**
+	 *
+	 * @description:  閫氳繃user鑾峰彇鍏宠仈鐨勮鑹�
+	 * @param userId 鐢ㄦ埛id
+	 * @return: 瑙掕壊id鍒楄〃
+	 *
+	 */
+	@Override
+	public List<String> getRoleByUser(String userId) {
+		List<Group> list = identityService.createGroupQuery().groupMember(userId).list();
+		if (list == null || list.isEmpty()) {
+			return null;
+		}
+		return  list.stream().map(group -> group.getId()).collect(Collectors.toList());
+	}
 
 
 }

--
Gitblit v1.8.0