From cbe05ba834b69e65f679be08d21eaf2cb1f7ee5d Mon Sep 17 00:00:00 2001 From: zhc077 <565291854> Date: Wed, 12 Apr 2023 17:17:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=BD=AF=E4=BB=B6=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=B3=BB=E7=BB=9F=20=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD=20=E4=BF=AE=E6=94=B9=204.1?= =?UTF-8?q?2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/controller/SysUserController.java | 1028 +++++++++-------- .../src/main/resources/application-dev.yml | 4 +- .../src/main/resources/application-prod.yml | 2 +- 3 files changed, 527 insertions(+), 507 deletions(-) diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/controller/SysUserController.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/controller/SysUserController.java index 113a1e0..4aa638e 100644 --- a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/controller/SysUserController.java +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/controller/SysUserController.java @@ -63,23 +63,23 @@ import java.util.stream.Collectors; @RestController @RequestMapping("/sys/user") public class SysUserController { - @Autowired - private ISysBaseAPI sysBaseAPI; + @Autowired + private ISysBaseAPI sysBaseAPI; - @Autowired - private ISysUserService sysUserService; + @Autowired + private ISysUserService sysUserService; @Autowired private ISysDepartService sysDepartService; - @Autowired - private ISysUserRoleService sysUserRoleService; + @Autowired + private ISysUserRoleService sysUserRoleService; - @Autowired - private ISysUserDepartService sysUserDepartService; + @Autowired + private ISysUserDepartService sysUserDepartService; - @Autowired - private ISysUserRoleService userRoleService; + @Autowired + private ISysUserRoleService userRoleService; @Autowired private ISysDepartRoleUserService departRoleUserService; @@ -87,8 +87,8 @@ public class SysUserController { @Autowired private ISysDepartRoleService departRoleService; - @Autowired - private RedisUtil redisUtil; + @Autowired + private RedisUtil redisUtil; @Value("${jeecg.path.upload}") private String upLoadPath; @@ -98,6 +98,7 @@ public class SysUserController { /** * 获取用户列表数据 + * * @param user * @param pageNo * @param pageSize @@ -105,35 +106,35 @@ public class SysUserController { * @return */ @PermissionData(pageComponent = "system/UserList") - @RequestMapping(value = "/list", method = RequestMethod.GET) - public Result> queryPageList(SysUser user,@RequestParam(name="pageNo", defaultValue="1") Integer pageNo, - @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest req) { - Result> result = new Result>(); - QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(user, req.getParameterMap()); - //TODO 外部模拟登陆临时账号,列表不显示 - queryWrapper.ne("username","_reserve_user_external"); - Page page = new Page(pageNo, pageSize); - IPage pageList = sysUserService.page(page, queryWrapper); - - //批量查询用户的所属部门 + @RequestMapping(value = "/list", method = RequestMethod.GET) + public Result> queryPageList(SysUser user, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, + @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { + Result> result = new Result>(); + QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(user, req.getParameterMap()); + //TODO 外部模拟登陆临时账号,列表不显示 + queryWrapper.ne("username", "_reserve_user_external"); + Page page = new Page(pageNo, pageSize); + IPage pageList = sysUserService.page(page, queryWrapper); + + //批量查询用户的所属部门 //step.1 先拿到全部的 useids //step.2 通过 useids,一次性查询用户的所属部门名字 List userIds = pageList.getRecords().stream().map(SysUser::getId).collect(Collectors.toList()); - if(userIds!=null && userIds.size()>0){ - Map useDepNames = sysUserService.getDepNamesByUserIds(userIds); - pageList.getRecords().forEach(item->{ + if (userIds != null && userIds.size() > 0) { + Map useDepNames = sysUserService.getDepNamesByUserIds(userIds); + pageList.getRecords().forEach(item -> { item.setOrgCodeTxt(useDepNames.get(item.getId())); }); } - result.setSuccess(true); - result.setResult(pageList); - log.info(pageList.toString()); - return result; - } - - @RequestMapping(value = "/student",method = RequestMethod.GET) - public Result> query(SysUser user,@RequestParam(name="pageNo", defaultValue="1") Integer pageNo, - @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest req) { + result.setSuccess(true); + result.setResult(pageList); + log.info(pageList.toString()); + return result; + } + + @RequestMapping(value = "/student", method = RequestMethod.GET) + public Result> query(SysUser user, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, + @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { Result> result = new Result>(); //QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(user, req.getParameterMap()); //TODO 外部模拟登陆临时账号,列表不显示 @@ -141,9 +142,9 @@ public class SysUserController { Page page = new Page(pageNo, pageSize); IPage pageList = sysUserService.selectStudent(page, user); List userIds = pageList.getRecords().stream().map(SysUser::getId).collect(Collectors.toList()); - if(userIds!=null && userIds.size()>0){ - Map useDepNames = sysUserService.getDepNamesByUserIds(userIds); - pageList.getRecords().forEach(item->{ + if (userIds != null && userIds.size() > 0) { + Map useDepNames = sysUserService.getDepNamesByUserIds(userIds); + pageList.getRecords().forEach(item -> { item.setOrgCodeTxt(useDepNames.get(item.getId())); }); } @@ -155,6 +156,7 @@ public class SysUserController { /** * 获取员工列表数据 + * * @param user * @param pageNo * @param pageSize @@ -163,13 +165,13 @@ public class SysUserController { */ @PermissionData(pageComponent = "system/StaffList") @RequestMapping(value = "/slist", method = RequestMethod.GET) - public Result> queryPageStaffList(SysUser user,@RequestParam(name="pageNo", defaultValue="1") Integer pageNo, - @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest req) { + public Result> queryPageStaffList(SysUser user, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, + @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { Result> result = new Result>(); QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(user, req.getParameterMap()); //TODO 外部模拟登陆临时账号,列表不显示 - queryWrapper.ne("username","_reserve_user_external"); - queryWrapper.eq("user_identity",3); + queryWrapper.ne("username", "_reserve_user_external"); + queryWrapper.eq("user_identity", 3); Page page = new Page(pageNo, pageSize); IPage pageList = sysUserService.page(page, queryWrapper); @@ -177,9 +179,9 @@ public class SysUserController { //step.1 先拿到全部的 useids //step.2 通过 useids,一次性查询用户的所属部门名字 List userIds = pageList.getRecords().stream().map(SysUser::getId).collect(Collectors.toList()); - if(userIds!=null && userIds.size()>0){ - Map useDepNames = sysUserService.getDepNamesByUserIds(userIds); - pageList.getRecords().forEach(item->{ + if (userIds != null && userIds.size() > 0) { + Map useDepNames = sysUserService.getDepNamesByUserIds(userIds); + pageList.getRecords().forEach(item -> { item.setOrgCodeTxt(useDepNames.get(item.getId())); }); } @@ -191,106 +193,110 @@ public class SysUserController { //@RequiresRoles({"admin"}) //@RequiresPermissions("user:add") - @RequestMapping(value = "/add", method = RequestMethod.POST) - public Result add(@RequestBody JSONObject jsonObject) { - Result result = new Result(); - String selectedRoles = jsonObject.getString("selectedroles"); - String selectedDeparts = jsonObject.getString("selecteddeparts"); - System.out.println("我的测试"+selectedRoles); - try { - SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class); - user.setCreateTime(new Date());//设置创建时间 - String salt = oConvertUtils.randomGen(8); - user.setSalt(salt); - String passwordEncode = PasswordUtil.encrypt(user.getUsername(), user.getPassword(), salt); - user.setPassword(passwordEncode); - user.setStatus(1); - user.setDelFlag(CommonConstant.DEL_FLAG_0); - // 保存用户走一个service 保证事务 - sysUserService.saveUser(user, selectedRoles, selectedDeparts); - result.success("添加成功!"); - } catch (Exception e) { - log.error(e.getMessage(), e); - result.error500("操作失败"); - } - return result; - } + @RequestMapping(value = "/add", method = RequestMethod.POST) + public Result add(@RequestBody JSONObject jsonObject) { + Result result = new Result(); + String selectedRoles = jsonObject.getString("selectedroles"); + String selectedDeparts = jsonObject.getString("selecteddeparts"); +// System.out.println("我的测试" + selectedRoles); + try { + SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class); + user.setCreateTime(new Date());//设置创建时间 + String salt = oConvertUtils.randomGen(8); + user.setSalt(salt); + String passwordEncode = PasswordUtil.encrypt(user.getUsername(), user.getPassword(), salt); + user.setPassword(passwordEncode); + user.setStatus(1); + user.setDelFlag(CommonConstant.DEL_FLAG_0); + // 保存用户走一个service 保证事务 + this.sysUserService.save(user); +// sysUserService.saveUser(user, selectedRoles, selectedDeparts); + result.success("添加成功!"); + } catch (Exception e) { + log.error(e.getMessage(), e); + result.error500("操作失败"); + } + return result; + } //@RequiresRoles({"admin"}) //@RequiresPermissions("user:edit") - @RequestMapping(value = "/edit", method = RequestMethod.PUT) - public Result edit(@RequestBody JSONObject jsonObject) { - Result result = new Result(); - try { - SysUser sysUser = sysUserService.getById(jsonObject.getString("id")); - baseCommonService.addLog("编辑用户,id: " +jsonObject.getString("id") ,CommonConstant.LOG_TYPE_2, 2); - if(sysUser==null) { - result.error500("未找到对应实体"); - }else { - SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class); - user.setUpdateTime(new Date()); - //String passwordEncode = PasswordUtil.encrypt(user.getUsername(), user.getPassword(), sysUser.getSalt()); - user.setPassword(sysUser.getPassword()); - String roles = jsonObject.getString("selectedroles"); + @RequestMapping(value = "/edit", method = RequestMethod.PUT) + public Result edit(@RequestBody JSONObject jsonObject) { + Result result = new Result(); + try { + SysUser sysUser = sysUserService.getById(jsonObject.getString("id")); + baseCommonService.addLog("编辑用户,id: " + jsonObject.getString("id"), CommonConstant.LOG_TYPE_2, 2); + if (sysUser == null) { + result.error500("未找到对应实体"); + } else { + SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class); + user.setUpdateTime(new Date()); + //String passwordEncode = PasswordUtil.encrypt(user.getUsername(), user.getPassword(), sysUser.getSalt()); + user.setPassword(sysUser.getPassword()); + String roles = jsonObject.getString("selectedroles"); String departs = jsonObject.getString("selecteddeparts"); // 修改用户走一个service 保证事务 - sysUserService.editUser(user, roles, departs); - result.success("修改成功!"); - } - } catch (Exception e) { - log.error(e.getMessage(), e); - result.error500("操作失败"); - } - return result; - } - - /** - * 删除用户 - */ - //@RequiresRoles({"admin"}) - @RequestMapping(value = "/delete", method = RequestMethod.DELETE) - public Result delete(@RequestParam(name="id",required=true) String id) { - baseCommonService.addLog("删除用户,id: " +id ,CommonConstant.LOG_TYPE_2, 3); - this.sysUserService.deleteUser(id); - return Result.ok("删除用户成功"); - } - - /** - * 批量删除用户 - */ - //@RequiresRoles({"admin"}) - @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE) - public Result deleteBatch(@RequestParam(name="ids",required=true) String ids) { - baseCommonService.addLog("批量删除用户, ids: " +ids ,CommonConstant.LOG_TYPE_2, 3); - this.sysUserService.deleteBatchUsers(ids); - return Result.ok("批量删除用户成功"); - } - - /** - * 冻结&解冻用户 - * @param jsonObject - * @return - */ - //@RequiresRoles({"admin"}) - @RequestMapping(value = "/frozenBatch", method = RequestMethod.PUT) - public Result frozenBatch(@RequestBody JSONObject jsonObject) { - Result result = new Result(); - try { - String ids = jsonObject.getString("ids"); - String status = jsonObject.getString("status"); - String[] arr = ids.split(","); - for (String id : arr) { - if(oConvertUtils.isNotEmpty(id)) { - this.sysUserService.update(new SysUser().setStatus(Integer.parseInt(status)), - new UpdateWrapper().lambda().eq(SysUser::getId,id)); - } - } - } catch (Exception e) { - log.error(e.getMessage(), e); - result.error500("操作失败"+e.getMessage()); - } - result.success("操作成功!"); - return result; + sysUserService.updateById(user); +// sysUserService.editUser(user, roles, departs); + result.success("修改成功!"); + } + } catch (Exception e) { + log.error(e.getMessage(), e); + result.error500("操作失败"); + } + return result; + } + + /** + * 删除用户 + */ + //@RequiresRoles({"admin"}) + @RequestMapping(value = "/delete", method = RequestMethod.DELETE) + public Result delete(@RequestParam(name = "id", required = true) String id) { + this.sysUserService.removeById(id); +// baseCommonService.addLog("删除用户,id: " + id, CommonConstant.LOG_TYPE_2, 3); +// this.sysUserService.deleteUser(id); + return Result.ok("删除用户成功"); + } + + /** + * 批量删除用户 + */ + //@RequiresRoles({"admin"}) + @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE) + public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) { + baseCommonService.addLog("批量删除用户, ids: " + ids, CommonConstant.LOG_TYPE_2, 3); + this.sysUserService.deleteBatchUsers(ids); + return Result.ok("批量删除用户成功"); + } + + /** + * 冻结&解冻用户 + * + * @param jsonObject + * @return + */ + //@RequiresRoles({"admin"}) + @RequestMapping(value = "/frozenBatch", method = RequestMethod.PUT) + public Result frozenBatch(@RequestBody JSONObject jsonObject) { + Result result = new Result(); + try { + String ids = jsonObject.getString("ids"); + String status = jsonObject.getString("status"); + String[] arr = ids.split(","); + for (String id : arr) { + if (oConvertUtils.isNotEmpty(id)) { + this.sysUserService.update(new SysUser().setStatus(Integer.parseInt(status)), + new UpdateWrapper().lambda().eq(SysUser::getId, id)); + } + } + } catch (Exception e) { + log.error(e.getMessage(), e); + result.error500("操作失败" + e.getMessage()); + } + result.success("操作成功!"); + return result; } @@ -315,11 +321,11 @@ public class SysUserController { * @return */ @AutoLog(value = "用户信息-通过username查询") - @ApiOperation(value="用户信息-通过username查询", notes="用户信息-通过username查询") + @ApiOperation(value = "用户信息-通过username查询", notes = "用户信息-通过username查询") @GetMapping(value = "/queryByName") public Result queryByName(@RequestParam(name = "username", required = true) String username) { SysUser sysUser = sysUserService.getUserByName(username); - if(sysUser==null) { + if (sysUser == null) { return Result.error("未找到对应数据"); } return Result.OK(sysUser); @@ -344,8 +350,8 @@ public class SysUserController { /** - * 校验用户账号是否唯一
- * 可以校验其他 需要检验什么就传什么。。。 + * 校验用户账号是否唯一
+ * 可以校验其他 需要检验什么就传什么。。。 * * @param sysUser * @return @@ -409,7 +415,7 @@ public class SysUserController { } return result; } catch (Exception e) { - log.error(e.getMessage(), e); + log.error(e.getMessage(), e); result.setSuccess(false); result.setMessage("查找过程中出现了异常: " + e.getMessage()); return result; @@ -439,19 +445,19 @@ public class SysUserController { * @return */ @RequestMapping(value = "/queryUserByDepId", method = RequestMethod.GET) - public Result> queryUserByDepId(@RequestParam(name = "id", required = true) String id,@RequestParam(name="realname",required=false) String realname) { + public Result> queryUserByDepId(@RequestParam(name = "id", required = true) String id, @RequestParam(name = "realname", required = false) String realname) { Result> result = new Result<>(); //List userList = sysUserDepartService.queryUserByDepId(id); SysDepart sysDepart = sysDepartService.getById(id); - List userList = sysUserDepartService.queryUserByDepCode(sysDepart.getOrgCode(),realname); + List userList = sysUserDepartService.queryUserByDepCode(sysDepart.getOrgCode(), realname); //批量查询用户的所属部门 //step.1 先拿到全部的 useids //step.2 通过 useids,一次性查询用户的所属部门名字 List userIds = userList.stream().map(SysUser::getId).collect(Collectors.toList()); - if(userIds!=null && userIds.size()>0){ - Map useDepNames = sysUserService.getDepNamesByUserIds(userIds); - userList.forEach(item->{ + if (userIds != null && userIds.size() > 0) { + Map useDepNames = sysUserService.getDepNamesByUserIds(userIds); + userList.forEach(item -> { //TODO 临时借用这个字段用于页面展示 item.setOrgCodeTxt(useDepNames.get(item.getId())); }); @@ -462,7 +468,7 @@ public class SysUserController { result.setResult(userList); return result; } catch (Exception e) { - log.error(e.getMessage(), e); + log.error(e.getMessage(), e); result.setSuccess(false); return result; } @@ -470,27 +476,29 @@ public class SysUserController { /** * 用户选择组件 专用 根据用户账号或部门分页查询 + * * @param departId * @param username * @return */ @RequestMapping(value = "/queryUserComponentData", method = RequestMethod.GET) public Result> queryUserComponentData( - @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, - @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, + @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, + @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, @RequestParam(name = "departId", required = false) String departId, - @RequestParam(name="realname",required=false) String realname, - @RequestParam(name="username",required=false) String username) { + @RequestParam(name = "realname", required = false) String realname, + @RequestParam(name = "username", required = false) String username) { IPage pageList = sysUserDepartService.queryDepartUserPageList(departId, username, realname, pageSize, pageNo); return Result.OK(pageList); } + @RequestMapping(value = "/queryStudentComponentData", method = RequestMethod.GET) public Result> queryStudentComponentData( - @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, - @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, + @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, + @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, @RequestParam(name = "departId", required = false) String departId, - @RequestParam(name="realname",required=false) String realname, - @RequestParam(name="username",required=false) String username) { + @RequestParam(name = "realname", required = false) String realname, + @RequestParam(name = "username", required = false) String username) { IPage pageList = sysUserDepartService.queryDepartStudentPageList(departId, username, realname, pageSize, pageNo); return Result.OK(pageList); } @@ -502,24 +510,24 @@ public class SysUserController { * @param sysUser */ @RequestMapping(value = "/exportXls") - public ModelAndView exportXls(SysUser sysUser,HttpServletRequest request) { + public ModelAndView exportXls(SysUser sysUser, HttpServletRequest request) { // Step.1 组装查询条件 QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(sysUser, request.getParameterMap()); //Step.2 AutoPoi 导出Excel ModelAndView mv = new ModelAndView(new JeecgEntityExcelView()); //update-begin--Author:kangxiaolin Date:20180825 for:[03]用户导出,如果选择数据则只导出相关数据-------------------- String selections = request.getParameter("selections"); - if(!oConvertUtils.isEmpty(selections)){ - queryWrapper.in("id",selections.split(",")); - } + if (!oConvertUtils.isEmpty(selections)) { + queryWrapper.in("id", selections.split(",")); + } //update-end--Author:kangxiaolin Date:20180825 for:[03]用户导出,如果选择数据则只导出相关数据---------------------- List pageList = sysUserService.list(queryWrapper); //导出文件名称 mv.addObject(NormalExcelConstants.FILE_NAME, "用户列表"); mv.addObject(NormalExcelConstants.CLASS, SysUser.class); - LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); - ExportParams exportParams = new ExportParams("用户列表数据", "导出人:"+user.getRealname(), "导出信息"); + LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); + ExportParams exportParams = new ExportParams("用户列表数据", "导出人:" + user.getRealname(), "导出信息"); exportParams.setImageBasePath(upLoadPath); mv.addObject(NormalExcelConstants.PARAMS, exportParams); mv.addObject(NormalExcelConstants.DATA_LIST, pageList); @@ -536,7 +544,7 @@ public class SysUserController { //@RequiresRoles({"admin"}) //@RequiresPermissions("user:import") @RequestMapping(value = "/importExcel", method = RequestMethod.POST) - public Result importExcel(HttpServletRequest request, HttpServletResponse response)throws IOException { + public Result importExcel(HttpServletRequest request, HttpServletResponse response) throws IOException { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Map fileMap = multipartRequest.getFileMap(); // 错误信息 @@ -602,58 +610,58 @@ public class SysUserController { try { file.getInputStream().close(); } catch (IOException e) { - log.error(e.getMessage(), e); + log.error(e.getMessage(), e); } } } - return ImportExcelUtil.imporReturnRes(errorLines,successLines,errorMessage); + return ImportExcelUtil.imporReturnRes(errorLines, successLines, errorMessage); } /** - * @功能:根据id 批量查询 - * @param userIds - * @return - */ - @RequestMapping(value = "/queryByIds", method = RequestMethod.GET) - public Result> queryByIds(@RequestParam String userIds) { - Result> result = new Result<>(); - String[] userId = userIds.split(","); - Collection idList = Arrays.asList(userId); - Collection userRole = sysUserService.listByIds(idList); - result.setSuccess(true); - result.setResult(userRole); - return result; - } - - /** - * 首页用户重置密码 - */ + * @param userIds + * @return + * @功能:根据id 批量查询 + */ + @RequestMapping(value = "/queryByIds", method = RequestMethod.GET) + public Result> queryByIds(@RequestParam String userIds) { + Result> result = new Result<>(); + String[] userId = userIds.split(","); + Collection idList = Arrays.asList(userId); + Collection userRole = sysUserService.listByIds(idList); + result.setSuccess(true); + result.setResult(userRole); + return result; + } + + /** + * 首页用户重置密码 + */ //@RequiresRoles({"admin"}) @RequestMapping(value = "/updatePassword", method = RequestMethod.PUT) - public Result updatePassword(@RequestBody JSONObject json) { - String username = json.getString("username"); - String oldpassword = json.getString("oldpassword"); - String password = json.getString("password"); - String confirmpassword = json.getString("confirmpassword"); - LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal(); - if(!sysUser.getUsername().equals(username)){ + public Result updatePassword(@RequestBody JSONObject json) { + String username = json.getString("username"); + String oldpassword = json.getString("oldpassword"); + String password = json.getString("password"); + String confirmpassword = json.getString("confirmpassword"); + LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); + if (!sysUser.getUsername().equals(username)) { return Result.error("只允许修改自己的密码!"); } - SysUser user = this.sysUserService.getOne(new LambdaQueryWrapper().eq(SysUser::getUsername, username)); - if(user==null) { - return Result.error("用户不存在!"); - } - return sysUserService.resetPassword(username,oldpassword,password,confirmpassword); - } + SysUser user = this.sysUserService.getOne(new LambdaQueryWrapper().eq(SysUser::getUsername, username)); + if (user == null) { + return Result.error("用户不存在!"); + } + return sysUserService.resetPassword(username, oldpassword, password, confirmpassword); + } @RequestMapping(value = "/userRoleList", method = RequestMethod.GET) - public Result> userRoleList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo, - @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) { + public Result> userRoleList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, + @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { Result> result = new Result>(); Page page = new Page(pageNo, pageSize); String roleId = req.getParameter("roleId"); String username = req.getParameter("username"); - IPage pageList = sysUserService.getUserByRoleId(page,roleId,username); + IPage pageList = sysUserService.getUserByRoleId(page, roleId, username); result.setSuccess(true); result.setResult(pageList); return result; @@ -671,12 +679,12 @@ public class SysUserController { Result result = new Result(); try { String sysRoleId = sysUserRoleVO.getRoleId(); - for(String sysUserId:sysUserRoleVO.getUserIdList()) { - SysUserRole sysUserRole = new SysUserRole(sysUserId,sysRoleId); + for (String sysUserId : sysUserRoleVO.getUserIdList()) { + SysUserRole sysUserRole = new SysUserRole(sysUserId, sysRoleId); QueryWrapper queryWrapper = new QueryWrapper(); - queryWrapper.eq("role_id", sysRoleId).eq("user_id",sysUserId); + queryWrapper.eq("role_id", sysRoleId).eq("user_id", sysUserId); SysUserRole one = sysUserRoleService.getOne(queryWrapper); - if(one==null){ + if (one == null) { sysUserRoleService.save(sysUserRole); } @@ -684,30 +692,32 @@ public class SysUserController { result.setMessage("添加成功!"); result.setSuccess(true); return result; - }catch(Exception e) { + } catch (Exception e) { log.error(e.getMessage(), e); result.setSuccess(false); result.setMessage("出错了: " + e.getMessage()); return result; } } + /** - * 删除指定角色的用户关系 + * 删除指定角色的用户关系 + * * @param * @return */ //@RequiresRoles({"admin"}) @RequestMapping(value = "/deleteUserRole", method = RequestMethod.DELETE) - public Result deleteUserRole(@RequestParam(name="roleId") String roleId, - @RequestParam(name="userId",required=true) String userId + public Result deleteUserRole(@RequestParam(name = "roleId") String roleId, + @RequestParam(name = "userId", required = true) String userId ) { Result result = new Result(); try { QueryWrapper queryWrapper = new QueryWrapper(); - queryWrapper.eq("role_id", roleId).eq("user_id",userId); + queryWrapper.eq("role_id", roleId).eq("user_id", userId); sysUserRoleService.remove(queryWrapper); result.success("删除成功!"); - }catch(Exception e) { + } catch (Exception e) { log.error(e.getMessage(), e); result.error500("删除失败!"); } @@ -723,15 +733,15 @@ public class SysUserController { //@RequiresRoles({"admin"}) @RequestMapping(value = "/deleteUserRoleBatch", method = RequestMethod.DELETE) public Result deleteUserRoleBatch( - @RequestParam(name="roleId") String roleId, - @RequestParam(name="userIds",required=true) String userIds) { + @RequestParam(name = "roleId") String roleId, + @RequestParam(name = "userIds", required = true) String userIds) { Result result = new Result(); try { QueryWrapper queryWrapper = new QueryWrapper(); - queryWrapper.eq("role_id", roleId).in("user_id",Arrays.asList(userIds.split(","))); + queryWrapper.eq("role_id", roleId).in("user_id", Arrays.asList(userIds.split(","))); sysUserRoleService.remove(queryWrapper); result.success("删除成功!"); - }catch(Exception e) { + } catch (Exception e) { log.error(e.getMessage(), e); result.error500("删除失败!"); } @@ -742,8 +752,8 @@ public class SysUserController { * 部门用户列表 */ @RequestMapping(value = "/departUserList", method = RequestMethod.GET) - public Result> departUserList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo, - @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) { + public Result> departUserList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, + @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { Result> result = new Result>(); Page page = new Page(pageNo, pageSize); String depId = req.getParameter("depId"); @@ -751,22 +761,22 @@ public class SysUserController { //根据部门ID查询,当前和下级所有的部门IDS List subDepids = new ArrayList<>(); //部门id为空时,查询我的部门下所有用户 - if(oConvertUtils.isEmpty(depId)){ + if (oConvertUtils.isEmpty(depId)) { LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); - int userIdentity = user.getUserIdentity() != null?user.getUserIdentity():CommonConstant.USER_IDENTITY_1; - if(oConvertUtils.isNotEmpty(userIdentity) && userIdentity == CommonConstant.USER_IDENTITY_2 ){ + int userIdentity = user.getUserIdentity() != null ? user.getUserIdentity() : CommonConstant.USER_IDENTITY_1; + if (oConvertUtils.isNotEmpty(userIdentity) && userIdentity == CommonConstant.USER_IDENTITY_2) { subDepids = sysDepartService.getMySubDepIdsByDepId(user.getDepartIds()); } - }else{ + } else { subDepids = sysDepartService.getSubDepIdsByDepId(depId); } - if(subDepids != null && subDepids.size()>0){ - IPage pageList = sysUserService.getUserByDepIds(page,subDepids,username); + if (subDepids != null && subDepids.size() > 0) { + IPage pageList = sysUserService.getUserByDepIds(page, subDepids, username); //批量查询用户的所属部门 //step.1 先拿到全部的 useids //step.2 通过 useids,一次性查询用户的所属部门名字 List userIds = pageList.getRecords().stream().map(SysUser::getId).collect(Collectors.toList()); - if(userIds!=null && userIds.size()>0){ + if (userIds != null && userIds.size() > 0) { Map useDepNames = sysUserService.getDepNamesByUserIds(userIds); pageList.getRecords().forEach(item -> { //批量查询用户的所属部门 @@ -775,7 +785,7 @@ public class SysUserController { } result.setSuccess(true); result.setResult(pageList); - }else{ + } else { result.setSuccess(true); result.setResult(null); } @@ -806,7 +816,7 @@ public class SysUserController { public Result queryByOrgCodeForAddressList( @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, - @RequestParam(name = "orgCode",required = false) String orgCode, + @RequestParam(name = "orgCode", required = false) String orgCode, SysUser userParams ) { IPage page = new Page(pageNo, pageSize); @@ -853,19 +863,19 @@ public class SysUserController { Result result = new Result(); try { String sysDepId = sysDepartUsersVO.getDepId(); - for(String sysUserId:sysDepartUsersVO.getUserIdList()) { - SysUserDepart sysUserDepart = new SysUserDepart(null,sysUserId,sysDepId); + for (String sysUserId : sysDepartUsersVO.getUserIdList()) { + SysUserDepart sysUserDepart = new SysUserDepart(null, sysUserId, sysDepId); QueryWrapper queryWrapper = new QueryWrapper(); - queryWrapper.eq("dep_id", sysDepId).eq("user_id",sysUserId); + queryWrapper.eq("dep_id", sysDepId).eq("user_id", sysUserId); SysUserDepart one = sysUserDepartService.getOne(queryWrapper); - if(one==null){ + if (one == null) { sysUserDepartService.save(sysUserDepart); } } result.setMessage("添加成功!"); result.setSuccess(true); return result; - }catch(Exception e) { + } catch (Exception e) { log.error(e.getMessage(), e); result.setSuccess(false); result.setMessage("出错了: " + e.getMessage()); @@ -874,31 +884,31 @@ public class SysUserController { } /** - * 删除指定机构的用户关系 + * 删除指定机构的用户关系 */ //@RequiresRoles({"admin"}) @RequestMapping(value = "/deleteUserInDepart", method = RequestMethod.DELETE) - public Result deleteUserInDepart(@RequestParam(name="depId") String depId, - @RequestParam(name="userId",required=true) String userId + public Result deleteUserInDepart(@RequestParam(name = "depId") String depId, + @RequestParam(name = "userId", required = true) String userId ) { Result result = new Result(); try { QueryWrapper queryWrapper = new QueryWrapper(); - queryWrapper.eq("dep_id", depId).eq("user_id",userId); + queryWrapper.eq("dep_id", depId).eq("user_id", userId); boolean b = sysUserDepartService.remove(queryWrapper); - if(b){ - List sysDepartRoleList = departRoleService.list(new QueryWrapper().eq("depart_id",depId)); + if (b) { + List sysDepartRoleList = departRoleService.list(new QueryWrapper().eq("depart_id", depId)); List roleIds = sysDepartRoleList.stream().map(SysDepartRole::getId).collect(Collectors.toList()); - if(roleIds != null && roleIds.size()>0){ + if (roleIds != null && roleIds.size() > 0) { QueryWrapper query = new QueryWrapper<>(); - query.eq("user_id",userId).in("drole_id",roleIds); + query.eq("user_id", userId).in("drole_id", roleIds); departRoleUserService.remove(query); } result.success("删除成功!"); - }else{ + } else { result.error500("当前选中部门与用户无关联关系!"); } - }catch(Exception e) { + } catch (Exception e) { log.error(e.getMessage(), e); result.error500("删除失败!"); } @@ -911,18 +921,18 @@ public class SysUserController { //@RequiresRoles({"admin"}) @RequestMapping(value = "/deleteUserInDepartBatch", method = RequestMethod.DELETE) public Result deleteUserInDepartBatch( - @RequestParam(name="depId") String depId, - @RequestParam(name="userIds",required=true) String userIds) { + @RequestParam(name = "depId") String depId, + @RequestParam(name = "userIds", required = true) String userIds) { Result result = new Result(); try { QueryWrapper queryWrapper = new QueryWrapper(); - queryWrapper.eq("dep_id", depId).in("user_id",Arrays.asList(userIds.split(","))); + queryWrapper.eq("dep_id", depId).in("user_id", Arrays.asList(userIds.split(","))); boolean b = sysUserDepartService.remove(queryWrapper); - if(b){ - departRoleUserService.removeDeptRoleUser(Arrays.asList(userIds.split(",")),depId); + if (b) { + departRoleUserService.removeDeptRoleUser(Arrays.asList(userIds.split(",")), depId); } result.success("删除成功!"); - }catch(Exception e) { + } catch (Exception e) { log.error(e.getMessage(), e); result.error500("删除失败!"); } @@ -930,21 +940,22 @@ public class SysUserController { } /** - * 查询当前用户的所有部门/当前部门编码 + * 查询当前用户的所有部门/当前部门编码 + * * @return */ @RequestMapping(value = "/getCurrentUserDeparts", method = RequestMethod.GET) - public Result> getCurrentUserDeparts() { - Result> result = new Result>(); + public Result> getCurrentUserDeparts() { + Result> result = new Result>(); try { - LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal(); + LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); List list = this.sysDepartService.queryUserDeparts(sysUser.getId()); - Map map = new HashMap(); + Map map = new HashMap(); map.put("list", list); map.put("orgCode", sysUser.getOrgCode()); result.setSuccess(true); result.setResult(map); - }catch(Exception e) { + } catch (Exception e) { log.error(e.getMessage(), e); result.error500("查询失败!"); } @@ -952,46 +963,44 @@ public class SysUserController { } - - - /** - * 用户注册接口 - * - * @param jsonObject - * @param user - * @return - */ - @PostMapping("/register") - public Result userRegister(@RequestBody JSONObject jsonObject, SysUser user) { - Result result = new Result(); - String phone = jsonObject.getString("phone"); - String smscode = jsonObject.getString("smscode"); - Object code = redisUtil.get(phone); - String username = jsonObject.getString("username"); - //未设置用户名,则用手机号作为用户名 - if(oConvertUtils.isEmpty(username)){ + /** + * 用户注册接口 + * + * @param jsonObject + * @param user + * @return + */ + @PostMapping("/register") + public Result userRegister(@RequestBody JSONObject jsonObject, SysUser user) { + Result result = new Result(); + String phone = jsonObject.getString("phone"); + String smscode = jsonObject.getString("smscode"); + Object code = redisUtil.get(phone); + String username = jsonObject.getString("username"); + //未设置用户名,则用手机号作为用户名 + if (oConvertUtils.isEmpty(username)) { username = phone; } //未设置密码,则随机生成一个密码 - String password = jsonObject.getString("password"); - if(oConvertUtils.isEmpty(password)){ + String password = jsonObject.getString("password"); + if (oConvertUtils.isEmpty(password)) { password = RandomUtil.randomString(8); } - String email = jsonObject.getString("email"); - SysUser sysUser1 = sysUserService.getUserByName(username); - if (sysUser1 != null) { - result.setMessage("用户名已注册"); - result.setSuccess(false); - return result; - } - SysUser sysUser2 = sysUserService.getUserByPhone(phone); - if (sysUser2 != null) { - result.setMessage("该手机号已注册"); - result.setSuccess(false); - return result; - } - - if(oConvertUtils.isNotEmpty(email)){ + String email = jsonObject.getString("email"); + SysUser sysUser1 = sysUserService.getUserByName(username); + if (sysUser1 != null) { + result.setMessage("用户名已注册"); + result.setSuccess(false); + return result; + } + SysUser sysUser2 = sysUserService.getUserByPhone(phone); + if (sysUser2 != null) { + result.setMessage("该手机号已注册"); + result.setSuccess(false); + return result; + } + + if (oConvertUtils.isNotEmpty(email)) { SysUser sysUser3 = sysUserService.getUserByEmail(email); if (sysUser3 != null) { result.setMessage("邮箱已被注册"); @@ -999,130 +1008,131 @@ public class SysUserController { return result; } } - if(null == code){ + if (null == code) { result.setMessage("手机验证码失效,请重新获取"); result.setSuccess(false); return result; } - if (!smscode.equals(code.toString())) { - result.setMessage("手机验证码错误"); - result.setSuccess(false); - return result; - } - - try { - user.setCreateTime(new Date());// 设置创建时间 - String salt = oConvertUtils.randomGen(8); - String passwordEncode = PasswordUtil.encrypt(username, password, salt); - user.setSalt(salt); - user.setUsername(username); - user.setRealname(username); - user.setPassword(passwordEncode); - user.setEmail(email); - user.setPhone(phone); - user.setStatus(CommonConstant.USER_UNFREEZE); - user.setDelFlag(CommonConstant.DEL_FLAG_0); - user.setActivitiSync(CommonConstant.ACT_SYNC_0); - sysUserService.addUserWithRole(user,"ee8626f80f7c2619917b6236f3a7f02b");//默认临时角色 VideoRecorder - result.success("注册成功"); - } catch (Exception e) { - result.error500("注册失败"); - } - return result; - } - - /** - * 根据用户名或手机号查询用户信息 - * @param - * @return - */ - @GetMapping("/querySysUser") - public Result> querySysUser(SysUser sysUser) { - String phone = sysUser.getPhone(); - String username = sysUser.getUsername(); - Result> result = new Result>(); - Map map = new HashMap(); - if (oConvertUtils.isNotEmpty(phone)) { - SysUser user = sysUserService.getUserByPhone(phone); - if(user!=null) { - map.put("username",user.getUsername()); - map.put("phone",user.getPhone()); - result.setSuccess(true); - result.setResult(map); - return result; - } - } - if (oConvertUtils.isNotEmpty(username)) { - SysUser user = sysUserService.getUserByName(username); - if(user!=null) { - map.put("username",user.getUsername()); - map.put("phone",user.getPhone()); - result.setSuccess(true); - result.setResult(map); - return result; - } - } - result.setSuccess(false); - result.setMessage("验证失败"); - return result; - } - - /** - * 用户手机号验证 - */ - @PostMapping("/phoneVerification") - public Result> phoneVerification(@RequestBody JSONObject jsonObject) { - Result> result = new Result>(); - String phone = jsonObject.getString("phone"); - String smscode = jsonObject.getString("smscode"); - Object code = redisUtil.get(phone); - if (!smscode.equals(code)) { - result.setMessage("手机验证码错误"); - result.setSuccess(false); - return result; - } - //设置有效时间 - redisUtil.set(phone, smscode,600); - //新增查询用户名 - LambdaQueryWrapper query = new LambdaQueryWrapper<>(); - query.eq(SysUser::getPhone,phone); + if (!smscode.equals(code.toString())) { + result.setMessage("手机验证码错误"); + result.setSuccess(false); + return result; + } + + try { + user.setCreateTime(new Date());// 设置创建时间 + String salt = oConvertUtils.randomGen(8); + String passwordEncode = PasswordUtil.encrypt(username, password, salt); + user.setSalt(salt); + user.setUsername(username); + user.setRealname(username); + user.setPassword(passwordEncode); + user.setEmail(email); + user.setPhone(phone); + user.setStatus(CommonConstant.USER_UNFREEZE); + user.setDelFlag(CommonConstant.DEL_FLAG_0); + user.setActivitiSync(CommonConstant.ACT_SYNC_0); + sysUserService.addUserWithRole(user, "ee8626f80f7c2619917b6236f3a7f02b");//默认临时角色 VideoRecorder + result.success("注册成功"); + } catch (Exception e) { + result.error500("注册失败"); + } + return result; + } + + /** + * 根据用户名或手机号查询用户信息 + * + * @param + * @return + */ + @GetMapping("/querySysUser") + public Result> querySysUser(SysUser sysUser) { + String phone = sysUser.getPhone(); + String username = sysUser.getUsername(); + Result> result = new Result>(); + Map map = new HashMap(); + if (oConvertUtils.isNotEmpty(phone)) { + SysUser user = sysUserService.getUserByPhone(phone); + if (user != null) { + map.put("username", user.getUsername()); + map.put("phone", user.getPhone()); + result.setSuccess(true); + result.setResult(map); + return result; + } + } + if (oConvertUtils.isNotEmpty(username)) { + SysUser user = sysUserService.getUserByName(username); + if (user != null) { + map.put("username", user.getUsername()); + map.put("phone", user.getPhone()); + result.setSuccess(true); + result.setResult(map); + return result; + } + } + result.setSuccess(false); + result.setMessage("验证失败"); + return result; + } + + /** + * 用户手机号验证 + */ + @PostMapping("/phoneVerification") + public Result> phoneVerification(@RequestBody JSONObject jsonObject) { + Result> result = new Result>(); + String phone = jsonObject.getString("phone"); + String smscode = jsonObject.getString("smscode"); + Object code = redisUtil.get(phone); + if (!smscode.equals(code)) { + result.setMessage("手机验证码错误"); + result.setSuccess(false); + return result; + } + //设置有效时间 + redisUtil.set(phone, smscode, 600); + //新增查询用户名 + LambdaQueryWrapper query = new LambdaQueryWrapper<>(); + query.eq(SysUser::getPhone, phone); SysUser user = sysUserService.getOne(query); - Map map = new HashMap<>(); - map.put("smscode",smscode); - map.put("username",user.getUsername()); + Map map = new HashMap<>(); + map.put("smscode", smscode); + map.put("username", user.getUsername()); result.setResult(map); - result.setSuccess(true); - return result; - } - - /** - * 用户更改密码 - */ - @GetMapping("/passwordChange") - public Result passwordChange(@RequestParam(name="username")String username, - @RequestParam(name="password")String password, - @RequestParam(name="smscode")String smscode, - @RequestParam(name="phone") String phone) { + result.setSuccess(true); + return result; + } + + /** + * 用户更改密码 + */ + @GetMapping("/passwordChange") + public Result passwordChange(@RequestParam(name = "username") String username, + @RequestParam(name = "password") String password, + @RequestParam(name = "smscode") String smscode, + @RequestParam(name = "phone") String phone) { Result result = new Result(); - if(oConvertUtils.isEmpty(username) || oConvertUtils.isEmpty(password) || oConvertUtils.isEmpty(smscode) || oConvertUtils.isEmpty(phone) ) { + if (oConvertUtils.isEmpty(username) || oConvertUtils.isEmpty(password) || oConvertUtils.isEmpty(smscode) || oConvertUtils.isEmpty(phone)) { result.setMessage("重置密码失败!"); result.setSuccess(false); return result; } - SysUser sysUser=new SysUser(); - Object object= redisUtil.get(phone); - if(null==object) { - result.setMessage("短信验证码失效!"); + SysUser sysUser = new SysUser(); + Object object = redisUtil.get(phone); + if (null == object) { + result.setMessage("短信验证码失效!"); result.setSuccess(false); return result; } - if(!smscode.equals(object)) { - result.setMessage("短信验证码不匹配!"); + if (!smscode.equals(object)) { + result.setMessage("短信验证码不匹配!"); result.setSuccess(false); return result; } - sysUser = this.sysUserService.getOne(new LambdaQueryWrapper().eq(SysUser::getUsername,username).eq(SysUser::getPhone,phone)); + sysUser = this.sysUserService.getOne(new LambdaQueryWrapper().eq(SysUser::getUsername, username).eq(SysUser::getPhone, phone)); if (sysUser == null) { result.setMessage("未找到用户!"); result.setSuccess(false); @@ -1140,79 +1150,80 @@ public class SysUserController { } - /** - * 根据TOKEN获取用户的部分信息(返回的数据是可供表单设计器使用的数据) - * - * @return - */ - @GetMapping("/getUserSectionInfoByToken") - public Result getUserSectionInfoByToken(HttpServletRequest request, @RequestParam(name = "token", required = false) String token) { - try { - String username = null; - // 如果没有传递token,就从header中获取token并获取用户信息 - if (oConvertUtils.isEmpty(token)) { - username = JwtUtil.getUserNameByToken(request); - } else { - username = JwtUtil.getUsername(token); - } - - log.debug(" ------ 通过令牌获取部分用户信息,当前用户: " + username); - - // 根据用户名查询用户信息 - SysUser sysUser = sysUserService.getUserByName(username); - Map map = new HashMap(); - map.put("sysUserId", sysUser.getId()); - map.put("sysUserCode", sysUser.getUsername()); // 当前登录用户登录账号 - map.put("sysUserName", sysUser.getRealname()); // 当前登录用户真实名称 - map.put("sysOrgCode", sysUser.getOrgCode()); // 当前登录用户部门编号 - - log.debug(" ------ 通过令牌获取部分用户信息,已获取的用户信息: " + map); - - return Result.ok(map); - } catch (Exception e) { - log.error(e.getMessage(), e); - return Result.error(500, "查询失败:" + e.getMessage()); - } - } - - /** - * 【APP端接口】获取用户列表 根据用户名和真实名 模糊匹配 - * @param keyword - * @param pageNo - * @param pageSize - * @return - */ - @GetMapping("/appUserList") - public Result appUserList(@RequestParam(name = "keyword", required = false) String keyword, - @RequestParam(name = "username", required = false) String username, - @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, - @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, - @RequestParam(name = "syncFlow", required = false) String syncFlow) { - try { - //TODO 从查询效率上将不要用mp的封装的page分页查询 建议自己写分页语句 - LambdaQueryWrapper query = new LambdaQueryWrapper(); - if(oConvertUtils.isNotEmpty(syncFlow)){ + /** + * 根据TOKEN获取用户的部分信息(返回的数据是可供表单设计器使用的数据) + * + * @return + */ + @GetMapping("/getUserSectionInfoByToken") + public Result getUserSectionInfoByToken(HttpServletRequest request, @RequestParam(name = "token", required = false) String token) { + try { + String username = null; + // 如果没有传递token,就从header中获取token并获取用户信息 + if (oConvertUtils.isEmpty(token)) { + username = JwtUtil.getUserNameByToken(request); + } else { + username = JwtUtil.getUsername(token); + } + + log.debug(" ------ 通过令牌获取部分用户信息,当前用户: " + username); + + // 根据用户名查询用户信息 + SysUser sysUser = sysUserService.getUserByName(username); + Map map = new HashMap(); + map.put("sysUserId", sysUser.getId()); + map.put("sysUserCode", sysUser.getUsername()); // 当前登录用户登录账号 + map.put("sysUserName", sysUser.getRealname()); // 当前登录用户真实名称 + map.put("sysOrgCode", sysUser.getOrgCode()); // 当前登录用户部门编号 + + log.debug(" ------ 通过令牌获取部分用户信息,已获取的用户信息: " + map); + + return Result.ok(map); + } catch (Exception e) { + log.error(e.getMessage(), e); + return Result.error(500, "查询失败:" + e.getMessage()); + } + } + + /** + * 【APP端接口】获取用户列表 根据用户名和真实名 模糊匹配 + * + * @param keyword + * @param pageNo + * @param pageSize + * @return + */ + @GetMapping("/appUserList") + public Result appUserList(@RequestParam(name = "keyword", required = false) String keyword, + @RequestParam(name = "username", required = false) String username, + @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, + @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, + @RequestParam(name = "syncFlow", required = false) String syncFlow) { + try { + //TODO 从查询效率上将不要用mp的封装的page分页查询 建议自己写分页语句 + LambdaQueryWrapper query = new LambdaQueryWrapper(); + if (oConvertUtils.isNotEmpty(syncFlow)) { query.eq(SysUser::getActivitiSync, CommonConstant.ACT_SYNC_1); } - query.eq(SysUser::getDelFlag,CommonConstant.DEL_FLAG_0); - if(oConvertUtils.isNotEmpty(username)){ - if(username.contains(",")){ - query.in(SysUser::getUsername,username.split(",")); - }else{ - query.eq(SysUser::getUsername,username); + query.eq(SysUser::getDelFlag, CommonConstant.DEL_FLAG_0); + if (oConvertUtils.isNotEmpty(username)) { + if (username.contains(",")) { + query.in(SysUser::getUsername, username.split(",")); + } else { + query.eq(SysUser::getUsername, username); } - }else{ + } else { query.and(i -> i.like(SysUser::getUsername, keyword).or().like(SysUser::getRealname, keyword)); } - Page page = new Page<>(pageNo, pageSize); - IPage res = this.sysUserService.page(page, query); - return Result.ok(res); - } catch (Exception e) { - log.error(e.getMessage(), e); - return Result.error(500, "查询失败:" + e.getMessage()); - } + Page page = new Page<>(pageNo, pageSize); + IPage res = this.sysUserService.page(page, query); + return Result.ok(res); + } catch (Exception e) { + log.error(e.getMessage(), e); + return Result.error(500, "查询失败:" + e.getMessage()); + } - } + } /** * 获取被逻辑删除的用户列表,无分页 @@ -1269,49 +1280,50 @@ public class SysUserController { /** * 移动端修改用户信息 + * * @param jsonObject * @return */ @RequestMapping(value = "/appEdit", method = RequestMethod.PUT) - public Result appEdit(HttpServletRequest request,@RequestBody JSONObject jsonObject) { + public Result appEdit(HttpServletRequest request, @RequestBody JSONObject jsonObject) { Result result = new Result(); try { String username = JwtUtil.getUserNameByToken(request); SysUser sysUser = sysUserService.getUserByName(username); - baseCommonService.addLog("移动端编辑用户,id: " +jsonObject.getString("id") ,CommonConstant.LOG_TYPE_2, 2); - String realname=jsonObject.getString("realname"); - String avatar=jsonObject.getString("avatar"); - String sex=jsonObject.getString("sex"); - String phone=jsonObject.getString("phone"); - String email=jsonObject.getString("email"); - Date birthday=jsonObject.getDate("birthday"); + baseCommonService.addLog("移动端编辑用户,id: " + jsonObject.getString("id"), CommonConstant.LOG_TYPE_2, 2); + String realname = jsonObject.getString("realname"); + String avatar = jsonObject.getString("avatar"); + String sex = jsonObject.getString("sex"); + String phone = jsonObject.getString("phone"); + String email = jsonObject.getString("email"); + Date birthday = jsonObject.getDate("birthday"); SysUser userPhone = sysUserService.getUserByPhone(phone); - if(sysUser==null) { + if (sysUser == null) { result.error500("未找到对应用户!"); - }else { - if(userPhone!=null){ + } else { + if (userPhone != null) { String userPhonename = userPhone.getUsername(); - if(!userPhonename.equals(username)){ + if (!userPhonename.equals(username)) { result.error500("手机号已存在!"); return result; } } - if(StringUtils.isNotBlank(realname)){ + if (StringUtils.isNotBlank(realname)) { sysUser.setRealname(realname); } - if(StringUtils.isNotBlank(avatar)){ + if (StringUtils.isNotBlank(avatar)) { sysUser.setAvatar(avatar); } - if(StringUtils.isNotBlank(sex)){ + if (StringUtils.isNotBlank(sex)) { sysUser.setSex(Integer.parseInt(sex)); } - if(StringUtils.isNotBlank(phone)){ + if (StringUtils.isNotBlank(phone)) { sysUser.setPhone(phone); } - if(StringUtils.isNotBlank(email)){ + if (StringUtils.isNotBlank(email)) { sysUser.setEmail(email); } - if(null != birthday){ + if (null != birthday) { sysUser.setBirthday(birthday); } sysUser.setUpdateTime(new Date()); @@ -1323,20 +1335,22 @@ public class SysUserController { } return result; } + /** * 移动端保存设备信息 + * * @param clientId * @return */ @RequestMapping(value = "/saveClientId", method = RequestMethod.GET) - public Result saveClientId(HttpServletRequest request,@RequestParam("clientId")String clientId) { + public Result saveClientId(HttpServletRequest request, @RequestParam("clientId") String clientId) { Result result = new Result(); try { String username = JwtUtil.getUserNameByToken(request); SysUser sysUser = sysUserService.getUserByName(username); - if(sysUser==null) { + if (sysUser == null) { result.error500("未找到对应用户!"); - }else { + } else { sysUser.setClientId(clientId); sysUserService.updateById(sysUser); } @@ -1346,6 +1360,7 @@ public class SysUserController { } return result; } + /** * 根据userid获取用户信息和部门员工信息 * @@ -1354,48 +1369,52 @@ public class SysUserController { @GetMapping("/queryChildrenByUsername") public Result queryChildrenByUsername(@RequestParam("userId") String userId) { //获取用户信息 - Map map=new HashMap(); + Map map = new HashMap(); SysUser sysUser = sysUserService.getById(userId); String username = sysUser.getUsername(); Integer identity = sysUser.getUserIdentity(); - map.put("sysUser",sysUser); - if(identity!=null && identity==2){ + map.put("sysUser", sysUser); + if (identity != null && identity == 2) { //获取部门用户信息 String departIds = sysUser.getDepartIds(); - if(StringUtils.isNotBlank(departIds)){ + if (StringUtils.isNotBlank(departIds)) { List departIdList = Arrays.asList(departIds.split(",")); - List childrenUser = sysUserService.queryByDepIds(departIdList,username); - map.put("children",childrenUser); + List childrenUser = sysUserService.queryByDepIds(departIdList, username); + map.put("children", childrenUser); } } return Result.ok(map); } + /** * 移动端查询部门用户信息 + * * @param departId * @return */ @GetMapping("/appQueryByDepartId") - public Result> appQueryByDepartId(@RequestParam(name="departId", required = false) String departId) { + public Result> appQueryByDepartId(@RequestParam(name = "departId", required = false) String departId) { Result> result = new Result>(); - List list=new ArrayList (); + List list = new ArrayList(); list.add(departId); - List childrenUser = sysUserService.queryByDepIds(list,null); + List childrenUser = sysUserService.queryByDepIds(list, null); result.setResult(childrenUser); return result; } + /** * 移动端查询用户信息(通过用户名模糊查询) + * * @param keyword * @return */ @GetMapping("/appQueryUser") public Result> appQueryUser(@RequestParam(name = "keyword", required = false) String keyword) { Result> result = new Result>(); - LambdaQueryWrapper queryWrapper =new LambdaQueryWrapper(); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); //TODO 外部模拟登陆临时账号,列表不显示 - queryWrapper.ne(SysUser::getUsername,"_reserve_user_external"); - if(StringUtils.isNotBlank(keyword)){ + queryWrapper.ne(SysUser::getUsername, "_reserve_user_external"); + if (StringUtils.isNotBlank(keyword)) { queryWrapper.and(i -> i.like(SysUser::getUsername, keyword).or().like(SysUser::getRealname, keyword)); } List list = sysUserService.list(queryWrapper); @@ -1403,9 +1422,9 @@ public class SysUserController { //step.1 先拿到全部的 useids //step.2 通过 useids,一次性查询用户的所属部门名字 List userIds = list.stream().map(SysUser::getId).collect(Collectors.toList()); - if(userIds!=null && userIds.size()>0){ - Map useDepNames = sysUserService.getDepNamesByUserIds(userIds); - list.forEach(item->{ + if (userIds != null && userIds.size() > 0) { + Map useDepNames = sysUserService.getDepNamesByUserIds(userIds); + list.forEach(item -> { item.setOrgCodeTxt(useDepNames.get(item.getId())); }); } @@ -1415,34 +1434,35 @@ public class SysUserController { /** * 根据用户名修改手机号 + * * @param json * @return */ @RequestMapping(value = "/updateMobile", method = RequestMethod.PUT) - public Result changMobile(@RequestBody JSONObject json,HttpServletRequest request) { + public Result changMobile(@RequestBody JSONObject json, HttpServletRequest request) { String smscode = json.getString("smscode"); String phone = json.getString("phone"); Result result = new Result(); //获取登录用户名 String username = JwtUtil.getUserNameByToken(request); - if(oConvertUtils.isEmpty(username) || oConvertUtils.isEmpty(smscode) || oConvertUtils.isEmpty(phone)) { + if (oConvertUtils.isEmpty(username) || oConvertUtils.isEmpty(smscode) || oConvertUtils.isEmpty(phone)) { result.setMessage("修改手机号失败!"); result.setSuccess(false); return result; } - Object object= redisUtil.get(phone); - if(null==object) { + Object object = redisUtil.get(phone); + if (null == object) { result.setMessage("短信验证码失效!"); result.setSuccess(false); return result; } - if(!smscode.equals(object)) { + if (!smscode.equals(object)) { result.setMessage("短信验证码不匹配!"); result.setSuccess(false); return result; } SysUser user = sysUserService.getUserByName(username); - if(user==null) { + if (user == null) { return Result.error("用户不存在!"); } user.setPhone(phone); diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/resources/application-dev.yml b/jeecg-boot/jeecg-boot-module-system/src/main/resources/application-dev.yml index d815c35..e67f7df 100644 --- a/jeecg-boot/jeecg-boot-module-system/src/main/resources/application-dev.yml +++ b/jeecg-boot/jeecg-boot-module-system/src/main/resources/application-dev.yml @@ -152,7 +152,7 @@ spring: max-wait: -1ms #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。 min-idle: 0 #最小等待连接中的数量,设 0 为没有限制 shutdown-timeout: 100ms - password: + password: port: 6379 # password: 'zy2021zy' # port: 7480 @@ -169,7 +169,7 @@ mybatis-plus: table-underline: true configuration: # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用 - # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 返回类型为Map,显示null对应的字段 call-setters-on-nulls: true #jeecg专用配置 diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/resources/application-prod.yml b/jeecg-boot/jeecg-boot-module-system/src/main/resources/application-prod.yml index 3a2f8a0..f818597 100644 --- a/jeecg-boot/jeecg-boot-module-system/src/main/resources/application-prod.yml +++ b/jeecg-boot/jeecg-boot-module-system/src/main/resources/application-prod.yml @@ -168,7 +168,7 @@ mybatis-plus: table-underline: true configuration: # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用 - #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 返回类型为Map,显示null对应的字段 call-setters-on-nulls: true #jeecg专用配置 From be9e09c84ccc8ca32ac54f7e7fcd187956682727 Mon Sep 17 00:00:00 2001 From: zhc077 <565291854> Date: Wed, 12 Apr 2023 17:45:51 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=BD=AF=E4=BB=B6=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=B3=BB=E7=BB=9F=20=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD=20=E4=BF=AE=E6=94=B9=204.1?= =?UTF-8?q?2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/controller/SysUserController.java | 29 +- .../service/impl/SysUserServiceImpl.java | 963 ++++++++---------- 2 files changed, 469 insertions(+), 523 deletions(-) diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/controller/SysUserController.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/controller/SysUserController.java index 4aa638e..38c08b9 100644 --- a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/controller/SysUserController.java +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/controller/SysUserController.java @@ -14,18 +14,19 @@ import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.apache.shiro.SecurityUtils; -import org.apache.shiro.authz.annotation.RequiresPermissions; -import org.apache.shiro.authz.annotation.RequiresRoles; import org.jeecg.common.api.vo.Result; import org.jeecg.common.aspect.annotation.AutoLog; import org.jeecg.common.aspect.annotation.PermissionData; import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.system.api.ISysBaseAPI; -import org.jeecg.modules.base.service.BaseCommonService; import org.jeecg.common.system.query.QueryGenerator; import org.jeecg.common.system.util.JwtUtil; import org.jeecg.common.system.vo.LoginUser; -import org.jeecg.common.util.*; +import org.jeecg.common.util.ImportExcelUtil; +import org.jeecg.common.util.PasswordUtil; +import org.jeecg.common.util.RedisUtil; +import org.jeecg.common.util.oConvertUtils; +import org.jeecg.modules.base.service.BaseCommonService; import org.jeecg.modules.system.entity.*; import org.jeecg.modules.system.model.DepartIdModel; import org.jeecg.modules.system.model.SysUserSysDepartModel; @@ -57,7 +58,7 @@ import java.util.stream.Collectors; *

* * @Author scott - * @since 2018-12-20 + * @since 2023-04-12 */ @Slf4j @RestController @@ -193,12 +194,12 @@ public class SysUserController { //@RequiresRoles({"admin"}) //@RequiresPermissions("user:add") + @AutoLog @RequestMapping(value = "/add", method = RequestMethod.POST) public Result add(@RequestBody JSONObject jsonObject) { Result result = new Result(); String selectedRoles = jsonObject.getString("selectedroles"); String selectedDeparts = jsonObject.getString("selecteddeparts"); -// System.out.println("我的测试" + selectedRoles); try { SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class); user.setCreateTime(new Date());//设置创建时间 @@ -208,9 +209,11 @@ public class SysUserController { user.setPassword(passwordEncode); user.setStatus(1); user.setDelFlag(CommonConstant.DEL_FLAG_0); + //用户表字段org_code不能在这里设置他的值 + user.setOrgCode(null); // 保存用户走一个service 保证事务 - this.sysUserService.save(user); -// sysUserService.saveUser(user, selectedRoles, selectedDeparts); + sysUserService.saveUser(user, selectedRoles, selectedDeparts); +// baseCommonService.addLog("添加用户,username: " + user.getUsername(), CommonConstant.LOG_TYPE_2, 2); result.success("添加成功!"); } catch (Exception e) { log.error(e.getMessage(), e); @@ -221,6 +224,7 @@ public class SysUserController { //@RequiresRoles({"admin"}) //@RequiresPermissions("user:edit") + @AutoLog @RequestMapping(value = "/edit", method = RequestMethod.PUT) public Result edit(@RequestBody JSONObject jsonObject) { Result result = new Result(); @@ -237,8 +241,8 @@ public class SysUserController { String roles = jsonObject.getString("selectedroles"); String departs = jsonObject.getString("selecteddeparts"); // 修改用户走一个service 保证事务 - sysUserService.updateById(user); -// sysUserService.editUser(user, roles, departs); +// sysUserService.updateById(user); + sysUserService.editUser(user, roles, departs); result.success("修改成功!"); } } catch (Exception e) { @@ -252,6 +256,7 @@ public class SysUserController { * 删除用户 */ //@RequiresRoles({"admin"}) + @AutoLog @RequestMapping(value = "/delete", method = RequestMethod.DELETE) public Result delete(@RequestParam(name = "id", required = true) String id) { this.sysUserService.removeById(id); @@ -266,8 +271,8 @@ public class SysUserController { //@RequiresRoles({"admin"}) @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE) public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) { - baseCommonService.addLog("批量删除用户, ids: " + ids, CommonConstant.LOG_TYPE_2, 3); - this.sysUserService.deleteBatchUsers(ids); +// baseCommonService.addLog("批量删除用户, ids: " + ids, CommonConstant.LOG_TYPE_2, 3); + this.sysUserService.removeByIds(Arrays.asList(ids.split(","))); return Result.ok("批量删除用户成功"); } diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/service/impl/SysUserServiceImpl.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/service/impl/SysUserServiceImpl.java index 90c9b1b..790af86 100644 --- a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/service/impl/SysUserServiceImpl.java +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/service/impl/SysUserServiceImpl.java @@ -44,34 +44,34 @@ import java.util.stream.Collectors; @Slf4j public class SysUserServiceImpl extends ServiceImpl implements ISysUserService { - @Autowired - private SysUserMapper userMapper; - @Autowired - private SysPermissionMapper sysPermissionMapper; - @Autowired - private SysUserRoleMapper sysUserRoleMapper; - @Autowired - private SysUserDepartMapper sysUserDepartMapper; - @Autowired - private ISysBaseAPI sysBaseAPI; - @Autowired - private SysDepartMapper sysDepartMapper; - @Autowired - private SysRoleMapper sysRoleMapper; - @Autowired - private SysDepartRoleUserMapper departRoleUserMapper; - @Autowired - private SysDepartRoleMapper sysDepartRoleMapper; - @Resource - private BaseCommonService baseCommonService; - @Autowired - private SysThirdAccountMapper sysThirdAccountMapper; - @Autowired - ThirdAppWechatEnterpriseServiceImpl wechatEnterpriseService; - @Autowired - ThirdAppDingtalkServiceImpl dingtalkService; - @Autowired - private IJxStudentService jxStudentService; + @Autowired + private SysUserMapper userMapper; + @Autowired + private SysPermissionMapper sysPermissionMapper; + @Autowired + private SysUserRoleMapper sysUserRoleMapper; + @Autowired + private SysUserDepartMapper sysUserDepartMapper; + @Autowired + private ISysBaseAPI sysBaseAPI; + @Autowired + private SysDepartMapper sysDepartMapper; + @Autowired + private SysRoleMapper sysRoleMapper; + @Autowired + private SysDepartRoleUserMapper departRoleUserMapper; + @Autowired + private SysDepartRoleMapper sysDepartRoleMapper; + @Resource + private BaseCommonService baseCommonService; + @Autowired + private SysThirdAccountMapper sysThirdAccountMapper; + @Autowired + ThirdAppWechatEnterpriseServiceImpl wechatEnterpriseService; + @Autowired + ThirdAppDingtalkServiceImpl dingtalkService; + @Autowired + private IJxStudentService jxStudentService; @Override @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true) @@ -105,338 +105,342 @@ public class SysUserServiceImpl extends ServiceImpl impl } @Override - @CacheEvict(value={CacheConstant.SYS_USERS_CACHE}, allEntries=true) - @Transactional(rollbackFor = Exception.class) - public boolean deleteUser(String userId) { - //1.删除用户 - this.removeById(userId); - //暂时是直接把学生信息给物理删除了,实际上可以使用逻辑删除,因为用户表的删除在当前就是逻辑删除,虽然有物理删除,但他们之前好像没写,如果需要后期我可以添加。 - jxStudentService.removeById(userId); - return false; - } - - @Override - @CacheEvict(value={CacheConstant.SYS_USERS_CACHE}, allEntries=true) - @Transactional(rollbackFor = Exception.class) - public boolean deleteBatchUsers(String userIds) { - //1.删除用户 - this.removeByIds(Arrays.asList(userIds.split(","))); - //同上单个删除。 - jxStudentService.removeByIds(Arrays.asList(userIds.split(","))); - return false; - } - - @Override - public SysUser getUserByName(String username) { - return userMapper.getUserByName(username); - } - - - @Override - @Transactional - public void addUserWithRole(SysUser user, String roles) { - this.save(user); - if(oConvertUtils.isNotEmpty(roles)) { - String[] arr = roles.split(","); - for (String roleId : arr) { - SysUserRole userRole = new SysUserRole(user.getId(), roleId); - sysUserRoleMapper.insert(userRole); - } - } - } - - @Override - @CacheEvict(value= {CacheConstant.SYS_USERS_CACHE}, allEntries=true) - @Transactional - public void editUserWithRole(SysUser user, String roles) { - this.updateById(user); - //先删后加 - sysUserRoleMapper.delete(new QueryWrapper().lambda().eq(SysUserRole::getUserId, user.getId())); - if(oConvertUtils.isNotEmpty(roles)) { - String[] arr = roles.split(","); - for (String roleId : arr) { - SysUserRole userRole = new SysUserRole(user.getId(), roleId); - sysUserRoleMapper.insert(userRole); - } - } - } - - - @Override - public List getRole(String username) { - return sysUserRoleMapper.getRoleByUserName(username); - } - - /** - * 通过用户名获取用户角色集合 - * @param username 用户名 + @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true) + @Transactional(rollbackFor = Exception.class) + public boolean deleteUser(String userId) { + //1.删除用户 + this.removeById(userId); + //暂时是直接把学生信息给物理删除了,实际上可以使用逻辑删除,因为用户表的删除在当前就是逻辑删除,虽然有物理删除,但他们之前好像没写,如果需要后期我可以添加。 + jxStudentService.removeById(userId); + return false; + } + + @Override + @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true) + @Transactional(rollbackFor = Exception.class) + public boolean deleteBatchUsers(String userIds) { + //1.删除用户 + this.removeByIds(Arrays.asList(userIds.split(","))); + //同上单个删除。 + jxStudentService.removeByIds(Arrays.asList(userIds.split(","))); + return false; + } + + @Override + public SysUser getUserByName(String username) { + return userMapper.getUserByName(username); + } + + + @Override + @Transactional + public void addUserWithRole(SysUser user, String roles) { + this.save(user); + if (oConvertUtils.isNotEmpty(roles)) { + String[] arr = roles.split(","); + for (String roleId : arr) { + SysUserRole userRole = new SysUserRole(user.getId(), roleId); + sysUserRoleMapper.insert(userRole); + } + } + } + + @Override + @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true) + @Transactional + public void editUserWithRole(SysUser user, String roles) { + this.updateById(user); + //先删后加 + sysUserRoleMapper.delete(new QueryWrapper().lambda().eq(SysUserRole::getUserId, user.getId())); + if (oConvertUtils.isNotEmpty(roles)) { + String[] arr = roles.split(","); + for (String roleId : arr) { + SysUserRole userRole = new SysUserRole(user.getId(), roleId); + sysUserRoleMapper.insert(userRole); + } + } + } + + + @Override + public List getRole(String username) { + return sysUserRoleMapper.getRoleByUserName(username); + } + + /** + * 通过用户名获取用户角色集合 + * + * @param username 用户名 * @return 角色集合 - */ - @Override - public Set getUserRolesSet(String username) { - // 查询用户拥有的角色集合 - List roles = sysUserRoleMapper.getRoleByUserName(username); - log.info("-------通过数据库读取用户拥有的角色Rules------username: " + username + ",Roles size: " + (roles == null ? 0 : roles.size())); - return new HashSet<>(roles); - } - - /** - * 通过用户名获取用户权限集合 - * - * @param username 用户名 - * @return 权限集合 - */ - @Override - public Set getUserPermissionsSet(String username) { - Set permissionSet = new HashSet<>(); - List permissionList = sysPermissionMapper.queryByUser(username); - for (SysPermission po : permissionList) { + */ + @Override + public Set getUserRolesSet(String username) { + // 查询用户拥有的角色集合 + List roles = sysUserRoleMapper.getRoleByUserName(username); + log.info("-------通过数据库读取用户拥有的角色Rules------username: " + username + ",Roles size: " + (roles == null ? 0 : roles.size())); + return new HashSet<>(roles); + } + + /** + * 通过用户名获取用户权限集合 + * + * @param username 用户名 + * @return 权限集合 + */ + @Override + public Set getUserPermissionsSet(String username) { + Set permissionSet = new HashSet<>(); + List permissionList = sysPermissionMapper.queryByUser(username); + for (SysPermission po : permissionList) { // // TODO URL规则有问题? // if (oConvertUtils.isNotEmpty(po.getUrl())) { // permissionSet.add(po.getUrl()); // } - if (oConvertUtils.isNotEmpty(po.getPerms())) { - permissionSet.add(po.getPerms()); - } - } - log.info("-------通过数据库读取用户拥有的权限Perms------username: "+ username+",Perms size: "+ (permissionSet==null?0:permissionSet.size()) ); - return permissionSet; - } - - @Override - public SysUserCacheInfo getCacheUser(String username) { - SysUserCacheInfo info = new SysUserCacheInfo(); - info.setOneDepart(true); + if (oConvertUtils.isNotEmpty(po.getPerms())) { + permissionSet.add(po.getPerms()); + } + } + log.info("-------通过数据库读取用户拥有的权限Perms------username: " + username + ",Perms size: " + (permissionSet == null ? 0 : permissionSet.size())); + return permissionSet; + } + + @Override + public SysUserCacheInfo getCacheUser(String username) { + SysUserCacheInfo info = new SysUserCacheInfo(); + info.setOneDepart(true); // SysUser user = userMapper.getUserByName(username); // info.setSysUserCode(user.getUsername()); // info.setSysUserName(user.getRealname()); - LoginUser user = sysBaseAPI.getUserByName(username); - if(user!=null) { - info.setSysUserCode(user.getUsername()); - info.setSysUserName(user.getRealname()); - info.setSysOrgCode(user.getOrgCode()); - } - - //多部门支持in查询 - List list = sysDepartMapper.queryUserDeparts(user.getId()); - List sysMultiOrgCode = new ArrayList(); - if(list==null || list.size()==0) { - //当前用户无部门 - //sysMultiOrgCode.add("0"); - }else if(list.size()==1) { - sysMultiOrgCode.add(list.get(0).getOrgCode()); - }else { - info.setOneDepart(false); - for (SysDepart dpt : list) { - sysMultiOrgCode.add(dpt.getOrgCode()); - } - } - info.setSysMultiOrgCode(sysMultiOrgCode); - - return info; - } - - // 根据部门Id查询 - @Override - public IPage getUserByDepId(Page page, String departId,String username) { - return userMapper.getUserByDepId(page, departId,username); - } - - @Override - public IPage getUserByDepIds(Page page, List departIds, String username) { - return userMapper.getUserByDepIds(page, departIds,username); - } - @Override - public List getUserByDepIds(List departIds, String username) { - return userMapper.getUserByDepIds(departIds,username); - } - @Override - public Map getDepNamesByUserIds(List userIds) { - List list = this.baseMapper.getDepNamesByUserIds(userIds); - - Map res = new HashMap(); - list.forEach(item -> { - if (res.get(item.getUserId()) == null) { - res.put(item.getUserId(), item.getDepartName()); - } else { - res.put(item.getUserId(), res.get(item.getUserId()) + "," + item.getDepartName()); - } - } - ); - return res; - } - - @Override - public IPage getUserByDepartIdAndQueryWrapper(Page page, String departId, QueryWrapper queryWrapper) { - LambdaQueryWrapper lambdaQueryWrapper = queryWrapper.lambda(); - - lambdaQueryWrapper.eq(SysUser::getDelFlag, CommonConstant.DEL_FLAG_0); + LoginUser user = sysBaseAPI.getUserByName(username); + if (user != null) { + info.setSysUserCode(user.getUsername()); + info.setSysUserName(user.getRealname()); + info.setSysOrgCode(user.getOrgCode()); + } + + //多部门支持in查询 + List list = sysDepartMapper.queryUserDeparts(user.getId()); + List sysMultiOrgCode = new ArrayList(); + if (list == null || list.size() == 0) { + //当前用户无部门 + //sysMultiOrgCode.add("0"); + } else if (list.size() == 1) { + sysMultiOrgCode.add(list.get(0).getOrgCode()); + } else { + info.setOneDepart(false); + for (SysDepart dpt : list) { + sysMultiOrgCode.add(dpt.getOrgCode()); + } + } + info.setSysMultiOrgCode(sysMultiOrgCode); + + return info; + } + + // 根据部门Id查询 + @Override + public IPage getUserByDepId(Page page, String departId, String username) { + return userMapper.getUserByDepId(page, departId, username); + } + + @Override + public IPage getUserByDepIds(Page page, List departIds, String username) { + return userMapper.getUserByDepIds(page, departIds, username); + } + + @Override + public List getUserByDepIds(List departIds, String username) { + return userMapper.getUserByDepIds(departIds, username); + } + + @Override + public Map getDepNamesByUserIds(List userIds) { + List list = this.baseMapper.getDepNamesByUserIds(userIds); + + Map res = new HashMap(); + list.forEach(item -> { + if (res.get(item.getUserId()) == null) { + res.put(item.getUserId(), item.getDepartName()); + } else { + res.put(item.getUserId(), res.get(item.getUserId()) + "," + item.getDepartName()); + } + } + ); + return res; + } + + @Override + public IPage getUserByDepartIdAndQueryWrapper(Page page, String departId, QueryWrapper queryWrapper) { + LambdaQueryWrapper lambdaQueryWrapper = queryWrapper.lambda(); + + lambdaQueryWrapper.eq(SysUser::getDelFlag, CommonConstant.DEL_FLAG_0); lambdaQueryWrapper.inSql(SysUser::getId, "SELECT user_id FROM sys_user_depart WHERE dep_id = '" + departId + "'"); return userMapper.selectPage(page, lambdaQueryWrapper); - } + } - @Override - public IPage queryUserByOrgCode(String orgCode, SysUser userParams, IPage page) { - List list = baseMapper.getUserByOrgCode(page, orgCode, userParams); - Integer total = baseMapper.getUserByOrgCodeTotal(orgCode, userParams); + @Override + public IPage queryUserByOrgCode(String orgCode, SysUser userParams, IPage page) { + List list = baseMapper.getUserByOrgCode(page, orgCode, userParams); + Integer total = baseMapper.getUserByOrgCodeTotal(orgCode, userParams); - IPage result = new Page<>(page.getCurrent(), page.getSize(), total); - result.setRecords(list); + IPage result = new Page<>(page.getCurrent(), page.getSize(), total); + result.setRecords(list); - return result; - } + return result; + } - // 根据角色Id查询 - @Override - public IPage getUserByRoleId(Page page, String roleId, String username) { - return userMapper.getUserByRoleId(page,roleId,username); - } + // 根据角色Id查询 + @Override + public IPage getUserByRoleId(Page page, String roleId, String username) { + return userMapper.getUserByRoleId(page, roleId, username); + } - @Override - @CacheEvict(value= {CacheConstant.SYS_USERS_CACHE}, key="#username") - public void updateUserDepart(String username,String orgCode) { - baseMapper.updateUserDepart(username, orgCode); - } + @Override + @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, key = "#username") + public void updateUserDepart(String username, String orgCode) { + baseMapper.updateUserDepart(username, orgCode); + } - @Override - public SysUser getUserByPhone(String phone) { - return userMapper.getUserByPhone(phone); - } + @Override + public SysUser getUserByPhone(String phone) { + return userMapper.getUserByPhone(phone); + } - @Override - public SysUser getUserByEmail(String email) { - return userMapper.getUserByEmail(email); - } + @Override + public SysUser getUserByEmail(String email) { + return userMapper.getUserByEmail(email); + } - @Override - @Transactional - public void addUserWithDepart(SysUser user, String selectedParts) { + @Override + @Transactional + public void addUserWithDepart(SysUser user, String selectedParts) { // this.save(user); //保存角色的时候已经添加过一次了 - if(oConvertUtils.isNotEmpty(selectedParts)) { - String[] arr = selectedParts.split(","); - for (String deaprtId : arr) { - SysUserDepart userDeaprt = new SysUserDepart(user.getId(), deaprtId); - sysUserDepartMapper.insert(userDeaprt); - } - } - } - - - @Override - @Transactional(rollbackFor = Exception.class) - @CacheEvict(value={CacheConstant.SYS_USERS_CACHE}, allEntries=true) - public void editUserWithDepart(SysUser user, String departs) { - this.updateById(user); //更新角色的时候已经更新了一次了,可以再跟新一次 - String[] arr = {}; - if(oConvertUtils.isNotEmpty(departs)){ - arr = departs.split(","); - } - //查询已关联部门 - List userDepartList = sysUserDepartMapper.selectList(new QueryWrapper().lambda().eq(SysUserDepart::getUserId, user.getId())); - if(userDepartList != null && userDepartList.size()>0){ - for(SysUserDepart depart : userDepartList ){ - //修改已关联部门删除部门用户角色关系 - if(!Arrays.asList(arr).contains(depart.getDepId())){ - List sysDepartRoleList = sysDepartRoleMapper.selectList( - new QueryWrapper().lambda().eq(SysDepartRole::getDepartId,depart.getDepId())); - List roleIds = sysDepartRoleList.stream().map(SysDepartRole::getId).collect(Collectors.toList()); - if(roleIds != null && roleIds.size()>0){ - departRoleUserMapper.delete(new QueryWrapper().lambda().eq(SysDepartRoleUser::getUserId, user.getId()) - .in(SysDepartRoleUser::getDroleId,roleIds)); - } - } - } - } - //先删后加 - sysUserDepartMapper.delete(new QueryWrapper().lambda().eq(SysUserDepart::getUserId, user.getId())); - if(oConvertUtils.isNotEmpty(departs)) { - for (String departId : arr) { - SysUserDepart userDepart = new SysUserDepart(user.getId(), departId); - sysUserDepartMapper.insert(userDepart); - } - } - } - - - /** - * 校验用户是否有效 - * @param sysUser - * @return - */ - @Override - public Result checkUserIsEffective(SysUser sysUser) { - Result result = new Result(); - //情况1:根据用户信息查询,该用户不存在 - if (sysUser == null) { - result.error500("该用户不存在,请注册"); - baseCommonService.addLog("用户登录失败,用户不存在!", CommonConstant.LOG_TYPE_1, null); - return result; - } - //情况2:根据用户信息查询,该用户已注销 - //update-begin---author:王帅 Date:20200601 for:if条件永远为falsebug------------ - if (CommonConstant.DEL_FLAG_1.equals(sysUser.getDelFlag())) { - //update-end---author:王帅 Date:20200601 for:if条件永远为falsebug------------ - baseCommonService.addLog("用户登录失败,用户名:" + sysUser.getUsername() + "已注销!", CommonConstant.LOG_TYPE_1, null); - result.error500("该用户已注销"); - return result; - } - //情况3:根据用户信息查询,该用户已冻结 - if (CommonConstant.USER_FREEZE.equals(sysUser.getStatus())) { - baseCommonService.addLog("用户登录失败,用户名:" + sysUser.getUsername() + "已冻结!", CommonConstant.LOG_TYPE_1, null); - result.error500("该用户已冻结"); - return result; - } - return result; - } - - @Override - public List queryLogicDeleted() { - return this.queryLogicDeleted(null); - } - - @Override - public List queryLogicDeleted(LambdaQueryWrapper wrapper) { - if (wrapper == null) { - wrapper = new LambdaQueryWrapper<>(); - } - wrapper.eq(SysUser::getDelFlag, CommonConstant.DEL_FLAG_1); - return userMapper.selectLogicDeleted(wrapper); - } - - @Override - public boolean revertLogicDeleted(List userIds, SysUser updateEntity) { - String ids = String.format("'%s'", String.join("','", userIds)); - return userMapper.revertLogicDeleted(ids, updateEntity) > 0; - } - - @Override - @Transactional(rollbackFor = Exception.class) - public boolean removeLogicDeleted(List userIds) { - String ids = String.format("'%s'", String.join("','", userIds)); - // 1. 删除用户 - int line = userMapper.deleteLogicDeleted(ids); - // 2. 删除用户部门关系 - line += sysUserDepartMapper.delete(new LambdaQueryWrapper().in(SysUserDepart::getUserId, userIds)); - //3. 删除用户角色关系 - line += sysUserRoleMapper.delete(new LambdaQueryWrapper().in(SysUserRole::getUserId, userIds)); - //4.同步删除第三方App的用户 - try { - dingtalkService.removeThirdAppUser(userIds); - wechatEnterpriseService.removeThirdAppUser(userIds); - } catch (Exception e) { - log.error("同步删除第三方App的用户失败:", e); - } - //5. 删除第三方用户表(因为第4步需要用到第三方用户表,所以在他之后删) - line += sysThirdAccountMapper.delete(new LambdaQueryWrapper().in(SysThirdAccount::getSysUserId, userIds)); - - return line != 0; - } + if (oConvertUtils.isNotEmpty(selectedParts)) { + String[] arr = selectedParts.split(","); + for (String deaprtId : arr) { + SysUserDepart userDeaprt = new SysUserDepart(user.getId(), deaprtId); + sysUserDepartMapper.insert(userDeaprt); + } + } + } + + + @Override + @Transactional(rollbackFor = Exception.class) + @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true) + public void editUserWithDepart(SysUser user, String departs) { + this.updateById(user); //更新角色的时候已经更新了一次了,可以再跟新一次 + String[] arr = {}; + if (oConvertUtils.isNotEmpty(departs)) { + arr = departs.split(","); + } + //查询已关联部门 + List userDepartList = sysUserDepartMapper.selectList(new QueryWrapper().lambda().eq(SysUserDepart::getUserId, user.getId())); + if (userDepartList != null && userDepartList.size() > 0) { + for (SysUserDepart depart : userDepartList) { + //修改已关联部门删除部门用户角色关系 + if (!Arrays.asList(arr).contains(depart.getDepId())) { + List sysDepartRoleList = sysDepartRoleMapper.selectList( + new QueryWrapper().lambda().eq(SysDepartRole::getDepartId, depart.getDepId())); + List roleIds = sysDepartRoleList.stream().map(SysDepartRole::getId).collect(Collectors.toList()); + if (roleIds != null && roleIds.size() > 0) { + departRoleUserMapper.delete(new QueryWrapper().lambda().eq(SysDepartRoleUser::getUserId, user.getId()) + .in(SysDepartRoleUser::getDroleId, roleIds)); + } + } + } + } + //先删后加 + sysUserDepartMapper.delete(new QueryWrapper().lambda().eq(SysUserDepart::getUserId, user.getId())); + if (oConvertUtils.isNotEmpty(departs)) { + for (String departId : arr) { + SysUserDepart userDepart = new SysUserDepart(user.getId(), departId); + sysUserDepartMapper.insert(userDepart); + } + } + } + + + /** + * 校验用户是否有效 + * + * @param sysUser + * @return + */ + @Override + public Result checkUserIsEffective(SysUser sysUser) { + Result result = new Result(); + //情况1:根据用户信息查询,该用户不存在 + if (sysUser == null) { + result.error500("该用户不存在,请注册"); + baseCommonService.addLog("用户登录失败,用户不存在!", CommonConstant.LOG_TYPE_1, null); + return result; + } + //情况2:根据用户信息查询,该用户已注销 + //update-begin---author:王帅 Date:20200601 for:if条件永远为falsebug------------ + if (CommonConstant.DEL_FLAG_1.equals(sysUser.getDelFlag())) { + //update-end---author:王帅 Date:20200601 for:if条件永远为falsebug------------ + baseCommonService.addLog("用户登录失败,用户名:" + sysUser.getUsername() + "已注销!", CommonConstant.LOG_TYPE_1, null); + result.error500("该用户已注销"); + return result; + } + //情况3:根据用户信息查询,该用户已冻结 + if (CommonConstant.USER_FREEZE.equals(sysUser.getStatus())) { + baseCommonService.addLog("用户登录失败,用户名:" + sysUser.getUsername() + "已冻结!", CommonConstant.LOG_TYPE_1, null); + result.error500("该用户已冻结"); + return result; + } + return result; + } + + @Override + public List queryLogicDeleted() { + return this.queryLogicDeleted(null); + } + + @Override + public List queryLogicDeleted(LambdaQueryWrapper wrapper) { + if (wrapper == null) { + wrapper = new LambdaQueryWrapper<>(); + } + wrapper.eq(SysUser::getDelFlag, CommonConstant.DEL_FLAG_1); + return userMapper.selectLogicDeleted(wrapper); + } + + @Override + public boolean revertLogicDeleted(List userIds, SysUser updateEntity) { + String ids = String.format("'%s'", String.join("','", userIds)); + return userMapper.revertLogicDeleted(ids, updateEntity) > 0; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public boolean removeLogicDeleted(List userIds) { + String ids = String.format("'%s'", String.join("','", userIds)); + // 1. 删除用户 + int line = userMapper.deleteLogicDeleted(ids); + // 2. 删除用户部门关系 + line += sysUserDepartMapper.delete(new LambdaQueryWrapper().in(SysUserDepart::getUserId, userIds)); + //3. 删除用户角色关系 + line += sysUserRoleMapper.delete(new LambdaQueryWrapper().in(SysUserRole::getUserId, userIds)); + //4.同步删除第三方App的用户 + try { + dingtalkService.removeThirdAppUser(userIds); + wechatEnterpriseService.removeThirdAppUser(userIds); + } catch (Exception e) { + log.error("同步删除第三方App的用户失败:", e); + } + //5. 删除第三方用户表(因为第4步需要用到第三方用户表,所以在他之后删) + line += sysThirdAccountMapper.delete(new LambdaQueryWrapper().in(SysThirdAccount::getSysUserId, userIds)); + + return line != 0; + } @Override @Transactional(rollbackFor = Exception.class) @@ -446,181 +450,118 @@ public class SysUserServiceImpl extends ServiceImpl impl return true; } - @Override - public void saveThirdUser(SysUser sysUser) { - //保存用户 - String userid = UUIDGenerator.generate(); - sysUser.setId(userid); - baseMapper.insert(sysUser); - //获取第三方角色 - SysRole sysRole = sysRoleMapper.selectOne(new LambdaQueryWrapper().eq(SysRole::getRoleCode, "third_role")); - //保存用户角色 - SysUserRole userRole = new SysUserRole(); - userRole.setRoleId(sysRole.getId()); - userRole.setUserId(userid); - sysUserRoleMapper.insert(userRole); - } - - @Override - public List queryByDepIds(List departIds, String username) { - return userMapper.queryByDepIds(departIds,username); - } - - @Override - @Transactional(rollbackFor = Exception.class) - public void saveUser(SysUser user, String selectedRoles, String selectedDeparts) { - //首先根据id看修改前有没有这个学生,如果有的话,调用edit修改,没有的话直接添加。 - int ans=jxStudentService.cont(user.getId()); - //step.1 保存用户 - this.save(user); - //step.2 保存角色 - if(oConvertUtils.isNotEmpty(selectedRoles)) { - String[] arr = selectedRoles.split(","); - for (String roleId : arr) { - //这串数字是学生角色id - if(roleId.equals("1296245993988284417")){ - JxStudent jxStudent=new JxStudent(); - jxStudent.setId(user.getId()); - jxStudent.setName(user.getRealname()); - jxStudent.setPhone(user.getPhone()); - jxStudent.setBirthday(user.getBirthday()); - if(user.getSex()==1) - jxStudent.setSex("1"); - else - jxStudent.setSex("2"); - //这里的判断主要是为了防止学生表新加后再来用户表新加会出现的问题。 - if(ans==0) - jxStudentService.save(jxStudent); - } - SysUserRole userRole = new SysUserRole(user.getId(), roleId); - sysUserRoleMapper.insert(userRole); - } - } - //step.3 保存所属部门 - if(oConvertUtils.isNotEmpty(selectedDeparts)) { - ans=jxStudentService.cont(user.getId()); - String[] arr = selectedDeparts.split(","); - System.out.println(arr.length); - for (String deaprtId : arr) { - if(arr.length==1&&ans!=0){ - JxStudent jxStudent=new JxStudent(); - jxStudent.setId(user.getId()); - jxStudent.setFaculty(deaprtId); - jxStudentService.updateById(jxStudent); - } - SysUserDepart userDeaprt = new SysUserDepart(user.getId(), deaprtId); - sysUserDepartMapper.insert(userDeaprt); - } - } - } - - @Override - @Transactional(rollbackFor = Exception.class) - @CacheEvict(value={CacheConstant.SYS_USERS_CACHE}, allEntries=true) - public void editUser(SysUser user, String roles, String departs) { - //首先根据id看修改前有没有这个学生,如果有的话,调用edit修改,没有的话直接添加。 - int ans=jxStudentService.cont(user.getId()); - //ans不等于0的话,就是有这条数据,调用edit修改,否则就是添加。 - //判断修改后,对应用户是否还有学生这个角色。 - boolean student=false; - //step.1 修改用户基础信息 - this.updateById(user); - //step.2 修改角色 - //处理用户角色 先删后加 - sysUserRoleMapper.delete(new QueryWrapper().lambda().eq(SysUserRole::getUserId, user.getId())); - //这里if中判断的是否角色为空,为空直接不执行了,所以说,如果之前有学生的话,要给删掉的。 - if(oConvertUtils.isNotEmpty(roles)) { - String[] arr = roles.split(","); - for (String roleId : arr) { - //在修改用户角色时,如果没了学生这个角色,一会就要把对应学生表中的数据给删掉了。 - if(roleId.equals("1296245993988284417")){ - student=true; - JxStudent jxStudent=new JxStudent(); - jxStudent.setId(user.getId()); - jxStudent.setName(user.getRealname()); - jxStudent.setPhone(user.getPhone()); - jxStudent.setBirthday(user.getBirthday()); - if(user.getSex()==1) - jxStudent.setSex("1"); - else - jxStudent.setSex("2"); - //判断是不是第一次给用户学生这个角色,如果是的话就会调用save新增,如果不是的话,就会在原表中进行修改。 - if(ans==0) - jxStudentService.save(jxStudent); - else - jxStudentService.updateById(jxStudent); - } - SysUserRole userRole = new SysUserRole(user.getId(), roleId); - sysUserRoleMapper.insert(userRole); - } - //如果student为false,则证明修改之后,没有学生这个角色了,要把学生表中对应的信息给删除。 - if(!student&&ans!=0){ - jxStudentService.removeById(user.getId()); - } - }else{ - //修改完一个角色都没有,而且学生表还存在的话,就直接删除。 - if(ans!=0) - jxStudentService.removeById(user.getId());} - - //step.3 修改部门 - String[] arr = {}; - if(oConvertUtils.isNotEmpty(departs)){ - arr = departs.split(","); - } - //查询已关联部门 - List userDepartList = sysUserDepartMapper.selectList(new QueryWrapper().lambda().eq(SysUserDepart::getUserId, user.getId())); - if(userDepartList != null && userDepartList.size()>0){ - for(SysUserDepart depart : userDepartList ){ - //修改已关联部门删除部门用户角色关系 - if(!Arrays.asList(arr).contains(depart.getDepId())){ - List sysDepartRoleList = sysDepartRoleMapper.selectList( - new QueryWrapper().lambda().eq(SysDepartRole::getDepartId,depart.getDepId())); - List roleIds = sysDepartRoleList.stream().map(SysDepartRole::getId).collect(Collectors.toList()); - if(roleIds != null && roleIds.size()>0){ - departRoleUserMapper.delete(new QueryWrapper().lambda().eq(SysDepartRoleUser::getUserId, user.getId()) - .in(SysDepartRoleUser::getDroleId,roleIds)); - } - } - } - } - //先删后加 - sysUserDepartMapper.delete(new QueryWrapper().lambda().eq(SysUserDepart::getUserId, user.getId())); - if(oConvertUtils.isNotEmpty(departs)) { - //ans=jxStudentService.cont(user.getId()); - for (String departId : arr) { - if(arr.length==1&&ans!=0){ - JxStudent jxStudent=new JxStudent(); - jxStudent.setId(user.getId()); - jxStudent.setFaculty(departId); - jxStudentService.updateById(jxStudent); - } - SysUserDepart userDepart = new SysUserDepart(user.getId(), departId); - sysUserDepartMapper.insert(userDepart); - } - } - //step.4 修改手机号和邮箱 - // 更新手机号、邮箱空字符串为 null - userMapper.updateNullByEmptyString("email"); - userMapper.updateNullByEmptyString("phone"); - - } - - @Override - public List userIdToUsername(Collection userIdList) { - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.in(SysUser::getId, userIdList); - List userList = super.list(queryWrapper); - return userList.stream().map(SysUser::getUsername).collect(Collectors.toList()); - } - - @Override - public IPage selectStudent(Page page, SysUser user) { - return userMapper.selctStudent(page,user); - } - - @Override - public IPage page(Page page, String username, String realname) { - return userMapper.page(page,username,realname); - } + @Override + public void saveThirdUser(SysUser sysUser) { + //保存用户 + String userid = UUIDGenerator.generate(); + sysUser.setId(userid); + baseMapper.insert(sysUser); + //获取第三方角色 + SysRole sysRole = sysRoleMapper.selectOne(new LambdaQueryWrapper().eq(SysRole::getRoleCode, "third_role")); + //保存用户角色 + SysUserRole userRole = new SysUserRole(); + userRole.setRoleId(sysRole.getId()); + userRole.setUserId(userid); + sysUserRoleMapper.insert(userRole); + } + + @Override + public List queryByDepIds(List departIds, String username) { + return userMapper.queryByDepIds(departIds, username); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void saveUser(SysUser user, String selectedRoles, String selectedDeparts) { + //step.1 保存用户 + this.save(user); + //step.2 保存角色 + if (oConvertUtils.isNotEmpty(selectedRoles)) { + String[] arr = selectedRoles.split(","); + for (String roleId : arr) { + SysUserRole userRole = new SysUserRole(user.getId(), roleId); + sysUserRoleMapper.insert(userRole); + } + } + //step.3 保存所属部门 + if (oConvertUtils.isNotEmpty(selectedDeparts)) { + String[] arr = selectedDeparts.split(","); + for (String deaprtId : arr) { + SysUserDepart userDeaprt = new SysUserDepart(user.getId(), deaprtId); + sysUserDepartMapper.insert(userDeaprt); + } + } + } + + @Override + @Transactional(rollbackFor = Exception.class) + @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true) + public void editUser(SysUser user, String roles, String departs) { + //step.1 修改用户基础信息 + this.updateById(user); + //step.2 修改角色 + //处理用户角色 先删后加 + sysUserRoleMapper.delete(new QueryWrapper().lambda().eq(SysUserRole::getUserId, user.getId())); + if (oConvertUtils.isNotEmpty(roles)) { + String[] arr = roles.split(","); + for (String roleId : arr) { + SysUserRole userRole = new SysUserRole(user.getId(), roleId); + sysUserRoleMapper.insert(userRole); + } + } + + //step.3 修改部门 + String[] arr = {}; + if (oConvertUtils.isNotEmpty(departs)) { + arr = departs.split(","); + } + //查询已关联部门 + List userDepartList = sysUserDepartMapper.selectList(new QueryWrapper().lambda().eq(SysUserDepart::getUserId, user.getId())); + if (userDepartList != null && userDepartList.size() > 0) { + for (SysUserDepart depart : userDepartList) { + //修改已关联部门删除部门用户角色关系 + if (!Arrays.asList(arr).contains(depart.getDepId())) { + List sysDepartRoleList = sysDepartRoleMapper.selectList( + new QueryWrapper().lambda().eq(SysDepartRole::getDepartId, depart.getDepId())); + List roleIds = sysDepartRoleList.stream().map(SysDepartRole::getId).collect(Collectors.toList()); + if (roleIds != null && roleIds.size() > 0) { + departRoleUserMapper.delete(new QueryWrapper().lambda().eq(SysDepartRoleUser::getUserId, user.getId()) + .in(SysDepartRoleUser::getDroleId, roleIds)); + } + } + } + } + //先删后加 + sysUserDepartMapper.delete(new QueryWrapper().lambda().eq(SysUserDepart::getUserId, user.getId())); + if (oConvertUtils.isNotEmpty(departs)) { + for (String departId : arr) { + SysUserDepart userDepart = new SysUserDepart(user.getId(), departId); + sysUserDepartMapper.insert(userDepart); + } + } + //step.4 修改手机号和邮箱 + // 更新手机号、邮箱空字符串为 null + userMapper.updateNullByEmptyString("email"); + userMapper.updateNullByEmptyString("phone"); + + } + + @Override + public List userIdToUsername(Collection userIdList) { + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.in(SysUser::getId, userIdList); + List userList = super.list(queryWrapper); + return userList.stream().map(SysUser::getUsername).collect(Collectors.toList()); + } + + @Override + public IPage selectStudent(Page page, SysUser user) { + return userMapper.selctStudent(page, user); + } + + @Override + public IPage page(Page page, String username, String realname) { + return userMapper.page(page, username, realname); + } }