feat: 全量提交
This commit is contained in:
commit
de9157143a
128 files changed
+7493
No files matched your search
@@ -0,0 +1,238 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.lang.Validator;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.common.constant.SystemConstants;
|
||||
import com.youlai.system.common.enums.StatusEnum;
|
||||
import com.youlai.system.common.model.Option;
|
||||
import com.youlai.system.converter.DeptConverter;
|
||||
import com.youlai.system.mapper.SysDeptMapper;
|
||||
import com.youlai.system.pojo.entity.SysDept;
|
||||
import com.youlai.system.pojo.form.DeptForm;
|
||||
import com.youlai.system.pojo.query.DeptQuery;
|
||||
import com.youlai.system.pojo.vo.dept.DeptVO;
|
||||
import com.youlai.system.service.SysDeptService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 部门业务实现类
|
||||
*
|
||||
* @author haoxr
|
||||
* @date 2021-08-22
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
|
||||
|
||||
|
||||
private final DeptConverter deptConverter;
|
||||
|
||||
/**
|
||||
* 部门列表
|
||||
*/
|
||||
@Override
|
||||
public List<DeptVO> listDepartments(DeptQuery queryParams) {
|
||||
// 查询参数
|
||||
String keywords = queryParams.getKeywords();
|
||||
Integer status = queryParams.getStatus();
|
||||
|
||||
// 查询数据
|
||||
List<SysDept> deptList = this.list(
|
||||
new LambdaQueryWrapper<SysDept>()
|
||||
.like(StrUtil.isNotBlank(keywords), SysDept::getName, keywords)
|
||||
.eq(Validator.isNotNull(status), SysDept::getStatus, status)
|
||||
.orderByAsc(SysDept::getSort)
|
||||
);
|
||||
|
||||
List<DeptVO> list = new ArrayList<>();
|
||||
|
||||
if (CollectionUtil.isNotEmpty(deptList)) {
|
||||
|
||||
Set<Long> cacheDeptIds = deptList.stream()
|
||||
.map(SysDept::getId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
|
||||
for (SysDept dept : deptList) {
|
||||
Long parentId = dept.getParentId();
|
||||
// 不在缓存ID列表的parentId是顶级节点ID,以此作为递归开始
|
||||
if (cacheDeptIds.contains(parentId) == false) {
|
||||
list.addAll(recurDepartments(parentId, deptList));
|
||||
cacheDeptIds.add(parentId); // 避免重复递归
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 列表为空说明所有的节点都是独立的
|
||||
if (list.isEmpty()) {
|
||||
return deptList.stream().map(item -> {
|
||||
DeptVO deptVO = new DeptVO();
|
||||
BeanUtil.copyProperties(item, deptVO);
|
||||
return deptVO;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归生成部门层级列表
|
||||
*
|
||||
* @param parentId
|
||||
* @param deptList
|
||||
* @return
|
||||
*/
|
||||
public List<DeptVO> recurDepartments(Long parentId, List<SysDept> deptList) {
|
||||
List<DeptVO> list = deptList.stream()
|
||||
.filter(dept -> dept.getParentId().equals(parentId))
|
||||
.map(dept -> {
|
||||
DeptVO deptVO = deptConverter.entity2Vo(dept);
|
||||
List<DeptVO> children = recurDepartments(dept.getId(), deptList);
|
||||
deptVO.setChildren(children);
|
||||
return deptVO;
|
||||
}).collect(Collectors.toList());
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 部门下拉选项
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<Option> listDeptOptions() {
|
||||
List<SysDept> deptList = this.list(new LambdaQueryWrapper<SysDept>()
|
||||
.eq(SysDept::getStatus, StatusEnum.ENABLE.getValue())
|
||||
.select(SysDept::getId, SysDept::getParentId, SysDept::getName)
|
||||
.orderByAsc(SysDept::getSort)
|
||||
);
|
||||
List<Option> options = recurDeptTreeOptions(SystemConstants.ROOT_NODE_ID, deptList);
|
||||
return options;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long saveDept(DeptForm formData) {
|
||||
SysDept entity = deptConverter.form2Entity(formData);
|
||||
// 部门路径
|
||||
String treePath = generateDeptTreePath(formData.getParentId());
|
||||
entity.setTreePath(treePath);
|
||||
// 保存部门并返回部门ID
|
||||
this.save(entity);
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long updateDept(Long deptId, DeptForm formData) {
|
||||
// form->entity
|
||||
SysDept entity = deptConverter.form2Entity(formData);
|
||||
entity.setId(deptId);
|
||||
// 部门路径
|
||||
String treePath = generateDeptTreePath(formData.getParentId());
|
||||
entity.setTreePath(treePath);
|
||||
// 保存部门并返回部门ID
|
||||
this.updateById(entity);
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归生成部门表格层级列表
|
||||
*
|
||||
* @param parentId
|
||||
* @param deptList
|
||||
* @return
|
||||
*/
|
||||
public static List<Option> recurDeptTreeOptions(long parentId, List<SysDept> deptList) {
|
||||
if (CollectionUtil.isEmpty(deptList)) {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
List<Option> list = deptList.stream()
|
||||
.filter(dept -> dept.getParentId().equals(parentId))
|
||||
.map(dept -> {
|
||||
Option option = new Option(dept.getId(), dept.getName());
|
||||
List<Option> children = recurDeptTreeOptions(dept.getId(), deptList);
|
||||
if (CollectionUtil.isNotEmpty(children)) {
|
||||
option.setChildren(children);
|
||||
}
|
||||
return option;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除部门
|
||||
*
|
||||
* @param ids 部门ID,多个以英文逗号,拼接字符串
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean deleteByIds(String ids) {
|
||||
// 删除部门及子部门
|
||||
Optional.ofNullable(Arrays.stream(ids.split(",")))
|
||||
.ifPresent(deptIds -> deptIds.forEach(deptId ->
|
||||
this.remove(new LambdaQueryWrapper<SysDept>()
|
||||
.eq(SysDept::getId, deptId)
|
||||
.or()
|
||||
.apply("concat (',',tree_path,',') like concat('%,',{0},',%')", deptId))
|
||||
));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取部门详情
|
||||
*
|
||||
* @param deptId
|
||||
* @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
|
||||
));
|
||||
|
||||
DeptForm deptForm = deptConverter.entity2Form(entity);
|
||||
return deptForm;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 部门路径生成
|
||||
*
|
||||
* @param parentId
|
||||
* @return
|
||||
*/
|
||||
private String generateDeptTreePath(Long parentId) {
|
||||
String treePath = null;
|
||||
if (SystemConstants.ROOT_NODE_ID.equals(parentId)) {
|
||||
treePath = parentId + "";
|
||||
} else {
|
||||
SysDept parent = this.getById(parentId);
|
||||
if (parent != null) {
|
||||
treePath = parent.getTreePath() + "," + parent.getId();
|
||||
}
|
||||
}
|
||||
return treePath;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
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.DictItemConverter;
|
||||
import com.youlai.system.mapper.SysDictItemMapper;
|
||||
import com.youlai.system.pojo.entity.SysDictItem;
|
||||
import com.youlai.system.pojo.form.DictItemForm;
|
||||
import com.youlai.system.pojo.query.DictItemPageQuery;
|
||||
import com.youlai.system.pojo.vo.dict.DictItemPageVO;
|
||||
import com.youlai.system.service.SysDictItemService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 数据字典项业务实现类
|
||||
*
|
||||
* @author haoxr
|
||||
* @date 2022/10/12
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements SysDictItemService {
|
||||
|
||||
private final DictItemConverter dictItemConverter;
|
||||
|
||||
/**
|
||||
* 字典数据项分页列表
|
||||
*
|
||||
* @param queryParams
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Page<DictItemPageVO> listDictItemPages(DictItemPageQuery queryParams) {
|
||||
// 查询参数
|
||||
int pageNum = queryParams.getPageNum();
|
||||
int pageSize = queryParams.getPageSize();
|
||||
String keywords = queryParams.getKeywords();
|
||||
String typeCode = queryParams.getTypeCode();
|
||||
|
||||
// 查询数据
|
||||
Page<SysDictItem> dictItemPage = this.page(
|
||||
new Page<>(pageNum, pageSize),
|
||||
new LambdaQueryWrapper<SysDictItem>()
|
||||
.like(StrUtil.isNotBlank(keywords), SysDictItem::getName, keywords)
|
||||
.eq(StrUtil.isNotBlank(typeCode), SysDictItem::getTypeCode, typeCode)
|
||||
.select(SysDictItem::getId, SysDictItem::getName, SysDictItem::getValue, SysDictItem::getStatus)
|
||||
);
|
||||
|
||||
// 实体转换
|
||||
Page<DictItemPageVO> pageResult = dictItemConverter.entity2Page(dictItemPage);
|
||||
return pageResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字典数据项表单详情
|
||||
*
|
||||
* @param id 字典数据项ID
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public DictItemForm getDictItemForm(Long id) {
|
||||
// 获取entity
|
||||
SysDictItem entity = this.getOne(new LambdaQueryWrapper<SysDictItem>()
|
||||
.eq(SysDictItem::getId, id)
|
||||
.select(
|
||||
SysDictItem::getId,
|
||||
SysDictItem::getTypeCode,
|
||||
SysDictItem::getName,
|
||||
SysDictItem::getValue,
|
||||
SysDictItem::getStatus,
|
||||
SysDictItem::getSort,
|
||||
SysDictItem::getRemark
|
||||
));
|
||||
Assert.isTrue(entity != null, "字典数据项不存在");
|
||||
|
||||
// 实体转换
|
||||
DictItemForm dictItemForm = dictItemConverter.entity2Form(entity);
|
||||
return dictItemForm;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增字典数据项
|
||||
*
|
||||
* @param dictItemForm 字典数据项表单
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean saveDictItem(DictItemForm dictItemForm) {
|
||||
// 实体对象转换 form->entity
|
||||
SysDictItem entity = dictItemConverter.form2Entity(dictItemForm);
|
||||
// 持久化
|
||||
boolean result = this.save(entity);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改字典数据项
|
||||
*
|
||||
* @param id 字典数据项ID
|
||||
* @param dictItemForm 字典数据项表单
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean updateDictItem(Long id, DictItemForm dictItemForm) {
|
||||
SysDictItem entity = dictItemConverter.form2Entity(dictItemForm);
|
||||
boolean result = this.updateById(entity);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除字典数据项
|
||||
*
|
||||
* @param idsStr 字典数据项ID,多个以英文逗号(,)分割
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean deleteDictItems(String idsStr) {
|
||||
Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除数据为空");
|
||||
//
|
||||
List<Long> ids = Arrays.asList(idsStr.split(","))
|
||||
.stream()
|
||||
.map(id -> Long.parseLong(id))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 删除字典数据项
|
||||
boolean result = this.removeByIds(ids);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
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.common.model.Option;
|
||||
import com.youlai.system.converter.DictTypeConverter;
|
||||
import com.youlai.system.mapper.SysDictTypeMapper;
|
||||
import com.youlai.system.pojo.entity.SysDictItem;
|
||||
import com.youlai.system.pojo.entity.SysDictType;
|
||||
import com.youlai.system.pojo.form.DictTypeForm;
|
||||
import com.youlai.system.pojo.query.DictTypePageQuery;
|
||||
import com.youlai.system.pojo.vo.dict.DictTypePageVO;
|
||||
import com.youlai.system.service.SysDictItemService;
|
||||
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
|
||||
* @date 2022/10/12
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {
|
||||
|
||||
|
||||
private final SysDictItemService dictItemService;
|
||||
private final DictTypeConverter dictTypeConverter;
|
||||
|
||||
/**
|
||||
* 字典分页列表
|
||||
*
|
||||
* @param queryParams 分页查询对象
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Page<DictTypePageVO> listDictTypePages(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)
|
||||
);
|
||||
|
||||
// 实体转换
|
||||
Page<DictTypePageVO> pageResult = dictTypeConverter.entity2Page(dictTypePage);
|
||||
return pageResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典类型表单详情
|
||||
*
|
||||
* @param id 字典类型ID
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public DictTypeForm getDictTypeFormData(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<SysDictItem>()
|
||||
.eq(SysDictItem::getTypeCode, oldCode)
|
||||
.set(SysDictItem::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<SysDictItem>()
|
||||
.in(SysDictItem::getTypeCode, dictTypeCodes));
|
||||
}
|
||||
// 删除字典类型
|
||||
boolean result = this.removeByIds(ids);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典类型的数据项
|
||||
*
|
||||
* @param typeCode
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<Option> listDictItemsByTypeCode(String typeCode) {
|
||||
// 数据字典项
|
||||
List<SysDictItem> dictItems = dictItemService.list(new LambdaQueryWrapper<SysDictItem>()
|
||||
.eq(SysDictItem::getTypeCode, typeCode)
|
||||
.select(SysDictItem::getValue, SysDictItem::getName)
|
||||
);
|
||||
|
||||
// 转换下拉数据
|
||||
List<Option> options = CollectionUtil.emptyIfNull(dictItems)
|
||||
.stream()
|
||||
.map(dictItem -> new Option(dictItem.getValue(), dictItem.getName()))
|
||||
.collect(Collectors.toList());
|
||||
return options;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,256 @@
|
||||
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.service.impl.ServiceImpl;
|
||||
import com.youlai.system.common.constant.SystemConstants;
|
||||
import com.youlai.system.common.enums.MenuTypeEnum;
|
||||
import com.youlai.system.common.enums.StatusEnum;
|
||||
import com.youlai.system.common.model.Option;
|
||||
import com.youlai.system.converter.MenuConverter;
|
||||
import com.youlai.system.mapper.SysMenuMapper;
|
||||
import com.youlai.system.pojo.entity.SysMenu;
|
||||
import com.youlai.system.pojo.po.RoutePO;
|
||||
import com.youlai.system.pojo.vo.menu.MenuVO;
|
||||
import com.youlai.system.pojo.vo.menu.ResourceVO;
|
||||
import com.youlai.system.pojo.vo.menu.RouteVO;
|
||||
import com.youlai.system.service.SysMenuService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
/**
|
||||
* 菜单业务实现类
|
||||
*
|
||||
* @author haoxr
|
||||
* @date 2020/11/06
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
|
||||
private final MenuConverter menuConverter;
|
||||
|
||||
/**
|
||||
* 菜单表格树形列表
|
||||
*/
|
||||
@Override
|
||||
public List<MenuVO> listMenus(String name) {
|
||||
List<SysMenu> menus = this.list(new LambdaQueryWrapper<SysMenu>()
|
||||
.like(StrUtil.isNotBlank(name), SysMenu::getName, name)
|
||||
.orderByAsc(SysMenu::getSort)
|
||||
);
|
||||
|
||||
Set<Long> cacheMenuIds = menus.stream().map(menu -> menu.getId()).collect(Collectors.toSet());
|
||||
|
||||
List<MenuVO> tableMenus = menus.stream().map(menu -> {
|
||||
Long parentId = menu.getParentId();
|
||||
// parentId不在当前菜单ID的列表,说明为顶级菜单ID,根据此ID作为递归的开始条件节点
|
||||
if (!cacheMenuIds.contains(parentId)) {
|
||||
cacheMenuIds.add(parentId);
|
||||
return recurTableMenus(parentId, menus);
|
||||
}
|
||||
return new LinkedList<MenuVO>();
|
||||
}).collect(ArrayList::new, ArrayList::addAll, ArrayList::addAll);
|
||||
return tableMenus;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 保存菜单
|
||||
*/
|
||||
@Override
|
||||
public boolean saveMenu(SysMenu menu) {
|
||||
String path = menu.getPath();
|
||||
|
||||
MenuTypeEnum menuType = menu.getType(); // 菜单类型
|
||||
switch (menuType) {
|
||||
case CATALOG: // 目录
|
||||
Assert.isTrue(path.startsWith("/"), "目录路由路径格式错误,必须以/开始");
|
||||
menu.setComponent("Layout");
|
||||
break;
|
||||
case EXTLINK: // 外链
|
||||
menu.setComponent(null);
|
||||
break;
|
||||
}
|
||||
|
||||
boolean result = this.saveOrUpdate(menu);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 菜单下拉数据
|
||||
*/
|
||||
@Override
|
||||
public List<Option> listMenuOptions() {
|
||||
List<SysMenu> menuList = this.list(new LambdaQueryWrapper<SysMenu>().orderByAsc(SysMenu::getSort));
|
||||
List<Option> menus = recurMenus(SystemConstants.ROOT_NODE_ID, menuList);
|
||||
return menus;
|
||||
}
|
||||
|
||||
/**
|
||||
* 路由列表
|
||||
*/
|
||||
@Override
|
||||
@Cacheable(cacheNames = "system", key = "'routes'")
|
||||
public List<RouteVO> listRoutes() {
|
||||
List<RoutePO> menuList = this.baseMapper.listRoutes();
|
||||
List<RouteVO> routeList = recurRoutes(SystemConstants.ROOT_NODE_ID, menuList);
|
||||
return routeList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归生成菜单路由层级列表
|
||||
*
|
||||
* @param parentId 父级ID
|
||||
* @param menuList 菜单列表
|
||||
* @return
|
||||
*/
|
||||
private List<RouteVO> recurRoutes(Long parentId, List<RoutePO> menuList) {
|
||||
List<RouteVO> list = new ArrayList<>();
|
||||
Optional.ofNullable(menuList).ifPresent(menus -> menus.stream()
|
||||
.filter(menu -> menu.getParentId().equals(parentId))
|
||||
.forEach(menu -> {
|
||||
RouteVO routeVO = new RouteVO();
|
||||
|
||||
MenuTypeEnum menuTypeEnum = menu.getType();
|
||||
|
||||
if (MenuTypeEnum.MENU.equals(menuTypeEnum)) {
|
||||
routeVO.setName(menu.getPath()); // 根据name路由跳转 this.$router.push({name:xxx})
|
||||
}
|
||||
routeVO.setPath(menu.getPath()); // 根据path路由跳转 this.$router.push({path:xxx})
|
||||
routeVO.setRedirect(menu.getRedirectUrl());
|
||||
routeVO.setComponent(menu.getComponent());
|
||||
|
||||
RouteVO.Meta meta = new RouteVO.Meta();
|
||||
meta.setTitle(menu.getName());
|
||||
meta.setIcon(menu.getIcon());
|
||||
meta.setRoles(menu.getRoles());
|
||||
meta.setHidden(StatusEnum.DISABLE.getValue().equals(menu.getVisible()));
|
||||
meta.setKeepAlive(true);
|
||||
|
||||
routeVO.setMeta(meta);
|
||||
List<RouteVO> children = recurRoutes(menu.getId(), menuList);
|
||||
// 含有子节点的目录设置为可见
|
||||
boolean alwaysShow = CollectionUtil.isNotEmpty(children) && children.stream().anyMatch(item -> item.getMeta().getHidden().equals(false));
|
||||
meta.setAlwaysShow(alwaysShow);
|
||||
routeVO.setChildren(children);
|
||||
|
||||
list.add(routeVO);
|
||||
}));
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取菜单资源树形列表
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<ResourceVO> listResources() {
|
||||
List<SysMenu> menuList = this.list(new LambdaQueryWrapper<SysMenu>()
|
||||
.orderByAsc(SysMenu::getSort));
|
||||
|
||||
List<ResourceVO> resources = recurResources(SystemConstants.ROOT_NODE_ID, menuList);
|
||||
|
||||
return resources;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改菜单显示状态
|
||||
*
|
||||
* @param menuId 菜单ID
|
||||
* @param visible 是否显示(1->显示;2->隐藏)
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean updateMenuVisible(Long menuId, Integer visible) {
|
||||
boolean result = this.update(new LambdaUpdateWrapper<SysMenu>()
|
||||
.eq(SysMenu::getId, menuId)
|
||||
.set(SysMenu::getVisible, visible)
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> listRolePerms(Set<String> roles) {
|
||||
Set<String> perms= this.baseMapper.listRolePerms(roles);
|
||||
return perms;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归生成菜单表格层级列表
|
||||
*
|
||||
* @param parentId 父级ID
|
||||
* @param menuList 菜单列表
|
||||
* @return
|
||||
*/
|
||||
private List<MenuVO> recurTableMenus(Long parentId, List<SysMenu> menuList) {
|
||||
List<MenuVO> tableMenus = Optional.ofNullable(menuList).orElse(new ArrayList<>())
|
||||
.stream()
|
||||
.filter(menu -> menu.getParentId().equals(parentId))
|
||||
.map(entity -> {
|
||||
MenuVO menuVO = menuConverter.entity2VO(entity);
|
||||
List<MenuVO> children = recurTableMenus(entity.getId(), menuList);
|
||||
menuVO.setChildren(children);
|
||||
return menuVO;
|
||||
}).collect(Collectors.toList());
|
||||
return tableMenus;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归生成菜单下拉层级列表
|
||||
*
|
||||
* @param parentId 父级ID
|
||||
* @param menuList 菜单列表
|
||||
* @return
|
||||
*/
|
||||
private static List<Option> recurMenus(Long parentId, List<SysMenu> menuList) {
|
||||
List<Option> menus = Optional.ofNullable(menuList).orElse(new ArrayList<>()).stream()
|
||||
.filter(menu -> menu.getParentId().equals(parentId))
|
||||
.map(menu -> new Option(menu.getId(), menu.getName(), recurMenus(menu.getId(), menuList)))
|
||||
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
|
||||
return menus;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归生成资源(菜单+权限)树形列表
|
||||
*
|
||||
* @param parentId 父级ID
|
||||
* @param menuList 菜单列表
|
||||
* @return
|
||||
*/
|
||||
private static List<ResourceVO> recurResources(Long parentId, List<SysMenu> menuList) {
|
||||
List<ResourceVO> menus = Optional.ofNullable(menuList).orElse(new ArrayList<>()).stream()
|
||||
.filter(menu -> menu.getParentId().equals(parentId))
|
||||
.map(menu -> {
|
||||
ResourceVO resourceVO = new ResourceVO();
|
||||
resourceVO.setValue(menu.getId());
|
||||
resourceVO.setLabel(menu.getName());
|
||||
|
||||
List<ResourceVO> children = recurResources(menu.getId(), menuList);
|
||||
resourceVO.setChildren(children);
|
||||
|
||||
return resourceVO;
|
||||
}).collect(Collectors.toList());
|
||||
return menus;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 清理路由缓存
|
||||
*/
|
||||
@Override
|
||||
@CacheEvict(cacheNames = "system", key = "'routes'")
|
||||
public void cleanCache() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.mapper.SysRoleMenuMapper;
|
||||
import com.youlai.system.pojo.entity.SysRoleMenu;
|
||||
import com.youlai.system.service.SysRoleMenuService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {
|
||||
|
||||
/**
|
||||
* 获取角色拥有的菜单ID集合
|
||||
*
|
||||
* @param roleId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<Long> listMenuIdsByRoleId(Long roleId) {
|
||||
List<Long> menuIds = this.baseMapper.listMenuIdsByRoleId(roleId);
|
||||
return menuIds;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
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.common.constant.SystemConstants;
|
||||
import com.youlai.system.common.model.Option;
|
||||
import com.youlai.system.converter.RoleConverter;
|
||||
import com.youlai.system.mapper.SysRoleMapper;
|
||||
import com.youlai.system.pojo.entity.SysRole;
|
||||
import com.youlai.system.pojo.entity.SysRoleMenu;
|
||||
import com.youlai.system.pojo.entity.SysUserRole;
|
||||
import com.youlai.system.pojo.form.RoleForm;
|
||||
import com.youlai.system.pojo.form.RoleResourceForm;
|
||||
import com.youlai.system.pojo.query.RolePageQuery;
|
||||
import com.youlai.system.pojo.vo.role.RolePageVO;
|
||||
import com.youlai.system.service.SysRoleMenuService;
|
||||
import com.youlai.system.service.SysRoleService;
|
||||
import com.youlai.system.service.SysUserRoleService;
|
||||
import com.youlai.system.util.SecurityUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 角色业务实现类
|
||||
*
|
||||
* @author haoxr
|
||||
* @date 2022/6/3
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
|
||||
|
||||
private final SysRoleMenuService sysRoleMenuService;
|
||||
private final SysUserRoleService sysUserRoleService;
|
||||
private final RoleConverter roleConverter;
|
||||
|
||||
/**
|
||||
* 角色分页列表
|
||||
*
|
||||
* @param queryParams
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Page<RolePageVO> listRolePages(RolePageQuery queryParams) {
|
||||
// 查询参数
|
||||
int pageNum = queryParams.getPageNum();
|
||||
int pageSize = queryParams.getPageSize();
|
||||
String keywords = queryParams.getKeywords();
|
||||
|
||||
// 查询数据
|
||||
Page<SysRole> rolePage = this.page(new Page<>(pageNum, pageSize),
|
||||
new LambdaQueryWrapper<SysRole>()
|
||||
.and(StrUtil.isNotBlank(keywords),
|
||||
wrapper ->
|
||||
wrapper.like(StrUtil.isNotBlank(keywords), SysRole::getName, keywords)
|
||||
.or()
|
||||
.like(StrUtil.isNotBlank(keywords), SysRole::getCode, keywords)
|
||||
)
|
||||
.ne(!SecurityUtils.isRoot(), SysRole::getCode, SystemConstants.ROOT_ROLE_CODE) // 非超级管理员不显示超级管理员角色
|
||||
);
|
||||
|
||||
// 实体转换
|
||||
Page<RolePageVO> pageResult = roleConverter.entity2Page(rolePage);
|
||||
return pageResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色下拉列表
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<Option> listRoleOptions() {
|
||||
// 查询数据
|
||||
List<SysRole> roleList = this.list(new LambdaQueryWrapper<SysRole>()
|
||||
.ne(!SecurityUtils.isRoot(), SysRole::getCode, SystemConstants.ROOT_ROLE_CODE)
|
||||
.select(SysRole::getId, SysRole::getName)
|
||||
.orderByAsc(SysRole::getSort)
|
||||
);
|
||||
|
||||
// 实体转换
|
||||
List<Option> list = roleConverter.roles2Options(roleList);
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param roleForm
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean saveRole(RoleForm roleForm) {
|
||||
|
||||
Long roleId = roleForm.getId();
|
||||
String roleCode = roleForm.getCode();
|
||||
|
||||
long count = this.count(new LambdaQueryWrapper<SysRole>()
|
||||
.ne(roleId != null, SysRole::getId, roleId)
|
||||
.and(wrapper ->
|
||||
wrapper.eq(SysRole::getCode, roleCode).or().eq(SysRole::getName, roleCode)
|
||||
));
|
||||
Assert.isTrue(count == 0, "角色名称或角色编码重复,请检查!");
|
||||
|
||||
// 实体转换
|
||||
SysRole role = roleConverter.form2Entity(roleForm);
|
||||
|
||||
boolean result = this.saveOrUpdate(role);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改角色状态
|
||||
*
|
||||
* @param roleId
|
||||
* @param status
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean updateRoleStatus(Long roleId, Integer status) {
|
||||
boolean result = this.update(new LambdaUpdateWrapper<SysRole>()
|
||||
.eq(SysRole::getId, roleId)
|
||||
.set(SysRole::getStatus, status));
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除角色
|
||||
*
|
||||
* @param ids
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean deleteRoles(String ids) {
|
||||
List<Long> roleIds = Arrays.asList(ids.split(",")).stream().map(id -> Long.parseLong(id)).collect(Collectors.toList());
|
||||
Optional.ofNullable(roleIds)
|
||||
.orElse(new ArrayList<>())
|
||||
.forEach(id -> {
|
||||
long count = sysUserRoleService.count(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getRoleId, id));
|
||||
Assert.isTrue(count <= 0, "该角色已分配用户,无法删除");
|
||||
sysRoleMenuService.remove(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, id));
|
||||
});
|
||||
|
||||
|
||||
boolean result = this.removeByIds(roleIds);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色的资源ID集合,资源包括菜单和权限
|
||||
*
|
||||
* @param roleId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public RoleResourceForm getRoleResources(Long roleId) {
|
||||
RoleResourceForm roleResources = new RoleResourceForm();
|
||||
|
||||
// 获取角色拥有的菜单ID集合
|
||||
List<Long> menuIds = sysRoleMenuService.listMenuIdsByRoleId(roleId);
|
||||
roleResources.setMenuIds(menuIds);
|
||||
return roleResources;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改角色的资源权限
|
||||
*
|
||||
* @param roleId
|
||||
* @param roleResourceForm
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
@CacheEvict(cacheNames = "system", key = "'routes'")
|
||||
public boolean updateRoleResource(Long roleId, RoleResourceForm roleResourceForm) {
|
||||
// 删除角色菜单
|
||||
sysRoleMenuService.remove(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
|
||||
// 新增角色菜单
|
||||
List<Long> menuIds = roleResourceForm.getMenuIds();
|
||||
if (CollectionUtil.isNotEmpty(menuIds)) {
|
||||
List<SysRoleMenu> roleMenus = menuIds.stream()
|
||||
.map(menuId -> new SysRoleMenu(roleId, menuId))
|
||||
.collect(Collectors.toList());
|
||||
sysRoleMenuService.saveBatch(roleMenus);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.mapper.SysUserRoleMapper;
|
||||
import com.youlai.system.pojo.entity.SysUserRole;
|
||||
import com.youlai.system.service.SysUserRoleService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {
|
||||
|
||||
/**
|
||||
* 保存用户角色
|
||||
*
|
||||
* @param userId
|
||||
* @param roleIds
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean saveUserRoles(Long userId, List<Long> roleIds) {
|
||||
|
||||
if (userId == null || CollectionUtil.isEmpty(roleIds)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 用户原角色ID集合
|
||||
List<Long> userRoleIds = this.list(new LambdaQueryWrapper<SysUserRole>()
|
||||
.eq(SysUserRole::getUserId, userId))
|
||||
.stream()
|
||||
.map(item -> item.getRoleId())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 新增用户角色
|
||||
List<Long> saveRoleIds;
|
||||
if (CollectionUtil.isEmpty(userRoleIds)) {
|
||||
saveRoleIds = roleIds;
|
||||
} else {
|
||||
saveRoleIds = roleIds.stream()
|
||||
.filter(roleId -> !userRoleIds.contains(roleId))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
List<SysUserRole> saveUserRoles = saveRoleIds
|
||||
.stream()
|
||||
.map(roleId -> new SysUserRole(userId, roleId))
|
||||
.collect(Collectors.toList());
|
||||
this.saveBatch(saveUserRoles);
|
||||
|
||||
// 删除用户角色
|
||||
if (CollectionUtil.isNotEmpty(userRoleIds)) {
|
||||
List<Long> removeRoleIds = userRoleIds.stream()
|
||||
.filter(roleId -> !roleIds.contains(roleId))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if(CollectionUtil.isNotEmpty(removeRoleIds)){
|
||||
this.remove(new LambdaQueryWrapper<SysUserRole>()
|
||||
.eq(SysUserRole::getUserId, userId)
|
||||
.in(SysUserRole::getRoleId, removeRoleIds)
|
||||
);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,359 @@
|
||||
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.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
|
||||
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.youlai.system.common.base.IBaseEnum;
|
||||
import com.youlai.system.common.constant.SystemConstants;
|
||||
import com.youlai.system.common.enums.GenderEnum;
|
||||
import com.youlai.system.converter.UserConverter;
|
||||
import com.youlai.system.listener.UserImportListener;
|
||||
import com.youlai.system.mapper.SysUserMapper;
|
||||
import com.youlai.system.pojo.dto.UserImportDTO;
|
||||
import com.youlai.system.pojo.entity.SysUser;
|
||||
import com.youlai.system.pojo.entity.SysUserRole;
|
||||
import com.youlai.system.pojo.form.UserForm;
|
||||
import com.youlai.system.pojo.po.UserAuthInfo;
|
||||
import com.youlai.system.pojo.po.UserFormPO;
|
||||
import com.youlai.system.pojo.po.UserPO;
|
||||
import com.youlai.system.pojo.query.UserPageQuery;
|
||||
import com.youlai.system.pojo.vo.user.UserExportVO;
|
||||
import com.youlai.system.pojo.vo.user.UserLoginVO;
|
||||
import com.youlai.system.pojo.vo.user.UserVO;
|
||||
import com.youlai.system.service.SysMenuService;
|
||||
import com.youlai.system.service.SysUserRoleService;
|
||||
import com.youlai.system.service.SysUserService;
|
||||
import com.youlai.system.util.SecurityUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 用户业务实现类
|
||||
*
|
||||
* @author haoxr
|
||||
* @date 2022/1/14
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
|
||||
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final SysUserRoleService userRoleService;
|
||||
private final UserImportListener userImportListener;
|
||||
|
||||
@Resource
|
||||
private UserConverter userConverter;
|
||||
|
||||
private final SysMenuService menuService;
|
||||
|
||||
/**
|
||||
* 获取用户分页列表
|
||||
*
|
||||
* @param queryParams
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public IPage<UserVO> listUserPages(UserPageQuery queryParams) {
|
||||
|
||||
// 参数构建
|
||||
int pageNum = queryParams.getPageNum();
|
||||
int pageSize = queryParams.getPageSize();
|
||||
Page<UserPO> page = new Page<>(pageNum, pageSize);
|
||||
|
||||
// 查询数据
|
||||
Page<UserPO> userPoPage = this.baseMapper.listUserPages(page, queryParams);
|
||||
|
||||
// 实体转换
|
||||
Page<UserVO> userVoPage = userConverter.po2Vo(userPoPage);
|
||||
|
||||
return userVoPage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户详情
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public UserForm getUserFormData(Long userId) {
|
||||
UserFormPO userFormPO = this.baseMapper.getUserDetail(userId);
|
||||
// 实体转换po->form
|
||||
UserForm userForm = userConverter.po2Form(userFormPO);
|
||||
return userForm;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增用户
|
||||
*
|
||||
* @param userForm 用户表单对象
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean saveUser(UserForm userForm) {
|
||||
|
||||
String username = userForm.getUsername();
|
||||
|
||||
long count = this.count(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
|
||||
Assert.isTrue(count == 0, "用户名已存在");
|
||||
|
||||
// 实体转换 form->entity
|
||||
SysUser entity = userConverter.form2Entity(userForm);
|
||||
|
||||
// 设置默认加密密码
|
||||
String defaultEncryptPwd = passwordEncoder.encode(SystemConstants.DEFAULT_USER_PASSWORD);
|
||||
entity.setPassword(defaultEncryptPwd);
|
||||
|
||||
// 新增用户
|
||||
boolean result = this.save(entity);
|
||||
|
||||
if (result) {
|
||||
// 保存用户角色
|
||||
userRoleService.saveUserRoles(entity.getId(), userForm.getRoleIds());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param userForm 用户表单对象
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public boolean updateUser(Long userId, UserForm userForm) {
|
||||
|
||||
String username = userForm.getUsername();
|
||||
|
||||
long count = this.count(new LambdaQueryWrapper<SysUser>()
|
||||
.eq(SysUser::getUsername, username)
|
||||
.ne(SysUser::getId, userId)
|
||||
);
|
||||
Assert.isTrue(count == 0, "用户名已存在");
|
||||
|
||||
// form -> entity
|
||||
SysUser entity = userConverter.form2Entity(userForm);
|
||||
|
||||
// 修改用户
|
||||
boolean result = this.updateById(entity);
|
||||
|
||||
if (result) {
|
||||
// 保存用户角色
|
||||
userRoleService.saveUserRoles(entity.getId(), userForm.getRoleIds());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
*
|
||||
* @param idsStr 用户ID,多个以英文逗号(,)分割
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean deleteUsers(String idsStr) {
|
||||
Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的用户数据为空");
|
||||
// 逻辑删除
|
||||
List<Long> ids = Arrays.asList(idsStr.split(",")).stream()
|
||||
.map(idStr -> Long.parseLong(idStr)).collect(Collectors.toList());
|
||||
boolean result = this.removeByIds(ids);
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改用户密码
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param password 用户密码
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean updatePassword(Long userId, String password) {
|
||||
String encryptedPassword = passwordEncoder.encode(password);
|
||||
boolean result = this.update(new LambdaUpdateWrapper<SysUser>()
|
||||
.eq(SysUser::getId, userId)
|
||||
.set(SysUser::getPassword, encryptedPassword)
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户名获取认证信息
|
||||
*
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public UserAuthInfo getUserAuthInfo(String username) {
|
||||
UserAuthInfo userAuthInfo = this.baseMapper.getUserAuthInfo(username);
|
||||
|
||||
Set<String> roles = userAuthInfo.getRoles();
|
||||
if(CollectionUtil.isNotEmpty(roles)){
|
||||
Set<String> perms= menuService.listRolePerms(roles);
|
||||
userAuthInfo.setPerms(perms);
|
||||
}
|
||||
return userAuthInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入用户
|
||||
*
|
||||
* @param userImportDTO
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
@Override
|
||||
public String importUsers(UserImportDTO userImportDTO) throws IOException {
|
||||
|
||||
Long deptId = userImportDTO.getDeptId();
|
||||
List<Long> roleIds = Arrays.stream(userImportDTO.getRoleIds().split(","))
|
||||
.map(roleId -> Convert.toLong(roleId))
|
||||
.collect(Collectors.toList());
|
||||
InputStream inputStream = userImportDTO.getFile().getInputStream();
|
||||
|
||||
ExcelReaderBuilder excelReaderBuilder = EasyExcel.read(inputStream, UserImportDTO.UserItem.class, userImportListener);
|
||||
ExcelReaderSheetBuilder sheet = excelReaderBuilder.sheet();
|
||||
List<UserImportDTO.UserItem> list = sheet.doReadSync();
|
||||
|
||||
Assert.isTrue(CollectionUtil.isNotEmpty(list), "未检测到任何数据");
|
||||
|
||||
// 有效数据集合
|
||||
List<UserImportDTO.UserItem> validDataList = list.stream()
|
||||
.filter(item -> StrUtil.isNotBlank(item.getUsername()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assert.isTrue(CollectionUtil.isNotEmpty(validDataList), "未检测到有效数据");
|
||||
|
||||
long distinctCount = validDataList.stream()
|
||||
.map(UserImportDTO.UserItem::getUsername)
|
||||
.distinct()
|
||||
.count();
|
||||
Assert.isTrue(validDataList.size() == distinctCount, "导入数据中有重复的用户名,请检查!");
|
||||
|
||||
List<SysUser> saveUserList = Lists.newArrayList();
|
||||
|
||||
StringBuilder errMsg = new StringBuilder();
|
||||
for (int i = 0; i < validDataList.size(); i++) {
|
||||
UserImportDTO.UserItem userItem = validDataList.get(i);
|
||||
|
||||
String username = userItem.getUsername();
|
||||
if (StrUtil.isBlank(username)) {
|
||||
errMsg.append(StrUtil.format("第{}条数据导入失败,原因:用户名为空", i + 1));
|
||||
continue;
|
||||
}
|
||||
|
||||
String nickname = userItem.getNickname();
|
||||
if (StrUtil.isBlank(nickname)) {
|
||||
errMsg.append(StrUtil.format("第{}条数据导入失败,原因:用户昵称为空", i + 1));
|
||||
continue;
|
||||
}
|
||||
|
||||
SysUser user = new SysUser();
|
||||
user.setUsername(username);
|
||||
user.setNickname(nickname);
|
||||
user.setMobile(userItem.getMobile());
|
||||
user.setEmail(userItem.getEmail());
|
||||
user.setDeptId(deptId);
|
||||
// 默认密码
|
||||
user.setPassword(passwordEncoder.encode(SystemConstants.DEFAULT_USER_PASSWORD));
|
||||
// 性别转换
|
||||
Integer gender = (Integer) IBaseEnum.getValueByLabel(userItem.getGender(), GenderEnum.class);
|
||||
user.setGender(gender);
|
||||
|
||||
saveUserList.add(user);
|
||||
}
|
||||
|
||||
if (CollectionUtil.isNotEmpty(saveUserList)) {
|
||||
boolean result = this.saveBatch(saveUserList);
|
||||
Assert.isTrue(result, "导入数据失败,原因:保存用户出错");
|
||||
|
||||
List<SysUserRole> userRoleList = new ArrayList<>();
|
||||
|
||||
if (CollectionUtil.isNotEmpty(roleIds)) {
|
||||
|
||||
roleIds.forEach(roleId -> {
|
||||
userRoleList.addAll(
|
||||
saveUserList.stream()
|
||||
.map(user -> new SysUserRole(user.getId(), roleId)).
|
||||
collect(Collectors.toList()));
|
||||
});
|
||||
}
|
||||
|
||||
userRoleService.saveBatch(userRoleList);
|
||||
}
|
||||
|
||||
errMsg.append(StrUtil.format("一共{}条数据,成功导入{}条数据,导入失败数据{}条", list.size(), saveUserList.size(), list.size() - saveUserList.size()));
|
||||
return errMsg.toString();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取导出用户列表
|
||||
*
|
||||
* @param queryParams
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<UserExportVO> listExportUsers(UserPageQuery queryParams) {
|
||||
List<UserExportVO> list = this.baseMapper.listExportUsers(queryParams);
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录用户信息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public UserLoginVO getUserLoginInfo() {
|
||||
// 登录用户entity
|
||||
SysUser user = this.getOne(new LambdaQueryWrapper<SysUser>()
|
||||
.eq(SysUser::getUsername, SecurityUtils.getUser().getUsername())
|
||||
.select(
|
||||
SysUser::getId,
|
||||
SysUser::getNickname,
|
||||
SysUser::getAvatar
|
||||
)
|
||||
);
|
||||
// entity->VO
|
||||
UserLoginVO userLoginVO = userConverter.entity2LoginUser(user);
|
||||
|
||||
// 用户角色集合
|
||||
Set<String> roles = SecurityUtils.getRoles();
|
||||
userLoginVO.setRoles(roles);
|
||||
|
||||
// 用户权限集合
|
||||
Set<String> perms = SecurityUtils.getPerms();
|
||||
userLoginVO.setPerms(perms);
|
||||
|
||||
return userLoginVO;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user