package com.jsowell.pile.util; import com.alibaba.fastjson2.JSONObject; import com.google.common.collect.Lists; import com.jsowell.common.constant.Constants; import com.jsowell.common.core.domain.entity.SysDept; import com.jsowell.common.core.domain.entity.SysUser; import com.jsowell.common.core.domain.model.LoginUser; import com.jsowell.common.core.domain.vo.AuthorizedDeptVO; import com.jsowell.common.util.SecurityUtils; import com.jsowell.pile.domain.PileMerchantInfo; import com.jsowell.pile.domain.PileStationInfo; import com.jsowell.pile.service.IMemberBasicInfoService; import com.jsowell.pile.service.IPileMerchantInfoService; import com.jsowell.pile.service.IPileStationInfoService; import com.jsowell.pile.vo.base.LoginUserDetailVO; import com.jsowell.pile.vo.base.MerchantInfoVO; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * 后管登录用户工具类 */ // 1.将工具类注入到容器中 @Slf4j @Component public class UserUtils { // 2.注入想引用的对象的静态实例 private static IMemberBasicInfoService memberBasicInfoService; private static IPileMerchantInfoService pileMerchantInfoService; private static IPileStationInfoService pileStationInfoService; // 3.初始化有参构造器 @Autowired public UserUtils(IMemberBasicInfoService memberBasicInfoService, IPileMerchantInfoService pileMerchantInfoService, IPileStationInfoService pileStationInfoService) { UserUtils.memberBasicInfoService = memberBasicInfoService; UserUtils.pileMerchantInfoService = pileMerchantInfoService; UserUtils.pileStationInfoService = pileStationInfoService; } // 4.使用 // 查询登录用户权限 public static AuthorizedDeptVO getAuthorizedMap() { // 登录用户 LoginUser loginUser = SecurityUtils.getLoginUser(); if (loginUser == null) { return null; } // 用户 SysUser user = loginUser.getUser(); if (user == null) { return null; } AuthorizedDeptVO resultVO = AuthorizedDeptVO.builder().build(); // 登录用户所属部门(运营商或站点 根据父级id判断) SysDept dept = user.getDept(); if (dept == null) { log.info("用户id:{}, 获取不到所属部门信息", user.getUserId()); return null; } String deptId = String.valueOf(dept.getDeptId()); resultVO.setDeptId(deptId); log.info("用户id:{}, 所属部门信息:{}", user.getUserId(), JSONObject.toJSONString(dept)); // 登录用户角色 Long parentId = dept.getParentId(); resultVO.setParentId(String.valueOf(parentId)); List merchantDeptIds = Lists.newArrayList(); List stationDeptIds = Lists.newArrayList(); if (parentId == 0L) { // 父级id是0,表明是该账号挂在平台下面,是系统管理员 } else if (parentId == 100L) { // 父级id是100,表明是该账号挂在运营商下面,是运营商管理员 merchantDeptIds.add(deptId); resultVO.setMerchantDeptIds(merchantDeptIds); } else { // 其他情况,表明是站点管理员 stationDeptIds.add(deptId); resultVO.setStationDeptIds(stationDeptIds); } return resultVO; } /** * 获取当前会员中的运营商列表(带权限校验) */ public static List getMerchantListByAuth() { List resultList = Lists.newArrayList(); // 获取登录用户 所有有权限运营商 LoginUser loginUser = SecurityUtils.getLoginUser(); if (Objects.isNull(loginUser)) { return resultList; } // 获取登录用户对应的user对象 SysUser user = loginUser.getUser(); if (Objects.isNull(user)) { return resultList; } // 获取所属部门id SysDept dept = user.getDept(); if (Objects.isNull(dept)) { return resultList; } String deptLevel = dept.getDeptLevel(); String deptId = String.valueOf(dept.getDeptId()); if (Constants.ZERO.equals(deptLevel)) { // 平台级权限 查询所有生效的运营商 resultList = pileMerchantInfoService.selectAll(); } else if (Constants.ONE.equals(deptLevel)) { // 一级运营商权限 查询该一级运营商下面的所有运营商 PileMerchantInfo merchantInfo = pileMerchantInfoService.queryInfoByDeptId(deptId); resultList = pileMerchantInfoService.selectListByFirstMerchant(merchantInfo.getId() + ""); } else if (Constants.TWO.equals(deptLevel) ) { // 二级运营商权限 返回自身运营商 MerchantInfoVO merchantInfoVO = pileMerchantInfoService.queryMerchantInfoVOByDeptId(deptId); if (Objects.nonNull(merchantInfoVO)) { resultList.add(merchantInfoVO); } } else if (Constants.THREE.equals(deptLevel)) { // 站点权限 返回自身运营商 PileStationInfo stationInfo = pileStationInfoService.queryInfoByDeptId(deptId); if (Objects.nonNull(stationInfo)) { MerchantInfoVO merchantInfoVO = pileMerchantInfoService.getMerchantInfoVO(stationInfo.getMerchantId() + ""); if (Objects.nonNull(merchantInfoVO)) { resultList.add(merchantInfoVO); } } } return resultList; } public static LoginUserDetailVO getLoginUserDetail() { LoginUserDetailVO resultVO = new LoginUserDetailVO(); // 获取登录用户 所有有权限运营商 LoginUser loginUser = SecurityUtils.getLoginUser(); if (Objects.isNull(loginUser)) { return resultVO; } // 获取登录用户对应的user对象 SysUser user = loginUser.getUser(); if (Objects.isNull(user)) { return resultVO; } // 获取所属部门 SysDept dept = user.getDept(); if (Objects.isNull(dept)) { return resultVO; } // 所属部门等级 String deptLevel = dept.getDeptLevel(); // 所属部门id String deptId = String.valueOf(dept.getDeptId()); List merchantInfoVOList = Lists.newArrayList(); List firstMerchantIdList = Lists.newArrayList(); if (Constants.ZERO.equals(deptLevel)) { // 平台级权限 查询所有生效的运营商 merchantInfoVOList = pileMerchantInfoService.selectAll(); // 获取一级运营商id List collect = merchantInfoVOList.stream() .map(MerchantInfoVO::getMerchantLevel) .filter("1"::equals) .collect(Collectors.toList()); firstMerchantIdList.addAll(collect); } else if (Constants.ONE.equals(deptLevel)) { // 一级运营商权限 查询该一级运营商下面的所有运营商 PileMerchantInfo merchantInfo = pileMerchantInfoService.queryInfoByDeptId(deptId); if (Objects.nonNull(merchantInfo)) { String firstMerchantId = merchantInfo.getId().toString(); // 获取一级运营商id firstMerchantIdList.add(firstMerchantId); merchantInfoVOList = pileMerchantInfoService.selectListByFirstMerchant(firstMerchantId); } } else if (Constants.TWO.equals(deptLevel)) { // 二级运营商权限 返回自身运营商 MerchantInfoVO merchantInfoVO = pileMerchantInfoService.queryMerchantInfoVOByDeptId(deptId); if (Objects.nonNull(merchantInfoVO)) { merchantInfoVOList.add(merchantInfoVO); } } else if (Constants.THREE.equals(deptLevel)) { // 站点权限 返回自身运营商 PileStationInfo stationInfo = pileStationInfoService.queryInfoByDeptId(deptId); if (Objects.nonNull(stationInfo)) { MerchantInfoVO merchantInfoVO = pileMerchantInfoService.getMerchantInfoVO(stationInfo.getMerchantId() + ""); if (Objects.nonNull(merchantInfoVO)) { merchantInfoVOList.add(merchantInfoVO); } } } resultVO.setMerchantInfoVOList(merchantInfoVOList); return null; } }