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

220 lines
9.1 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;
2025-01-02 14:54:57 +08:00
import com.jsowell.pile.service.AdapayMemberAccountService;
2024-01-06 15:20:28 +08:00
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;
2024-09-18 17:05:49 +08:00
import org.springframework.util.CollectionUtils;
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 PileMerchantInfoService pileMerchantInfoService;
private static PileStationInfoService pileStationInfoService;
2025-01-02 14:54:57 +08:00
private static AdapayMemberAccountService adapayMemberAccountService;
// 3.初始化有参构造器
@Autowired
2025-01-02 14:54:57 +08:00
public UserUtils(PileMerchantInfoService pileMerchantInfoService,
PileStationInfoService pileStationInfoService,
AdapayMemberAccountService adapayMemberAccountService) {
UserUtils.pileMerchantInfoService = pileMerchantInfoService;
UserUtils.pileStationInfoService = pileStationInfoService;
2025-01-02 14:54:57 +08:00
UserUtils.adapayMemberAccountService = adapayMemberAccountService;
}
// 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-06-13 14:43:01 +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);
}
2024-06-13 14:43:01 +08:00
log.info("用户id:{}, AuthorizedDeptVO:{}", user.getUserId(), JSON.toJSONString(resultVO));
return resultVO;
}
/**
* 获取登录用户信息
* 返回信息包含
* 一级运营商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() + "");
}
}
}
2025-01-02 14:54:57 +08:00
// 设置汇付会员id
for (MerchantInfoVO merchantInfoVO : merchantInfoVOList) {
merchantInfoVO.setAdapayMemberId(adapayMemberAccountService.selectAdapayMemberIdByMerchantId(merchantInfoVO.getMerchantId()));
}
resultVO.setMerchantInfoVOList(merchantInfoVOList);
resultVO.setFirstMerchantIdList(firstMerchantIdList);
return resultVO;
}
/**
* 获取当前登录用户中有权限的运营商列表
* @return
*/
public static List<MerchantInfoVO> getMerchantInfoVOList() {
LoginUserDetailVO loginUserDetail = UserUtils.getLoginUserDetail();
return loginUserDetail.getMerchantInfoVOList();
}
/**
* 获取当前登录用户有权限的一级运营商idList
* @return
*/
public static List<String> getFirstMerchantIdList() {
LoginUserDetailVO loginUserDetail = UserUtils.getLoginUserDetail();
return loginUserDetail.getFirstMerchantIdList();
}
2024-09-18 17:05:49 +08:00
/**
* 校验当前登录用户是否有操作站点权限
* @param stationId 站点id
* @return boolean true:有权限false:无权限
*/
public static boolean checkStationAuth(String stationId) {
AuthorizedDeptVO authorizedMap = UserUtils.getAuthorizedMap();
if (authorizedMap == null) {
return false;
}
if (CollectionUtils.isEmpty(authorizedMap.getStationDeptIds())) {
return false;
}
List<String> list = pileStationInfoService.queryByStationDeptIds(authorizedMap.getStationDeptIds());
return list.contains(stationId);
}
}