Skip to content

Commit

Permalink
refactor: 优化系统管理相关代码及初始菜单脚本
Browse files Browse the repository at this point in the history
1.优化方法排序
2.优化前端模板
3.完善菜单
  • Loading branch information
Charles7c committed Nov 16, 2024
1 parent 4856366 commit 5717d03
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
*/
public interface RoleService extends BaseService<RoleResp, RoleDetailResp, RoleQuery, RoleReq>, IService<RoleDO> {

/**
* 分配角色给用户
*
* @param id 角色 ID
* @param userIds 用户 ID 列表
*/
void assignToUsers(Long id, List<Long> userIds);

/**
* 根据用户 ID 查询权限码
*
Expand Down Expand Up @@ -91,12 +99,4 @@ public interface RoleService extends BaseService<RoleResp, RoleDetailResp, RoleQ
* @return 角色数量
*/
int countByNames(List<String> roleNames);

/**
* 分配角色给用户
*
* @param id 角色 ID
* @param userIds 用户 ID 列表
*/
void assignToUsers(Long id, List<Long> userIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@
*/
public interface UserService extends BaseService<UserResp, UserDetailResp, UserQuery, UserReq>, IService<UserDO> {

/**
* 新增
*
* @param user 用户信息
* @return ID
*/
Long add(UserDO user);

/**
* 下载导入模板
*
Expand Down Expand Up @@ -132,6 +124,14 @@ public interface UserService extends BaseService<UserResp, UserDetailResp, UserQ
*/
void updateEmail(String newEmail, String oldPassword, Long id);

/**
* 新增
*
* @param user 用户信息
* @return ID
*/
Long add(UserDO user);

/**
* 根据用户名查询
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,44 +52,22 @@
@RequiredArgsConstructor
public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptResp, DeptResp, DeptQuery, DeptReq> implements DeptService {

private final RoleDeptService roleDeptService;
@Resource
private UserService userService;
private final RoleDeptService roleDeptService;
@Resource
private DataSource dataSource;

@Override
public List<DeptDO> listChildren(Long id) {
DatabaseType databaseType = MetaUtils.getDatabaseTypeOrDefault(dataSource, DatabaseType.MYSQL);
return baseMapper.lambdaQuery().apply(databaseType.findInSet(id, "ancestors")).list();
}

@Override
public List<DeptDO> listByNames(List<String> list) {
if (CollUtil.isEmpty(list)) {
return Collections.emptyList();
}
return this.list(Wrappers.<DeptDO>lambdaQuery().in(DeptDO::getName, list));
}

@Override
public int countByNames(List<String> deptNames) {
if (CollUtil.isEmpty(deptNames)) {
return 0;
}
return (int)this.count(Wrappers.<DeptDO>lambdaQuery().in(DeptDO::getName, deptNames));
}

@Override
protected void beforeAdd(DeptReq req) {
public void beforeAdd(DeptReq req) {
String name = req.getName();
boolean isExists = this.isNameExists(name, req.getParentId(), null);
CheckUtils.throwIf(isExists, "新增失败,[{}] 已存在", name);
req.setAncestors(this.getAncestors(req.getParentId()));
}

@Override
protected void beforeUpdate(DeptReq req, Long id) {
public void beforeUpdate(DeptReq req, Long id) {
String name = req.getName();
boolean isExists = this.isNameExists(name, req.getParentId(), id);
CheckUtils.throwIf(isExists, "修改失败,[{}] 已存在", name);
Expand Down Expand Up @@ -124,7 +102,7 @@ protected void beforeUpdate(DeptReq req, Long id) {
}

@Override
protected void beforeDelete(List<Long> ids) {
public void beforeDelete(List<Long> ids) {
List<DeptDO> list = baseMapper.lambdaQuery()
.select(DeptDO::getName, DeptDO::getIsSystem)
.in(DeptDO::getId, ids)
Expand All @@ -138,6 +116,28 @@ protected void beforeDelete(List<Long> ids) {
roleDeptService.deleteByDeptIds(ids);
}

@Override
public List<DeptDO> listChildren(Long id) {
DatabaseType databaseType = MetaUtils.getDatabaseTypeOrDefault(dataSource, DatabaseType.MYSQL);
return baseMapper.lambdaQuery().apply(databaseType.findInSet(id, "ancestors")).list();
}

@Override
public List<DeptDO> listByNames(List<String> list) {
if (CollUtil.isEmpty(list)) {
return Collections.emptyList();
}
return this.list(Wrappers.<DeptDO>lambdaQuery().in(DeptDO::getName, list));
}

@Override
public int countByNames(List<String> deptNames) {
if (CollUtil.isEmpty(deptNames)) {
return 0;
}
return (int)this.count(Wrappers.<DeptDO>lambdaQuery().in(DeptDO::getName, deptNames));
}

/**
* 名称是否存在
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
private static final Map<String, List<LabelValueResp>> ENUM_DICT_CACHE = new ConcurrentHashMap<>();

@Override
protected void beforeAdd(DictItemReq req) {
public void beforeAdd(DictItemReq req) {
String value = req.getValue();
CheckUtils.throwIf(this.isValueExists(value, null, req.getDictId()), "新增失败,字典值 [{}] 已存在", value);
RedisUtils.deleteByPattern(CacheConstants.DICT_KEY_PREFIX + StringConstants.ASTERISK);
}

@Override
protected void beforeUpdate(DictItemReq req, Long id) {
public void beforeUpdate(DictItemReq req, Long id) {
String value = req.getValue();
CheckUtils.throwIf(this.isValueExists(value, id, req.getDictId()), "修改失败,字典值 [{}] 已存在", value);
RedisUtils.deleteByPattern(CacheConstants.DICT_KEY_PREFIX + StringConstants.ASTERISK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ public class DictServiceImpl extends BaseServiceImpl<DictMapper, DictDO, DictRes
private final DictItemService dictItemService;

@Override
protected void beforeAdd(DictReq req) {
public void beforeAdd(DictReq req) {
String name = req.getName();
CheckUtils.throwIf(this.isNameExists(name, null), "新增失败,[{}] 已存在", name);
String code = req.getCode();
CheckUtils.throwIf(this.isCodeExists(code, null), "新增失败,[{}] 已存在", code);
}

@Override
protected void beforeUpdate(DictReq req, Long id) {
public void beforeUpdate(DictReq req, Long id) {
String name = req.getName();
CheckUtils.throwIf(this.isNameExists(name, id), "修改失败,[{}] 已存在", name);
DictDO oldDict = super.getById(id);
CheckUtils.throwIfNotEqual(req.getCode(), oldDict.getCode(), "不允许修改字典编码");
}

@Override
protected void beforeDelete(List<Long> ids) {
public void beforeDelete(List<Long> ids) {
List<DictDO> list = baseMapper.lambdaQuery()
.select(DictDO::getName, DictDO::getIsSystem)
.in(DictDO::getId, ids)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public Set<String> listPermissionByUserId(Long userId) {
public List<MenuResp> listByRoleCode(String roleCode) {
List<MenuDO> menuList = baseMapper.selectListByRoleCode(roleCode);
List<MenuResp> list = BeanUtil.copyToList(menuList, MenuResp.class);
list.forEach(this::fill);
list.forEach(super::fill);
return list;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void update(RoleReq req, Long id) {
}

@Override
protected void beforeDelete(List<Long> ids) {
public void beforeDelete(List<Long> ids) {
List<RoleDO> list = baseMapper.lambdaQuery()
.select(RoleDO::getName, RoleDO::getIsSystem)
.in(RoleDO::getId, ids)
Expand All @@ -122,7 +122,16 @@ protected void beforeDelete(List<Long> ids) {
}

@Override
protected void fill(Object obj) {
public void assignToUsers(Long id, List<Long> userIds) {
super.getById(id);
// 保存用户和角色关联
userRoleService.assignRoleToUsers(id, userIds);
// 更新用户上下文
this.updateUserContext(id);
}

@Override
public void fill(Object obj) {
super.fill(obj);
if (obj instanceof RoleDetailResp detail) {
Long roleId = detail.getId();
Expand Down Expand Up @@ -188,15 +197,6 @@ public int countByNames(List<String> roleNames) {
return (int)this.count(Wrappers.<RoleDO>lambdaQuery().in(RoleDO::getName, roleNames));
}

@Override
public void assignToUsers(Long id, List<Long> userIds) {
super.getById(id);
// 保存用户和角色关联
userRoleService.assignRoleToUsers(id, userIds);
// 更新用户上下文
this.updateUserContext(id);
}

/**
* 名称是否存在
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class StorageServiceImpl extends BaseServiceImpl<StorageMapper, StorageDO
private FileService fileService;

@Override
protected void beforeAdd(StorageReq req) {
public void beforeAdd(StorageReq req) {
this.decodeSecretKey(req, null);
CheckUtils.throwIf(Boolean.TRUE.equals(req.getIsDefault()) && this.isDefaultExists(null), "请先取消原有默认存储");
String code = req.getCode();
Expand All @@ -74,7 +74,7 @@ protected void beforeAdd(StorageReq req) {
}

@Override
protected void beforeUpdate(StorageReq req, Long id) {
public void beforeUpdate(StorageReq req, Long id) {
StorageDO oldStorage = super.getById(id);
CheckUtils.throwIfNotEqual(req.getCode(), oldStorage.getCode(), "不允许修改存储编码");
CheckUtils.throwIfNotEqual(req.getType(), oldStorage.getType(), "不允许修改存储类型");
Expand All @@ -99,7 +99,7 @@ protected void beforeUpdate(StorageReq req, Long id) {
}

@Override
protected void beforeDelete(List<Long> ids) {
public void beforeDelete(List<Long> ids) {
CheckUtils.throwIf(fileService.countByStorageIds(ids) > 0, "所选存储存在文件关联,请删除文件后重试");
List<StorageDO> storageList = baseMapper.lambdaQuery().in(StorageDO::getId, ids).list();
storageList.forEach(s -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,6 @@ public void delete(List<Long> ids) {
ids.forEach(onlineUserService::kickOut);
}

@Override
public Long add(UserDO user) {
user.setStatus(DisEnableStatusEnum.ENABLE);
baseMapper.insert(user);
return user.getId();
}

@Override
public void downloadImportTemplate(HttpServletResponse response) throws IOException {
try {
Expand Down Expand Up @@ -360,19 +353,6 @@ public UserImportResp importUser(UserImportReq req) {
return new UserImportResp(insertList.size() + updateList.size(), insertList.size(), updateList.size());
}

public void doImportUser(List<UserDO> insertList, List<UserDO> updateList, List<UserRoleDO> userRoleDOList) {
if (CollUtil.isNotEmpty(insertList)) {
baseMapper.insert(insertList);
}
if (CollUtil.isNotEmpty(updateList)) {
this.updateBatchById(updateList);
userRoleService.deleteByUserIds(updateList.stream().map(UserDO::getId).toList());
}
if (CollUtil.isNotEmpty(userRoleDOList)) {
userRoleService.saveBatch(userRoleDOList);
}
}

@Override
public void resetPassword(UserPasswordResetReq req, Long id) {
super.getById(id);
Expand Down Expand Up @@ -459,6 +439,13 @@ public void updateEmail(String newEmail, String oldPassword, Long id) {
baseMapper.lambdaUpdate().set(UserDO::getEmail, newEmail).eq(UserDO::getId, id).update();
}

@Override
public Long add(UserDO user) {
user.setStatus(DisEnableStatusEnum.ENABLE);
baseMapper.insert(user);
return user.getId();
}

@Override
public UserDO getByUsername(String username) {
return baseMapper.selectByUsername(username);
Expand Down Expand Up @@ -523,6 +510,26 @@ protected QueryWrapper<UserDO> buildQueryWrapper(UserQuery query) {
.in(CollUtil.isNotEmpty(userIdList), "t1.id", userIdList);
}

/**
* 导入用户
*
* @param insertList 新增用户
* @param updateList 修改用户
* @param userRoleDOList 用户角色关联
*/
private void doImportUser(List<UserDO> insertList, List<UserDO> updateList, List<UserRoleDO> userRoleDOList) {
if (CollUtil.isNotEmpty(insertList)) {
baseMapper.insert(insertList);
}
if (CollUtil.isNotEmpty(updateList)) {
this.updateBatchById(updateList);
userRoleService.deleteByUserIds(updateList.stream().map(UserDO::getId).toList());
}
if (CollUtil.isNotEmpty(userRoleDOList)) {
userRoleService.saveBatch(userRoleDOList);
}
}

/**
* 判断是否跳过导入
*
Expand Down Expand Up @@ -681,6 +688,12 @@ private boolean isPhoneExists(String phone, Long id) {
return null != count && count > 0;
}

/**
* 根据用户名获取用户列表
*
* @param usernames 用户名列表
* @return 用户列表
*/
private List<UserDO> listByUsernames(List<String> usernames) {
return this.list(Wrappers.<UserDO>lambdaQuery()
.in(UserDO::getUsername, usernames)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="top.continew.admin.system.mapper.DictItemMapper">
<select id="listByDictCode" resultType="top.continew.starter.extension.crud.model.resp.LabelValueResp">
SELECT t1.label, t1.value, t1.color AS extend
SELECT t1.label, t1.value, t1.color AS extra
FROM sys_dict_item AS t1
LEFT JOIN sys_dict AS t2 ON t1.dict_id = t2.id
WHERE t1.status = 1 AND t2.code = #{dictCode}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
:title="title"
:mask-closable="false"
:esc-to-close="false"
draggable
:width="width >= 600 ? 600 : '100%'"
@before-ok="save"
@close="reset"
Expand Down Expand Up @@ -31,7 +32,6 @@ const visible = ref(false)
const isUpdate = computed(() => !!dataId.value)
const title = computed(() => (isUpdate.value ? '修改${businessName}' : '新增${businessName}'))
const formRef = ref<InstanceType<typeof GiForm>>()
<#if hasDictField>
const { <#list dictCodes as dictCode>${dictCode}<#if dictCode_has_next>,</#if></#list> } = useDict(<#list dictCodes as dictCode>'${dictCode}'<#if dictCode_has_next>,</#if></#list>)
</#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<script setup lang="ts">
import { useWindowSize } from '@vueuse/core'
import { type ${classNamePrefix}DetailResp, get${classNamePrefix} } from '@/apis/${apiModuleName}/${apiName}'
import { type ${classNamePrefix}DetailResp, get${classNamePrefix} as getDetail } from '@/apis/${apiModuleName}/${apiName}'
const { width } = useWindowSize()
Expand All @@ -25,7 +25,7 @@ const visible = ref(false)
// 查询详情
const getDataDetail = async () => {
const { data } = await get${classNamePrefix}(dataId.value)
const { data } = await getDetail(dataId.value)
dataDetail.value = data
}
Expand Down
Loading

0 comments on commit 5717d03

Please sign in to comment.