package com.glxp.api.controller.auth; import cn.dev33.satoken.annotation.SaCheckPermission; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import com.github.pagehelper.PageInfo; import com.glxp.api.annotation.Log; import com.glxp.api.constant.BusinessType; import com.glxp.api.controller.BaseController; import com.glxp.api.entity.auth.AuthAdmin; import com.glxp.api.entity.auth.DeptUserEntity; import com.glxp.api.entity.auth.SysRole; import com.glxp.api.req.auth.FilterAuthUserRequest; import com.glxp.api.req.auth.AuthUserSaveRequest; import com.glxp.api.res.PageSimpleResponse; import com.glxp.api.res.auth.AuthAdminResponse; import com.glxp.api.res.auth.DeptUserResponse; import com.glxp.api.service.auth.AuthAdminService; import com.glxp.api.service.auth.DeptUserService; import com.glxp.api.service.auth.ISysRoleService; import com.glxp.api.util.PasswordUtils; import com.glxp.api.util.StreamUtils; import com.glxp.api.common.enums.ResultEnum; import com.glxp.api.common.res.BaseResponse; import com.glxp.api.common.util.ResultVOUtils; import lombok.RequiredArgsConstructor; import org.springframework.beans.BeanUtils; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.validation.Valid; import java.util.*; import java.util.stream.Collectors; /** * 用户信息 * * @author Lion Li */ @Validated @RequiredArgsConstructor @RestController @RequestMapping("/system/user") public class SysUserController extends BaseController { private final AuthAdminService userService; private final ISysRoleService roleService; @Resource ISysRoleService sysRoleService; @GetMapping("/list") public BaseResponse list(FilterAuthUserRequest filterAuthUserRequest, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage()); } List authAdminList = userService.listAdminPage(filterAuthUserRequest); //查询对应角色和部门 List authAdminResponseList = authAdminList.stream().map(item -> { AuthAdminResponse authAdminResponse = new AuthAdminResponse(); BeanUtils.copyProperties(item, authAdminResponse); List roles = sysRoleService.selectRoleListByUserId(authAdminResponse.getId()); List deptUserResponses = deptUserService.selectByUserId(authAdminResponse.getId()); if (CollUtil.isNotEmpty(deptUserResponses)) { List depts = new ArrayList<>(); String deptName = ""; for (DeptUserResponse deptUserResponse : deptUserResponses) { depts.add(deptUserResponse.getDeptId()); deptName = deptName + "," + deptUserResponse.getDeptName(); } authAdminResponse.setDeptName(deptName.substring(1)); authAdminResponse.setDepts(depts); } authAdminResponse.setRoles(roles); return authAdminResponse; }).collect(Collectors.toList()); PageInfo authAdminPageInfo = new PageInfo<>(authAdminList); PageSimpleResponse authAdminPageSimpleResponse = new PageSimpleResponse<>(); authAdminPageSimpleResponse.setTotal(authAdminPageInfo.getTotal()); authAdminPageSimpleResponse.setList(authAdminResponseList); return ResultVOUtils.success(authAdminPageSimpleResponse); } /** * 根据用户编号获取详细信息 * * @param userId 用户ID */ @SaCheckPermission("system:user:query") @GetMapping(value = {"/", "/{userId}"}) public BaseResponse getInfo(@PathVariable(value = "userId", required = false) Long userId) { Map ajax = new HashMap<>(); List roles = roleService.selectRoleAll(); ajax.put("roles", isAdmin(userId) ? roles : StreamUtils.filter(roles, r -> !r.isAdmin())); if (ObjectUtil.isNotNull(userId)) { AuthAdmin sysUser = userService.findById(userId); ajax.put("user", sysUser); ajax.put("roleIds", StreamUtils.toList(sysUser.getRoles(), SysRole::getRoleId)); } return ResultVOUtils.success(ajax); } /** * 根据用户编号获取授权角色 * * @param userId 用户ID */ @SaCheckPermission("system:user:query") @GetMapping("/authRole/{userId}") public BaseResponse authRole(@PathVariable Long userId) { AuthAdmin user = userService.findById(userId); List roles = roleService.selectRolesByUserId(userId); Map ajax = new HashMap<>(); ajax.put("user", user); ajax.put("roles", isAdmin(userId) ? roles : StreamUtils.filter(roles, r -> !r.isAdmin())); return ResultVOUtils.success(ajax); } /** * 用户授权角色 * * @param userId 用户Id * @param roleIds 角色ID串 */ @SaCheckPermission("system:user:edit") @PutMapping("/authRole") public BaseResponse inseinsertUserAuthrtAuthRole(Long userId, Long[] roleIds) { userService.insertUserAuth(userId, CollUtil.toList(roleIds)); return ResultVOUtils.success("授权成功!"); } @Log(title = "用户管理", businessType = BusinessType.INSERT) @PostMapping("/save") public BaseResponse save(@RequestBody @Valid AuthUserSaveRequest authUserSaveRequest, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage()); } AuthAdmin curUser = getUser(); // 检查是否存在相同名称的管理员 AuthAdmin byUserName = userService.findByUserName(authUserSaveRequest.getUserName()); if (byUserName != null) { return ResultVOUtils.error(ResultEnum.DATA_REPEAT, "当前管理员已存在"); } AuthAdmin authAdmin = new AuthAdmin(); BeanUtils.copyProperties(authUserSaveRequest, authAdmin); if (authAdmin.getPassWord() != null) { authAdmin.setPassWord(PasswordUtils.authAdminPwd(authAdmin.getPassWord())); } authAdmin.setCustomerId(curUser.getCustomerId()); authAdmin.setLastModifyTime(new Date()); boolean b = userService.insertAuthAdmin(authAdmin); authAdmin = userService.findByUserName(authAdmin.getUserName()); if (!b) { return ResultVOUtils.error(ResultEnum.NOT_NETWORK); } // 插入角色 if (authUserSaveRequest.getRoles() != null) { userService.insertUserAuth(authAdmin.getId(), authUserSaveRequest.getRoles()); } //插入部门 if (CollUtil.isNotEmpty(authUserSaveRequest.getDepts())) { List list = new ArrayList<>(); for (int i = 0; i < authUserSaveRequest.getDepts().size(); i++) { DeptUserEntity deptUserEntity = new DeptUserEntity(); deptUserEntity.setUserId(authAdmin.getId()); deptUserEntity.setDeptId(authUserSaveRequest.getDepts().get(i)); list.add(deptUserEntity); } deptUserService.insertBatch(list); } else { } Map res = new HashMap<>(); res.put("id", authAdmin.getId()); return ResultVOUtils.success(res); } @Resource DeptUserService deptUserService; /** * 修改用户 */ @Log(title = "用户管理", businessType = BusinessType.UPDATE) @PostMapping("/edit") public BaseResponse edit(@RequestBody @Valid AuthUserSaveRequest authUserSaveRequest, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage()); } if (authUserSaveRequest.getId() == null) { return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, "参数错误!"); } // 检查是否存在除了当前管理员的其它名称的管理员 AuthAdmin byUserName = userService.findByUserName(authUserSaveRequest.getUserName()); if (byUserName != null && !authUserSaveRequest.getId().equals(byUserName.getId())) { return ResultVOUtils.error(ResultEnum.DATA_REPEAT, "当前管理员已存在"); } AuthAdmin authAdmin = new AuthAdmin(); BeanUtils.copyProperties(authUserSaveRequest, authAdmin); if (authAdmin.getPassWord() != null) { authAdmin.setPassWord(PasswordUtils.authAdminPwd(authAdmin.getPassWord())); } authAdmin.setLastModifyTime(new Date()); boolean b = userService.updateAuthAdmin(authAdmin); if (!b) { return ResultVOUtils.error(ResultEnum.NOT_NETWORK); } // 修改角色 // userService.insertUserAuth(authAdmin.getId(), authUserSaveRequest.getRoles()); //修改所属部门 deptUserService.deleteByUser(authUserSaveRequest.getId()); if (CollUtil.isNotEmpty(authUserSaveRequest.getDepts())) { List list = new ArrayList<>(); for (int i = 0; i < authUserSaveRequest.getDepts().size(); i++) { DeptUserEntity deptUserEntity = new DeptUserEntity(); deptUserEntity.setUserId(authUserSaveRequest.getId()); deptUserEntity.setDeptId(authUserSaveRequest.getDepts().get(i)); list.add(deptUserEntity); } deptUserService.insertBatch(list); } return ResultVOUtils.success(); } }