package com.xzx.gc.user.service; import cn.hutool.core.convert.Convert; import cn.hutool.core.util.StrUtil; import com.xzx.gc.common.constant.CommonEnum; import com.xzx.gc.common.constant.Constants; import com.xzx.gc.common.constant.UserEnum; import com.xzx.gc.entity.OtherUserInfo; import com.xzx.gc.entity.UserAuth; import com.xzx.gc.entity.UserRole; import com.xzx.gc.model.dto.FunctionNodeView; import com.xzx.gc.user.mapper.OtherUserMapper; import com.xzx.gc.user.mapper.UserAuthMapper; import com.xzx.gc.user.mapper.UserRoleMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.TreeSet; import java.util.stream.Collectors; @Service @Transactional @Slf4j public class UserAuthService { @Autowired private UserAuthMapper userAuthMapper; @Autowired private UserRoleMapper userRoleMapper; @Autowired private OtherUserMapper otherUserMapper; @Autowired private OtherUserService otherUserService; /** * @param userId * @param homePageType 0:查询权限,1:查询首页 * @return */ public List findMenu(String userId, int homePageType) { List list = new ArrayList<>(); OtherUserInfo otherUserInfo = otherUserMapper.selectByPrimaryKey(userId); String mobilePhone = otherUserInfo.getMobilePhone(); List select = otherUserService.findByMobile(mobilePhone); String roleIds = select.stream().map(OtherUserInfo::getRoleIds).collect(Collectors.joining(",")); String userIds = select.stream().map(OtherUserInfo::getUserId).collect(Collectors.joining(",")); String userTypes = select.stream().map(OtherUserInfo::getUserType).collect(Collectors.joining(",")); if (StrUtil.isNotBlank(roleIds) && !Constants.NULL.equals(roleIds)) { String[] split = roleIds.split(","); String[] split2 = userIds.split(","); String[] split3 = userTypes.split(","); for (int i = 0; i < split.length; i++) { String roleId = split[i]; String authUserId = split2[i]; String authUserType = split3[i]; if (StrUtil.isNotBlank(roleId) && !Constants.NULL.equals(roleId)) { UserRole userRole = userRoleMapper.selectByPrimaryKey(roleId); if (userRole != null && StrUtil.isNotBlank(userRole.getAuthIds())) { String authIds = userRole.getAuthIds(); String roleCode = userRole.getRoleCode(); String[] split1 = authIds.split(","); for (String authId : split1) { if (StrUtil.isNotBlank(authId)) { UserAuth userAuth = userAuthMapper.selectByPrimaryKey(authId); if (0 == userAuth.getDelFlag()) { if (CommonEnum.回收员.getValue().equals(roleCode) && UserEnum.统计.getValue().equals(userAuth.getAuthCode())) { userAuth.setAuthName("回收" + userAuth.getAuthName()); } else if (CommonEnum.入库员.getValue().equals(roleCode) && UserEnum.统计.getValue().equals(userAuth.getAuthCode())) { userAuth.setAuthName("入库" + userAuth.getAuthName()); } userAuth.setUserId(authUserId); userAuth.setUserType(authUserType); list.add(userAuth); } } } } } } } //list去重 list = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparingLong(UserAuth::getId))), ArrayList::new)); if(homePageType==0) { //过滤掉可配置的首页的 list = list.stream().filter(x -> x.getHomePageFlag() == null || Convert.toShort(0).equals(x.getHomePageFlag())).collect(Collectors.toList()); }else if(homePageType==1){ //只查询首页的 list = list.stream().filter(x -> x.getHomePageFlag() != null && Convert.toShort(1).equals(x.getHomePageFlag())).collect(Collectors.toList()); } return list; } public List buildViewList() { List viewList = new ArrayList<>(); List list = userAuthMapper.selectAll(); //客户管理 FunctionNodeView userManager = new FunctionNodeView(); userManager.setCode("KH"); userManager.setName("客户管理"); userManager.setId(Long.parseLong("10000")); List userChildren = new ArrayList<>(); //数据统计 FunctionNodeView dateManager = new FunctionNodeView(); dateManager.setCode("SJ"); dateManager.setName("数据统计"); dateManager.setId(Long.parseLong("10001")); List dateChildren = new ArrayList<>(); //物品价格 FunctionNodeView productManager = new FunctionNodeView(); productManager.setCode("WP"); productManager.setName("工作管理"); productManager.setId(Long.parseLong("10002")); List productChildren = new ArrayList<>(); for (UserAuth authInfo : list) { if (authInfo.getCategoryCode().equals(userManager.getCode())) { FunctionNodeView uc = new FunctionNodeView(); uc.setName(authInfo.getAuthName()); uc.setCode(authInfo.getAuthCode()); uc.setId(authInfo.getId()); userChildren.add(uc); } else if (authInfo.getCategoryCode().equals(dateManager.getCode())) { FunctionNodeView uc = new FunctionNodeView(); uc.setName(authInfo.getAuthName()); uc.setCode(authInfo.getAuthCode()); uc.setId(authInfo.getId()); dateChildren.add(uc); } else { FunctionNodeView uc = new FunctionNodeView(); uc.setName(authInfo.getAuthName()); uc.setCode(authInfo.getAuthCode()); uc.setId(authInfo.getId()); productChildren.add(uc); } } userManager.setChildren(userChildren); dateManager.setChildren(dateChildren); productManager.setChildren(productChildren); viewList.add(userManager); viewList.add(dateManager); viewList.add(productManager); return viewList; } }