| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- package com.jtzx.crm.module.sys.service;
- import cc.uncarbon.framework.core.context.UserContextHolder;
- import cc.uncarbon.framework.core.exception.BusinessException;
- import cc.uncarbon.framework.core.page.PageResult;
- import cc.uncarbon.framework.crud.entity.HelioBaseEntity;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.google.common.collect.Lists;
- import com.jtzx.crm.config.AsyncVThreadConfiguration;
- import com.jtzx.crm.module.api.util.CrmAdminStpUtil;
- import com.jtzx.crm.module.sys.entity.SysGroupEntity;
- import com.jtzx.crm.module.sys.entity.SysRoleEntity;
- import com.jtzx.crm.module.sys.entity.SysUserEntity;
- import com.jtzx.crm.module.sys.entity.SysUserRoleRelationEntity;
- import com.jtzx.crm.module.sys.mapper.SysGroupMapper;
- import com.jtzx.crm.module.sys.mapper.SysRoleMapper;
- import com.jtzx.crm.module.sys.mapper.SysUserMapper;
- import com.jtzx.crm.module.sys.mapper.SysUserRoleRelationMapper;
- import com.jtzx.crm.module.sys.model.request.UserRoleRelationDTO;
- import com.jtzx.crm.module.sys.model.request.UserRoleRelationInsertDTO;
- import com.jtzx.crm.module.sys.model.response.*;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.Comparator;
- import java.util.HashSet;
- import java.util.List;
- /**
- * 后台用户-角色关联
- */
- @RequiredArgsConstructor
- @Service
- @Slf4j
- public class SysUserRoleRelationService {
- private final SysUserRoleRelationMapper sysUserRoleRelationMapper;
- private final SysRoleMapper sysRoleMapper;
- private final SysUserMapper sysUserMapper;
- private final SysGroupMapper sysGroupMapper;
- private final AsyncVThreadConfiguration asyncVThreadConfiguration;
- /**
- * 更新用户redis中的角色信息
- *
- * @param userId
- */
- @Transactional(rollbackFor = Exception.class)
- public void updateUserRole(Long userId) {
- List<String> roleValueLists = Lists.newArrayList();
- List<Long> roleIdList = Lists.newArrayList();
- List<SysUserRoleRelationEntity> roleRelationEntityList =
- sysUserRoleRelationMapper.selectList(
- new LambdaQueryWrapper<>(SysUserRoleRelationEntity.class)
- .select(
- SysUserRoleRelationEntity::getRoleValue,
- SysUserRoleRelationEntity::getRoleId
- )
- .eq(SysUserRoleRelationEntity::getUserId, userId)
- );
- if (CollUtil.isNotEmpty(roleRelationEntityList)) {
- roleValueLists.addAll(
- roleRelationEntityList.stream()
- .map(SysUserRoleRelationEntity::getRoleValue)
- .toList());
- roleIdList.addAll(
- roleRelationEntityList.stream()
- .map(SysUserRoleRelationEntity::getRoleId)
- .toList());
- }
- UserContextHolder.getUserContext().setRoles(roleValueLists.stream().distinct().toList());
- UserContextHolder.getUserContext().setRolesIds(new HashSet<>(roleIdList));
- }
- /**
- * 查询角色下拉数据列表
- *
- * @return
- */
- public List<SysRoleListBO> queryRoleData() {
- List<SysRoleEntity> sysRoleEntities = sysRoleMapper.selectList(
- new LambdaQueryWrapper<>(SysRoleEntity.class)
- .ne(SysRoleEntity::getValue, "Member")
- );
- List<SysRoleListBO> result = Lists.newArrayList();
- sysRoleEntities.stream()
- .sorted(Comparator.comparing(SysRoleEntity::getSortCode).reversed())
- .forEach(sysRoleEntity -> {
- SysRoleListBO bo = SysRoleListBO.builder().build();
- bo.setId(sysRoleEntity.getId());
- bo.setTitle(sysRoleEntity.getTitle());
- result.add(bo);
- });
- return result;
- }
- /**
- * 分页查询表格数据列表
- *
- * @param dto
- * @return
- */
- public PageResult<UserRoleRelationPageListBO> queryTableListByPage(UserRoleRelationDTO dto) {
- //查询角色数据
- List<SysRoleEntity> roleEntityList =
- sysRoleMapper.selectList(
- new LambdaQueryWrapper<>(SysRoleEntity.class)
- .select(
- SysRoleEntity::getSortCode,
- SysRoleEntity::getId,
- SysRoleEntity::getValue,
- SysRoleEntity::getTitle
- )
- .eq(dto.getRoleId() != null, SysRoleEntity::getId, dto.getRoleId())
- .ne(SysRoleEntity::getValue, "Member")
- );
- if (CollUtil.isEmpty(roleEntityList)) {
- return new PageResult<UserRoleRelationPageListBO>()
- .setCurrent(1)
- .setSize(dto.getPageSize())
- .setTotal(0)
- .setRecords(Lists.newArrayList());
- }
- //查询分组数据
- List<SysGroupEntity> groupEntityList =
- sysGroupMapper.selectList(
- new LambdaQueryWrapper<>(SysGroupEntity.class)
- .select(
- SysGroupEntity::getSortCode,
- SysGroupEntity::getId,
- SysGroupEntity::getTitle
- )
- .eq(dto.getGroupId() != null, HelioBaseEntity::getId, dto.getGroupId())
- );
- if (CollUtil.isEmpty(groupEntityList)) {
- return new PageResult<UserRoleRelationPageListBO>()
- .setCurrent(1)
- .setSize(dto.getPageSize())
- .setTotal(0)
- .setRecords(Lists.newArrayList());
- }
- //查询用户
- List<SysUserEntity> userEntityList =
- sysUserMapper.selectList(
- new LambdaQueryWrapper<>(SysUserEntity.class)
- .select(
- SysUserEntity::getUserName,
- SysUserEntity::getId,
- SysUserEntity::getPin
- )
- .eq(dto.getUserId() != null, SysUserEntity::getId, dto.getUserId())
- .eq(SysUserEntity::getStatus, 1)
- .and(StrUtil.isNotEmpty(dto.getQueryStr()), z -> z.like(SysUserEntity::getUserName, dto.getQueryStr())
- .or().like(SysUserEntity::getPin, dto.getQueryStr())
- )
- );
- if (CollUtil.isEmpty(userEntityList)) {
- return new PageResult<UserRoleRelationPageListBO>()
- .setCurrent(1)
- .setSize(dto.getPageSize())
- .setTotal(0)
- .setRecords(Lists.newArrayList());
- }
- List<Long> userIds =
- userEntityList.stream()
- .sorted(Comparator.comparing(SysUserEntity::getId))
- .map(SysUserEntity::getId)
- .toList();
- Page<SysUserRoleRelationEntity> entityPage =
- sysUserRoleRelationMapper.selectPage(
- new Page<>(dto.getPageNum(), dto.getPageSize()),
- new LambdaQueryWrapper<>(SysUserRoleRelationEntity.class)
- .eq(dto.getRoleId() != null, SysUserRoleRelationEntity::getRoleId, dto.getRoleId())
- .eq(dto.getGroupId() != null, SysUserRoleRelationEntity::getGroupId, dto.getGroupId())
- .eq(dto.getUserId() != null && StrUtil.isEmpty(dto.getQueryStr()), SysUserRoleRelationEntity::getUserId, dto.getUserId())
- .in(StrUtil.isNotEmpty(dto.getQueryStr()), SysUserRoleRelationEntity::getUserId, userIds)
- .ne(SysUserRoleRelationEntity::getRoleValue, "Member")
- .orderByDesc(SysUserRoleRelationEntity::getCreatedAt)
- );
- if (CollUtil.isEmpty(entityPage.getRecords())) {
- return new PageResult<UserRoleRelationPageListBO>()
- .setCurrent(1)
- .setSize(dto.getPageSize())
- .setTotal(0)
- .setRecords(Lists.newArrayList());
- }
- List<UserRoleRelationPageListBO> result = Lists.newArrayList();
- entityPage.getRecords().forEach(record -> {
- UserRoleRelationPageListBO bo = UserRoleRelationPageListBO.builder().build();
- bo.setId(record.getId());
- bo.setGroupId(null);
- bo.setGroupName("-");
- if (record.getGroupId() != null) {
- groupEntityList.stream()
- .filter(x -> x.getId().equals(record.getGroupId()))
- .findFirst()
- .ifPresent(group -> {
- bo.setGroupId(group.getId());
- bo.setGroupName(group.getTitle());
- });
- }
- userEntityList.stream()
- .filter(x -> x.getId().equals(record.getUserId()))
- .findFirst()
- .ifPresent(user -> {
- bo.setUserId(user.getId());
- bo.setUserName(user.getUserName());
- bo.setAccount(user.getPin());
- });
- roleEntityList.stream()
- .filter(x -> x.getId().equals(record.getRoleId()))
- .findFirst()
- .ifPresent(role -> {
- bo.setRoleId(role.getId());
- bo.setRoleName(role.getTitle());
- });
- bo.setDelStatus(0);
- result.add(bo);
- });
- return new PageResult<UserRoleRelationPageListBO>()
- .setCurrent(entityPage.getCurrent())
- .setSize(entityPage.getSize())
- .setTotal(entityPage.getTotal())
- .setRecords(result);
- }
- /**
- * 查询用户下拉数据列表
- * @return
- */
- public List<RoleUserBO> queryUserData() {
- List<RoleUserBO> result = Lists.newArrayList();
- //查询关系数据
- List<SysUserRoleRelationEntity> relationEntityList = sysUserRoleRelationMapper.selectList(
- new LambdaQueryWrapper<>(SysUserRoleRelationEntity.class)
- .ne(SysUserRoleRelationEntity::getRoleValue, "Member")
- .orderByDesc(SysUserRoleRelationEntity::getCreatedAt)
- );
- if (CollUtil.isNotEmpty(relationEntityList)) {
- List<Long> userIds =
- relationEntityList.stream()
- .map(SysUserRoleRelationEntity::getUserId)
- .distinct().toList();
- //查询用户
- List<SysUserEntity> sysUserEntityList =
- sysUserMapper.selectList(
- new LambdaQueryWrapper<>(SysUserEntity.class)
- .in(SysUserEntity::getId, userIds)
- .eq(SysUserEntity::getStatus, 1)
- );
- relationEntityList.forEach(record -> {
- if (CollUtil.isNotEmpty(sysUserEntityList)) {
- sysUserEntityList.stream()
- .filter(x -> x.getId().equals(record.getUserId()))
- .findFirst()
- .ifPresent(user -> {
- RoleUserBO bo = RoleUserBO.builder().build();
- bo.setId(record.getId());
- bo.setUserId(record.getUserId());
- bo.setUserName(sysUserMapper.selectById(record.getUserId()).getUserName());
- bo.setGroupId(record.getGroupId());
- bo.setRoleId(record.getRoleId());
- bo.setUserName(user.getUserName());
- bo.setAccount(user.getPin());
- result.add(bo);
- });
- }
- });
- }
- return result;
- }
- /**
- * 查询权限表格数据
- */
- public List<UserRoleRelationBO> queryTableData() {
- List<UserRoleRelationBO> userRoleRelationBOList = Lists.newArrayList();
- //查询关系数据
- List<SysUserRoleRelationEntity> relationEntityList = sysUserRoleRelationMapper.selectList(
- new LambdaQueryWrapper<>(SysUserRoleRelationEntity.class)
- .ne(SysUserRoleRelationEntity::getRoleValue, "Member")
- );
- //查询角色
- List<SysRoleEntity> sysRoleEntities = sysRoleMapper.selectList(
- new LambdaQueryWrapper<>(SysRoleEntity.class)
- .ne(SysRoleEntity::getValue, "Member")
- );
- //查询用户
- List<SysUserEntity> sysUserEntityList =
- sysUserMapper.selectList(
- new LambdaQueryWrapper<>(SysUserEntity.class)
- .eq(SysUserEntity::getStatus, 1)
- );
- //查询分组数据
- List<SysGroupEntity> groupEntityList = sysGroupMapper.selectList(null);
- //数据处理
- if (CollUtil.isNotEmpty(sysRoleEntities)) {
- sysRoleEntities.stream()
- .sorted(Comparator.comparing(SysRoleEntity::getSortCode).reversed())
- .forEach(roleEntity -> {
- List<SysUserRoleRelationEntity> roleRelationEntityList = Lists.newArrayList();
- if (CollUtil.isNotEmpty(relationEntityList)) {
- roleRelationEntityList.addAll(
- relationEntityList.stream()
- .filter(relationEntity -> relationEntity.getRoleId().equals(roleEntity.getId()))
- .toList());
- }
- List<SysUserEntity> sysUserEntities = Lists.newArrayList();
- if (CollUtil.isNotEmpty(roleRelationEntityList) && CollUtil.isNotEmpty(sysUserEntityList)) {
- List<Long> userIds =
- roleRelationEntityList.stream()
- .map(SysUserRoleRelationEntity::getUserId)
- .distinct().toList();
- sysUserEntities.addAll(
- sysUserEntityList.stream()
- .filter(x -> userIds.contains(x.getId()))
- .toList());
- }
- //处理数据
- userRoleRelationBOList.add(
- this.handleResultData(roleEntity, groupEntityList, roleRelationEntityList, sysUserEntities));
- });
- }
- return userRoleRelationBOList;
- }
- /**
- * 处理数据
- *
- * @param roleEntity
- * @param groupEntityList
- * @param roleRelationEntityList
- * @param sysUserEntities
- * @return
- */
- public UserRoleRelationBO handleResultData(SysRoleEntity roleEntity,
- List<SysGroupEntity> groupEntityList,
- List<SysUserRoleRelationEntity> roleRelationEntityList,
- List<SysUserEntity> sysUserEntities) {
- UserRoleRelationBO roleRelationBO = UserRoleRelationBO.builder().build();
- roleRelationBO.setRoleId(roleEntity.getId());
- roleRelationBO.setRoleName(roleEntity.getTitle());
- roleRelationBO.setSortCode(roleEntity.getSortCode());
- //处理分组数据
- List<SysGroupBO> groupData = Lists.newArrayList();
- if (roleEntity.getValue().equals("GroupManager")) {
- groupEntityList.stream()
- .sorted(Comparator.comparing(SysGroupEntity::getSortCode))
- .forEach(groupEntity -> {
- SysGroupBO sysGroupBO = SysGroupBO.builder().build();
- sysGroupBO.setId(groupEntity.getId());
- sysGroupBO.setTitle(groupEntity.getTitle());
- sysGroupBO.setSortCode(groupEntity.getSortCode());
- groupData.add(sysGroupBO);
- });
- }
- roleRelationBO.setGroupData(groupData);
- //处理用户数据
- List<RoleUserBO> userData = Lists.newArrayList();
- if (CollUtil.isNotEmpty(roleRelationEntityList)) {
- if (CollUtil.isNotEmpty(groupData)) {
- groupData.stream()
- .sorted(Comparator.comparing(SysGroupBO::getSortCode))
- .forEach(sysGroupBO -> {
- List<SysUserRoleRelationEntity> userRoleRelationEntities =
- roleRelationEntityList.stream()
- .filter(x -> x.getGroupId().equals(sysGroupBO.getId()))
- .toList();
- if (CollUtil.isNotEmpty(userRoleRelationEntities)) {
- //处理用户数据
- userData.addAll(
- this.handleUserData(userRoleRelationEntities, sysUserEntities));
- }
- });
- } else {
- //处理用户数据
- userData.addAll(
- this.handleUserData(roleRelationEntityList, sysUserEntities));
- }
- }
- roleRelationBO.setUserData(userData);
- return roleRelationBO;
- }
- /**
- * 处理用户数据
- *
- * @param roleRelationEntityList
- * @param sysUserEntities
- * @return
- */
- public List<RoleUserBO> handleUserData(List<SysUserRoleRelationEntity> roleRelationEntityList,
- List<SysUserEntity> sysUserEntities) {
- List<RoleUserBO> userData = Lists.newArrayList();
- roleRelationEntityList.stream()
- .sorted(Comparator.comparing(HelioBaseEntity::getCreatedAt))
- .forEach(roleRelation -> {
- if (CollUtil.isNotEmpty(sysUserEntities)) {
- sysUserEntities.stream()
- .filter(x -> x.getId().equals(roleRelation.getUserId()))
- .findFirst()
- .ifPresent(user -> {
- if (CollUtil.isEmpty(userData) || userData.stream().map(RoleUserBO::getUserId).noneMatch(user.getId()::equals)) {
- RoleUserBO roleUserBO = RoleUserBO.builder().build();
- roleUserBO.setId(roleRelation.getId());
- roleUserBO.setUserId(user.getId());
- roleUserBO.setUserName(user.getUserName());
- roleUserBO.setAccount(user.getPin());
- roleUserBO.setGroupId(roleRelation.getGroupId());
- userData.add(roleUserBO);
- }
- });
- }
- });
- return userData;
- }
- /**
- * 后台管理-新增
- * 注:本方法较为特殊,仅供SysTenantFacadeImpl调用
- */
- @Transactional(rollbackFor = Exception.class)
- public Long adminInsert(UserRoleRelationInsertDTO dto) {
- long count =
- sysUserRoleRelationMapper.selectCount(
- new LambdaQueryWrapper<>(SysUserRoleRelationEntity.class)
- .select(SysUserRoleRelationEntity::getUserId)
- .eq(SysUserRoleRelationEntity::getRoleId, dto.getRoleId())
- .eq(SysUserRoleRelationEntity::getUserId, dto.getUserId())
- .eq(dto.getGroupId() != null, SysUserRoleRelationEntity::getGroupId, dto.getGroupId())
- );
- if (count > 0) {
- throw new BusinessException(400, "请勿重复设置");
- }
- SysRoleEntity sysRoleEntity = sysRoleMapper.selectById(dto.getRoleId());
- SysUserRoleRelationEntity entity = new SysUserRoleRelationEntity()
- .setUserId(dto.getUserId()).setRoleId(dto.getRoleId()).setGroupId(dto.getGroupId()).setRoleValue(sysRoleEntity.getValue());
- sysUserRoleRelationMapper.insert(entity);
- CrmAdminStpUtil.kickout(dto.getUserId());
- this.updateUserRole(dto.getUserId());
- // if (sysRoleEntity.getValue().equals("Admin")) {
- // CrmAdminStpUtil.kickout(dto.getUserId());
- // }
- // else {
- // //异步执行更新用户角色问题
- // asyncVThreadConfiguration.getAsyncExecutor().execute(() ->
- // this.updateUserRole(dto.getUserId()));
- // }
- return entity.getId();
- }
- @Transactional(rollbackFor = Exception.class)
- public void adminInsertGroupUser(UserRoleRelationInsertDTO dto) {
- long count =
- sysUserRoleRelationMapper.selectCount(
- new LambdaQueryWrapper<>(SysUserRoleRelationEntity.class)
- .select(SysUserRoleRelationEntity::getUserId)
- .eq(SysUserRoleRelationEntity::getRoleId, dto.getRoleId())
- .eq(SysUserRoleRelationEntity::getUserId, dto.getUserId())
- .eq(dto.getGroupId() != null, SysUserRoleRelationEntity::getGroupId, dto.getGroupId())
- );
- if (count == 0) {
- SysRoleEntity sysRoleEntity = sysRoleMapper.selectById(dto.getRoleId());
- SysUserRoleRelationEntity entity = new SysUserRoleRelationEntity()
- .setUserId(dto.getUserId()).setRoleId(dto.getRoleId()).setGroupId(dto.getGroupId()).setRoleValue(sysRoleEntity.getValue());
- sysUserRoleRelationMapper.insert(entity);
- this.updateUserRole(dto.getUserId());
- // if (sysRoleEntity.getValue().equals("Admin")) {
- // CrmAdminStpUtil.kickout(dto.getUserId());
- // } else {
- // //异步执行更新用户角色问题
- // asyncVThreadConfiguration.getAsyncExecutor().execute(() ->
- // this.updateUserRole(dto.getUserId()));
- // }
- }
- }
- @Transactional(rollbackFor = Exception.class)
- public void delete(Long id) {
- SysUserRoleRelationEntity roleRelationEntity = sysUserRoleRelationMapper.selectById(id);
- if (roleRelationEntity != null) {
- sysUserRoleRelationMapper.deleteById(id);
- //如果处理的是管理员,需要重新登陆
- CrmAdminStpUtil.kickout(roleRelationEntity.getUserId());
- // this.updateUserRole(roleRelationEntity.getUserId());
- // if (roleRelationEntity.getRoleValue().equals("Admin")) {
- // CrmAdminStpUtil.kickout(roleRelationEntity.getUserId());
- // } else {
- // //异步执行更新用户角色问题
- // asyncVThreadConfiguration.getAsyncExecutor().execute(() ->
- // this.updateUserRole(roleRelationEntity.getUserId()));
- // }
- }
- }
- @Transactional(rollbackFor = Exception.class)
- public void delete(Long userId, Long groupId, String roleValue) {
- sysUserRoleRelationMapper.delete(
- new LambdaQueryWrapper<>(SysUserRoleRelationEntity.class)
- .eq(SysUserRoleRelationEntity::getUserId, userId)
- .eq(SysUserRoleRelationEntity::getGroupId, groupId)
- .eq(SysUserRoleRelationEntity::getRoleValue, roleValue)
- );
- }
- }
|