Files
jsowell-charger-web/jsowell-pile/src/main/java/com/jsowell/pile/util/UserUtils.java

224 lines
9.6 KiB
Java
Raw Normal View History

package com.jsowell.pile.util;
2024-03-19 16:22:40 +08:00
import com.alibaba.fastjson2.JSON;
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;
2024-01-06 15:20:28 +08:00
import com.jsowell.pile.service.MemberBasicInfoService;
import com.jsowell.pile.service.PileMerchantInfoService;
import com.jsowell.pile.service.PileStationInfoService;
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.注入想引用的对象的静态实例
2024-01-06 15:20:28 +08:00
private static MemberBasicInfoService memberBasicInfoService;
2024-01-06 15:20:28 +08:00
private static PileMerchantInfoService pileMerchantInfoService;
2024-01-06 15:20:28 +08:00
private static PileStationInfoService pileStationInfoService;
// 3.初始化有参构造器
@Autowired
2024-01-06 15:20:28 +08:00
public UserUtils(MemberBasicInfoService memberBasicInfoService, PileMerchantInfoService pileMerchantInfoService,
PileStationInfoService 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);
2024-03-19 16:22:40 +08:00
log.info("用户id:{}, 所属部门信息:{}", user.getUserId(), JSON.toJSONString(dept));
// 登录用户角色
Long parentId = dept.getParentId();
resultVO.setParentId(String.valueOf(parentId));
List<String> merchantDeptIds = Lists.newArrayList();
List<String> 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<MerchantInfoVO> getMerchantListByAuth() {
List<MerchantInfoVO> 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;
}*/
/**
* 获取登录用户信息
* 返回信息包含
* 一级运营商idList
* 有权限的运营商信息list
* @return LoginUserDetailVO 登录用户详情VO
*/
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<MerchantInfoVO> merchantInfoVOList = Lists.newArrayList();
List<String> firstMerchantIdList = Lists.newArrayList();
if (Constants.ZERO.equals(deptLevel)) { // 平台级权限
// 查询所有生效的运营商
merchantInfoVOList = pileMerchantInfoService.selectAll();
// 获取一级运营商id
List<String> collect = merchantInfoVOList.stream()
.filter(x -> Constants.ONE.equals(x.getMerchantLevel()))
.map(MerchantInfoVO::getMerchantId)
.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);
// 获取一级运营商id
PileMerchantInfo firstLevelMerchant = pileMerchantInfoService.getFirstLevelMerchantByMerchantId(merchantInfoVO.getMerchantId());
if (Objects.nonNull(firstLevelMerchant)) {
firstMerchantIdList.add(firstLevelMerchant.getId() + "");
}
}
} else if (Constants.THREE.equals(deptLevel)) { // 站点权限
// 返回站点所属运营商
PileStationInfo stationInfo = pileStationInfoService.queryInfoByDeptId(deptId);
if (Objects.nonNull(stationInfo)) {
String merchantId = stationInfo.getMerchantId() + "";
// 查询站点所属运营商
MerchantInfoVO merchantInfoVO = pileMerchantInfoService.getMerchantInfoVO(merchantId);
if (Objects.nonNull(merchantInfoVO)) {
merchantInfoVOList.add(merchantInfoVO);
}
// 获取一级运营商id
PileMerchantInfo firstLevelMerchant = pileMerchantInfoService.getFirstLevelMerchantByMerchantId(merchantId);
if (Objects.nonNull(firstLevelMerchant)) {
firstMerchantIdList.add(firstLevelMerchant.getId() + "");
}
}
}
resultVO.setMerchantInfoVOList(merchantInfoVOList);
resultVO.setFirstMerchantIdList(firstMerchantIdList);
return resultVO;
}
}