mirror of https://github.com/elunez/eladmin
新增后端带条件导出功能(示例见用户管理控制器)
parent
e775de19bf
commit
e6146cb1c8
|
@ -1,10 +1,17 @@
|
|||
package me.zhengjie.utils;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.poi.excel.ExcelUtil;
|
||||
import cn.hutool.poi.excel.ExcelWriter;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* File工具类,扩展 hutool 工具包
|
||||
|
@ -139,4 +146,32 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
|
|||
ins.close();
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出excel
|
||||
* @param list
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response){
|
||||
// 通过工具类创建writer
|
||||
ExcelWriter writer = ExcelUtil.getWriter();
|
||||
// 一次性写出内容,使用默认样式,强制输出标题
|
||||
writer.write(list, true);
|
||||
//response为HttpServletResponse对象
|
||||
response.setContentType("application/vnd.ms-excel;charset=utf-8");
|
||||
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
|
||||
response.setHeader("Content-Disposition","attachment;filename=file.xls");
|
||||
ServletOutputStream out= null;
|
||||
try {
|
||||
out = response.getOutputStream();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
writer.flush(out, true);
|
||||
// 关闭writer,释放内存
|
||||
writer.close();
|
||||
//此处记得关闭输出Servlet流
|
||||
IoUtil.close(out);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import me.zhengjie.aop.log.Log;
|
|||
import me.zhengjie.modules.system.domain.Role;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.system.service.RoleService;
|
||||
import me.zhengjie.modules.system.service.dto.MenuQueryCriteria;
|
||||
import me.zhengjie.modules.system.service.dto.RoleQueryCriteria;
|
||||
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
|
||||
import me.zhengjie.utils.SecurityUtils;
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.springframework.util.ObjectUtils;
|
|||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -55,6 +56,13 @@ public class UserController {
|
|||
@Autowired
|
||||
private VerificationCodeService verificationCodeService;
|
||||
|
||||
@Log("导出用户数据")
|
||||
@GetMapping(value = "/users/download")
|
||||
@PreAuthorize("hasAnyRole('ADMIN','USER_ALL','USER_SELECT')")
|
||||
public void update(HttpServletResponse response, UserQueryCriteria criteria){
|
||||
userService.download(userService.queryAll(criteria), response);
|
||||
}
|
||||
|
||||
@Log("查询用户")
|
||||
@GetMapping(value = "/users")
|
||||
@PreAuthorize("hasAnyRole('ADMIN','USER_ALL','USER_SELECT')")
|
||||
|
|
|
@ -10,6 +10,7 @@ import org.springframework.cache.annotation.CacheEvict;
|
|||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -86,6 +87,7 @@ public interface RoleService {
|
|||
* @param pageable
|
||||
* @return
|
||||
*/
|
||||
@Cacheable(keyGenerator = "keyGenerator")
|
||||
Object queryAll(Pageable pageable);
|
||||
|
||||
/**
|
||||
|
@ -94,5 +96,14 @@ public interface RoleService {
|
|||
* @param criteria
|
||||
* @return
|
||||
*/
|
||||
@Cacheable(keyGenerator = "keyGenerator")
|
||||
Object queryAll(RoleQueryCriteria criteria, Pageable pageable);
|
||||
|
||||
/**
|
||||
* queryAll
|
||||
* @param criteria
|
||||
* @return
|
||||
*/
|
||||
@Cacheable(keyGenerator = "keyGenerator")
|
||||
List<RoleDTO> queryAll(RoleQueryCriteria criteria);
|
||||
}
|
||||
|
|
|
@ -9,6 +9,9 @@ import org.springframework.cache.annotation.CacheEvict;
|
|||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-23
|
||||
|
@ -80,4 +83,9 @@ public interface UserService {
|
|||
|
||||
@Cacheable(keyGenerator = "keyGenerator")
|
||||
Object queryAll(UserQueryCriteria criteria, Pageable pageable);
|
||||
|
||||
@Cacheable(keyGenerator = "keyGenerator")
|
||||
List<UserDTO> queryAll(UserQueryCriteria criteria);
|
||||
|
||||
void download(List<UserDTO> queryAll, HttpServletResponse response);
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import me.zhengjie.modules.system.service.dto.RoleQueryCriteria;
|
|||
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
|
||||
import me.zhengjie.modules.system.service.mapper.RoleMapper;
|
||||
import me.zhengjie.modules.system.service.mapper.RoleSmallMapper;
|
||||
import me.zhengjie.utils.FileUtil;
|
||||
import me.zhengjie.utils.PageUtil;
|
||||
import me.zhengjie.utils.QueryHelp;
|
||||
import me.zhengjie.utils.ValidationUtil;
|
||||
|
@ -19,6 +20,8 @@ import org.springframework.data.domain.Pageable;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -44,6 +47,11 @@ public class RoleServiceImpl implements RoleService {
|
|||
return roleMapper.toDto(roleRepository.findAll(pageable).getContent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RoleDTO> queryAll(RoleQueryCriteria criteria) {
|
||||
return roleMapper.toDto(roleRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object queryAll(RoleQueryCriteria criteria, Pageable pageable) {
|
||||
Page<Role> page = roleRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
|
||||
|
|
|
@ -1,14 +1,19 @@
|
|||
package me.zhengjie.modules.system.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.poi.excel.ExcelUtil;
|
||||
import cn.hutool.poi.excel.ExcelWriter;
|
||||
import me.zhengjie.modules.monitor.service.RedisService;
|
||||
import me.zhengjie.modules.system.domain.User;
|
||||
import me.zhengjie.exception.EntityExistException;
|
||||
import me.zhengjie.exception.EntityNotFoundException;
|
||||
import me.zhengjie.modules.system.repository.UserRepository;
|
||||
import me.zhengjie.modules.system.service.UserService;
|
||||
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
|
||||
import me.zhengjie.modules.system.service.dto.UserDTO;
|
||||
import me.zhengjie.modules.system.service.dto.UserQueryCriteria;
|
||||
import me.zhengjie.modules.system.service.mapper.UserMapper;
|
||||
import me.zhengjie.utils.FileUtil;
|
||||
import me.zhengjie.utils.PageUtil;
|
||||
import me.zhengjie.utils.QueryHelp;
|
||||
import me.zhengjie.utils.ValidationUtil;
|
||||
|
@ -18,8 +23,11 @@ import org.springframework.data.domain.Pageable;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
|
@ -44,6 +52,12 @@ public class UserServiceImpl implements UserService {
|
|||
return PageUtil.toPage(page.map(userMapper::toDto));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserDTO> queryAll(UserQueryCriteria criteria) {
|
||||
List<User> users = userRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
|
||||
return userMapper.toDto(users);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserDTO findById(long id) {
|
||||
Optional<User> user = userRepository.findById(id);
|
||||
|
@ -144,4 +158,25 @@ public class UserServiceImpl implements UserService {
|
|||
public void updateEmail(String username, String email) {
|
||||
userRepository.updateEmail(username,email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void download(List<UserDTO> queryAll, HttpServletResponse response) {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
for (UserDTO userDTO : queryAll) {
|
||||
List roles = userDTO.getRoles().stream().map(RoleSmallDTO::getName).collect(Collectors.toList());
|
||||
Map map = new LinkedHashMap();
|
||||
map.put("用户名", userDTO.getUsername());
|
||||
map.put("头像", userDTO.getAvatar());
|
||||
map.put("邮箱", userDTO.getEmail());
|
||||
map.put("状态", userDTO.getEnabled() ? "启用" : "禁用");
|
||||
map.put("手机号码", userDTO.getPhone());
|
||||
map.put("角色", roles);
|
||||
map.put("部门", userDTO.getDept().getName());
|
||||
map.put("岗位", userDTO.getJob().getName());
|
||||
map.put("最后修改密码的时间", userDTO.getLastPasswordResetTime());
|
||||
map.put("创建日期", userDTO.getCreateTime());
|
||||
list.add(map);
|
||||
}
|
||||
FileUtil.downloadExcel(list, response);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,8 @@
|
|||
<appender-ref ref="console" />
|
||||
</logger>
|
||||
|
||||
<logger name="jdbc.resultsettable" level="INFO" additivity="false">
|
||||
<!-- 如想看到表格数据,将OFF改为INFO -->
|
||||
<logger name="jdbc.resultsettable" level="OFF" additivity="false">
|
||||
<appender-ref ref="console" />
|
||||
</logger>
|
||||
|
||||
|
|
Loading…
Reference in New Issue