权限服务代码调整
This commit is contained in:
parent
0aeddb7de1
commit
0f719c9356
19
das/src/main/java/com/das/common/config/SysAuthorityIds.java
Normal file
19
das/src/main/java/com/das/common/config/SysAuthorityIds.java
Normal file
@ -0,0 +1,19 @@
|
||||
package com.das.common.config;
|
||||
|
||||
/**
|
||||
* 系统权限ID定义列表
|
||||
*/
|
||||
public class SysAuthorityIds {
|
||||
/**
|
||||
* 系统管理权限
|
||||
*/
|
||||
public static final Long SYS_AUTHORITY_ID_ADMIN = 1000L;
|
||||
/**
|
||||
* 设备台账维护权限
|
||||
*/
|
||||
public static final Long SYS_AUTHORITY_ID_DEVICE_MGR = 1001L;
|
||||
/**
|
||||
* 设备台账浏览权限
|
||||
*/
|
||||
public static final Long SYS_AUTHORITY_ID_DEVICE_VIEW = 1002L;
|
||||
}
|
@ -3,6 +3,8 @@ package com.das.modules.auth.mapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.das.modules.auth.entity.SysAuthority;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -13,4 +15,7 @@ import com.das.modules.auth.entity.SysAuthority;
|
||||
*/
|
||||
public interface SysAuthorityMapper extends BaseMapper<SysAuthority> {
|
||||
|
||||
List<SysAuthority> selectByAuthorities(List<SysAuthority> list);
|
||||
|
||||
void batchInsert(List<SysAuthority> list);
|
||||
}
|
||||
|
@ -3,9 +3,9 @@ package com.das.modules.auth.service;
|
||||
import com.das.modules.auth.domain.dto.SysAuthorityDto;
|
||||
import com.das.modules.auth.entity.SysAuthority;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public interface SysAuthorityService {
|
||||
SysAuthority createAuthority(SysAuthorityDto sysAuthorityDto);
|
||||
|
||||
List<SysAuthority> queryAll();
|
||||
}
|
||||
|
@ -1,31 +1,78 @@
|
||||
package com.das.modules.auth.service.impl;
|
||||
|
||||
|
||||
import com.das.common.config.SysAuthorityIds;
|
||||
import com.das.common.utils.BeanCopyUtils;
|
||||
import com.das.common.utils.SequenceUtils;
|
||||
import com.das.modules.auth.domain.dto.SysAuthorityDto;
|
||||
import com.das.modules.auth.entity.SysAuthority;
|
||||
import com.das.modules.auth.mapper.SysAuthorityMapper;
|
||||
import com.das.modules.auth.service.SysAuthorityService;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SysAuthorityServiceImpl implements SysAuthorityService {
|
||||
@Autowired
|
||||
SysAuthorityMapper sysAuthorityMapper;
|
||||
// 在类中声明一个日志对象
|
||||
|
||||
|
||||
/**
|
||||
* 初始化系统权限
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
List<SysAuthority> list = new ArrayList<>();
|
||||
list.add(new SysAuthority(SysAuthorityIds.SYS_AUTHORITY_ID_ADMIN,"SYS_AUTHORITY_ID_ADMIN","系统管理权限",1));
|
||||
list.add(new SysAuthority(SysAuthorityIds.SYS_AUTHORITY_ID_ADMIN,"SYS_AUTHORITY_ID_DEVICE_MGR","设备台账维护权限",1));
|
||||
list.add(new SysAuthority(SysAuthorityIds.SYS_AUTHORITY_ID_ADMIN,"SYS_AUTHORITY_ID_DEVICE_VIEW","设备台账浏览权限",1));
|
||||
|
||||
try {
|
||||
// 性能优化:先查询所有需要的权限是否存在,减少数据库访问次数
|
||||
List<SysAuthority> existingAuthorities = sysAuthorityMapper.selectByAuthorities(list);
|
||||
|
||||
// 过滤出需要插入的权限
|
||||
list.removeAll(existingAuthorities);
|
||||
|
||||
// 批量插入不存在的权限
|
||||
if (!list.isEmpty()) {
|
||||
sysAuthorityMapper.batchInsert(list);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 异常处理:记录日志或执行其他恢复策略
|
||||
log.error(e.getMessage(), e); // 实际应用中应使用日志框架如Log4j记录错误信息
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回全部权限列表
|
||||
* 注意:此方法封装了对空结果的处理和基础的异常处理逻辑。
|
||||
* @return 权限列表,如果没有找到任何权限,则返回一个空列表
|
||||
*/
|
||||
@Override
|
||||
public SysAuthority createAuthority(SysAuthorityDto sysAuthorityDto) {
|
||||
SysAuthority sysAuthority = new SysAuthority();
|
||||
BeanCopyUtils.copy(sysAuthorityDto,sysAuthority);
|
||||
sysAuthority.setId(SequenceUtils.generateId());
|
||||
sysAuthority.setRevision(1);
|
||||
sysAuthority.setCreatedTime(new Date());
|
||||
sysAuthority.setUpdatedTime(new Date());
|
||||
sysAuthorityMapper.insert(sysAuthority);
|
||||
return sysAuthority;
|
||||
public List<SysAuthority> queryAll() {
|
||||
try {
|
||||
List<SysAuthority> authorities = sysAuthorityMapper.selectList(null);
|
||||
// 检查返回的结果是否为空,如果为空则返回一个空的列表
|
||||
if (authorities == null || authorities.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return authorities;
|
||||
} catch (Exception e) {
|
||||
// 处理可能的异常,例如数据库查询异常等
|
||||
// 根据你的项目实践,这里可以记录日志、抛出自定义异常或者进行其他处理
|
||||
// 以下是一个简单的示例,打印异常信息并返回空列表
|
||||
log.error("查询权限列表时发生异常:" + e.getMessage());
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user