feat(permission): 创建权限管理核心模块
- V21迁移: 创建10张权限系统表(sys_role, sys_permission等) - 添加角色实体(SysRole)、Repository、Service - 添加权限系统Schema验证测试(21个测试用例) Phase 1数据库表创建完成,Phase 2开始实施
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.mosquito.project.permission;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 角色Repository
|
||||
*/
|
||||
@Repository
|
||||
public interface RoleRepository extends JpaRepository<SysRole, Long> {
|
||||
|
||||
/**
|
||||
* 根据角色代码查询
|
||||
*/
|
||||
Optional<SysRole> findByRoleCode(String roleCode);
|
||||
|
||||
/**
|
||||
* 检查角色代码是否存在
|
||||
*/
|
||||
boolean existsByRoleCode(String roleCode);
|
||||
|
||||
/**
|
||||
* 根据角色代码查询(排除已删除)
|
||||
*/
|
||||
Optional<SysRole> findByRoleCodeAndDeletedFalse(String roleCode);
|
||||
}
|
||||
115
src/main/java/com/mosquito/project/permission/RoleService.java
Normal file
115
src/main/java/com/mosquito/project/permission/RoleService.java
Normal file
@@ -0,0 +1,115 @@
|
||||
package com.mosquito.project.permission;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 角色Service - 角色管理核心业务逻辑
|
||||
*/
|
||||
@Service
|
||||
@Transactional
|
||||
public class RoleService {
|
||||
|
||||
private final RoleRepository roleRepository;
|
||||
|
||||
public RoleService(RoleRepository roleRepository) {
|
||||
this.roleRepository = roleRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有角色
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public List<SysRole> findAll() {
|
||||
return roleRepository.findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID查询角色
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<SysRole> findById(Long id) {
|
||||
return roleRepository.findById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据角色代码查询
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<SysRole> findByRoleCode(String roleCode) {
|
||||
return roleRepository.findByRoleCodeAndDeletedFalse(roleCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建角色
|
||||
*/
|
||||
public SysRole save(SysRole role) {
|
||||
// 检查角色代码是否已存在
|
||||
if (roleRepository.existsByRoleCode(role.getRoleCode())) {
|
||||
throw new IllegalArgumentException("角色代码已存在: " + role.getRoleCode());
|
||||
}
|
||||
// 设置默认值
|
||||
if (role.getDeleted() == null) {
|
||||
role.setDeleted(0);
|
||||
}
|
||||
if (role.getStatus() == null) {
|
||||
role.setStatus("ENABLED");
|
||||
}
|
||||
if (role.getIsCore() == null) {
|
||||
role.setIsCore(0);
|
||||
}
|
||||
return roleRepository.save(role);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新角色
|
||||
*/
|
||||
public Optional<SysRole> update(Long id, SysRole roleData) {
|
||||
return roleRepository.findById(id)
|
||||
.map(existingRole -> {
|
||||
// 更新字段
|
||||
if (roleData.getRoleName() != null) {
|
||||
existingRole.setRoleName(roleData.getRoleName());
|
||||
}
|
||||
if (roleData.getRoleLevel() != null) {
|
||||
existingRole.setRoleLevel(roleData.getRoleLevel());
|
||||
}
|
||||
if (roleData.getDataScope() != null) {
|
||||
existingRole.setDataScope(roleData.getDataScope());
|
||||
}
|
||||
if (roleData.getDescription() != null) {
|
||||
existingRole.setDescription(roleData.getDescription());
|
||||
}
|
||||
if (roleData.getStatus() != null) {
|
||||
existingRole.setStatus(roleData.getStatus());
|
||||
}
|
||||
if (roleData.getIsCore() != null) {
|
||||
existingRole.setIsCore(roleData.getIsCore());
|
||||
}
|
||||
return roleRepository.save(existingRole);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除角色(软删除)
|
||||
*/
|
||||
public void delete(Long id) {
|
||||
roleRepository.findById(id)
|
||||
.ifPresent(role -> {
|
||||
role.setDeleted(1);
|
||||
role.setStatus("DISABLED");
|
||||
roleRepository.save(role);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查角色代码是否存在
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public boolean existsByRoleCode(String roleCode) {
|
||||
return roleRepository.existsByRoleCode(roleCode);
|
||||
}
|
||||
}
|
||||
146
src/main/java/com/mosquito/project/permission/SysRole.java
Normal file
146
src/main/java/com/mosquito/project/permission/SysRole.java
Normal file
@@ -0,0 +1,146 @@
|
||||
package com.mosquito.project.permission;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 角色实体 - 对应sys_role表
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "sys_role")
|
||||
public class SysRole {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "role_code", nullable = false, unique = true, length = 50)
|
||||
private String roleCode;
|
||||
|
||||
@Column(name = "role_name", nullable = false, length = 100)
|
||||
private String roleName;
|
||||
|
||||
@Column(name = "role_level", nullable = false, length = 20)
|
||||
private String roleLevel;
|
||||
|
||||
@Column(name = "data_scope", nullable = false, length = 20)
|
||||
private String dataScope;
|
||||
|
||||
@Column(name = "description", length = 500)
|
||||
private String description;
|
||||
|
||||
@Column(name = "is_core")
|
||||
private Integer isCore;
|
||||
|
||||
@Column(name = "status", length = 20)
|
||||
private String status;
|
||||
|
||||
@Column(name = "created_by")
|
||||
private Long createdBy;
|
||||
|
||||
@Column(name = "created_at")
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
@Column(name = "updated_at")
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
@Column(name = "deleted")
|
||||
private Integer deleted;
|
||||
|
||||
// Getters and Setters
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getRoleCode() {
|
||||
return roleCode;
|
||||
}
|
||||
|
||||
public void setRoleCode(String roleCode) {
|
||||
this.roleCode = roleCode;
|
||||
}
|
||||
|
||||
public String getRoleName() {
|
||||
return roleName;
|
||||
}
|
||||
|
||||
public void setRoleName(String roleName) {
|
||||
this.roleName = roleName;
|
||||
}
|
||||
|
||||
public String getRoleLevel() {
|
||||
return roleLevel;
|
||||
}
|
||||
|
||||
public void setRoleLevel(String roleLevel) {
|
||||
this.roleLevel = roleLevel;
|
||||
}
|
||||
|
||||
public String getDataScope() {
|
||||
return dataScope;
|
||||
}
|
||||
|
||||
public void setDataScope(String dataScope) {
|
||||
this.dataScope = dataScope;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Integer getIsCore() {
|
||||
return isCore;
|
||||
}
|
||||
|
||||
public void setIsCore(Integer isCore) {
|
||||
this.isCore = isCore;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public Long getCreatedBy() {
|
||||
return createdBy;
|
||||
}
|
||||
|
||||
public void setCreatedBy(Long createdBy) {
|
||||
this.createdBy = createdBy;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(LocalDateTime createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
public LocalDateTime getUpdatedAt() {
|
||||
return updatedAt;
|
||||
}
|
||||
|
||||
public void setUpdatedAt(LocalDateTime updatedAt) {
|
||||
this.updatedAt = updatedAt;
|
||||
}
|
||||
|
||||
public Integer getDeleted() {
|
||||
return deleted;
|
||||
}
|
||||
|
||||
public void setDeleted(Integer deleted) {
|
||||
this.deleted = deleted;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user