mirror of
https://github.com/FatttSnake/Pinnacle-OA.git
synced 2026-04-05 23:11:24 +08:00
Added GroupManagement. Fixed some errors in RoleManagement.
This commit is contained in:
@@ -1,7 +1,15 @@
|
||||
package com.cfive.pinnacle.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.cfive.pinnacle.entity.Group;
|
||||
import com.cfive.pinnacle.entity.common.ResponseCode;
|
||||
import com.cfive.pinnacle.entity.common.ResponseResult;
|
||||
import com.cfive.pinnacle.service.IGroupService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -14,5 +22,51 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@RestController
|
||||
@RequestMapping("/group")
|
||||
public class GroupController {
|
||||
private IGroupService groupService;
|
||||
|
||||
@Autowired
|
||||
public void setGroupService(IGroupService groupService) {
|
||||
this.groupService = groupService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseResult getAllGroup() {
|
||||
List<Group> groups = groupService.getAllGroup();
|
||||
return ResponseResult.build(ResponseCode.DATABASE_SELECT_OK, "success", groups);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseResult addGroup(@RequestBody Group group) {
|
||||
if (!StringUtils.hasText(group.getName())) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_SAVE_ERROR, "Name cannot be empty", null);
|
||||
}
|
||||
if (groupService.addGroup(group)) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_SAVE_OK, "success", null);
|
||||
} else {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_SAVE_ERROR, "error", null);
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseResult deleteGroup(@PathVariable Long id) {
|
||||
LambdaQueryWrapper<Group> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Group::getId, id);
|
||||
if (groupService.remove(wrapper)) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_DELETE_OK, "success", null);
|
||||
} else {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_DELETE_ERROR, "error", null);
|
||||
}
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public ResponseResult modifyGroup(@RequestBody Group group) {
|
||||
if (!StringUtils.hasText(group.getName())) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_UPDATE_ERROR, "Name cannot be empty", null);
|
||||
}
|
||||
if (groupService.modifyGroup(group)) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_UPDATE_OK, "success", null);
|
||||
} else {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_UPDATE_ERROR, "error", null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.annotation.Version;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
@@ -47,4 +48,7 @@ public class Group implements Serializable {
|
||||
@TableField("version")
|
||||
@Version
|
||||
private Integer version;
|
||||
|
||||
@TableField(exist = false)
|
||||
private List<Role> roles;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ package com.cfive.pinnacle.mapper;
|
||||
import com.cfive.pinnacle.entity.Group;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -14,5 +17,7 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
*/
|
||||
@Mapper
|
||||
public interface GroupMapper extends BaseMapper<Group> {
|
||||
List<Group> getAll();
|
||||
|
||||
Group getOneById(@Param("id") long id);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.cfive.pinnacle.service;
|
||||
import com.cfive.pinnacle.entity.Group;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 用户组 服务类
|
||||
@@ -12,5 +14,11 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||
* @since 2023-04-30
|
||||
*/
|
||||
public interface IGroupService extends IService<Group> {
|
||||
List<Group> getAllGroup();
|
||||
|
||||
Group getGroup(Long id);
|
||||
|
||||
boolean addGroup(Group group);
|
||||
|
||||
boolean modifyGroup(Group group);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
package com.cfive.pinnacle.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.cfive.pinnacle.entity.Group;
|
||||
import com.cfive.pinnacle.entity.RoleGroup;
|
||||
import com.cfive.pinnacle.mapper.GroupMapper;
|
||||
import com.cfive.pinnacle.mapper.RoleGroupMapper;
|
||||
import com.cfive.pinnacle.service.IGroupService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 用户组 服务实现类
|
||||
@@ -16,5 +23,73 @@ import org.springframework.stereotype.Service;
|
||||
*/
|
||||
@Service
|
||||
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements IGroupService {
|
||||
private GroupMapper groupMapper;
|
||||
private RoleGroupMapper roleGroupMapper;
|
||||
|
||||
@Autowired
|
||||
public void setGroupMapper(GroupMapper groupMapper) {
|
||||
this.groupMapper = groupMapper;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setRoleGroupMapper(RoleGroupMapper roleGroupMapper) {
|
||||
this.roleGroupMapper = roleGroupMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Group> getAllGroup() {
|
||||
return groupMapper.getAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Group getGroup(Long id) {
|
||||
return groupMapper.getOneById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addGroup(Group group) {
|
||||
if (groupMapper.insert(group) == 1) {
|
||||
group.getRoles().forEach(role -> {
|
||||
RoleGroup roleGroup = new RoleGroup();
|
||||
roleGroup.setGroupId(group.getId());
|
||||
roleGroup.setRoleId(role.getId());
|
||||
if (roleGroupMapper.insert(roleGroup) != 1) {
|
||||
throw new RuntimeException("Add role_group failure");
|
||||
}
|
||||
});
|
||||
return true;
|
||||
} else {
|
||||
throw new RuntimeException("Add group failure");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean modifyGroup(Group group) {
|
||||
groupMapper.updateById(group);
|
||||
Group originalGroup = getGroup(group.getId());
|
||||
HashSet<Long> newRoleIds = new HashSet<>();
|
||||
group.getRoles().forEach(role -> newRoleIds.add(role.getId()));
|
||||
HashSet<Long> addRoleIds = new HashSet<>(newRoleIds);
|
||||
if (originalGroup != null) {
|
||||
HashSet<Long> originalRoleIds = new HashSet<>();
|
||||
originalGroup.getRoles().forEach(role -> originalRoleIds.add(role.getId()));
|
||||
HashSet<Long> deleteRoleIds = new HashSet<>(originalRoleIds);
|
||||
deleteRoleIds.removeAll(newRoleIds);
|
||||
addRoleIds.removeAll(originalRoleIds);
|
||||
deleteRoleIds.forEach(deleteRoleId -> {
|
||||
LambdaQueryWrapper<RoleGroup> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(RoleGroup::getGroupId, group.getId())
|
||||
.eq(RoleGroup::getRoleId, deleteRoleId);
|
||||
roleGroupMapper.delete(wrapper);
|
||||
});
|
||||
}
|
||||
addRoleIds.forEach(addRoleId -> {
|
||||
RoleGroup roleGroup = new RoleGroup();
|
||||
roleGroup.setGroupId(group.getId());
|
||||
roleGroup.setRoleId(addRoleId);
|
||||
roleGroupMapper.insert(roleGroup);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,4 +2,51 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.cfive.pinnacle.mapper.GroupMapper">
|
||||
|
||||
<select id="getAll" resultMap="groupMap">
|
||||
select t_group.id as group_id,
|
||||
t_group.name as group_name,
|
||||
t_group.deleted as group_deleted,
|
||||
t_group.version as group_version,
|
||||
tr.id as role_id,
|
||||
tr.name as role_name,
|
||||
tr.deleted as role_deleted,
|
||||
tr.version as role_version
|
||||
from t_group
|
||||
left join t_role_group trg on t_group.id = trg.group_id
|
||||
left join t_role tr on tr.id = trg.role_id
|
||||
where t_group.deleted = 0
|
||||
and (tr.deleted = 0 or tr.deleted is null)
|
||||
and (trg.deleted = 0 or trg.deleted is null)
|
||||
</select>
|
||||
|
||||
<select id="getOneById" resultMap="groupMap">
|
||||
select t_group.id as group_id,
|
||||
t_group.name as group_name,
|
||||
t_group.deleted as group_deleted,
|
||||
t_group.version as group_version,
|
||||
tr.id as role_id,
|
||||
tr.name as role_name,
|
||||
tr.deleted as role_deleted,
|
||||
tr.version as role_version
|
||||
from t_group
|
||||
left join t_role_group trg on t_group.id = trg.group_id
|
||||
left join t_role tr on tr.id = trg.role_id
|
||||
where t_group.deleted = 0
|
||||
and (tr.deleted = 0 or tr.deleted is null)
|
||||
and (trg.deleted = 0 or trg.deleted is null)
|
||||
and t_group.id = #{id}
|
||||
</select>
|
||||
|
||||
<resultMap id="groupMap" type="group">
|
||||
<id property="id" column="group_id"/>
|
||||
<result property="name" column="group_name"/>
|
||||
<result property="deleted" column="group_deleted"/>
|
||||
<result property="version" column="group_version"/>
|
||||
<collection property="roles" ofType="role">
|
||||
<id property="id" column="role_id"/>
|
||||
<result property="name" column="role_name"/>
|
||||
<result property="deleted" column="role_deleted"/>
|
||||
<result property="version" column="role_version"/>
|
||||
</collection>
|
||||
</resultMap>
|
||||
</mapper>
|
||||
|
||||
@@ -3,32 +3,33 @@
|
||||
<mapper namespace="com.cfive.pinnacle.mapper.RoleMapper">
|
||||
|
||||
<select id="getAll" resultMap="roleMap">
|
||||
select t_role.id as role_id,
|
||||
t_role.name as role_name,
|
||||
select t_role.id as role_id,
|
||||
t_role.name as role_name,
|
||||
t_role.deleted as role_deleted,
|
||||
t_role.version as role_version,
|
||||
tm.id as menu_id,
|
||||
tm.name as menu_name,
|
||||
tm.url as menu_url,
|
||||
tm.power_id as menu_power_id,
|
||||
tm.parent_id as menu_parent_id,
|
||||
te.id as element_id,
|
||||
te.name as element_name,
|
||||
te.power_id as element_power_id,
|
||||
te.menu_id as element_menu_id,
|
||||
t.id as operation_id,
|
||||
t.name as operation_name,
|
||||
t.code as operation_code,
|
||||
t.power_id as operation_power_id,
|
||||
t.element_id as operation_element_id,
|
||||
t.parent_id as operation_parent_id
|
||||
tm.id as menu_id,
|
||||
tm.name as menu_name,
|
||||
tm.url as menu_url,
|
||||
tm.power_id as menu_power_id,
|
||||
tm.parent_id as menu_parent_id,
|
||||
te.id as element_id,
|
||||
te.name as element_name,
|
||||
te.power_id as element_power_id,
|
||||
te.menu_id as element_menu_id,
|
||||
t.id as operation_id,
|
||||
t.name as operation_name,
|
||||
t.code as operation_code,
|
||||
t.power_id as operation_power_id,
|
||||
t.element_id as operation_element_id,
|
||||
t.parent_id as operation_parent_id
|
||||
from t_role
|
||||
left join t_power_role tpr on t_role.id = tpr.role_id
|
||||
left join t_power tp on tp.id = tpr.power_id
|
||||
left join t_menu tm on tp.id = tm.power_id
|
||||
left join t_element te on tp.id = te.power_id
|
||||
left join t_operation t on tp.id = t.power_id
|
||||
where t_role.deleted = 0 and (tpr.deleted = 0 or tpr.deleted is null);
|
||||
where t_role.deleted = 0
|
||||
and (tpr.deleted = 0 or tpr.deleted is null);
|
||||
</select>
|
||||
<select id="getOneById" resultMap="roleMap">
|
||||
select t_role.id as role_id,
|
||||
@@ -57,7 +58,7 @@
|
||||
left join t_element te on tp.id = te.power_id
|
||||
left join t_operation t on tp.id = t.power_id
|
||||
where t_role.deleted = 0
|
||||
and tpr.deleted = 0
|
||||
and (tpr.deleted = 0 or tpr.deleted is null)
|
||||
and t_role.id = #{id};
|
||||
</select>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user