mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-09 04:49:02 +08:00
✨ 角色管理导入导出
This commit is contained in:
1 parent
ef10b99d33
commit
8969ad36ee
5 files changed
+176
No files matched your search
+56
@@ -0,0 +1,56 @@
|
||||
package com.pig4cloud.pigx.admin.api.vo;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.alibaba.excel.annotation.write.style.ColumnWidth;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 角色excel 对应的实体
|
||||
*
|
||||
* @author fxz
|
||||
* @date 2022/3/21
|
||||
*/
|
||||
@Data
|
||||
@ColumnWidth(30)
|
||||
public class RoleExcelVO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 主键ID
|
||||
*/
|
||||
@ExcelProperty("角色编号")
|
||||
private Long roleId;
|
||||
|
||||
/**
|
||||
* 角色名称
|
||||
*/
|
||||
@NotBlank(message = "角色名称不能为空")
|
||||
@ExcelProperty("角色名称")
|
||||
private String roleName;
|
||||
|
||||
/**
|
||||
* 角色标识
|
||||
*/
|
||||
@NotBlank(message = "角色标识不能为空")
|
||||
@ExcelProperty("角色标识")
|
||||
private String roleCode;
|
||||
|
||||
/**
|
||||
* 角色描述
|
||||
*/
|
||||
@NotBlank(message = "角色描述不能为空")
|
||||
@ExcelProperty("角色描述")
|
||||
private String roleDesc;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@ExcelProperty(value = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
}
|
||||
+27
@@ -24,15 +24,19 @@ import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysRole;
|
||||
import com.pig4cloud.pigx.admin.api.vo.RoleExcelVO;
|
||||
import com.pig4cloud.pigx.admin.api.vo.RoleVO;
|
||||
import com.pig4cloud.pigx.admin.service.SysRoleService;
|
||||
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import com.pig4cloud.pigx.common.excel.annotation.RequestExcel;
|
||||
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
|
||||
import com.pig4cloud.pigx.common.log.annotation.SysLog;
|
||||
import io.swagger.annotations.Api;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
@@ -151,4 +155,27 @@ public class SysRoleController {
|
||||
return R.ok(sysRoleService.findRolesByRoleIds(roleIdList, CollUtil.join(roleIdList, StrUtil.UNDERLINE)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出excel 表格
|
||||
* @return
|
||||
*/
|
||||
@ResponseExcel
|
||||
@GetMapping("/export")
|
||||
@PreAuthorize("@pms.hasPermission('sys_role_export')")
|
||||
public List<RoleExcelVO> export() {
|
||||
return sysRoleService.listRole();
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入角色
|
||||
* @param excelVOList 角色列表
|
||||
* @param bindingResult 错误信息列表
|
||||
* @return ok fail
|
||||
*/
|
||||
@PostMapping("/import")
|
||||
@PreAuthorize("@pms.hasPermission('sys_role_export')")
|
||||
public R importRole(@RequestExcel List<RoleExcelVO> excelVOList, BindingResult bindingResult) {
|
||||
return sysRoleService.importRole(excelVOList, bindingResult);
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -21,7 +21,10 @@ package com.pig4cloud.pigx.admin.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysRole;
|
||||
import com.pig4cloud.pigx.admin.api.vo.RoleExcelVO;
|
||||
import com.pig4cloud.pigx.admin.api.vo.RoleVO;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import org.springframework.validation.BindingResult;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -64,4 +67,18 @@ public interface SysRoleService extends IService<SysRole> {
|
||||
*/
|
||||
Boolean updateRoleMenus(RoleVO roleVo);
|
||||
|
||||
/**
|
||||
* 导入角色
|
||||
* @param excelVOList 角色列表
|
||||
* @param bindingResult 错误信息列表
|
||||
* @return ok fail
|
||||
*/
|
||||
R importRole(List<RoleExcelVO> excelVOList, BindingResult bindingResult);
|
||||
|
||||
/**
|
||||
* 查询全部的角色
|
||||
* @return list
|
||||
*/
|
||||
List<RoleExcelVO> listRole();
|
||||
|
||||
}
|
||||
+76
@@ -19,21 +19,30 @@
|
||||
|
||||
package com.pig4cloud.pigx.admin.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysRole;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysRoleMenu;
|
||||
import com.pig4cloud.pigx.admin.api.vo.RoleExcelVO;
|
||||
import com.pig4cloud.pigx.admin.api.vo.RoleVO;
|
||||
import com.pig4cloud.pigx.admin.mapper.SysRoleMapper;
|
||||
import com.pig4cloud.pigx.admin.service.SysRoleMenuService;
|
||||
import com.pig4cloud.pigx.admin.service.SysRoleService;
|
||||
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import com.pig4cloud.pigx.common.excel.vo.ErrorMessage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.BindingResult;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -94,4 +103,71 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
||||
return roleMenuService.saveRoleMenus(sysRole.getRoleCode(), roleVo.getRoleId(), roleVo.getMenuIds());
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入角色
|
||||
* @param excelVOList 角色列表
|
||||
* @param bindingResult 错误信息列表
|
||||
* @return ok fail
|
||||
*/
|
||||
@Override
|
||||
public R importRole(List<RoleExcelVO> excelVOList, BindingResult bindingResult) {
|
||||
// 通用校验获取失败的数据
|
||||
List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();
|
||||
|
||||
// 个性化校验逻辑
|
||||
List<SysRole> roleList = this.list();
|
||||
|
||||
// 执行数据插入操作 组装 RoleDto
|
||||
for (int i = 0; i < excelVOList.size(); i++) {
|
||||
RoleExcelVO excel = excelVOList.get(i);
|
||||
Set<String> errorMsg = new HashSet<>();
|
||||
// 检验角色名称或者角色编码是否存在
|
||||
boolean existRole = roleList.stream().anyMatch(sysRole -> excel.getRoleName().equals(sysRole.getRoleName())
|
||||
|| excel.getRoleCode().equals(sysRole.getRoleCode()));
|
||||
|
||||
if (existRole) {
|
||||
errorMsg.add(String.format("%s %s 角色名或角色编码已经存在", excel.getRoleName(), excel.getRoleDesc()));
|
||||
}
|
||||
|
||||
// 数据合法情况
|
||||
if (CollUtil.isEmpty(errorMsg)) {
|
||||
insertExcelRole(excel);
|
||||
}
|
||||
else {
|
||||
// 数据不合法情况
|
||||
errorMessageList.add(new ErrorMessage((long) (i + 2), errorMsg));
|
||||
}
|
||||
}
|
||||
if (CollUtil.isNotEmpty(errorMessageList)) {
|
||||
return R.failed(errorMessageList);
|
||||
}
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询全部的角色
|
||||
* @return list
|
||||
*/
|
||||
@Override
|
||||
public List<RoleExcelVO> listRole() {
|
||||
List<SysRole> roleList = this.list(Wrappers.emptyWrapper());
|
||||
// 转换成execl 对象输出
|
||||
return roleList.stream().map(role -> {
|
||||
RoleExcelVO roleExcelVO = new RoleExcelVO();
|
||||
BeanUtil.copyProperties(role, roleExcelVO);
|
||||
return roleExcelVO;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入excel Role
|
||||
*/
|
||||
private void insertExcelRole(RoleExcelVO excel) {
|
||||
SysRole sysRole = new SysRole();
|
||||
sysRole.setRoleName(excel.getRoleName());
|
||||
sysRole.setRoleDesc(excel.getRoleDesc());
|
||||
sysRole.setRoleCode(excel.getRoleCode());
|
||||
this.save(sysRole);
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
Reference in new issue
Block a user