2023-11-11 15:21:25 +08:00
|
|
|
|
package com.jsowell.pile.util;
|
|
|
|
|
|
|
2024-03-19 16:22:40 +08:00
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
2023-11-11 15:21:25 +08:00
|
|
|
|
import com.google.common.collect.Lists;
|
2023-11-11 15:40:30 +08:00
|
|
|
|
import com.jsowell.common.constant.Constants;
|
2023-11-11 15:21:25 +08:00
|
|
|
|
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;
|
2023-11-11 15:40:30 +08:00
|
|
|
|
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;
|
2023-11-11 16:05:04 +08:00
|
|
|
|
import com.jsowell.pile.vo.base.LoginUserDetailVO;
|
2023-11-11 15:40:30 +08:00
|
|
|
|
import com.jsowell.pile.vo.base.MerchantInfoVO;
|
2023-11-11 15:21:25 +08:00
|
|
|
|
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;
|
2023-11-11 15:21:25 +08:00
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
2023-11-11 15:40:30 +08:00
|
|
|
|
import java.util.Objects;
|
2023-11-11 16:05:04 +08:00
|
|
|
|
import java.util.stream.Collectors;
|
2023-11-11 15:21:25 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 后管登录用户工具类
|
|
|
|
|
|
*/
|
|
|
|
|
|
// 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;
|
2023-11-11 15:40:30 +08:00
|
|
|
|
|
2023-11-11 15:21:25 +08:00
|
|
|
|
// 3.初始化有参构造器
|
|
|
|
|
|
@Autowired
|
2025-01-02 14:54:57 +08:00
|
|
|
|
public UserUtils(PileMerchantInfoService pileMerchantInfoService,
|
|
|
|
|
|
PileStationInfoService pileStationInfoService,
|
|
|
|
|
|
AdapayMemberAccountService adapayMemberAccountService) {
|
2023-11-11 15:40:30 +08:00
|
|
|
|
UserUtils.pileMerchantInfoService = pileMerchantInfoService;
|
|
|
|
|
|
UserUtils.pileStationInfoService = pileStationInfoService;
|
2025-01-02 14:54:57 +08:00
|
|
|
|
UserUtils.adapayMemberAccountService = adapayMemberAccountService;
|
2023-11-11 15:21:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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));
|
2023-11-11 15:21:25 +08:00
|
|
|
|
// 登录用户角色
|
|
|
|
|
|
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));
|
2023-11-11 15:21:25 +08:00
|
|
|
|
return resultVO;
|
|
|
|
|
|
}
|
2023-11-11 15:40:30 +08:00
|
|
|
|
|
2024-05-28 11:54:02 +08:00
|
|
|
|
|
2023-11-11 16:05:04 +08:00
|
|
|
|
|
2023-11-11 16:28:23 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取登录用户信息
|
|
|
|
|
|
* 返回信息包含:
|
|
|
|
|
|
* 一级运营商idList
|
|
|
|
|
|
* 有权限的运营商信息list
|
|
|
|
|
|
* @return LoginUserDetailVO 登录用户详情VO
|
|
|
|
|
|
*/
|
2023-11-11 16:05:04 +08:00
|
|
|
|
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();
|
2023-11-11 16:22:01 +08:00
|
|
|
|
if (Constants.ZERO.equals(deptLevel)) { // 平台级权限
|
|
|
|
|
|
// 查询所有生效的运营商
|
2023-11-11 16:05:04 +08:00
|
|
|
|
merchantInfoVOList = pileMerchantInfoService.selectAll();
|
|
|
|
|
|
|
|
|
|
|
|
// 获取一级运营商id
|
|
|
|
|
|
List<String> collect = merchantInfoVOList.stream()
|
2023-11-11 16:22:01 +08:00
|
|
|
|
.filter(x -> Constants.ONE.equals(x.getMerchantLevel()))
|
|
|
|
|
|
.map(MerchantInfoVO::getMerchantId)
|
2023-11-11 16:05:04 +08:00
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
firstMerchantIdList.addAll(collect);
|
2023-11-11 16:22:01 +08:00
|
|
|
|
} else if (Constants.ONE.equals(deptLevel)) { // 一级运营商权限
|
|
|
|
|
|
// 查询该一级运营商下面的所有运营商
|
2023-11-11 16:05:04 +08:00
|
|
|
|
PileMerchantInfo merchantInfo = pileMerchantInfoService.queryInfoByDeptId(deptId);
|
|
|
|
|
|
if (Objects.nonNull(merchantInfo)) {
|
|
|
|
|
|
String firstMerchantId = merchantInfo.getId().toString();
|
|
|
|
|
|
// 获取一级运营商id
|
|
|
|
|
|
firstMerchantIdList.add(firstMerchantId);
|
|
|
|
|
|
merchantInfoVOList = pileMerchantInfoService.selectListByFirstMerchant(firstMerchantId);
|
|
|
|
|
|
}
|
2023-11-11 16:22:01 +08:00
|
|
|
|
} else if (Constants.TWO.equals(deptLevel)) { // 二级运营商权限
|
|
|
|
|
|
// 返回自身运营商
|
2023-11-11 16:05:04 +08:00
|
|
|
|
MerchantInfoVO merchantInfoVO = pileMerchantInfoService.queryMerchantInfoVOByDeptId(deptId);
|
|
|
|
|
|
if (Objects.nonNull(merchantInfoVO)) {
|
|
|
|
|
|
merchantInfoVOList.add(merchantInfoVO);
|
2023-11-11 16:22:01 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取一级运营商id
|
|
|
|
|
|
PileMerchantInfo firstLevelMerchant = pileMerchantInfoService.getFirstLevelMerchantByMerchantId(merchantInfoVO.getMerchantId());
|
|
|
|
|
|
if (Objects.nonNull(firstLevelMerchant)) {
|
|
|
|
|
|
firstMerchantIdList.add(firstLevelMerchant.getId() + "");
|
|
|
|
|
|
}
|
2023-11-11 16:05:04 +08:00
|
|
|
|
}
|
2023-11-11 16:22:01 +08:00
|
|
|
|
} else if (Constants.THREE.equals(deptLevel)) { // 站点权限
|
|
|
|
|
|
// 返回站点所属运营商
|
2023-11-11 16:05:04 +08:00
|
|
|
|
PileStationInfo stationInfo = pileStationInfoService.queryInfoByDeptId(deptId);
|
|
|
|
|
|
if (Objects.nonNull(stationInfo)) {
|
2023-11-11 16:22:01 +08:00
|
|
|
|
String merchantId = stationInfo.getMerchantId() + "";
|
|
|
|
|
|
// 查询站点所属运营商
|
|
|
|
|
|
MerchantInfoVO merchantInfoVO = pileMerchantInfoService.getMerchantInfoVO(merchantId);
|
2023-11-11 16:05:04 +08:00
|
|
|
|
if (Objects.nonNull(merchantInfoVO)) {
|
|
|
|
|
|
merchantInfoVOList.add(merchantInfoVO);
|
|
|
|
|
|
}
|
2023-11-11 16:22:01 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取一级运营商id
|
|
|
|
|
|
PileMerchantInfo firstLevelMerchant = pileMerchantInfoService.getFirstLevelMerchantByMerchantId(merchantId);
|
|
|
|
|
|
if (Objects.nonNull(firstLevelMerchant)) {
|
|
|
|
|
|
firstMerchantIdList.add(firstLevelMerchant.getId() + "");
|
|
|
|
|
|
}
|
2023-11-11 16:05:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-02 14:54:57 +08:00
|
|
|
|
|
|
|
|
|
|
// 设置汇付会员id
|
|
|
|
|
|
for (MerchantInfoVO merchantInfoVO : merchantInfoVOList) {
|
|
|
|
|
|
merchantInfoVO.setAdapayMemberId(adapayMemberAccountService.selectAdapayMemberIdByMerchantId(merchantInfoVO.getMerchantId()));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-11 16:05:04 +08:00
|
|
|
|
resultVO.setMerchantInfoVOList(merchantInfoVOList);
|
2023-11-11 16:22:01 +08:00
|
|
|
|
resultVO.setFirstMerchantIdList(firstMerchantIdList);
|
|
|
|
|
|
return resultVO;
|
2023-11-11 16:05:04 +08:00
|
|
|
|
}
|
2024-05-28 11:54:02 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取当前登录用户中有权限的运营商列表
|
|
|
|
|
|
* @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);
|
|
|
|
|
|
}
|
2023-11-11 15:21:25 +08:00
|
|
|
|
}
|