| | |
| | | package com.ruoyi.web.controller.system; |
| | | |
| | | import cn.afterturn.easypoi.excel.ExcelExportUtil; |
| | | import cn.afterturn.easypoi.excel.ExcelImportUtil; |
| | | import cn.afterturn.easypoi.excel.entity.ExportParams; |
| | | import cn.afterturn.easypoi.excel.entity.ImportParams; |
| | | import cn.hutool.json.JSONObject; |
| | | import com.ruoyi.common.annotation.Log; |
| | | import com.ruoyi.common.basic.PageInfo; |
| | | import com.ruoyi.common.core.controller.BaseController; |
| | |
| | | import com.ruoyi.common.core.domain.entity.SysDept; |
| | | import com.ruoyi.common.core.domain.entity.SysRole; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | | import com.ruoyi.common.enums.BusinessType; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.WebUtils; |
| | | import com.ruoyi.framework.web.service.TokenService; |
| | | import com.ruoyi.system.dto.SysUserUpdateStatusDTO; |
| | | import com.ruoyi.system.importExcel.TCleanerImportExcel; |
| | | import com.ruoyi.system.importExcel.TSysUserImportExcel; |
| | | import com.ruoyi.system.model.TCleaner; |
| | | import com.ruoyi.system.model.TDept; |
| | | import com.ruoyi.system.model.TProjectDept; |
| | | import com.ruoyi.system.model.TTemplate; |
| | | import com.ruoyi.system.query.SysUserQuery; |
| | | import com.ruoyi.system.service.*; |
| | | import com.ruoyi.system.vo.system.SysUserVO; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.security.access.prepost.PreAuthorize; |
| | | import org.springframework.transaction.annotation.Propagation; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.URLEncoder; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 用户信息 |
| | | * |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Api(tags = "用户信息") |
| | | @RestController |
| | | @RequestMapping("/system/user") |
| | | public class SysUserController extends BaseController |
| | | { |
| | | public class SysUserController extends BaseController { |
| | | @Autowired |
| | | private ISysUserService userService; |
| | | |
| | |
| | | private TProjectDeptService projectDeptService; |
| | | @Resource |
| | | private TDeptService tdeptService; |
| | | @Resource |
| | | private TTemplateService templateService; |
| | | |
| | | /** |
| | | * 获取用户列表 |
| | | */ |
| | | @ApiOperation(value = "获取用户列表") |
| | | @PostMapping("/list") |
| | | public AjaxResult<PageInfo<SysUserVO>> list(@RequestBody SysUserQuery query) |
| | | { |
| | | public AjaxResult<PageInfo<SysUserVO>> list(@RequestBody SysUserQuery query) { |
| | | List<SysUser> sysUsers = userService.selectAllList(); |
| | | |
| | | if (org.springframework.util.StringUtils.hasLength(query.getDeptName())){ |
| | | if (org.springframework.util.StringUtils.hasLength(query.getDeptName())) { |
| | | List<String> projectIds = projectDeptService.lambdaQuery().like(TProjectDept::getProjectName, query.getDeptName()).list() |
| | | .stream().map(TProjectDept::getId).collect(Collectors.toList()); |
| | | List<String> deptIds = tdeptService.lambdaQuery().like(TDept::getDeptName, query.getDeptName()).list() |
| | | .stream().map(TDept::getId).collect(Collectors.toList()); |
| | | projectIds.addAll(deptIds); |
| | | if (projectIds.isEmpty()){ |
| | | if (projectIds.isEmpty()) { |
| | | return AjaxResult.success(new PageInfo<>()); |
| | | } |
| | | List<Long> patrolInspectorIds = sysUsers.stream().filter(sysUser -> |
| | | projectIds.contains(sysUser.getDeptId()) |
| | | ).map(SysUser::getUserId).collect(Collectors.toList()); |
| | | if (patrolInspectorIds.isEmpty()){ |
| | | return AjaxResult.success(new PageInfo<>()); |
| | | } |
| | | query.setPatrolInspectorIds(patrolInspectorIds); |
| | | List<Long> patrolInspectorIds = sysUsers.stream().filter(sysUser -> |
| | | projectIds.contains(sysUser.getDeptId()) |
| | | ).map(SysUser::getUserId).collect(Collectors.toList()); |
| | | if (patrolInspectorIds.isEmpty()) { |
| | | return AjaxResult.success(new PageInfo<>()); |
| | | } |
| | | query.setPatrolInspectorIds(patrolInspectorIds); |
| | | } |
| | | PageInfo<SysUserVO> list = userService.pageList(query); |
| | | return AjaxResult.success(list); |
| | |
| | | @ApiOperation(value = "获取用户列表-不分页") |
| | | @PostMapping("/listNotPage") |
| | | |
| | | public AjaxResult listNotPage() |
| | | { |
| | | public AjaxResult listNotPage() { |
| | | List<SysUser> list = userService.selectList(); |
| | | return AjaxResult.success(list); |
| | | } |
| | |
| | | */ |
| | | @ApiOperation(value = "获取用户详情") |
| | | @GetMapping("/getDetail") |
| | | public AjaxResult getDetail(@RequestParam Long userId) |
| | | { |
| | | public AjaxResult getDetail(@RequestParam Long userId) { |
| | | SysUser sysUser = userService.selectUserById(userId); |
| | | SysUserVO sysUserVO = new SysUserVO(); |
| | | BeanUtils.copyProperties(sysUser,sysUserVO); |
| | | BeanUtils.copyProperties(sysUser, sysUserVO); |
| | | |
| | | return AjaxResult.success(sysUser); |
| | | } |
| | |
| | | */ |
| | | @ApiOperation(value = "获取用户详情") |
| | | @GetMapping("/queryDetail") |
| | | public AjaxResult queryDetail() |
| | | { |
| | | public AjaxResult queryDetail() { |
| | | Long userId = tokenService.getLoginUser().getUserId(); |
| | | SysUser sysUser = userService.selectUserById(userId); |
| | | SysUserVO sysUserVO = new SysUserVO(); |
| | | BeanUtils.copyProperties(sysUser,sysUserVO); |
| | | BeanUtils.copyProperties(sysUser, sysUserVO); |
| | | |
| | | return AjaxResult.success(sysUser); |
| | | } |
| | |
| | | */ |
| | | @ApiOperation(value = "获取用户数量统计") |
| | | @PostMapping("/getUserCount") |
| | | public AjaxResult getUserCount() |
| | | { |
| | | Map<String,Integer> map = new HashMap<>(); |
| | | public AjaxResult getUserCount() { |
| | | Map<String, Integer> map = new HashMap<>(); |
| | | |
| | | Integer userCountSum = userService.selectCount(null); |
| | | Integer normalCount = userService.selectCount(0);// 正常 |
| | | Integer stopCount = userService.selectCount(1);// 停用 |
| | | |
| | | map.put("all",userCountSum); |
| | | map.put("normal",normalCount); |
| | | map.put("stop",stopCount); |
| | | map.put("all", userCountSum); |
| | | map.put("normal", normalCount); |
| | | map.put("stop", stopCount); |
| | | |
| | | return AjaxResult.success(map); |
| | | } |
| | |
| | | * 移除黑名单 |
| | | */ |
| | | @GetMapping("/removeBlackList") |
| | | public AjaxResult removeBlackList(@RequestParam String ids) |
| | | { |
| | | public AjaxResult removeBlackList(@RequestParam String ids) { |
| | | String[] split = ids.split(","); |
| | | List<Long> id = new ArrayList<>(); |
| | | for (String s : split) { |
| | |
| | | @ApiOperation(value = "新增用户管理") |
| | | @Log(title = "用户信息-新增用户", businessType = BusinessType.INSERT) |
| | | @PostMapping("/add") |
| | | public AjaxResult add(@Validated @RequestBody SysUser user) |
| | | { |
| | | public AjaxResult add(@Validated @RequestBody SysUser user) { |
| | | user.setUserName(user.getUserName()); |
| | | if (!userService.checkUserNameUnique(user)) |
| | | { |
| | | if (!userService.checkUserNameUnique(user)) { |
| | | return error("新增用户'" + user.getUserName() + "'失败,登录账号已存在"); |
| | | } |
| | | else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) |
| | | { |
| | | } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) { |
| | | return error("新增用户'" + user.getUserName() + "'失败,手机号码已存在"); |
| | | } |
| | | user.setCreateBy(getUsername()); |
| | | user.setPassword(SecurityUtils.encryptPassword("123456")); |
| | | userService.insertUser(user); |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | @Log(title = "员工导入", businessType = BusinessType.INSERT) |
| | | @ApiOperation(value = "员工导入") |
| | | @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) |
| | | @PostMapping("/importUser") |
| | | public R<String> importUser(@RequestPart("file") MultipartFile file) { |
| | | List<SysRole> sysRoles = roleService.selectRoleAll(); |
| | | ImportParams params = new ImportParams(); |
| | | // params.setTitleRows(1); // 标题行数 |
| | | params.setHeadRows(1); //表头行数 |
| | | InputStream inputStream = null; |
| | | // List<CustomerImportFailedData> failedData = new ArrayList<>(); |
| | | List<TSysUserImportExcel> locationExcelList; |
| | | try { |
| | | inputStream = file.getInputStream(); |
| | | locationExcelList = ExcelImportUtil.importExcel(inputStream, |
| | | TSysUserImportExcel.class, params); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new ServiceException("员工导入失败!"); |
| | | } finally { |
| | | try { |
| | | inputStream.close(); |
| | | } catch (IOException e) { |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | } |
| | | if (CollectionUtils.isEmpty(locationExcelList)) { |
| | | throw new ServiceException("员工数据为空!"); |
| | | |
| | | } |
| | | JSONObject result = new JSONObject(); |
| | | List<TProjectDept> projects = projectDeptService.list(); |
| | | List<SysUser> sysUsers = new ArrayList<>(); |
| | | for (TSysUserImportExcel locationExcel : locationExcelList) { |
| | | System.err.println(locationExcel); |
| | | SysUser sysUser = new SysUser(); |
| | | if (locationExcel.getDeptType() == null) { |
| | | result.append("所属部门", "所属部门请填写1或2"); |
| | | continue; |
| | | } else { |
| | | sysUser.setDeptType(locationExcel.getDeptType()); |
| | | switch (locationExcel.getDeptType()) { |
| | | case 1: |
| | | TProjectDept projectDept = projectDeptService.lambdaQuery().eq(TProjectDept::getProjectName, locationExcel.getDeptName()) |
| | | .last("limit 1").one(); |
| | | if (projectDept==null){ |
| | | result.append("部门名称", "部门名称不存在"); |
| | | continue; |
| | | }else{ |
| | | sysUser.setDeptId(projectDept.getId()); |
| | | } |
| | | |
| | | break; |
| | | case 2: |
| | | TDept dept = tdeptService.lambdaQuery().eq(TDept::getDeptName, locationExcel.getDeptName()) |
| | | .last("limit 1").one(); |
| | | if (dept==null){ |
| | | result.append("部门名称", "部门名称不存在"); |
| | | continue; |
| | | }else{ |
| | | sysUser.setDeptId(dept.getId()); |
| | | } |
| | | break; |
| | | } |
| | | } |
| | | if (!org.springframework.util.StringUtils.hasLength(locationExcel.getRoleName())){ |
| | | result.append("角色名称", "角色名称不能为空"); |
| | | continue; |
| | | }else{ |
| | | SysRole sysRole = sysRoles.stream().filter(e -> e.getRoleName().equals(locationExcel.getRoleName()) && |
| | | e.getDelFlag().equals(0)).findFirst().orElse(null); |
| | | if (sysRole==null){ |
| | | result.append("角色", "角色名称不存在"); |
| | | continue; |
| | | }else{ |
| | | if (sysRole.getStatus()==1){ |
| | | result.append("角色", "角色已禁用"); |
| | | continue; |
| | | }else{ |
| | | sysRole.setRoleId(sysRole.getRoleId()); |
| | | } |
| | | } |
| | | } |
| | | if (!org.springframework.util.StringUtils.hasLength(locationExcel.getNickName())){ |
| | | result.append("员工名称", "员工名称不能为空"); |
| | | continue; |
| | | }else{ |
| | | sysUser.setNickName(locationExcel.getNickName()); |
| | | } |
| | | if (!org.springframework.util.StringUtils.hasLength(locationExcel.getUserName())){ |
| | | result.append("登录账号", "登录账号不能为空"); |
| | | continue; |
| | | }else{ |
| | | sysUser.setUserName(locationExcel.getUserName()); |
| | | } |
| | | if (!org.springframework.util.StringUtils.hasLength(locationExcel.getEmail())){ |
| | | result.append("邮箱", "邮箱不能为空"); |
| | | continue; |
| | | }else{ |
| | | sysUser.setEmail(locationExcel.getEmail()); |
| | | } |
| | | if (!org.springframework.util.StringUtils.hasLength(locationExcel.getPhonenumber())){ |
| | | result.append("联系电话", "联系电话不能为空"); |
| | | continue; |
| | | }else{ |
| | | sysUser.setPhonenumber(locationExcel.getPhonenumber()); |
| | | // 如果手机号小于六位 |
| | | if (locationExcel.getPhonenumber().length() < 6){ |
| | | result.append("联系电话", "联系电话格式错误"); |
| | | continue; |
| | | } |
| | | // 截取手机号后六位 |
| | | sysUser.setPassword(SecurityUtils.encryptPassword(locationExcel.getPhonenumber().substring(locationExcel.getPhonenumber().length() - 6))); |
| | | } |
| | | sysUser.setStatus("0"); |
| | | sysUser.setDelFlag("0"); |
| | | if (!org.springframework.util.StringUtils.hasLength(locationExcel.getTemplateName())){ |
| | | result.append("模板名称", "模板名称不能为空"); |
| | | continue; |
| | | }else{ |
| | | TTemplate template = templateService.lambdaQuery().eq(TTemplate::getTemplateName, locationExcel.getTemplateName()) |
| | | .last("limit 1").one(); |
| | | if (template==null){ |
| | | result.append("模板名称", "模板名称不存在"); |
| | | continue; |
| | | }else{ |
| | | sysUser.setTemplateId(template.getId()); |
| | | } |
| | | } |
| | | sysUser.setCreateBy(getUsername()); |
| | | userService.insertUser(sysUser); |
| | | } |
| | | if (!result.isEmpty()) { |
| | | return R.ok(result.toString()); |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |
| | | @ApiOperation(value = "员工导入模板下载") |
| | | @GetMapping("/importDownload") |
| | | public void importDownload() { |
| | | List<TSysUserImportExcel> locationImportExcels = new ArrayList<>(); |
| | | TSysUserImportExcel tLocationImportExcel = new TSysUserImportExcel(); |
| | | locationImportExcels.add(tLocationImportExcel); |
| | | Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TSysUserImportExcel.class, locationImportExcels); |
| | | HttpServletResponse response = WebUtils.response(); |
| | | ServletOutputStream outputStream = null; |
| | | try { |
| | | String fileName = URLEncoder.encode("员工导入模板.xls", "utf-8"); |
| | | response.setHeader("Content-Disposition", "attachment;filename=" + fileName); |
| | | response.setHeader("content-Type", "application/vnd.ms-excel"); |
| | | response.setHeader("Pragma", "no-cache"); |
| | | response.setHeader("Cache-Control", "no-cache"); |
| | | outputStream = response.getOutputStream(); |
| | | workbook.write(outputStream); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | System.out.println("员工导入模板下载失败!"); |
| | | } finally { |
| | | try { |
| | | outputStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | @ApiOperation(value = "修改用户管理") |
| | | @Log(title = "用户信息-修改用户", businessType = BusinessType.UPDATE) |
| | | @PostMapping("/edit") |
| | | public AjaxResult edit(@Validated @RequestBody SysUser user) |
| | | { |
| | | public AjaxResult edit(@Validated @RequestBody SysUser user) { |
| | | user.setUserName(user.getPhonenumber()); |
| | | // userService.checkUserAllowed(user); |
| | | // userService.checkUserDataScope(user.getUserId()); |
| | | if (!userService.checkUserNameUnique(user)) |
| | | { |
| | | if (!userService.checkUserNameUnique(user)) { |
| | | return error("修改用户'" + user.getUserName() + "'失败,登录账号已存在"); |
| | | } |
| | | else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) |
| | | { |
| | | } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) { |
| | | return error("修改用户'" + user.getUserName() + "'失败,手机号码已存在"); |
| | | } |
| | | |
| | | user.setUpdateBy(getUsername()); |
| | | if(StringUtils.isNotEmpty(user.getPassword())){ |
| | | if (StringUtils.isNotEmpty(user.getPassword())) { |
| | | user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); |
| | | } |
| | | return AjaxResult.success(userService.updateUser(user)); |
| | |
| | | @ApiOperation(value = "批量删除用户") |
| | | @Log(title = "用户信息-批量删除用户", businessType = BusinessType.DELETE) |
| | | @DeleteMapping("/deleteById/{ids}") |
| | | public AjaxResult remove(@PathVariable String ids) |
| | | { |
| | | public AjaxResult remove(@PathVariable String ids) { |
| | | String[] split = ids.split(","); |
| | | List<Long> userIds = new ArrayList<>(); |
| | | for (String s : split) { |
| | | userIds.add(Long.valueOf(s)); |
| | | } |
| | | if (userIds.contains(getUserId())) |
| | | { |
| | | if (userIds.contains(getUserId())) { |
| | | return error("当前用户不能删除"); |
| | | } |
| | | return AjaxResult.success(userService.deleteUserByIds(userIds)); |
| | |
| | | @ApiOperation(value = "重置密码") |
| | | @Log(title = "用户信息-重置密码", businessType = BusinessType.UPDATE) |
| | | @PostMapping("/resetPwd") |
| | | public AjaxResult resetPwd(@RequestBody SysUser user) |
| | | { |
| | | public AjaxResult resetPwd(@RequestBody SysUser user) { |
| | | userService.checkUserAllowed(user); |
| | | // userService.checkUserDataScope(user.getUserId()); |
| | | user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); |
| | |
| | | @ApiOperation(value = "状态修改/启用禁用") |
| | | @Log(title = "用户信息-状态修改", businessType = BusinessType.UPDATE) |
| | | @PutMapping("/changeStatus") |
| | | public AjaxResult changeStatus(@RequestBody SysUserUpdateStatusDTO dto) |
| | | { |
| | | public AjaxResult changeStatus(@RequestBody SysUserUpdateStatusDTO dto) { |
| | | SysUser loginUser = tokenService.getLoginUser().getUser(); |
| | | SysUser user = new SysUser(); |
| | | user.setUserId(dto.getUserId()); |
| | |
| | | user.setUpdateBy(getUsername()); |
| | | user.setDisableRemark(dto.getRemark()); |
| | | user.setOperatingTime(LocalDateTime.now()); |
| | | user.setOperatingPerson(loginUser.getNickName()+"("+loginUser.getUserName()+")"); |
| | | user.setOperatingPerson(loginUser.getNickName() + "(" + loginUser.getUserName() + ")"); |
| | | return AjaxResult.success(userService.updateUserStatus(user)); |
| | | } |
| | | |
| | |
| | | */ |
| | | // @PreAuthorize("@ss.hasPermi('system:user:query')") |
| | | @GetMapping("/authRole/{userId}") |
| | | public AjaxResult authRole(@PathVariable("userId") Long userId) |
| | | { |
| | | public AjaxResult authRole(@PathVariable("userId") Long userId) { |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | SysUser user = userService.selectUserById(userId); |
| | | List<SysRole> roles = roleService.selectRolesByUserId(userId); |
| | |
| | | // @PreAuthorize("@ss.hasPermi('system:user:edit')") |
| | | @Log(title = "用户管理", businessType = BusinessType.GRANT) |
| | | @PutMapping("/authRole") |
| | | public AjaxResult insertAuthRole(Long userId, Long[] roleIds) |
| | | { |
| | | public AjaxResult insertAuthRole(Long userId, Long[] roleIds) { |
| | | userService.checkUserDataScope(userId); |
| | | userService.insertUserAuth(userId, roleIds); |
| | | return AjaxResult.success(); |
| | |
| | | */ |
| | | // @PreAuthorize("@ss.hasPermi('system:user:list')") |
| | | @GetMapping("/deptTree") |
| | | public AjaxResult deptTree(SysDept dept) |
| | | { |
| | | public AjaxResult deptTree(SysDept dept) { |
| | | return AjaxResult.success(deptService.selectDeptTreeList(dept)); |
| | | } |
| | | } |