From f942b34d02928e65aea16e49036e9b230fb4f7e1 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 14 Jan 2021 01:18:56 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=AE=8C=E6=88=90=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E7=9A=84=E5=8A=9F=E8=83=BD=202.=20=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E5=89=8D=E5=90=8E=E7=AB=AF=E7=9A=84=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E7=9A=84=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-ui/src/main.js | 3 +- ruoyi-ui/src/utils/ruoyi.js | 15 +++ ruoyi-ui/src/views/system/user/index.vue | 17 +-- .../excel/core/convert/DictConvert.java | 2 +- .../framework/excel/core/util/ExcelUtils.java | 27 +++++ .../controller/user/SysUserController.java | 45 +++++--- .../user/vo/user/SysUserExcelVO.java | 2 +- .../user/vo/user/SysUserExportReqVO.java | 40 +++++++ .../system/convert/user/SysUserConvert.java | 3 + .../dal/mysql/dao/user/SysUserMapper.java | 12 +- .../system/service/dept/SysDeptService.java | 18 +++ .../dict/impl/SysDictDataServiceImpl.java | 2 + .../system/service/user/SysUserService.java | 106 ++---------------- .../service/user/SysUserServiceImpl.java | 31 +++-- .../util/collection/CollectionUtils.java | 25 +++++ .../dashboard/util/collection/MapUtils.java | 29 +++++ 16 files changed, 246 insertions(+), 131 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/excel/core/util/ExcelUtils.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserExportReqVO.java diff --git a/ruoyi-ui/src/main.js b/ruoyi-ui/src/main.js index f8f3980f1..c7fafc017 100644 --- a/ruoyi-ui/src/main.js +++ b/ruoyi-ui/src/main.js @@ -16,7 +16,7 @@ import './assets/icons' // icon import './permission' // permission control import { getDicts } from "@/api/system/dict/data"; import { getConfigKey } from "@/api/system/config"; -import { parseTime, resetForm, addDateRange, selectDictLabel, selectDictLabels, download, handleTree } from "@/utils/ruoyi"; +import { parseTime, resetForm, addDateRange, selectDictLabel, selectDictLabels, download, handleTree, downloadExcel } from "@/utils/ruoyi"; import Pagination from "@/components/Pagination"; // 自定义表格工具扩展 import RightToolbar from "@/components/RightToolbar" @@ -33,6 +33,7 @@ Vue.prototype.addDateRange = addDateRange Vue.prototype.selectDictLabel = selectDictLabel Vue.prototype.selectDictLabels = selectDictLabels Vue.prototype.download = download +Vue.prototype.downloadExcel = downloadExcel Vue.prototype.handleTree = handleTree Vue.prototype.msgSuccess = function (msg) { diff --git a/ruoyi-ui/src/utils/ruoyi.js b/ruoyi-ui/src/utils/ruoyi.js index 46eb9d271..5bb492dff 100644 --- a/ruoyi-ui/src/utils/ruoyi.js +++ b/ruoyi-ui/src/utils/ruoyi.js @@ -101,6 +101,21 @@ export function download(fileName) { window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true; } +// 下载 Excel 方法 +export function downloadExcel(data, fileName) { + // 创建 blob + let blob = new Blob([data], {type: 'application/vnd.ms-excel'}); + // 创建 href 超链接,点击进行下载 + window.URL = window.URL || window.webkitURL; + let href = URL.createObjectURL(blob); + let downA = document.createElement("a"); + downA.href = href; + downA.download = fileName; + downA.click(); + // 销毁超连接 + window.URL.revokeObjectURL(href); +} + // 字符串格式化(%s ) export function sprintf(str) { var args = arguments, flag = true, i = 1; diff --git a/ruoyi-ui/src/views/system/user/index.vue b/ruoyi-ui/src/views/system/user/index.vue index 15e18205a..b9c6dcc06 100644 --- a/ruoyi-ui/src/views/system/user/index.vue +++ b/ruoyi-ui/src/views/system/user/index.vue @@ -510,7 +510,7 @@ export default { this.reset(); }, // 取消按钮(角色权限) - cancelMenu() { + cancelRole() { this.openRole = false; this.reset(); }, @@ -647,7 +647,10 @@ export default { }, /** 导出按钮操作 */ handleExport() { - const queryParams = this.queryParams; + const queryParams = this.addDateRange(this.queryParams, [ + this.dateRange[0] ? this.dateRange[0] + ' 00:00:00' : undefined, + this.dateRange[1] ? this.dateRange[1] + ' 23:59:59' : undefined, + ]); this.$confirm('是否确认导出所有用户数据项?', "警告", { confirmButtonText: "确定", cancelButtonText: "取消", @@ -655,15 +658,7 @@ export default { }).then(function() { return exportUser(queryParams); }).then(response => { - // this.download(response.msg); - let blob = new Blob([response], {type: 'application/vnd.ms-excel'}); - window.URL = window.URL || window.webkitURL; - let href = URL.createObjectURL(blob); - let downA = document.createElement("a"); - downA.href = href;// - downA.download = '123321.xls'; - downA.click(); - window.URL.revokeObjectURL(href); + this.downloadExcel(response, '用户数据.xls'); }) }, /** 导入按钮操作 */ diff --git a/src/main/java/cn/iocoder/dashboard/framework/excel/core/convert/DictConvert.java b/src/main/java/cn/iocoder/dashboard/framework/excel/core/convert/DictConvert.java index 1a65505d9..f52ddca29 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/excel/core/convert/DictConvert.java +++ b/src/main/java/cn/iocoder/dashboard/framework/excel/core/convert/DictConvert.java @@ -54,7 +54,7 @@ public class DictConvert implements Converter { return null; } // 生成 Excel 小表格 - return new CellData<>(dictData.getValue()); + return new CellData<>(dictData.getLabel()); } private static DictTypeEnum getType(ExcelContentProperty contentProperty) { diff --git a/src/main/java/cn/iocoder/dashboard/framework/excel/core/util/ExcelUtils.java b/src/main/java/cn/iocoder/dashboard/framework/excel/core/util/ExcelUtils.java new file mode 100644 index 000000000..d79e82013 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/excel/core/util/ExcelUtils.java @@ -0,0 +1,27 @@ +package cn.iocoder.dashboard.framework.excel.core.util; + +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.net.URLEncoder; +import java.util.List; + +/** + * Excel 工具类 + */ +public class ExcelUtils { + + public static void write(HttpServletResponse response, String filename, String sheetName, + Class head, List data) throws IOException { + // 设置 header 和 contentType + response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); + response.setContentType("application/vnd.ms-excel;charset=UTF-8"); + // 输出 Excel + EasyExcel.write(response.getOutputStream(), head) + .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度 + .sheet(sheetName).doWrite(data); + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java index 4018a5a22..188e4520e 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java @@ -3,6 +3,7 @@ package cn.iocoder.dashboard.modules.system.controller.user; import cn.hutool.core.collection.CollUtil; import cn.iocoder.dashboard.common.pojo.CommonResult; import cn.iocoder.dashboard.common.pojo.PageResult; +import cn.iocoder.dashboard.framework.excel.core.util.ExcelUtils; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.*; import cn.iocoder.dashboard.modules.system.convert.user.SysUserConvert; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dept.SysDeptDO; @@ -10,7 +11,7 @@ import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO; import cn.iocoder.dashboard.modules.system.service.dept.SysDeptService; import cn.iocoder.dashboard.modules.system.service.user.SysUserService; import cn.iocoder.dashboard.util.collection.CollectionUtils; -import com.alibaba.excel.EasyExcel; +import cn.iocoder.dashboard.util.collection.MapUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; @@ -20,7 +21,10 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; import static cn.iocoder.dashboard.common.pojo.CommonResult.success; @@ -37,7 +41,7 @@ public class SysUserController { @ApiOperation("获得用户分页列表") @GetMapping("/page") // @PreAuthorize("@ss.hasPermi('system:user:list')") - public CommonResult> pageUsers(SysUserPageReqVO reqVO) { + public CommonResult> pageUsers(@Validated SysUserPageReqVO reqVO) { // 获得用户分页列表 PageResult pageResult = userService.pageUsers(reqVO); if (CollUtil.isEmpty(pageResult.getList())) { @@ -45,13 +49,8 @@ public class SysUserController { } // 获得拼接需要的数据 - Map deptMap; Collection deptIds = CollectionUtils.convertList(pageResult.getList(), SysUserDO::getDeptId); - if (CollUtil.isNotEmpty(deptIds)) { - deptMap = CollectionUtils.convertMap(deptService.listDepts(deptIds), SysDeptDO::getId); - } else { - deptMap = Collections.emptyMap(); - } + Map deptMap = deptService.getDeptMap(deptIds); // 拼接结果返回 List userList = new ArrayList<>(pageResult.getList().size()); pageResult.getList().forEach(user -> { @@ -121,11 +120,29 @@ public class SysUserController { @ApiOperation("导出用户") @GetMapping("/export") - public void exportUsers(HttpServletResponse response) throws IOException { - String fileName = "测试文件.xls"; - response.addHeader("Content-Disposition", "attachment;filename=" + fileName); - response.setContentType("application/vnd.ms-excel;charset=UTF-8"); - EasyExcel.write(response.getOutputStream(), SysUserExcelVO.class).sheet().doWrite(new ArrayList<>()); +// @PreAuthorize("@ss.hasPermi('system:user:export')") , @Validated SysUserExportReqVO reqVO +// @Log(title = "用户管理", businessType = BusinessType.EXPORT) + public void exportUsers(HttpServletResponse response, @Validated SysUserExportReqVO reqVO) throws IOException { + // 获得用户列表 + List users = userService.listUsers(reqVO); + + // 获得拼接需要的数据 + Collection deptIds = CollectionUtils.convertList(users, SysUserDO::getDeptId); + Map deptMap = deptService.getDeptMap(deptIds); + // 拼接数据 + List excelUsers = new ArrayList<>(users.size()); + users.forEach(user -> { + SysUserExcelVO excelVO = SysUserConvert.INSTANCE.convert02(user); + MapUtils.findAndThen(deptMap, user.getDeptId(), dept -> { + excelVO.setDeptName(dept.getName()); + excelVO.setDeptLeader(dept.getLeader()); + }); + excelUsers.add(excelVO); + }); + + // 输出 + ExcelUtils.write(response, "用户数据.xls", "用户列表", + SysUserExcelVO.class, excelUsers); } // @Log(title = "用户管理", businessType = BusinessType.EXPORT) diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserExcelVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserExcelVO.java index b3ca002dd..149fb175c 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserExcelVO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserExcelVO.java @@ -22,7 +22,7 @@ public class SysUserExcelVO { @ExcelProperty("用户名称") private String username; - @ExcelProperty("用户名称") + @ExcelProperty("用户昵称") private String nickname; @ExcelProperty("用户邮箱") diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserExportReqVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserExportReqVO.java new file mode 100644 index 000000000..8076c4748 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserExportReqVO.java @@ -0,0 +1,40 @@ +package cn.iocoder.dashboard.modules.system.controller.user.vo.user; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; + +@ApiModel(value = "用户导出 Request VO", description = "参数和 SysUserPageReqVO 是一致的") +@Data +@NoArgsConstructor +@AllArgsConstructor +public class SysUserExportReqVO { + + @ApiModelProperty(value = "用户账号", example = "yudao", notes = "模糊匹配") + private String username; + + @ApiModelProperty(value = "手机号码", example = "yudao", notes = "模糊匹配") + private String mobile; + + @ApiModelProperty(value = "展示状态", example = "1", notes = "参见 SysCommonStatusEnum 枚举类") + private Integer status; + + @ApiModelProperty(value = "开始时间", example = "2020-10-24") + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + private Date beginTime; + + @ApiModelProperty(value = "结束时间", example = "2020-10-24") + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + private Date endTime; + + @ApiModelProperty(value = "部门编号", example = "1024", notes = "同时筛选子部门") + private Long deptId; + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/convert/user/SysUserConvert.java b/src/main/java/cn/iocoder/dashboard/modules/system/convert/user/SysUserConvert.java index 00219604a..ea5b87f79 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/convert/user/SysUserConvert.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/convert/user/SysUserConvert.java @@ -2,6 +2,7 @@ package cn.iocoder.dashboard.modules.system.convert.user; import cn.iocoder.dashboard.common.pojo.PageResult; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserCreateReqVO; +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserExcelVO; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageItemRespVO; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dept.SysDeptDO; @@ -27,4 +28,6 @@ public interface SysUserConvert { SysUserDO convert(SysUserUpdateReqVO bean); + SysUserExcelVO convert02(SysUserDO bean); + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/user/SysUserMapper.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/user/SysUserMapper.java index 0f7ab5d9a..225195f12 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/user/SysUserMapper.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/user/SysUserMapper.java @@ -2,6 +2,7 @@ package cn.iocoder.dashboard.modules.system.dal.mysql.dao.user; import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX; import cn.iocoder.dashboard.framework.mybatis.core.util.MyBatisUtils; +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserExportReqVO; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageReqVO; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; @@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import org.apache.ibatis.annotations.Mapper; +import java.util.Collection; import java.util.List; @Mapper @@ -26,7 +28,7 @@ public interface SysUserMapper extends BaseMapper { return selectOne(new QueryWrapper().eq("email", email)); } - default IPage selectList(SysUserPageReqVO reqVO, List deptIds) { + default IPage selectList(SysUserPageReqVO reqVO, Collection deptIds) { return selectPage(MyBatisUtils.buildPage(reqVO), new QueryWrapperX().likeIfPresent("username", reqVO.getUsername()) .likeIfPresent("mobile", reqVO.getMobile()) @@ -35,5 +37,13 @@ public interface SysUserMapper extends BaseMapper { .inIfPresent("dept_id", deptIds)); } + default List selectList(SysUserExportReqVO reqVO, Collection deptIds) { + return selectList(new QueryWrapperX().likeIfPresent("username", reqVO.getUsername()) + .likeIfPresent("mobile", reqVO.getMobile()) + .eqIfPresent("status", reqVO.getStatus()) + .betweenIfPresent("create_time", reqVO.getBeginTime(), reqVO.getEndTime()) + .inIfPresent("dept_id", deptIds)); + } + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/dept/SysDeptService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/dept/SysDeptService.java index 01fcdba40..247634bb6 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/dept/SysDeptService.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/dept/SysDeptService.java @@ -1,12 +1,16 @@ package cn.iocoder.dashboard.modules.system.service.dept; +import cn.hutool.core.collection.CollUtil; import cn.iocoder.dashboard.modules.system.controller.dept.vo.dept.SysDeptCreateReqVO; import cn.iocoder.dashboard.modules.system.controller.dept.vo.dept.SysDeptListReqVO; import cn.iocoder.dashboard.modules.system.controller.dept.vo.dept.SysDeptUpdateReqVO; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dept.SysDeptDO; +import cn.iocoder.dashboard.util.collection.CollectionUtils; import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.Map; /** * 部门 Service 接口 @@ -35,6 +39,20 @@ public interface SysDeptService { */ List listDepts(Collection ids); + /** + * 获得指定编号的部门 Map + * + * @param ids 部门编号数组 + * @return 部门 Map + */ + default Map getDeptMap(Collection ids) { + if (CollUtil.isEmpty(ids)) { + return Collections.emptyMap(); + } + List list = listDepts(ids); + return CollectionUtils.convertMap(list, SysDeptDO::getId); + } + /** * 筛选部门列表 * diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/impl/SysDictDataServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/impl/SysDictDataServiceImpl.java index f15b7fb5a..3321c0e92 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/impl/SysDictDataServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/impl/SysDictDataServiceImpl.java @@ -15,6 +15,7 @@ import cn.iocoder.dashboard.modules.system.service.dict.SysDictTypeService; import com.google.common.collect.ImmutableTable; import org.springframework.stereotype.Service; +import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Comparator; @@ -56,6 +57,7 @@ public class SysDictDataServiceImpl implements SysDictDataService { private SysDictDataMapper dictDataMapper; @Override + @PostConstruct public void init() { // 获得字典数据 List list = this.listDictDatas(); diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserService.java index e0d8faee6..897474864 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserService.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserService.java @@ -2,23 +2,19 @@ package cn.iocoder.dashboard.modules.system.service.user; import cn.iocoder.dashboard.common.pojo.PageResult; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserCreateReqVO; +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserExportReqVO; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageReqVO; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO; +import java.util.List; + /** * 用户 Service 接口 * * @author 芋道源码 */ public interface SysUserService { -// /** -// * 根据条件分页查询用户列表 -// * -// * @param user 用户信息 -// * @return 用户信息集合信息 -// */ -// public List selectUserList(SysUser user); /** * 通过用户名查询用户 @@ -44,6 +40,14 @@ public interface SysUserService { */ PageResult pageUsers(SysUserPageReqVO reqVO); + /** + * 获得用户列表 + * + * @param reqVO 列表请求 + * @return 用户列表 + */ + List listUsers(SysUserExportReqVO reqVO); + /** * 创建用户 * @@ -82,77 +86,6 @@ public interface SysUserService { */ void updateUserStatus(Long id, Integer status); -// -// /** -// * 根据用户ID查询用户所属角色组 -// * -// * @param userName 用户名 -// * @return 结果 -// */ -// public String selectUserRoleGroup(String userName); -// -// /** -// * 根据用户ID查询用户所属岗位组 -// * -// * @param userName 用户名 -// * @return 结果 -// */ -// public String selectUserPostGroup(String userName); -// -// /** -// * 校验用户名称是否唯一 -// * -// * @param userName 用户名称 -// * @return 结果 -// */ -// public String checkUserNameUnique(String userName); -// -// /** -// * 校验手机号码是否唯一 -// * -// * @param user 用户信息 -// * @return 结果 -// */ -// public String checkPhoneUnique(SysUser user); -// -// /** -// * 校验email是否唯一 -// * -// * @param user 用户信息 -// * @return 结果 -// */ -// public String checkEmailUnique(SysUser user); -// -// /** -// * 校验用户是否允许操作 -// * -// * @param user 用户信息 -// */ -// public void checkUserAllowed(SysUser user); -// -// /** -// * 新增用户信息 -// * -// * @param user 用户信息 -// * @return 结果 -// */ -// public int insertUser(SysUser user); -// -// /** -// * 修改用户信息 -// * -// * @param user 用户信息 -// * @return 结果 -// */ -// public int updateUser(SysUser user); -// -// /** -// * 修改用户状态 -// * -// * @param user 用户信息 -// * @return 结果 -// */ -// public int updateUserStatus(SysUser user); // // /** // * 修改用户基本信息 @@ -187,22 +120,7 @@ public interface SysUserService { // * @return 结果 // */ // public int resetUserPwd(String userName, String password); -// -// /** -// * 通过用户ID删除用户 -// * -// * @param userId 用户ID -// * @return 结果 -// */ -// public int deleteUserById(Long userId); -// -// /** -// * 批量删除用户信息 -// * -// * @param userIds 需要删除的用户ID -// * @return 结果 -// */ -// public int deleteUserByIds(Long[] userIds); + // // /** // * 导入用户数据 diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java index 882f56817..577222e89 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java @@ -6,6 +6,7 @@ import cn.iocoder.dashboard.common.enums.CommonStatusEnum; import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil; import cn.iocoder.dashboard.common.pojo.PageResult; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserCreateReqVO; +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserExportReqVO; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageReqVO; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO; import cn.iocoder.dashboard.modules.system.convert.user.SysUserConvert; @@ -77,15 +78,29 @@ public class SysUserServiceImpl implements SysUserService { @Override public PageResult pageUsers(SysUserPageReqVO reqVO) { - // 处理部门查询条件 - List deptIds = Collections.emptyList(); - if (reqVO.getDeptId() != null) { - deptIds = CollectionUtils.convertList(deptService.listDeptsByParentIdFromCache( - reqVO.getDeptId(), true), SysDeptDO::getId); - deptIds.add(reqVO.getDeptId()); + return SysUserConvert.INSTANCE.convertPage(userMapper.selectList(reqVO, + this.getDeptCondition(reqVO.getDeptId()))); + } + + @Override + public List listUsers(SysUserExportReqVO reqVO) { + return userMapper.selectList(reqVO, this.getDeptCondition(reqVO.getDeptId())); + } + + /** + * 获得部门条件:查询指定部门的子部门编号们,包括自身 + * + * @param deptId 部门编号 + * @return 部门编号集合 + */ + private Set getDeptCondition(Long deptId) { + if (deptId == null) { + return Collections.emptySet(); } - // 执行查询 - return SysUserConvert.INSTANCE.convertPage(userMapper.selectList(reqVO, deptIds)); + Set deptIds = CollectionUtils.convertSet(deptService.listDeptsByParentIdFromCache( + deptId, true), SysDeptDO::getId); + deptIds.add(deptId); // 包括自身 + return deptIds; } @Override diff --git a/src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java b/src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java index afb67fb3e..497c40780 100644 --- a/src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java +++ b/src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java @@ -1,5 +1,6 @@ package cn.iocoder.dashboard.util.collection; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; import java.util.*; @@ -19,37 +20,61 @@ public class CollectionUtils { } public static List filterList(Collection from, Predicate predicate) { + if (CollUtil.isEmpty(from)) { + return new ArrayList<>(); + } return from.stream().filter(predicate).collect(Collectors.toList()); } public static List convertList(List from, Function func) { + if (CollUtil.isEmpty(from)) { + return new ArrayList<>(); + } return from.stream().map(func).collect(Collectors.toList()); } public static Set convertSet(List from, Function func) { + if (CollUtil.isEmpty(from)) { + return new HashSet<>(); + } return from.stream().map(func).collect(Collectors.toSet()); } public static Map convertMap(List from, Function keyFunc) { + if (CollUtil.isEmpty(from)) { + return new HashMap<>(); + } return from.stream().collect(Collectors.toMap(keyFunc, item -> item)); } public static Map convertMap(List from, Function keyFunc, Function valueFunc) { + if (CollUtil.isEmpty(from)) { + return new HashMap<>(); + } return from.stream().collect(Collectors.toMap(keyFunc, valueFunc)); } public static Map> convertMultiMap(List from, Function keyFunc) { + if (CollUtil.isEmpty(from)) { + return new HashMap<>(); + } return from.stream().collect(Collectors.groupingBy(keyFunc, Collectors.mapping(t -> t, Collectors.toList()))); } public static Map> convertMultiMap(List from, Function keyFunc, Function valueFunc) { + if (CollUtil.isEmpty(from)) { + return new HashMap<>(); + } return from.stream().collect(Collectors.groupingBy(keyFunc, Collectors.mapping(valueFunc, Collectors.toList()))); } // 暂时没想好名字,先以 2 结尾噶 public static Map> convertMultiMap2(List from, Function keyFunc, Function valueFunc) { + if (CollUtil.isEmpty(from)) { + return new HashMap<>(); + } return from.stream().collect(Collectors.groupingBy(keyFunc, Collectors.mapping(valueFunc, Collectors.toSet()))); } diff --git a/src/main/java/cn/iocoder/dashboard/util/collection/MapUtils.java b/src/main/java/cn/iocoder/dashboard/util/collection/MapUtils.java index da54d431c..ebe29648f 100644 --- a/src/main/java/cn/iocoder/dashboard/util/collection/MapUtils.java +++ b/src/main/java/cn/iocoder/dashboard/util/collection/MapUtils.java @@ -1,11 +1,14 @@ package cn.iocoder.dashboard.util.collection; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; import com.google.common.collect.Multimap; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Map; +import java.util.function.Consumer; /** * Map 工具类 @@ -14,6 +17,13 @@ import java.util.List; */ public class MapUtils { + /** + * 从哈希表表中,获得 keys 对应的所有 value 数组 + * + * @param multimap 哈希表 + * @param keys keys + * @return value 数组 + */ public static List getList(Multimap multimap, Collection keys) { List result = new ArrayList<>(); keys.forEach(k -> { @@ -26,4 +36,23 @@ public class MapUtils { return result; } + /** + * 从哈希表查找到 key 对应的 value,然后进一步处理 + * 注意,如果查找到的 value 为 null 时,不进行处理 + * + * @param map 哈希表 + * @param key key + * @param consumer 进一步处理的逻辑 + */ + public static void findAndThen(Map map, K key, Consumer consumer) { + if (CollUtil.isEmpty(map)) { + return; + } + V value = map.get(key); + if (value == null) { + return; + } + consumer.accept(value); + } + }