Introducing new features. closed #I449GN 用户导入导出、日志导出

This commit is contained in:
wjie 2021-08-24 16:12:48 +08:00
parent b86defa0a8
commit 00b4e9ecc4
9 changed files with 320 additions and 23 deletions

View File

@ -51,5 +51,11 @@
<groupId>com.pig4cloud</groupId> <groupId>com.pig4cloud</groupId>
<artifactId>pig-common-mybatis</artifactId> <artifactId>pig-common-mybatis</artifactId>
</dependency> </dependency>
<!-- excel 导入导出 -->
<dependency>
<groupId>com.pig4cloud.excel</groupId>
<artifactId>excel-spring-boot-starter</artifactId>
<version>0.5.0</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -0,0 +1,69 @@
package com.pig4cloud.pig.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 lengleng
* @date 2021/8/4
*/
@Data
@ColumnWidth(30)
public class UserExcelVO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键ID
*/
@ExcelProperty("用户编号")
private Integer userId;
/**
* 用户名
*/
@NotBlank(message = "用户名不能为空")
@ExcelProperty("用户名")
private String username;
/**
* 手机号
*/
@NotBlank(message = "手机号不能为空")
@ExcelProperty("手机号")
private String phone;
/**
* 部门名称
*/
@NotBlank(message = "部门名称不能为空")
@ExcelProperty("部门名称")
private String deptName;
/**
* 角色列表
*/
@NotBlank(message = "角色不能为空")
@ExcelProperty("角色")
private String roleNameList;
/**
* 锁定标记
*/
@ExcelProperty("锁定标记,0:正常,9:已锁定")
private String lockFlag;
/**
* 创建时间
*/
@ExcelProperty(value = "创建时间")
private LocalDateTime createTime;
}

View File

@ -70,7 +70,7 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId> <artifactId>spring-boot-starter-undertow</artifactId>
</dependency> </dependency>
<!--单元测试-->
<dependency> <dependency>
<groupId>com.pig4cloud</groupId> <groupId>com.pig4cloud</groupId>
<artifactId>pig-common-test</artifactId> <artifactId>pig-common-test</artifactId>
@ -88,6 +88,24 @@
<artifactId>docker-maven-plugin</artifactId> <artifactId>docker-maven-plugin</artifactId>
</plugin> </plugin>
</plugins> </plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.xlsx</exclude>
<exclude>**/*.xls</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.xlsx</include>
<include>**/*.xls</include>
</includes>
</resource>
</resources>
</build> </build>
</project> </project>

View File

@ -0,0 +1,36 @@
package com.pig4cloud.pig.admin.controller;
import cn.hutool.core.io.IoUtil;
import lombok.SneakyThrows;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
/**
* 文件管理
*
* @author lengleng
* @date 2021/8/23
*/
@RestController
@RequestMapping("/file")
public class FileController {
/**
* 获取本地文件
* @param fileName 文件名称
* @param response 本地文件
*/
@SneakyThrows
@GetMapping("/local-file/{fileName}")
public void localFile(@PathVariable String fileName, HttpServletResponse response) {
ClassPathResource resource = new ClassPathResource("file/" + fileName);
response.setContentType("application/octet-stream; charset=UTF-8");
IoUtil.copy(resource.getInputStream(), response.getOutputStream());
}
}

View File

@ -21,12 +21,14 @@ import com.pig4cloud.pig.admin.api.entity.SysLog;
import com.pig4cloud.pig.admin.service.SysLogService; import com.pig4cloud.pig.admin.service.SysLogService;
import com.pig4cloud.pig.common.core.util.R; import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.security.annotation.Inner; import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.List;
/** /**
* <p> * <p>
@ -77,4 +79,16 @@ public class LogController {
return R.ok(sysLogService.save(sysLog)); return R.ok(sysLogService.save(sysLog));
} }
/**
* 导出excel 表格
* @param sysLog 查询条件
* @return EXCEL
*/
@ResponseExcel
@GetMapping("/export")
@PreAuthorize("@pms.hasPermission('sys_log_import_export')")
public List<SysLog> export(SysLogDTO sysLog) {
return sysLogService.getLogList(sysLog);
}
} }

View File

@ -21,17 +21,22 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.admin.api.dto.UserDTO; import com.pig4cloud.pig.admin.api.dto.UserDTO;
import com.pig4cloud.pig.admin.api.entity.SysUser; import com.pig4cloud.pig.admin.api.entity.SysUser;
import com.pig4cloud.pig.admin.api.vo.UserExcelVO;
import com.pig4cloud.pig.admin.service.SysUserService; import com.pig4cloud.pig.admin.service.SysUserService;
import com.pig4cloud.pig.common.core.util.R; import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog; import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner; import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.common.security.util.SecurityUtils; import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.List;
/** /**
* @author lengleng * @author lengleng
@ -163,4 +168,28 @@ public class UserController {
return R.ok(userService.listAncestorUsersByUsername(username)); return R.ok(userService.listAncestorUsersByUsername(username));
} }
/**
* 导出excel 表格
* @param userDTO 查询条件
* @return
*/
@ResponseExcel
@GetMapping("/export")
@PreAuthorize("@pms.hasPermission('sys_user_import_export')")
public List export(UserDTO userDTO) {
return userService.listUser(userDTO);
}
/**
* 导入用户
* @param excelVOList 用户列表
* @param bindingResult 错误信息列表
* @return R
*/
@PostMapping("/import")
@PreAuthorize("@pms.hasPermission('sys_user_import_export')")
public R importUser(@RequestExcel List<UserExcelVO> excelVOList, BindingResult bindingResult) {
return userService.importUser(excelVOList, bindingResult);
}
} }

View File

@ -22,7 +22,10 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.pig4cloud.pig.admin.api.dto.UserDTO; import com.pig4cloud.pig.admin.api.dto.UserDTO;
import com.pig4cloud.pig.admin.api.dto.UserInfo; import com.pig4cloud.pig.admin.api.dto.UserInfo;
import com.pig4cloud.pig.admin.api.entity.SysUser; import com.pig4cloud.pig.admin.api.entity.SysUser;
import com.pig4cloud.pig.admin.api.vo.UserExcelVO;
import com.pig4cloud.pig.admin.api.vo.UserVO; import com.pig4cloud.pig.admin.api.vo.UserVO;
import com.pig4cloud.pig.common.core.util.R;
import org.springframework.validation.BindingResult;
import java.util.List; import java.util.List;
@ -89,4 +92,19 @@ public interface SysUserService extends IService<SysUser> {
*/ */
Boolean saveUser(UserDTO userDto); Boolean saveUser(UserDTO userDto);
/**
* 查询全部的用户
* @param userDTO 查询条件
* @return list
*/
List<UserExcelVO> listUser(UserDTO userDTO);
/**
* excel 导入用户
* @param excelVOList excel 列表数据
* @param bindingResult 错误数据
* @return ok fail
*/
R importUser(List<UserExcelVO> excelVOList, BindingResult bindingResult);
} }

View File

@ -16,6 +16,7 @@
package com.pig4cloud.pig.admin.service.impl; package com.pig4cloud.pig.admin.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
@ -25,11 +26,19 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.admin.api.dto.UserDTO; import com.pig4cloud.pig.admin.api.dto.UserDTO;
import com.pig4cloud.pig.admin.api.dto.UserInfo; import com.pig4cloud.pig.admin.api.dto.UserInfo;
import com.pig4cloud.pig.admin.api.entity.*; import com.pig4cloud.pig.admin.api.entity.*;
import com.pig4cloud.pig.admin.api.vo.UserExcelVO;
import com.pig4cloud.pig.admin.api.vo.UserVO; import com.pig4cloud.pig.admin.api.vo.UserVO;
import com.pig4cloud.pig.admin.mapper.SysDeptMapper;
import com.pig4cloud.pig.admin.mapper.SysRoleMapper;
import com.pig4cloud.pig.admin.mapper.SysUserMapper; import com.pig4cloud.pig.admin.mapper.SysUserMapper;
import com.pig4cloud.pig.admin.service.*; import com.pig4cloud.pig.admin.mapper.SysUserRoleMapper;
import com.pig4cloud.pig.admin.service.SysMenuService;
import com.pig4cloud.pig.admin.service.SysUserService;
import com.pig4cloud.pig.common.core.constant.CacheConstants; import com.pig4cloud.pig.common.core.constant.CacheConstants;
import com.pig4cloud.pig.common.core.constant.CommonConstants; import com.pig4cloud.pig.common.core.constant.CommonConstants;
import com.pig4cloud.pig.common.core.constant.enums.MenuTypeEnum;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.plugin.excel.vo.ErrorMessage;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -39,10 +48,12 @@ import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -57,13 +68,13 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder(); private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();
private final SysRoleMapper sysRoleMapper;
private final SysDeptMapper sysDeptMapper;
private final SysMenuService sysMenuService; private final SysMenuService sysMenuService;
private final SysRoleService sysRoleService; private final SysUserRoleMapper sysUserRoleMapper;
private final SysDeptService sysDeptService;
private final SysUserRoleService sysUserRoleService;
/** /**
* 保存用户信息 * 保存用户信息
@ -78,13 +89,13 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
sysUser.setDelFlag(CommonConstants.STATUS_NORMAL); sysUser.setDelFlag(CommonConstants.STATUS_NORMAL);
sysUser.setPassword(ENCODER.encode(userDto.getPassword())); sysUser.setPassword(ENCODER.encode(userDto.getPassword()));
baseMapper.insert(sysUser); baseMapper.insert(sysUser);
List<SysUserRole> userRoleList = userDto.getRole().stream().map(roleId -> { userDto.getRole().stream().map(roleId -> {
SysUserRole userRole = new SysUserRole(); SysUserRole userRole = new SysUserRole();
userRole.setUserId(sysUser.getUserId()); userRole.setUserId(sysUser.getUserId());
userRole.setRoleId(roleId); userRole.setRoleId(roleId);
return userRole; return userRole;
}).collect(Collectors.toList()); }).forEach(sysUserRoleMapper::insert);
return sysUserRoleService.saveBatch(userRoleList); return Boolean.TRUE;
} }
/** /**
@ -97,18 +108,14 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
UserInfo userInfo = new UserInfo(); UserInfo userInfo = new UserInfo();
userInfo.setSysUser(sysUser); userInfo.setSysUser(sysUser);
// 设置角色列表 ID // 设置角色列表 ID
List<Integer> roleIds = sysRoleService.findRolesByUserId(sysUser.getUserId()).stream().map(SysRole::getRoleId) List<Integer> roleIds = sysRoleMapper.listRolesByUserId(sysUser.getUserId()).stream().map(SysRole::getRoleId)
.collect(Collectors.toList()); .collect(Collectors.toList());
userInfo.setRoles(ArrayUtil.toArray(roleIds, Integer.class)); userInfo.setRoles(ArrayUtil.toArray(roleIds, Integer.class));
// 设置权限列表menu.permission // 设置权限列表menu.permission
Set<String> permissions = new HashSet<>(); Set<String> permissions = sysMenuService.findMenuByRoleId(CollUtil.join(roleIds, StrUtil.COMMA)).stream()
roleIds.forEach(roleId -> { .filter(m -> MenuTypeEnum.BUTTON.getType().equals(m.getType()))
List<String> permissionList = sysMenuService.findMenuByRoleId(roleId).stream() .filter(m -> StrUtil.isNotBlank(m.getPermission())).map(SysMenu::getPermission)
.filter(menuVo -> StrUtil.isNotEmpty(menuVo.getPermission())).map(SysMenu::getPermission) .collect(Collectors.toSet());
.collect(Collectors.toList());
permissions.addAll(permissionList);
});
userInfo.setPermissions(ArrayUtil.toArray(permissions, String.class)); userInfo.setPermissions(ArrayUtil.toArray(permissions, String.class));
return userInfo; return userInfo;
} }
@ -142,7 +149,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
@Override @Override
@CacheEvict(value = CacheConstants.USER_DETAILS, key = "#sysUser.username") @CacheEvict(value = CacheConstants.USER_DETAILS, key = "#sysUser.username")
public Boolean removeUserById(SysUser sysUser) { public Boolean removeUserById(SysUser sysUser) {
sysUserRoleService.removeRoleByUserId(sysUser.getUserId()); sysUserRoleMapper.deleteByUserId(sysUser.getUserId());
this.removeById(sysUser.getUserId()); this.removeById(sysUser.getUserId());
return Boolean.TRUE; return Boolean.TRUE;
} }
@ -177,8 +184,8 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
} }
this.updateById(sysUser); this.updateById(sysUser);
sysUserRoleService sysUserRoleMapper
.remove(Wrappers.<SysUserRole>update().lambda().eq(SysUserRole::getUserId, userDto.getUserId())); .delete(Wrappers.<SysUserRole>update().lambda().eq(SysUserRole::getUserId, userDto.getUserId()));
userDto.getRole().forEach(roleId -> { userDto.getRole().forEach(roleId -> {
SysUserRole userRole = new SysUserRole(); SysUserRole userRole = new SysUserRole();
userRole.setUserId(sysUser.getUserId()); userRole.setUserId(sysUser.getUserId());
@ -197,7 +204,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
public List<SysUser> listAncestorUsersByUsername(String username) { public List<SysUser> listAncestorUsersByUsername(String username) {
SysUser sysUser = this.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, username)); SysUser sysUser = this.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, username));
SysDept sysDept = sysDeptService.getById(sysUser.getDeptId()); SysDept sysDept = sysDeptMapper.selectById(sysUser.getDeptId());
if (sysDept == null) { if (sysDept == null) {
return null; return null;
} }
@ -206,4 +213,104 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
return this.list(Wrappers.<SysUser>query().lambda().eq(SysUser::getDeptId, parentId)); return this.list(Wrappers.<SysUser>query().lambda().eq(SysUser::getDeptId, parentId));
} }
/**
* 查询全部的用户
* @param userDTO 查询条件
* @return list
*/
@Override
public List<UserExcelVO> listUser(UserDTO userDTO) {
List<UserVO> voList = baseMapper.selectVoList(userDTO);
// 转换成execl 对象输出
List<UserExcelVO> userExcelVOList = voList.stream().map(userVO -> {
UserExcelVO excelVO = new UserExcelVO();
BeanUtils.copyProperties(userVO, excelVO);
String roleNameList = userVO.getRoleList().stream().map(SysRole::getRoleName)
.collect(Collectors.joining(StrUtil.COMMA));
excelVO.setRoleNameList(roleNameList);
return excelVO;
}).collect(Collectors.toList());
return userExcelVOList;
}
/**
* excel 导入用户, 插入正确的 错误的提示行号
* @param excelVOList excel 列表数据
* @param bindingResult 错误数据
* @return ok fail
*/
@Override
public R importUser(List<UserExcelVO> excelVOList, BindingResult bindingResult) {
// 通用校验获取失败的数据
List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();
// 个性化校验逻辑
List<SysUser> userList = this.list();
List<SysDept> deptList = sysDeptMapper.selectList(Wrappers.emptyWrapper());
List<SysRole> roleList = sysRoleMapper.selectList(Wrappers.emptyWrapper());
// 执行数据插入操作 组装 UserDto
for (int i = 0; i < excelVOList.size(); i++) {
UserExcelVO excel = excelVOList.get(i);
Set<String> errorMsg = new HashSet<>();
// 校验用户名是否存在
boolean exsitUserName = userList.stream()
.anyMatch(sysUser -> excel.getUsername().equals(sysUser.getUsername()));
if (exsitUserName) {
errorMsg.add(String.format("%s 用户名已存在", excel.getUsername()));
}
// 判断输入的部门名称列表是否合法
Optional<SysDept> deptOptional = deptList.stream()
.filter(dept -> excel.getDeptName().equals(dept.getName())).findFirst();
if (!deptOptional.isPresent()) {
errorMsg.add(String.format("%s 部门名称不存在", excel.getDeptName()));
}
// 判断输入的角色名称列表是否合法
List<String> roleNameList = StrUtil.split(excel.getRoleNameList(), StrUtil.COMMA);
List<SysRole> roleCollList = roleList.stream()
.filter(role -> roleNameList.stream().anyMatch(name -> role.getRoleName().equals(name)))
.collect(Collectors.toList());
if (roleCollList.size() != roleNameList.size()) {
errorMsg.add(String.format("%s 角色名称不存在", excel.getRoleNameList()));
}
// 数据合法情况
if (CollUtil.isEmpty(errorMsg)) {
insertExcelUser(excel, deptOptional, roleCollList);
}
else {
// 数据不合法情况
errorMessageList.add(new ErrorMessage((long) (i + 2), errorMsg));
}
}
if (CollUtil.isNotEmpty(errorMessageList)) {
return R.failed(errorMessageList);
}
return R.ok();
}
/**
* 插入excel User
*/
private void insertExcelUser(UserExcelVO excel, Optional<SysDept> deptOptional, List<SysRole> roleCollList) {
UserDTO userDTO = new UserDTO();
userDTO.setUsername(excel.getUsername());
userDTO.setPhone(excel.getPhone());
// 批量导入初始密码为手机号
userDTO.setPassword(userDTO.getPhone());
// 根据部门名称查询部门ID
userDTO.setDeptId(deptOptional.get().getDeptId());
// 根据角色名称查询角色ID
List<Integer> roleIdList = roleCollList.stream().map(SysRole::getRoleId).collect(Collectors.toList());
userDTO.setRole(roleIdList);
// 插入用户
this.saveUser(userDTO);
}
} }