refactor: 系统功能重构
This commit is contained in:
@@ -24,10 +24,10 @@ import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 部门业务实现类
|
||||
* 部门 业务实现类
|
||||
*
|
||||
* @author haoxr
|
||||
* @since 2021-08-22
|
||||
* @author Ray
|
||||
* @since 2021/08/22
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@@ -40,7 +40,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
||||
* 获取部门列表
|
||||
*/
|
||||
@Override
|
||||
public List<DeptVO> listDepartments(DeptQuery queryParams) {
|
||||
public List<DeptVO> getDeptList(DeptQuery queryParams) {
|
||||
// 查询参数
|
||||
String keywords = queryParams.getKeywords();
|
||||
Integer status = queryParams.getStatus();
|
||||
@@ -85,11 +85,11 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
||||
return deptList.stream()
|
||||
.filter(dept -> dept.getParentId().equals(parentId))
|
||||
.map(dept -> {
|
||||
DeptVO deptVO = deptConverter.entity2Vo(dept);
|
||||
DeptVO deptVO = deptConverter.convertToVo(dept);
|
||||
List<DeptVO> children = recurDeptList(dept.getId(), deptList);
|
||||
deptVO.setChildren(children);
|
||||
return deptVO;
|
||||
}).collect(Collectors.toList());
|
||||
}).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,14 +134,14 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
||||
@Override
|
||||
public Long saveDept(DeptForm formData) {
|
||||
// 校验部门名称是否存在
|
||||
String name = formData.getName();
|
||||
String code = formData.getCode();
|
||||
long count = this.count(new LambdaQueryWrapper<SysDept>()
|
||||
.eq(SysDept::getName, name)
|
||||
.eq(SysDept::getCode, code)
|
||||
);
|
||||
Assert.isTrue(count == 0, "部门名称已存在");
|
||||
Assert.isTrue(count == 0, "部门编号已存在");
|
||||
|
||||
// form->entity
|
||||
SysDept entity = deptConverter.form2Entity(formData);
|
||||
SysDept entity = deptConverter.convertToEntity(formData);
|
||||
|
||||
// 生成部门路径(tree_path),格式:父节点tree_path + , + 父节点ID,用于删除部门时级联删除子部门
|
||||
String treePath = generateDeptTreePath(formData.getParentId());
|
||||
@@ -154,6 +154,20 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取部门表单
|
||||
*
|
||||
* @param deptId 部门ID
|
||||
* @return 部门表单对象
|
||||
*/
|
||||
@Override
|
||||
public DeptForm getDeptForm(Long deptId) {
|
||||
SysDept entity = this.getById(deptId);
|
||||
return deptConverter.convertToForm(entity);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新部门
|
||||
*
|
||||
@@ -163,16 +177,17 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
||||
*/
|
||||
@Override
|
||||
public Long updateDept(Long deptId, DeptForm formData) {
|
||||
// 校验部门名称是否存在
|
||||
String name = formData.getName();
|
||||
// 校验部门名称/部门编号是否存在
|
||||
String code = formData.getCode();
|
||||
long count = this.count(new LambdaQueryWrapper<SysDept>()
|
||||
.eq(SysDept::getName, name)
|
||||
.ne(SysDept::getId, deptId)
|
||||
.eq(SysDept::getCode, code)
|
||||
);
|
||||
Assert.isTrue(count == 0, "部门名称已存在");
|
||||
Assert.isTrue(count == 0, "部门编号已存在");
|
||||
|
||||
|
||||
// form->entity
|
||||
SysDept entity = deptConverter.form2Entity(formData);
|
||||
SysDept entity = deptConverter.convertToEntity(formData);
|
||||
entity.setId(deptId);
|
||||
|
||||
// 生成部门路径(tree_path),格式:父节点tree_path + , + 父节点ID,用于删除部门时级联删除子部门
|
||||
@@ -194,7 +209,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
||||
* @return 部门表格层级列表
|
||||
*/
|
||||
public static List<Option> recurDeptTreeOptions(long parentId, List<SysDept> deptList) {
|
||||
List<Option> list = CollectionUtil.emptyIfNull(deptList).stream()
|
||||
return CollectionUtil.emptyIfNull(deptList).stream()
|
||||
.filter(dept -> dept.getParentId().equals(parentId))
|
||||
.map(dept -> {
|
||||
Option option = new Option(dept.getId(), dept.getName());
|
||||
@@ -205,7 +220,6 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
||||
return option;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
@@ -230,28 +244,6 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取部门详情
|
||||
*
|
||||
* @param deptId 部门ID
|
||||
* @return 部门表单对象
|
||||
*/
|
||||
@Override
|
||||
public DeptForm getDeptForm(Long deptId) {
|
||||
|
||||
SysDept entity = this.getOne(new LambdaQueryWrapper<SysDept>()
|
||||
.eq(SysDept::getId, deptId)
|
||||
.select(
|
||||
SysDept::getId,
|
||||
SysDept::getName,
|
||||
SysDept::getParentId,
|
||||
SysDept::getStatus,
|
||||
SysDept::getSort
|
||||
));
|
||||
|
||||
return deptConverter.entity2Form(entity);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 部门路径生成
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.mapper.SysDictItemMapper;
|
||||
import com.youlai.system.model.entity.SysDictItem;
|
||||
import com.youlai.system.service.SysDictItemService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 数据字典 服务实现类
|
||||
*
|
||||
* @author haoxr
|
||||
* @since 2022/10/12
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements SysDictItemService {
|
||||
|
||||
/**
|
||||
* 根据字典ID删除字典项
|
||||
*
|
||||
* @param dictId 字典ID
|
||||
*/
|
||||
@Override
|
||||
public void removeByDictId(Long dictId) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,28 +1,32 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.converter.DictConverter;
|
||||
import com.youlai.system.mapper.SysDictMapper;
|
||||
import com.youlai.system.converter.DictItemConverter;
|
||||
import com.youlai.system.model.entity.SysDict;
|
||||
import com.youlai.system.model.form.DictForm;
|
||||
import com.youlai.system.model.query.DictPageQuery;
|
||||
import com.youlai.system.model.vo.DictPageVO;
|
||||
import com.youlai.system.model.entity.SysDictItem;
|
||||
import com.youlai.system.common.model.Option;
|
||||
import com.youlai.system.mapper.SysDictMapper;
|
||||
import com.youlai.system.model.form.DictForm;
|
||||
import com.youlai.system.model.query.DictTypePageQuery;
|
||||
import com.youlai.system.model.vo.DictPageVO;
|
||||
import com.youlai.system.service.SysDictItemService;
|
||||
import com.youlai.system.service.SysDictService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 数据字典项业务实现类
|
||||
* 数据字典业务实现类
|
||||
*
|
||||
* @author haoxr
|
||||
* @since 2022/10/12
|
||||
@@ -31,133 +35,193 @@ import java.util.stream.Collectors;
|
||||
@RequiredArgsConstructor
|
||||
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {
|
||||
|
||||
private final SysDictItemService dictItemService;
|
||||
private final DictConverter dictConverter;
|
||||
private final DictItemConverter dictItemConverter;
|
||||
|
||||
/**
|
||||
* 字典数据项分页列表
|
||||
* 字典分页列表
|
||||
*
|
||||
* @param queryParams
|
||||
* @return
|
||||
* @param queryParams 分页查询对象
|
||||
*/
|
||||
@Override
|
||||
public Page<DictPageVO> getDictPage(DictPageQuery queryParams) {
|
||||
public Page<DictPageVO> getDictPage(DictTypePageQuery queryParams) {
|
||||
// 查询参数
|
||||
int pageNum = queryParams.getPageNum();
|
||||
int pageSize = queryParams.getPageSize();
|
||||
String keywords = queryParams.getKeywords();
|
||||
String typeCode = queryParams.getTypeCode();
|
||||
|
||||
// 查询数据
|
||||
Page<SysDict> dictItemPage = this.page(
|
||||
new Page<>(pageNum, pageSize),
|
||||
new LambdaQueryWrapper<SysDict>()
|
||||
.like(StrUtil.isNotBlank(keywords), SysDict::getName, keywords)
|
||||
.eq(StrUtil.isNotBlank(typeCode), SysDict::getTypeCode, typeCode)
|
||||
.select(SysDict::getId, SysDict::getName, SysDict::getValue, SysDict::getStatus)
|
||||
);
|
||||
|
||||
// 实体转换
|
||||
Page<DictPageVO> pageResult = dictConverter.entity2Page(dictItemPage);
|
||||
return pageResult;
|
||||
return this.baseMapper.getDictPage(new Page<>(pageNum, pageSize), queryParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字典数据项表单详情
|
||||
* 新增字典
|
||||
*
|
||||
* @param id 字典数据项ID
|
||||
* @return
|
||||
* @param dictForm 字典表单数据
|
||||
*/
|
||||
@Override
|
||||
public boolean saveDict(DictForm dictForm) {
|
||||
// 保存字典
|
||||
SysDict entity = dictConverter.convertToEntity(dictForm);
|
||||
|
||||
// 校验 code 是否唯一
|
||||
long count = this.count(new LambdaQueryWrapper<SysDict>()
|
||||
.eq(SysDict::getCode, entity.getCode())
|
||||
);
|
||||
Assert.isTrue(count == 0, "字典编码已存在");
|
||||
|
||||
boolean result = this.save(entity);
|
||||
// 保存字典项
|
||||
if (result) {
|
||||
List<DictForm.DictItem> dictFormDictItems = dictForm.getDictItems();
|
||||
List<SysDictItem> dictItems = dictItemConverter.convertToEntity(dictFormDictItems);
|
||||
dictItems.forEach(dictItem -> dictItem.setDictId(entity.getId()));
|
||||
dictItemService.saveBatch(dictItems);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取字典表单详情
|
||||
*
|
||||
* @param id 字典ID
|
||||
*/
|
||||
@Override
|
||||
public DictForm getDictForm(Long id) {
|
||||
// 获取entity
|
||||
SysDict entity = this.getOne(new LambdaQueryWrapper<SysDict>()
|
||||
.eq(SysDict::getId, id)
|
||||
.select(
|
||||
SysDict::getId,
|
||||
SysDict::getTypeCode,
|
||||
SysDict::getName,
|
||||
SysDict::getValue,
|
||||
SysDict::getStatus,
|
||||
SysDict::getSort,
|
||||
SysDict::getRemark
|
||||
));
|
||||
Assert.isTrue(entity != null, "字典数据项不存在");
|
||||
// 获取字典
|
||||
SysDict entity = this.getById(id);
|
||||
Assert.isTrue(entity != null, "字典不存在");
|
||||
DictForm dictForm = dictConverter.convertToForm(entity);
|
||||
|
||||
// 实体转换
|
||||
DictForm dictForm = dictConverter.entity2Form(entity);
|
||||
// 获取字典项集合
|
||||
List<SysDictItem> dictItems = dictItemService.list(new LambdaQueryWrapper<SysDictItem>()
|
||||
.eq(SysDictItem::getDictId, id)
|
||||
);
|
||||
// 转换数据项
|
||||
List<DictForm.DictItem> dictItemList = dictItemConverter.convertToDictFormDictItem(dictItems);
|
||||
dictForm.setDictItems(dictItemList);
|
||||
return dictForm;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增字典数据项
|
||||
* 修改字典
|
||||
*
|
||||
* @param dictForm 字典数据项表单
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean saveDict(DictForm dictForm) {
|
||||
// 实体对象转换 form->entity
|
||||
SysDict entity = dictConverter.form2Entity(dictForm);
|
||||
// 持久化
|
||||
boolean result = this.save(entity);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改字典数据项
|
||||
*
|
||||
* @param id 字典数据项ID
|
||||
* @param dictForm 字典数据项表单
|
||||
* @return
|
||||
* @param id 字典ID
|
||||
* @param dictForm 字典表单
|
||||
*/
|
||||
@Override
|
||||
public boolean updateDict(Long id, DictForm dictForm) {
|
||||
SysDict entity = dictConverter.form2Entity(dictForm);
|
||||
// 更新字典
|
||||
SysDict entity = dictConverter.convertToEntity(dictForm);
|
||||
|
||||
// 校验 code 是否唯一
|
||||
long count = this.count(new LambdaQueryWrapper<SysDict>()
|
||||
.eq(SysDict::getCode, entity.getCode())
|
||||
.ne(SysDict::getId, id)
|
||||
);
|
||||
Assert.isTrue(count == 0, "字典编码已存在");
|
||||
|
||||
boolean result = this.updateById(entity);
|
||||
|
||||
if (result) {
|
||||
// 更新字典项
|
||||
List<DictForm.DictItem> dictFormDictItems = dictForm.getDictItems();
|
||||
List<SysDictItem> dictItems = dictItemConverter.convertToEntity(dictFormDictItems);
|
||||
|
||||
// 获取当前数据库中的字典项
|
||||
List<SysDictItem> currentDictItemEntities = dictItemService.list(new LambdaQueryWrapper<SysDictItem>()
|
||||
.eq(SysDictItem::getDictId, id)
|
||||
);
|
||||
|
||||
// 获取当前数据库中存在的字典项ID集合
|
||||
Set<Long> currentDictItemIds = currentDictItemEntities.stream()
|
||||
.map(SysDictItem::getId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 获取新提交的字典项ID集合
|
||||
Set<Long> newAttrIds = dictItems.stream()
|
||||
.map(SysDictItem::getId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 需要删除的字典项ID集合(存在于数据库但不在新提交的字典项中)
|
||||
Set<Long> idsToDelete = new HashSet<>(currentDictItemIds);
|
||||
idsToDelete.removeAll(newAttrIds);
|
||||
|
||||
// 删除不在新提交字典项中的旧字典项
|
||||
if (!idsToDelete.isEmpty()) {
|
||||
dictItemService.removeByIds(idsToDelete);
|
||||
}
|
||||
|
||||
// 更新或新增字典项
|
||||
for (SysDictItem dictItem : dictItems) {
|
||||
if (dictItem.getId() != null && currentDictItemIds.contains(dictItem.getId())) {
|
||||
// 更新现有字典项
|
||||
dictItemService.updateById(dictItem);
|
||||
} else {
|
||||
// 新增字典项
|
||||
dictItem.setDictId(id);
|
||||
dictItemService.save(dictItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除字典数据项
|
||||
* 删除字典
|
||||
*
|
||||
* @param idsStr 字典数据项ID,多个以英文逗号(,)分割
|
||||
* @return
|
||||
* @param ids 字典ID,多个以英文逗号(,)分割
|
||||
*/
|
||||
@Override
|
||||
public boolean deleteDict(String idsStr) {
|
||||
Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除数据为空");
|
||||
//
|
||||
List<Long> ids = Arrays.asList(idsStr.split(","))
|
||||
.stream()
|
||||
.map(id -> Long.parseLong(id))
|
||||
.collect(Collectors.toList());
|
||||
@Transactional
|
||||
public void deleteDictByIds(String ids) {
|
||||
|
||||
// 删除字典数据项
|
||||
boolean result = this.removeByIds(ids);
|
||||
return result;
|
||||
Assert.isTrue(StrUtil.isNotBlank(ids), "请选择需要删除的字典");
|
||||
|
||||
List<String> idList = Arrays.stream(ids.split(","))
|
||||
.toList();
|
||||
|
||||
for (String id : idList) {
|
||||
boolean result = this.removeById(id);
|
||||
if (result) {
|
||||
// 删除字典下的字典项
|
||||
dictItemService.removeByDictId(Convert.toLong(id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典下拉列表
|
||||
* 获取字典的数据项
|
||||
*
|
||||
* @param typeCode
|
||||
* @return
|
||||
* @param code 字典编码
|
||||
*/
|
||||
@Override
|
||||
public List<Option> listDictOptions(String typeCode) {
|
||||
// 数据字典项
|
||||
List<SysDict> dictList = this.list(new LambdaQueryWrapper<SysDict>()
|
||||
.eq(SysDict::getTypeCode, typeCode)
|
||||
.select(SysDict::getValue, SysDict::getName)
|
||||
public List<Option> listDictItemsByCode(String code) {
|
||||
// 根据字典编码获取字典ID
|
||||
SysDict dict = this.getOne(new LambdaQueryWrapper<SysDict>()
|
||||
.eq(SysDict::getCode, code)
|
||||
.select(SysDict::getId)
|
||||
.last("limit 1")
|
||||
);
|
||||
// 如果字典不存在,则返回空集合
|
||||
if (dict == null) {
|
||||
return CollectionUtil.newArrayList();
|
||||
}
|
||||
|
||||
// 获取字典项
|
||||
List<SysDictItem> dictItems = dictItemService.list(
|
||||
new LambdaQueryWrapper<SysDictItem>()
|
||||
.eq(SysDictItem::getDictId, dict.getId())
|
||||
);
|
||||
|
||||
// 转换下拉数据
|
||||
List<Option> options = CollectionUtil.emptyIfNull(dictList)
|
||||
.stream()
|
||||
.map(dictItem -> new Option(dictItem.getValue(), dictItem.getName()))
|
||||
.collect(Collectors.toList());
|
||||
// 转换为 Option
|
||||
List<Option> options = dictItemConverter.convertToOption(dictItems);
|
||||
return options;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,206 +0,0 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.model.entity.SysDict;
|
||||
import com.youlai.system.common.model.Option;
|
||||
import com.youlai.system.converter.DictTypeConverter;
|
||||
import com.youlai.system.mapper.SysDictTypeMapper;
|
||||
import com.youlai.system.model.entity.SysDictType;
|
||||
import com.youlai.system.model.form.DictTypeForm;
|
||||
import com.youlai.system.model.query.DictTypePageQuery;
|
||||
import com.youlai.system.model.vo.DictTypePageVO;
|
||||
import com.youlai.system.service.SysDictService;
|
||||
import com.youlai.system.service.SysDictTypeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 数据字典类型业务实现类
|
||||
*
|
||||
* @author haoxr
|
||||
* @since 2022/10/12
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {
|
||||
|
||||
|
||||
private final SysDictService dictItemService;
|
||||
private final DictTypeConverter dictTypeConverter;
|
||||
|
||||
/**
|
||||
* 字典分页列表
|
||||
*
|
||||
* @param queryParams 分页查询对象
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Page<DictTypePageVO> getDictTypePage(DictTypePageQuery queryParams) {
|
||||
// 查询参数
|
||||
int pageNum = queryParams.getPageNum();
|
||||
int pageSize = queryParams.getPageSize();
|
||||
String keywords = queryParams.getKeywords();
|
||||
|
||||
// 查询数据
|
||||
Page<SysDictType> dictTypePage = this.page(
|
||||
new Page<>(pageNum, pageSize),
|
||||
new LambdaQueryWrapper<SysDictType>()
|
||||
.like(StrUtil.isNotBlank(keywords), SysDictType::getName, keywords)
|
||||
.or()
|
||||
.like(StrUtil.isNotBlank(keywords), SysDictType::getCode, keywords)
|
||||
.select(
|
||||
SysDictType::getId,
|
||||
SysDictType::getName,
|
||||
SysDictType::getCode,
|
||||
SysDictType::getStatus,
|
||||
SysDictType::getRemark
|
||||
)
|
||||
);
|
||||
|
||||
// 实体转换
|
||||
Page<DictTypePageVO> pageResult = dictTypeConverter.entity2Page(dictTypePage);
|
||||
return pageResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典类型表单详情
|
||||
*
|
||||
* @param id 字典类型ID
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public DictTypeForm getDictTypeForm(Long id) {
|
||||
// 获取entity
|
||||
SysDictType entity = this.getOne(new LambdaQueryWrapper<SysDictType>()
|
||||
.eq(SysDictType::getId, id)
|
||||
.select(
|
||||
SysDictType::getId,
|
||||
SysDictType::getName,
|
||||
SysDictType::getCode,
|
||||
SysDictType::getStatus,
|
||||
SysDictType::getRemark
|
||||
));
|
||||
Assert.isTrue(entity != null, "字典类型不存在");
|
||||
|
||||
// 实体转换
|
||||
DictTypeForm dictTypeForm = dictTypeConverter.entity2Form(entity);
|
||||
return dictTypeForm;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增字典类型
|
||||
*
|
||||
* @param dictTypeForm
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean saveDictType(DictTypeForm dictTypeForm) {
|
||||
// 实体对象转换 form->entity
|
||||
SysDictType entity = dictTypeConverter.form2Entity(dictTypeForm);
|
||||
// 持久化
|
||||
boolean result = this.save(entity);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 修改字典类型
|
||||
*
|
||||
* @param id 字典类型ID
|
||||
* @param dictTypeForm 字典类型表单
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean updateDictType(Long id, DictTypeForm dictTypeForm) {
|
||||
// 获取字典类型
|
||||
SysDictType sysDictType = this.getById(id);
|
||||
Assert.isTrue(sysDictType != null, "字典类型不存在");
|
||||
|
||||
SysDictType entity = dictTypeConverter.form2Entity(dictTypeForm);
|
||||
boolean result = this.updateById(entity);
|
||||
if (result) {
|
||||
// 字典类型code变化,同步修改字典项的类型code
|
||||
String oldCode = sysDictType.getCode();
|
||||
String newCode = dictTypeForm.getCode();
|
||||
if (!StrUtil.equals(oldCode, newCode)) {
|
||||
dictItemService.update(new LambdaUpdateWrapper<SysDict>()
|
||||
.eq(SysDict::getTypeCode, oldCode)
|
||||
.set(SysDict::getTypeCode, newCode)
|
||||
);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除字典类型
|
||||
*
|
||||
* @param idsStr 字典类型ID,多个以英文逗号(,)分割
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public boolean deleteDictTypes(String idsStr) {
|
||||
|
||||
Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除数据为空");
|
||||
|
||||
List ids = Arrays.asList(idsStr.split(","))
|
||||
.stream()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 删除字典数据项
|
||||
List<String> dictTypeCodes = this.list(new LambdaQueryWrapper<SysDictType>()
|
||||
.in(SysDictType::getId, ids)
|
||||
.select(SysDictType::getCode))
|
||||
.stream()
|
||||
.map(dictType -> dictType.getCode())
|
||||
.collect(Collectors.toList()
|
||||
);
|
||||
if (CollectionUtil.isNotEmpty(dictTypeCodes)) {
|
||||
dictItemService.remove(new LambdaQueryWrapper<SysDict>()
|
||||
.in(SysDict::getTypeCode, dictTypeCodes));
|
||||
}
|
||||
// 删除字典类型
|
||||
boolean result = this.removeByIds(ids);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典类型的数据项
|
||||
*
|
||||
* @param typeCode
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<Option> listDictItemsByTypeCode(String typeCode) {
|
||||
// 数据字典项
|
||||
List<SysDict> dictItems = dictItemService.list(new LambdaQueryWrapper<SysDict>()
|
||||
.eq(SysDict::getTypeCode, typeCode)
|
||||
.select(SysDict::getValue, SysDict::getName)
|
||||
);
|
||||
|
||||
// 转换下拉数据
|
||||
List<Option> options = CollectionUtil.emptyIfNull(dictItems)
|
||||
.stream()
|
||||
.map(dictItem -> new Option(dictItem.getValue(), dictItem.getName()))
|
||||
.collect(Collectors.toList());
|
||||
return options;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
.stream()
|
||||
.filter(menu -> menu.getParentId().equals(parentId))
|
||||
.map(entity -> {
|
||||
MenuVO menuVO = menuConverter.entity2Vo(entity);
|
||||
MenuVO menuVO = menuConverter.convertToVo(entity);
|
||||
List<MenuVO> children = buildMenuTree(entity.getId(), menuList);
|
||||
menuVO.setChildren(children);
|
||||
return menuVO;
|
||||
@@ -144,8 +144,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
@Cacheable(cacheNames = "menu", key = "'routes'")
|
||||
public List<RouteVO> listRoutes() {
|
||||
List<RouteBO> menuList = this.baseMapper.listRoutes();
|
||||
List<RouteVO> routes = buildRoutes(SystemConstants.ROOT_NODE_ID, menuList);
|
||||
return routes;
|
||||
return buildRoutes(SystemConstants.ROOT_NODE_ID, menuList);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -177,9 +176,17 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
*/
|
||||
private RouteVO toRouteVo(RouteBO routeBO) {
|
||||
RouteVO routeVO = new RouteVO();
|
||||
String routeName = StringUtils.capitalize(StrUtil.toCamelCase(routeBO.getPath(), '-')); // 路由 name 需要驼峰,首字母大写
|
||||
routeVO.setName(routeName); // 根据name路由跳转 this.$router.push({name:xxx})
|
||||
routeVO.setPath(routeBO.getPath()); // 根据path路由跳转 this.$router.push({path:xxx})
|
||||
// 获取路由名称
|
||||
String routeName = routeBO.getRouteName();
|
||||
if (StrUtil.isBlank(routeName)) {
|
||||
// 路由 name 需要驼峰,首字母大写
|
||||
routeName = StringUtils.capitalize(StrUtil.toCamelCase(routeBO.getRoutePath(), '-'));
|
||||
}
|
||||
// 根据name路由跳转 this.$router.push({name:xxx})
|
||||
routeVO.setName(routeName);
|
||||
|
||||
// 根据path路由跳转 this.$router.push({path:xxx})
|
||||
routeVO.setPath(routeBO.getRoutePath());
|
||||
routeVO.setRedirect(routeBO.getRedirect());
|
||||
routeVO.setComponent(routeBO.getComponent());
|
||||
|
||||
@@ -200,7 +207,8 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
if (StrUtil.isNotBlank(paramsJson)) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
Map<String, String> paramMap = objectMapper.readValue(paramsJson, new TypeReference<>() {});
|
||||
Map<String, String> paramMap = objectMapper.readValue(paramsJson, new TypeReference<>() {
|
||||
});
|
||||
meta.setParams(paramMap);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("解析参数失败", e);
|
||||
@@ -220,9 +228,9 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
MenuTypeEnum menuType = menuForm.getType();
|
||||
|
||||
if (menuType == MenuTypeEnum.CATALOG) { // 如果是外链
|
||||
String path = menuForm.getPath();
|
||||
String path = menuForm.getRoutePath();
|
||||
if (menuForm.getParentId() == 0 && !path.startsWith("/")) {
|
||||
menuForm.setPath("/" + path); // 一级目录需以 / 开头
|
||||
menuForm.setRoutePath("/" + path); // 一级目录需以 / 开头
|
||||
}
|
||||
menuForm.setComponent("Layout");
|
||||
} else if (menuType == MenuTypeEnum.EXTLINK) { // 如果是目录
|
||||
@@ -239,7 +247,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
if (CollectionUtil.isNotEmpty(params)) {
|
||||
entity.setParams(JSONUtil.toJsonStr(params.stream()
|
||||
.collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue))));
|
||||
}else{
|
||||
} else {
|
||||
entity.setParams(null);
|
||||
}
|
||||
|
||||
@@ -285,17 +293,6 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色权限(Code)集合
|
||||
*
|
||||
* @param roles 角色Code集合
|
||||
* @return 权限集合
|
||||
*/
|
||||
@Override
|
||||
public Set<String> listRolePerms(Set<String> roles) {
|
||||
return this.baseMapper.listRolePerms(roles);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取菜单表单数据
|
||||
*
|
||||
@@ -313,7 +310,8 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
// 解析 JSON 字符串为 Map<String, String>
|
||||
Map<String, String> paramMap = objectMapper.readValue(params, new TypeReference<>() {});
|
||||
Map<String, String> paramMap = objectMapper.readValue(params, new TypeReference<>() {
|
||||
});
|
||||
|
||||
// 转换为 List<KeyValue> 格式 [{key:"id", value:"1"}, {key:"name", value:"张三"}]
|
||||
List<KeyValue> transformedList = paramMap.entrySet().stream()
|
||||
@@ -353,5 +351,4 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.youlai.system.model.entity.SysRoleMenu;
|
||||
import com.youlai.system.service.SysRoleMenuService;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -19,11 +20,12 @@ import java.util.Set;
|
||||
/**
|
||||
* 角色菜单业务实现
|
||||
*
|
||||
* @author haoxr
|
||||
* @author Ray
|
||||
* @since 2.5.0
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {
|
||||
|
||||
private final RedisTemplate<String, Object> redisTemplate;
|
||||
@@ -33,6 +35,7 @@ public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRo
|
||||
*/
|
||||
@PostConstruct
|
||||
public void initRolePermsCache() {
|
||||
log.info("初始化权限缓存... ");
|
||||
refreshRolePermsCache();
|
||||
}
|
||||
|
||||
@@ -83,7 +86,7 @@ public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRo
|
||||
redisTemplate.opsForHash().delete(SecurityConstants.ROLE_PERMS_PREFIX, oldRoleCode);
|
||||
|
||||
// 添加新角色权限缓存
|
||||
List<RolePermsBO> list =this.baseMapper.getRolePermsList(newRoleCode);
|
||||
List<RolePermsBO> list = this.baseMapper.getRolePermsList(newRoleCode);
|
||||
if (CollectionUtil.isNotEmpty(list)) {
|
||||
RolePermsBO rolePerms = list.get(0);
|
||||
if (rolePerms == null) {
|
||||
@@ -95,6 +98,16 @@ public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRo
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色权限集合
|
||||
*
|
||||
* @param roles 角色编码集合
|
||||
* @return 权限集合
|
||||
*/
|
||||
@Override
|
||||
public Set<String> getRolePermsByRoleCodes(Set<String> roles) {
|
||||
return this.baseMapper.listRolePerms(roles);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色拥有的菜单ID集合
|
||||
@@ -104,8 +117,7 @@ public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRo
|
||||
*/
|
||||
@Override
|
||||
public List<Long> listMenuIdsByRoleId(Long roleId) {
|
||||
List<Long> menuIds = this.baseMapper.listMenuIdsByRoleId(roleId);
|
||||
return menuIds;
|
||||
return this.baseMapper.listMenuIdsByRoleId(roleId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
||||
);
|
||||
|
||||
// 实体转换
|
||||
return roleConverter.entity2Page(rolePage);
|
||||
return roleConverter.convertToPageVo(rolePage);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,7 +118,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
||||
Assert.isTrue(count == 0, "角色名称或角色编码已存在,请修改后重试!");
|
||||
|
||||
// 实体转换
|
||||
SysRole role = roleConverter.form2Entity(roleForm);
|
||||
SysRole role = roleConverter.convertToEntity(roleForm);
|
||||
|
||||
boolean result = this.saveOrUpdate(role);
|
||||
if (result) {
|
||||
@@ -143,7 +143,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
||||
@Override
|
||||
public RoleForm getRoleForm(Long roleId) {
|
||||
SysRole entity = this.getById(roleId);
|
||||
return roleConverter.entity2Form(entity);
|
||||
return roleConverter.convertToForm(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,7 +18,7 @@ import com.youlai.system.model.bo.UserBO;
|
||||
import com.youlai.system.model.entity.SysUser;
|
||||
import com.youlai.system.model.form.UserForm;
|
||||
import com.youlai.system.model.query.UserPageQuery;
|
||||
import com.youlai.system.model.vo.UserExportVO;
|
||||
import com.youlai.system.model.dto.UserExportDTO;
|
||||
import com.youlai.system.model.vo.UserInfoVO;
|
||||
import com.youlai.system.model.vo.UserPageVO;
|
||||
import com.youlai.system.security.service.PermissionService;
|
||||
@@ -49,7 +49,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
|
||||
private final UserConverter userConverter;
|
||||
|
||||
private final SysMenuService menuService;
|
||||
private final SysRoleMenuService roleMenuService;
|
||||
|
||||
private final SysRoleService roleService;
|
||||
|
||||
@@ -105,7 +105,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
Assert.isTrue(count == 0, "用户名已存在");
|
||||
|
||||
// 实体转换 form->entity
|
||||
SysUser entity = userConverter.form2Entity(userForm);
|
||||
SysUser entity = userConverter.convertToEntity(userForm);
|
||||
|
||||
// 设置默认加密密码
|
||||
String defaultEncryptPwd = passwordEncoder.encode(SystemConstants.DEFAULT_PASSWORD);
|
||||
@@ -141,7 +141,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
Assert.isTrue(count == 0, "用户名已存在");
|
||||
|
||||
// form -> entity
|
||||
SysUser entity = userConverter.form2Entity(userForm);
|
||||
SysUser entity = userConverter.convertToEntity(userForm);
|
||||
|
||||
// 修改用户
|
||||
boolean result = this.updateById(entity);
|
||||
@@ -197,7 +197,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
if (userAuthInfo != null) {
|
||||
Set<String> roles = userAuthInfo.getRoles();
|
||||
if (CollectionUtil.isNotEmpty(roles)) {
|
||||
Set<String> perms = menuService.listRolePerms(roles);
|
||||
Set<String> perms = roleMenuService.getRolePermsByRoleCodes(roles);
|
||||
userAuthInfo.setPerms(perms);
|
||||
}
|
||||
|
||||
@@ -213,10 +213,10 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
* 获取导出用户列表
|
||||
*
|
||||
* @param queryParams 查询参数
|
||||
* @return {@link List<UserExportVO>} 导出用户列表
|
||||
* @return {@link List< UserExportDTO >} 导出用户列表
|
||||
*/
|
||||
@Override
|
||||
public List<UserExportVO> listExportUsers(UserPageQuery queryParams) {
|
||||
public List<UserExportDTO> listExportUsers(UserPageQuery queryParams) {
|
||||
return this.baseMapper.listExportUsers(queryParams);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user