|
@@ -3,7 +3,6 @@ package com.qunzhixinxi.hnqz.admin.service.impl;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.date.DatePattern;
|
|
|
-import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
@@ -127,7 +126,7 @@ public class SysUserExportServiceImpl implements SysUserExportService {
|
|
|
* @param user 用户
|
|
|
* @param roles 角色
|
|
|
* @param query 查询
|
|
|
- * @return {@link WmReportOpt } 状态和结果
|
|
|
+ * @return {@link Boolean } 是否成功
|
|
|
*/
|
|
|
@Override
|
|
|
public Boolean export(HnqzUser user, List<Integer> roles, SysUserDTO.OnList query) {
|
|
@@ -139,49 +138,53 @@ public class SysUserExportServiceImpl implements SysUserExportService {
|
|
|
// 缓存文件名
|
|
|
String fileName = "人员_" + DateTimeFormatter.ofPattern(DatePattern.PURE_DATE_PATTERN)
|
|
|
.format(LocalDateTime.now()) + RandomStringUtils.randomNumeric(6) + ".xlsx";
|
|
|
+ String fullPath = tempPath + fileName;
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 查询用户列表
|
|
|
+ List<SysUser> users = this.listUsers(user, roles, query);
|
|
|
|
|
|
- // 查询用户列表
|
|
|
- List<SysUser> users = this.listUsers(user, roles, query);
|
|
|
+ if (CollUtil.isEmpty(users)) {
|
|
|
+ log.info("用户列表为空");
|
|
|
+ redisTemplate.opsForValue().set(key, ERROR_MSG_NO_DATA, DEF_REPORT_TTL, TimeUnit.MILLISECONDS);
|
|
|
+ return Boolean.FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建Excel数据并导出
|
|
|
+ String resultValue = buildAndExportExcel(users, fullPath, fileName, user);
|
|
|
+ redisTemplate.opsForValue().set(key, resultValue, DEF_REPORT_TTL, TimeUnit.MILLISECONDS);
|
|
|
+
|
|
|
+ return !StrUtil.startWith(resultValue, "ERROR");
|
|
|
+ } finally {
|
|
|
+ // 清理临时文件
|
|
|
+ cleanupTempFile(fullPath);
|
|
|
|
|
|
- if (CollUtil.isEmpty(users)) {
|
|
|
- log.info("用户列表为空");
|
|
|
- redisTemplate.opsForValue().set(key, ERROR_MSG_NO_DATA, DEF_REPORT_TTL, TimeUnit.MILLISECONDS);
|
|
|
- return Boolean.FALSE;
|
|
|
+ // 删除限流key
|
|
|
+ String limitKey = CacheConstants.ASYNC_EXPORT_LIMIT_KEY;
|
|
|
+ redisTemplate.delete(limitKey);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- String resultValue;
|
|
|
+ /**
|
|
|
+ * 构建Excel数据并导出到文件
|
|
|
+ *
|
|
|
+ * @param users 用户列表
|
|
|
+ * @param fullPath 完整文件路径
|
|
|
+ * @param fileName 文件名
|
|
|
+ * @param user 当前用户
|
|
|
+ * @return 结果值(URL或错误信息)
|
|
|
+ */
|
|
|
+ private String buildAndExportExcel(List<SysUser> users, String fullPath, String fileName, HnqzUser user) {
|
|
|
try {
|
|
|
// 查询企业信息
|
|
|
- Set<Integer> deptIds = users.stream().map(SysUser::getDeptId).collect(Collectors.toSet());
|
|
|
- Map<Integer, SysDept> deptMap = new HashMap<>();
|
|
|
- if (CollUtil.isNotEmpty(deptIds)) {
|
|
|
- List<SysDept> sysDepts = deptMapper.selectBatchIds(deptIds);
|
|
|
- deptMap.putAll(sysDepts.stream().collect(Collectors.toMap(SysDept::getDeptId, Function.identity())));
|
|
|
- }
|
|
|
- // 查询角色
|
|
|
- Set<Integer> userIds = users.stream().map(SysUser::getUserId).collect(Collectors.toSet());
|
|
|
- List<SysUserRole> userRoles = userRoleMapper.selectList(Wrappers.<SysUserRole>lambdaQuery().in(SysUserRole::getUserId, userIds));
|
|
|
-
|
|
|
- Map<Integer, List<SysUserRole>> userRolesMap = userRoles.stream().collect(Collectors.groupingBy(SysUserRole::getUserId));
|
|
|
-
|
|
|
- Map<Integer, String> roleMap = new HashMap<>();
|
|
|
- if (CollUtil.isNotEmpty(userRoles)) {
|
|
|
- Set<Integer> roleIds = userRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toSet());
|
|
|
- List<SysRole> resultRoles = roleMapper.selectBatchIds(roleIds);
|
|
|
- if (CollUtil.isNotEmpty(resultRoles)) {
|
|
|
- roleMap.putAll(resultRoles.stream().collect(Collectors.toMap(SysRole::getRoleId, SysRole::getRoleName)));
|
|
|
- }
|
|
|
- }
|
|
|
+ Map<Integer, SysDept> deptMap = queryDeptInfo(users);
|
|
|
|
|
|
- // 查询备案信息
|
|
|
- List<SysUserCertificate> userCertificates =
|
|
|
- userCertificateMapper.selectList(Wrappers.<SysUserCertificate>lambdaQuery()
|
|
|
- .in(SysUserCertificate::getUserId, userIds)
|
|
|
- .eq(SysUserCertificate::getType, "REG")
|
|
|
- .select(SysUserCertificate::getCertificateId, SysUserCertificate::getUserId));
|
|
|
+ // 查询角色信息
|
|
|
+ Map<Integer, String> roleMap = queryRoleInfo(users);
|
|
|
+ Map<Integer, List<SysUserRole>> userRolesMap = queryUserRoles(users);
|
|
|
|
|
|
- Map<Integer, Long> userCertMap = userCertificates.stream()
|
|
|
- .collect(Collectors.groupingBy(SysUserCertificate::getUserId, Collectors.counting()));
|
|
|
+ // 查询备案信息
|
|
|
+ Map<Integer, Long> userCertMap = queryCertificateInfo(users);
|
|
|
|
|
|
// 转为excel列表
|
|
|
List<SysUserExcelModel> excelModels = users.stream().map(u -> {
|
|
@@ -215,43 +218,98 @@ public class SysUserExportServiceImpl implements SysUserExportService {
|
|
|
return excelModel;
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
- String fullPath = tempPath + fileName;
|
|
|
// 写入excel文件
|
|
|
EasyExcel.write(fullPath, SysUserExcelModel.class).sheet("人员")
|
|
|
.doWrite(excelModels);
|
|
|
log.info("人员导出生成缓存文件:{}", fullPath);
|
|
|
|
|
|
- FileInputStream inputStream = new FileInputStream(fullPath);
|
|
|
// 上传oss
|
|
|
- Map<String, String> uploadResult = fileService.upload(inputStream, fileName, fileName, user.getUsername());
|
|
|
- inputStream.close();
|
|
|
- log.info("人员导出生成oss文件:{}", uploadResult);
|
|
|
-
|
|
|
- if (CollUtil.isNotEmpty(uploadResult)) {
|
|
|
- resultValue = uploadResult.get("url");
|
|
|
- } else {
|
|
|
- resultValue = ERROR_MSG_UPLOAD_FAIL;
|
|
|
+ try (FileInputStream inputStream = new FileInputStream(fullPath)) {
|
|
|
+ Map<String, String> uploadResult = fileService.upload(inputStream, fileName, fileName, user.getUsername());
|
|
|
+ log.info("人员导出生成oss文件:{}", uploadResult);
|
|
|
+
|
|
|
+ if (CollUtil.isNotEmpty(uploadResult)) {
|
|
|
+ return uploadResult.get("url");
|
|
|
+ } else {
|
|
|
+ return ERROR_MSG_UPLOAD_FAIL;
|
|
|
+ }
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.error("人员导出失败", e);
|
|
|
- resultValue = ERROR_MSG_UNKNOWN;
|
|
|
- } finally {
|
|
|
- // 移除缓存文件
|
|
|
- File file = new File(fileName);
|
|
|
- if (file.exists()) {
|
|
|
- file.delete();
|
|
|
- }
|
|
|
+ return ERROR_MSG_UNKNOWN;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // 删除限流key
|
|
|
- String limitKey = CacheConstants.ASYNC_EXPORT_LIMIT_KEY;
|
|
|
- redisTemplate.delete(limitKey);
|
|
|
+ /**
|
|
|
+ * 查询部门信息
|
|
|
+ */
|
|
|
+ private Map<Integer, SysDept> queryDeptInfo(List<SysUser> users) {
|
|
|
+ Set<Integer> deptIds = users.stream().map(SysUser::getDeptId).collect(Collectors.toSet());
|
|
|
+ Map<Integer, SysDept> deptMap = new HashMap<>();
|
|
|
+ if (CollUtil.isNotEmpty(deptIds)) {
|
|
|
+ List<SysDept> sysDepts = deptMapper.selectBatchIds(deptIds);
|
|
|
+ deptMap.putAll(sysDepts.stream().collect(Collectors.toMap(SysDept::getDeptId, Function.identity())));
|
|
|
+ }
|
|
|
+ return deptMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询角色信息
|
|
|
+ */
|
|
|
+ private Map<Integer, String> queryRoleInfo(List<SysUser> users) {
|
|
|
+ Set<Integer> userIds = users.stream().map(SysUser::getUserId).collect(Collectors.toSet());
|
|
|
+ List<SysUserRole> userRoles = userRoleMapper.selectList(Wrappers.<SysUserRole>lambdaQuery().in(SysUserRole::getUserId, userIds));
|
|
|
+
|
|
|
+ Map<Integer, String> roleMap = new HashMap<>();
|
|
|
+ if (CollUtil.isNotEmpty(userRoles)) {
|
|
|
+ Set<Integer> roleIds = userRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toSet());
|
|
|
+ List<SysRole> resultRoles = roleMapper.selectBatchIds(roleIds);
|
|
|
+ if (CollUtil.isNotEmpty(resultRoles)) {
|
|
|
+ roleMap.putAll(resultRoles.stream().collect(Collectors.toMap(SysRole::getRoleId, SysRole::getRoleName)));
|
|
|
+ }
|
|
|
}
|
|
|
+ return roleMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询用户角色映射
|
|
|
+ */
|
|
|
+ private Map<Integer, List<SysUserRole>> queryUserRoles(List<SysUser> users) {
|
|
|
+ Set<Integer> userIds = users.stream().map(SysUser::getUserId).collect(Collectors.toSet());
|
|
|
+ List<SysUserRole> userRoles = userRoleMapper.selectList(Wrappers.<SysUserRole>lambdaQuery().in(SysUserRole::getUserId, userIds));
|
|
|
+ return userRoles.stream().collect(Collectors.groupingBy(SysUserRole::getUserId));
|
|
|
+ }
|
|
|
|
|
|
- redisTemplate.opsForValue().set(key, resultValue, DEF_REPORT_TTL, TimeUnit.MILLISECONDS);
|
|
|
+ /**
|
|
|
+ * 查询备案信息
|
|
|
+ */
|
|
|
+ private Map<Integer, Long> queryCertificateInfo(List<SysUser> users) {
|
|
|
+ Set<Integer> userIds = users.stream().map(SysUser::getUserId).collect(Collectors.toSet());
|
|
|
+ List<SysUserCertificate> userCertificates =
|
|
|
+ userCertificateMapper.selectList(Wrappers.<SysUserCertificate>lambdaQuery()
|
|
|
+ .in(SysUserCertificate::getUserId, userIds)
|
|
|
+ .eq(SysUserCertificate::getType, "REG")
|
|
|
+ .select(SysUserCertificate::getCertificateId, SysUserCertificate::getUserId));
|
|
|
+
|
|
|
+ return userCertificates.stream()
|
|
|
+ .collect(Collectors.groupingBy(SysUserCertificate::getUserId, Collectors.counting()));
|
|
|
+ }
|
|
|
|
|
|
- return StrUtil.startWith(resultValue, "ERROR") ? Boolean.FALSE : Boolean.TRUE;
|
|
|
+ /**
|
|
|
+ * 清理临时文件
|
|
|
+ */
|
|
|
+ private void cleanupTempFile(String fullPath) {
|
|
|
+ try {
|
|
|
+ File file = new File(fullPath);
|
|
|
+ if (file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("删除临时文件失败: {}", fullPath, e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 列出用户
|
|
|
*
|