自定义组件代码bug解决

This commit is contained in:
2022-09-13 14:39:32 +08:00
parent 56182aa79c
commit da281bfde5
11 changed files with 93 additions and 28 deletions

View File

@@ -1,6 +1,7 @@
package com.njcn.user.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.njcn.user.pojo.po.Role;
import com.njcn.user.pojo.po.UserRole;
import org.apache.ibatis.annotations.Param;
@@ -21,4 +22,14 @@ public interface UserRoleMapper extends BaseMapper<UserRole> {
* @return
*/
List<UserRole> selectUserRole(@Param("ids")List<String> ids);
/**
* 根据用户id获取角色详情
* @param userId 用户id
* @author cdf
* @date 2022/9/8
* @return 角色结果集
*/
List<Role> getRoleListByUserId(String userId);
}

View File

@@ -10,4 +10,12 @@
#{item}
</foreach>
</select>
<select id="getRoleListByUserId" resultType="Role">
select b.* from sys_user_role a
inner join sys_role b on a.role_id = b.id
where a.user_id = #{userId}
and b.state = 1
</select>
</mapper>

View File

@@ -78,4 +78,6 @@ public interface IRoleService extends IService<Role> {
boolean deleteRole(List<String> ids);
Boolean selectRelevance(List<String> ids);
}

View File

@@ -1,6 +1,7 @@
package com.njcn.user.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.njcn.user.pojo.po.Role;
import com.njcn.user.pojo.po.UserRole;
import java.util.List;
@@ -45,4 +46,14 @@ public interface IUserRoleService extends IService<UserRole> {
* @date 2022/1/13 14:14
*/
boolean updateUserRole(String id,List<String> roles);
/**
* 根据用户id获取角色详情
* @param userId 用户id
* @author cdf
* @date 2022/9/8
* @return 角色结果集
*/
List<Role> getRoleListByUserId(String userId);
}

View File

@@ -11,10 +11,12 @@ import com.njcn.user.pojo.constant.FunctionState;
import com.njcn.user.pojo.dto.ComponentDTO;
import com.njcn.user.pojo.param.ComponentParam;
import com.njcn.user.pojo.po.Component;
import com.njcn.user.pojo.po.Role;
import com.njcn.user.pojo.vo.ComponentVO;
import com.njcn.user.service.IComponentService;
import com.njcn.user.service.IRoleComponentService;
import com.njcn.user.service.IRoleService;
import com.njcn.user.service.IUserRoleService;
import com.njcn.web.utils.RequestUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
@@ -26,7 +28,7 @@ import java.util.stream.Stream;
/**
* <p>
* 服务实现类
* 服务实现类
* </p>
*
* @author hongawen
@@ -40,25 +42,27 @@ public class ComponentServiceImpl extends ServiceImpl<ComponentMapper, Component
private final IRoleService roleService;
private final IUserRoleService iUserRoleService;
private final IRoleComponentService roleComponentService;
@Override
public boolean addComponent(ComponentParam componentParam) {
checkComponentParam(componentParam,false);
checkComponentParam(componentParam, false);
Component component = new Component();
BeanUtil.copyProperties(componentParam, component);
String functionGroup = componentParam.getFunctionGroup().stream().map(String::valueOf).collect(Collectors.joining(","));
component.setFunctionGroup(functionGroup);
component.setState(ComponentState.ENABLE);
if (Objects.equals(componentParam.getPid(), FunctionState.FATHER_PID)){
if (Objects.equals(componentParam.getPid(), FunctionState.FATHER_PID)) {
component.setPids(FunctionState.FATHER_PID);
} else {
Component fatherComponent = this.lambdaQuery().eq(Component::getId,componentParam.getPid()).one();
if (Objects.equals(fatherComponent.getPid(),FunctionState.FATHER_PID)){
Component fatherComponent = this.lambdaQuery().eq(Component::getId, componentParam.getPid()).one();
if (Objects.equals(fatherComponent.getPid(), FunctionState.FATHER_PID)) {
component.setPids(componentParam.getPid());
} else {
String pidS = fatherComponent.getPids();
component.setPids(pidS+","+componentParam.getPid());
component.setPids(pidS + "," + componentParam.getPid());
}
}
return this.save(component);
@@ -74,7 +78,7 @@ public class ComponentServiceImpl extends ServiceImpl<ComponentMapper, Component
@Override
public boolean updateComponent(ComponentParam.ComponentUpdateParam componentParam) {
checkComponentParam(componentParam,true);
checkComponentParam(componentParam, true);
Component component = new Component();
BeanUtil.copyProperties(componentParam, component);
String functionGroup = componentParam.getFunctionGroup().stream().map(String::valueOf).collect(Collectors.joining(","));
@@ -86,14 +90,14 @@ public class ComponentServiceImpl extends ServiceImpl<ComponentMapper, Component
public List<ComponentVO> getComponentTree() {
List<ComponentVO> list = new ArrayList<>();
List<ComponentDTO> componentList = componentMapper.getAllComponent();
componentList.forEach(item->{
componentList.forEach(item -> {
ComponentVO componentVO = new ComponentVO();
BeanUtil.copyProperties(item, componentVO);
componentVO.setFunctionGroup(Arrays.stream(item.getFunctionGroup().split(",")).map(s -> Integer.valueOf(s.trim())).collect(Collectors.toList()));
list.add(componentVO);
});
return list.stream()
.filter(fun -> Objects.equals(ComponentState.FATHER_PID,fun.getPid()))
.filter(fun -> Objects.equals(ComponentState.FATHER_PID, fun.getPid()))
.peek(funS -> funS.setChildren(getChildCategoryList(funS, list)))
.collect(Collectors.toList());
}
@@ -102,31 +106,35 @@ public class ComponentServiceImpl extends ServiceImpl<ComponentMapper, Component
public List<ComponentVO> getUserComponentTree() {
List<ComponentVO> result = new ArrayList<>();
List<ComponentVO> componentVOList = new ArrayList<>();
List<String> roleList = roleService.getIdByUserId(RequestUtil.getUserIndex());
if (!CollectionUtils.isEmpty(roleList)){
List<String> componentList = roleComponentService.selectRoleComponent(roleList);
if (!CollectionUtils.isEmpty(componentList)){
List<ComponentDTO> list = componentMapper.getComponentByList(componentList);
list.forEach(item->{
ComponentVO componentVO = new ComponentVO();
BeanUtil.copyProperties(item, componentVO);
componentVO.setFunctionGroup(Arrays.stream(item.getFunctionGroup().split(",")).map(s -> Integer.valueOf(s.trim())).collect(Collectors.toList()));
componentVOList.add(componentVO);
});
result = componentVOList.stream()
.filter(fun -> Objects.equals(ComponentState.FATHER_PID,fun.getPid()))
.peek(funS -> funS.setChildren(getChildCategoryList(funS, componentVOList)))
.collect(Collectors.toList());
List<Role> roleList = iUserRoleService.getRoleListByUserId(RequestUtil.getUserIndex());
if (!CollectionUtils.isEmpty(roleList)) {
List<ComponentDTO> list;
if (roleList.stream().anyMatch(item -> "root".equals(item.getCode()))) {
list = componentMapper.getAllComponent();
} else {
List<String> componentList = roleComponentService.selectRoleComponent(roleList.stream().map(Role::getId).collect(Collectors.toList()));
list = componentMapper.getComponentByList(componentList);
}
list.forEach(item -> {
ComponentVO componentVO = new ComponentVO();
BeanUtil.copyProperties(item, componentVO);
componentVO.setFunctionGroup(Arrays.stream(item.getFunctionGroup().split(",")).map(s -> Integer.valueOf(s.trim())).collect(Collectors.toList()));
componentVOList.add(componentVO);
});
result = componentVOList.stream()
.filter(fun -> Objects.equals(ComponentState.FATHER_PID, fun.getPid()))
.peek(funS -> funS.setChildren(getChildCategoryList(funS, componentVOList)))
.collect(Collectors.toList());
}
return result;
}
/**
* 递归组装组件表
* 递归组装组件表
*/
private List<ComponentVO> getChildCategoryList(ComponentVO currMenu, List<ComponentVO> categories) {
return categories.stream().filter(o -> Objects.equals(o.getPid(),currMenu.getId()))
return categories.stream().filter(o -> Objects.equals(o.getPid(), currMenu.getId()))
.peek(o -> o.setChildren(getChildCategoryList(o, categories)))
.collect(Collectors.toList());
}

View File

@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.njcn.user.mapper.UserMapper;
import com.njcn.user.mapper.UserRoleMapper;
import com.njcn.user.pojo.po.Role;
import com.njcn.user.pojo.po.UserRole;
import com.njcn.user.service.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -94,4 +95,9 @@ public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> i
// }
return true;
}
@Override
public List<Role> getRoleListByUserId(String userId) {
return this.baseMapper.getRoleListByUserId(userId);
}
}