From 33d8416c5a16fee7464df9774678bc60e47ff956 Mon Sep 17 00:00:00 2001
From: 田源 <lastanimals@163.com>
Date: 星期二, 19 三月 2024 09:53:25 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 Source/UBCS/ubcs-service/ubcs-system/src/main/java/com/vci/ubcs/system/service/impl/ClassifyAuthServiceImpl.java |  226 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 208 insertions(+), 18 deletions(-)

diff --git a/Source/UBCS/ubcs-service/ubcs-system/src/main/java/com/vci/ubcs/system/service/impl/ClassifyAuthServiceImpl.java b/Source/UBCS/ubcs-service/ubcs-system/src/main/java/com/vci/ubcs/system/service/impl/ClassifyAuthServiceImpl.java
index e0eb813..40166e5 100644
--- a/Source/UBCS/ubcs-service/ubcs-system/src/main/java/com/vci/ubcs/system/service/impl/ClassifyAuthServiceImpl.java
+++ b/Source/UBCS/ubcs-service/ubcs-system/src/main/java/com/vci/ubcs/system/service/impl/ClassifyAuthServiceImpl.java
@@ -1,32 +1,34 @@
 package com.vci.ubcs.system.service.impl;
 
-import cn.hutool.db.ds.pooled.ConnectionWraper;
-import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.vci.ubcs.code.feign.ICodeClassifyClient;
+import com.vci.ubcs.starter.web.util.VciBaseUtil;
+import com.vci.ubcs.system.dto.ClassifyAuthDTO;
 import com.vci.ubcs.system.entity.ClassifyAuth;
+import com.vci.ubcs.system.entity.Menu;
 import com.vci.ubcs.system.mapper.ClassifyAuthMapper;
 import com.vci.ubcs.system.service.IClassifyAuthService;
+import com.vci.ubcs.system.service.IMenuService;
+import com.vci.ubcs.system.service.IRoleService;
 import com.vci.ubcs.system.vo.ClassifyAuthVO;
 import com.vci.ubcs.system.wrapper.ClassifyAuthWrapper;
 import lombok.AllArgsConstructor;
-import lombok.RequiredArgsConstructor;
-import org.redisson.api.condition.Conditions;
 import org.springblade.core.log.exception.ServiceException;
-import org.springblade.core.mp.support.Condition;
+import org.springblade.core.secure.utils.AuthUtil;
 import org.springblade.core.tool.api.R;
 import org.springblade.core.tool.utils.Func;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.ArrayList;
-import java.util.List;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
- * 鍒嗙被鎺堟潈
+ * 鍒嗙被鎺堟潈,鏁版嵁鎺堟潈
  * @author ludc
  * @date 2023/12/25 15:35
  */
@@ -36,29 +38,66 @@
 
 	private final ClassifyAuthMapper classifyAuthMapper;
 
+	private final IMenuService menuService;
+
+	private final ICodeClassifyClient codeClassifyClient;
+
+	private final IRoleService roleService;
+
+	/**
+	 * 涓夊憳绠$悊瑙掕壊缂栧彿閰嶇疆锛氬垎鍒负绯荤粺绠$悊鍛橈紝瀹夊叏绠$悊鍛橈紝瀹¤绠$悊鍛橀『搴�
+	 */
+	@Value("#{'${ssa.ssa-names}'.split(',')}")
+	private List<String> SSANAMES;
+
 	/**
 	 * 鍒嗙被鎺堟潈淇濆瓨鎺ュ彛
-	 * @param classifyAuthList
+	 * @param classifyAuthListDTO
 	 * @return
 	 */
 	@Override
 	@Transactional(rollbackFor = Exception.class)
-	public R submit(List<ClassifyAuth> classifyAuthList) {
-		if(classifyAuthList.isEmpty()){
+	public R submit(ClassifyAuthDTO classifyAuthListDTO) throws ServiceException{
+		// 鏄竻绌烘巿鏉冨垪琛�
+		if(classifyAuthListDTO.getIsCLear()){
+			if(Func.isEmpty(classifyAuthListDTO.getClassifyId())){
+				return R.fail("娓呯┖鎺堟潈鍒楄〃鏃讹紝鏈幏鍙栧埌鍒嗙被id");
+			}
+			this.classifyAuthMapper.delete(
+				Wrappers.<ClassifyAuth>update()
+				.lambda().eq(ClassifyAuth::getClassifyId, classifyAuthListDTO.getClassifyId())
+				.eq(ClassifyAuth::getAuthType,classifyAuthListDTO.getAuthType())
+			);
+			return R.success("鎺堟潈鍒楄〃娓呯┖鎴愬姛");
+		}
+
+		if(!classifyAuthListDTO.getIsCLear() && classifyAuthListDTO.getClassifyAuthList().isEmpty()){
 			R.fail("鎺堟潈鍒楄〃涓嶈兘涓虹┖锛�");
 		}
+		// 鍒ら噸锛屾煡鐪嬫槸鍚﹀瓨鍦ㄥ悓涓�涓猚lassid涓嬮厤缃簡鐩稿悓鐨勮鑹�
+		Map<String, Long> roleidCounts = classifyAuthListDTO.getClassifyAuthList().stream()
+			.collect(Collectors.groupingBy(ClassifyAuth::getRoleId, Collectors.counting()));
+
+		// 妫�鏌ユ槸鍚︽湁roleid鍑虹幇娆℃暟澶т簬1鐨勬儏鍐�
+		boolean hasDuplicateRoleid = roleidCounts.values().stream()
+			.anyMatch(count -> count > 1);
+		if(hasDuplicateRoleid){
+			R.fail("瑙掕壊鍜屽垎绫诲凡缁忓瓨鍦紝璇烽噸鏂伴厤缃紒");
+		}
+
 		// 濡傛灉浼犺繃鏉ョ殑闆嗗悎涓鍒嗙被id涓嬪垹闄や簡閮ㄥ垎瑙掕壊鐨勬巿鏉冿紝灏遍渶瑕佸皢璇ュ簱涓璫lassifyId涓嬩笉瀛樺湪鐨勬暟鎹垹鎺�
-		List<String> roleIds = classifyAuthList.stream().map(ClassifyAuth::getRoleId).collect(Collectors.toList());
+		List<String> roleIds = classifyAuthListDTO.getClassifyAuthList().stream().map(ClassifyAuth::getRoleId).collect(Collectors.toList());
 		// 鍒犻櫎
 		LambdaUpdateWrapper<ClassifyAuth> updateWrapper = Wrappers.<ClassifyAuth>update()
-			.lambda().eq(ClassifyAuth::getClassifyId, classifyAuthList.get(0).getClassifyId())
+			.lambda().eq(ClassifyAuth::getClassifyId, classifyAuthListDTO.getClassifyAuthList().get(0).getClassifyId())
+			.eq(ClassifyAuth::getAuthType,classifyAuthListDTO.getAuthType())
 			.notIn(ClassifyAuth::getRoleId, roleIds);
 		try {
 			this.classifyAuthMapper.delete(updateWrapper);
 		}catch (Exception e){
 			throw new ServiceException("鍒嗙被鎺堟潈杩囩▼涓嚭鐜伴敊璇紝閿欒鍘熷洜锛�"+e.getMessage());
 		}
-		return R.status(saveOrUpdateBatch(classifyAuthList));
+		return R.status(saveOrUpdateBatch(classifyAuthListDTO.getClassifyAuthList()));
 	}
 
 	/**
@@ -72,12 +111,163 @@
 			throw new ServiceException("缂哄皯蹇呬紶鍙傛暟鍒嗙被id");
 		}
 		LambdaQueryWrapper<ClassifyAuth> wrapper = Wrappers.<ClassifyAuth>query()
-			.lambda().eq(ClassifyAuth::getClassifyId,classifyAuthVO.getClassifyId());
+			.lambda().eq(ClassifyAuth::getClassifyId,classifyAuthVO.getClassifyId())
+			.eq(ClassifyAuth::getAuthType,classifyAuthVO.getAuthType());
 		List<ClassifyAuth> classifyAuths = this.classifyAuthMapper.selectList(wrapper);
-		if(!classifyAuths.isEmpty()){
-			return ClassifyAuthWrapper.build().listVO(classifyAuths);
+		if(classifyAuths.isEmpty()){
+			return new ArrayList<ClassifyAuthVO>();
 		}
-		return new ArrayList<ClassifyAuthVO>();
+		return ClassifyAuthWrapper.build().listVO(classifyAuths);
+	}
+
+	/**
+	 * 鏌ヨ璇ュ垎绫讳笅锛屽綋鍓嶇櫥褰曠殑瑙掕壊鏈夊摢浜涙寜閽潈闄�
+	 * @param classifyId
+	 * @param menuCode
+	 * @param authType
+	 * @return
+	 */
+	public Map<String,Boolean> getAuthButtonList(String classifyId,String menuCode,String authType){
+		List<Menu> menuList = this.getButtonList(classifyId, menuCode, authType);
+		if(menuList.isEmpty()){
+			return new HashMap<>();
+		}
+		Map<String, Boolean> buttonMaps = menuList.stream()
+			.collect(Collectors.toMap(Menu::getCode, menu -> true));
+		return buttonMaps;
+	}
+
+	/**
+	 * 鏌ヨ璇ヤ富鏁版嵁涓嬶紝褰撳墠鐧诲綍鐨勮鑹叉湁鍝簺鎸夐挳鑿滃崟鏉冮檺
+	 * @param classifyId
+	 * @param menuCode
+	 * @param authType
+	 * @return
+	 */
+	public List<Menu> getAuthMenuButtonList(String classifyId,String menuCode,String authType){
+		List<Menu> buttonList = getButtonList(classifyId, menuCode, authType);
+		return buttonList;
+	}
+
+	private List<Menu> getButtonList(String classifyId,String menuCode,String authType){
+		if(Func.isBlank(classifyId)){
+			throw new ServiceException("蹇呬紶鍙傛暟鍒嗙被oid涓嶈兘涓虹┖锛�");
+		}
+		//鏌ヨ鍒嗙被鑺傜偣鐨勬墍鏈夌埗绾ц妭鐐�
+		R<List<String>> listR = codeClassifyClient.selectAllParentOid(classifyId);
+		if (!listR.isSuccess() && !listR.getData().isEmpty()) {
+			throw new ServiceException("鑾峰彇鍒嗙被淇℃伅澶辫触锛�");
+		}
+		// 杩斿洖鐨勫垎绫籵id鏄綋鍓嶈妭鐐逛负绗竴涓紝鍚庨潰渚濇鏄粬鐨勪笂灞傝妭鐐�
+		List<String> classifyOidList = listR.getData();
+		final List<String> roleIds = Func.toStrList(",",AuthUtil.getUser().getRoleId());
+		// 鍏堟煡璇㈡寜閽甶d鍒楄〃
+		LambdaQueryWrapper<ClassifyAuth> wrapper = Wrappers.<ClassifyAuth>query()
+			.lambda().eq(ClassifyAuth::getClassifyId, classifyId)
+			.eq(ClassifyAuth::getAuthType,authType)
+			.in(ClassifyAuth::getRoleId, roleIds);
+		List<ClassifyAuth> classifyAuths = this.classifyAuthMapper.selectList(wrapper);
+		//濡傛灉褰撳墠鍒嗙被娌℃湁鎵惧埌鎺堟潈閰嶇疆锛屽氨渚濇浠庡綋鍓嶈妭鐐瑰線涓婂眰鑺傜偣鎵炬巿鏉冮厤缃紝鎵惧埌浜嗗氨鍋滄锛屾病鎵惧埌灏变竴鐩存壘鍒版渶鍚�
+		if(classifyAuths.isEmpty()){
+			// 涓嬫爣浠�1寮�濮嬪洜涓哄綋鍓嶈妭鐐�0宸茬粡鏌ヨ杩�
+			for (int i = 1; i < classifyOidList.size(); i++) {
+				classifyAuths = this.classifyAuthMapper.selectList(
+					Wrappers.<ClassifyAuth>query()
+						.lambda().eq(ClassifyAuth::getClassifyId, classifyOidList.get(i))
+						.eq(ClassifyAuth::getAuthType,authType)
+						.in(ClassifyAuth::getRoleId, roleIds)
+				);
+				//鍙褰撳墠鑺傜偣鐨勪笂灞傝妭鐐逛腑鎵惧埌浜嗗垎绫绘巿鏉冧俊鎭氨涓嶅啀缁х画寰�涓婃壘浜�
+				if(!classifyAuths.isEmpty()){
+					break;
+				}
+			}
+		}
+		//鍑虹幇浜嗗鏉℃暟鎹�
+		if(classifyAuths.size()>1){
+			// 鏍¢獙鏄惁瀛樺湪閿欒鏁版嵁锛屽悓涓�涓鑹插拰鍚屼竴涓垎绫籭d瀛樺湪澶氭潯鎺堟潈璁板綍
+			List<ClassifyAuth> finalClassifyAuths = classifyAuths;
+			boolean hasDuplicate = classifyAuths.stream()
+				.anyMatch(auth1 -> finalClassifyAuths.stream()
+					.filter(auth2 -> auth1 != auth2)
+					.anyMatch(auth2 -> auth1.getRoleId().equals(auth2.getRoleId()) && auth1.getClassifyId().equals(auth2.getClassifyId())));
+			if (hasDuplicate) {
+				throw new ServiceException("瑙掕壊鍜屽垎绫婚厤缃瓨鍦ㄥ鏉¤褰曪紝璇疯仈绯荤鐞嗕汉鍛樻竻鐞嗛敊璇厤缃紒");
+			}
+		}
+		// 鏄惁涓鸿秴绠�
+		Boolean isAdmin = VciBaseUtil.checkAdminTenant();
+		// 鏈厤缃寜閽潈闄�
+		if(!isAdmin && (classifyAuths.isEmpty() || Func.isBlank(classifyAuths.get(0).getButtonIds()))){
+			return new ArrayList<>();
+		}
+		List<String> ids = new ArrayList<>();
+		// 濡傛灉涓嶆槸瓒呯鐢ㄦ埛
+		if(!isAdmin){
+			String concatenatedButtonIds = classifyAuths.stream()
+				.map(ClassifyAuth::getButtonIds) // 鑾峰彇姣忎釜classifyAuths瀵硅薄鐨刡uttonIds
+				.collect(Collectors.joining(",")); // 鐢ㄩ�楀彿鍒嗛殧鎷兼帴鎴愪竴涓瓧绗︿覆
+			ids.addAll(Arrays.asList(concatenatedButtonIds.split(",")));
+		}
+		return menuService.getMenuListByCode(ids,menuCode,roleIds);
+	}
+
+	/**
+	 * 鏍规嵁瑙掕壊id鏌ョ湅鏈夊摢浜涘垎绫诲叿澶囨煡鐪嬫潈闄�
+	 * @param roleIds
+	 * @return
+	 */
+	@Override
+	public List<String> getViewClassByRoleIds(List<String> roleIds,String authType,String buttonCode,String menuCode) {
+		if(roleIds.isEmpty()){
+			return new ArrayList<>();
+		}
+		return this.classifyAuthMapper.getViewClassByRoleIds(roleIds, authType,buttonCode,menuCode);
+	}
+
+	/**
+	 * 鏍规嵁瑙掕壊鍚嶇О鍒嗙被id鎺堜簣榛樿鐨勫垎绫绘潈闄愬拰涓绘暟鎹闂潈闄�
+	 * @param classifyAuthDTO
+	 * @return
+	 */
+	@Transactional(rollbackFor = Exception.class)
+	public R saveAddClassifyDefaultAuth(ClassifyAuthDTO classifyAuthDTO) throws ServiceException{
+		if (Func.isEmpty(classifyAuthDTO.getClassifyId())) {
+			return R.fail("鏈幏鍙栧埌鍒嗙被id");
+		}
+		// 閰嶇疆鐨勭郴缁熺鐞嗗憳鍚嶇О
+		String sysAdmin = SSANAMES.get(0);
+		List<ClassifyAuth> classifyAuths = new ArrayList<>();
+		//鍏堥粯璁ょ敓鎴愮郴缁熺鐞嗗憳鐨勬巿鏉冩暟鎹�
+		ClassifyAuth classifyAuth = new ClassifyAuth();
+		classifyAuth.setClassifyId(classifyAuthDTO.getClassifyId());
+		classifyAuth.setAuthType("classify_auth");
+		String roleIds = roleService.getRoleIds(AuthUtil.getTenantId(), sysAdmin);
+		if(Func.isBlank(roleIds)){
+			return R.fail(AuthUtil.getTenantId()+"绉熸埛涓嬫湭鎵惧埌锛屽悕涓�"+sysAdmin+"绯荤粺绠$悊鍛樿鑹诧紒");
+		}
+		classifyAuth.setRoleId(roleIds);
+		List<Menu> classifyTreeMenus = menuService.getButtonsByRoleId(roleIds, "classifyTree");
+		if(!classifyTreeMenus.isEmpty()){
+			String menuIds = classifyTreeMenus.stream()
+				.map(menu -> String.valueOf(menu.getId()))
+				.collect(Collectors.joining(","));
+			classifyAuth.setButtonIds(menuIds);
+			classifyAuths.add(classifyAuth);
+		}
+		/*ClassifyAuth dataAuth = new ClassifyAuth();
+		dataAuth.setAuthType("data_auth");
+		dataAuth.setClassifyId(classifyAuthDTO.getClassifyId());
+		dataAuth.setRoleId(roleIds);
+		List<Menu> masterDatas = menuService.getButtonsByRoleId(roleIds, classifyAuthDTO.getClassId());
+		if(!masterDatas.isEmpty()){
+			String menuIds = masterDatas.stream()
+				.map(menu -> String.valueOf(menu.getId()))
+				.collect(Collectors.joining(","));
+			dataAuth.setButtonIds(menuIds);
+			classifyAuths.add(dataAuth);
+		}*/
+		return R.status(this.saveBatch(classifyAuths));
 	}
 
 }

--
Gitblit v1.9.3