| | |
| | | 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.domain.entity.TDept; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | | import com.ruoyi.common.enums.BusinessType; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | |
| | | import com.ruoyi.common.utils.WebUtils; |
| | | import com.ruoyi.framework.web.service.TokenService; |
| | | import com.ruoyi.system.dto.SysUserUpdateStatusDTO; |
| | | import com.ruoyi.system.importExcel.TSysUserImportExcel; |
| | | import com.ruoyi.system.importExcel.EmployeeImportExcel; |
| | | 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 io.swagger.annotations.ApiParam; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | | import org.apache.poi.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.usermodel.Row; |
| | | import org.apache.poi.ss.usermodel.Cell; |
| | | import org.apache.poi.ss.usermodel.CellStyle; |
| | | import org.apache.poi.ss.usermodel.Font; |
| | | import org.apache.poi.ss.usermodel.HorizontalAlignment; |
| | | import org.apache.poi.ss.usermodel.VerticalAlignment; |
| | | import org.apache.poi.ss.usermodel.BorderStyle; |
| | | import org.apache.poi.ss.util.CellRangeAddress; |
| | | import org.apache.poi.ss.util.RegionUtil; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.transaction.annotation.Propagation; |
| | |
| | | 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.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | |
| | | @Autowired |
| | | private ISysDeptService deptService; |
| | | @Autowired |
| | | private TDeptService tDeptService; |
| | | @Autowired |
| | | private TokenService tokenService; |
| | | |
| | | |
| | |
| | | return AjaxResult.success(list); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户列表 |
| | | */ |
| | | @ApiOperation(value = "根据部门id获取用户列表") |
| | | @GetMapping("/listByDeptId") |
| | | public AjaxResult<PageInfo<SysUserVO>> listByDeptId(@ApiParam(value = "部门id")Integer deptId, |
| | | @ApiParam(value = "分页参数,当前页码") |
| | | Integer pageNum, |
| | | @ApiParam(value = "分页参数,每页数量,默认为10") Integer pageSize ) { |
| | | PageInfo<SysUserVO> list = userService.listByDeptId(deptId,pageNum,pageSize); |
| | | return AjaxResult.success(list); |
| | | } |
| | | @ApiOperation(value = "获取用户列表-不分页") |
| | | @PostMapping("/listNotPage") |
| | | |
| | |
| | | } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) { |
| | | return error("新增用户'" + user.getUserName() + "'失败,手机号码已存在"); |
| | | } |
| | | user.setUserName(user.getPhonenumber()); |
| | | user.setCreateBy(getUsername()); |
| | | user.setPassword(SecurityUtils.encryptPassword("123456")); |
| | | userService.insertUser(user); |
| | |
| | | @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(); |
| | | public R<Object> importUser(@RequestPart("file") MultipartFile file) { |
| | | ImportParams params = new ImportParams(); |
| | | // params.setTitleRows(1); // 标题行数 |
| | | params.setHeadRows(1); //表头行数 |
| | | params.setHeadRows(1); // 表头行数 |
| | | InputStream inputStream = null; |
| | | // List<CustomerImportFailedData> failedData = new ArrayList<>(); |
| | | List<TSysUserImportExcel> locationExcelList; |
| | | List<EmployeeImportExcel> employeeExcelList; |
| | | |
| | | try { |
| | | inputStream = file.getInputStream(); |
| | | locationExcelList = ExcelImportUtil.importExcel(inputStream, |
| | | TSysUserImportExcel.class, params); |
| | | employeeExcelList = ExcelImportUtil.importExcel(inputStream, |
| | | EmployeeImportExcel.class, params); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new ServiceException("员工导入失败!"); |
| | | throw new ServiceException("员工导入失败: " + e.getMessage()); |
| | | } finally { |
| | | try { |
| | | inputStream.close(); |
| | | } catch (IOException e) { |
| | | throw new ServiceException(e.getMessage()); |
| | | if (inputStream != null) { |
| | | try { |
| | | inputStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | if (CollectionUtils.isEmpty(locationExcelList)) { |
| | | |
| | | if (CollectionUtils.isEmpty(employeeExcelList)) { |
| | | throw new ServiceException("员工数据为空!"); |
| | | } |
| | | |
| | | int successCount = 0; |
| | | int failCount = 0; |
| | | List<String> errorMessages = new ArrayList<>(); |
| | | |
| | | for (EmployeeImportExcel employeeExcel : employeeExcelList) { |
| | | try { |
| | | // 数据验证 |
| | | |
| | | |
| | | // 数据验证 |
| | | if (StringUtils.isEmpty(employeeExcel.getNickName())) { |
| | | errorMessages.add("第" + (successCount + failCount + 1) + "行:姓名为空"); |
| | | failCount++; |
| | | continue; |
| | | } |
| | | |
| | | if (StringUtils.isEmpty(employeeExcel.getPhoneNumber())) { |
| | | errorMessages.add("第" + (successCount + failCount + 1) + "行:联系电话为空"); |
| | | failCount++; |
| | | continue; |
| | | } |
| | | |
| | | // 转换并保存用户 |
| | | SysUser sysUser = convertToSysUser(employeeExcel); |
| | | if (StringUtils.isEmpty(employeeExcel.getDepartment())) { |
| | | errorMessages.add("第" + (successCount + failCount + 1) + "行:所在股室为空"); |
| | | failCount++; |
| | | continue; |
| | | } |
| | | |
| | | TDept dept = tDeptService.lambdaQuery().eq(TDept::getDeptName, employeeExcel.getDepartment()) |
| | | .last("limit 1").one(); |
| | | if (dept == null){ |
| | | errorMessages.add("第" + (successCount + failCount + 1) + "行:所在股室不存在"); |
| | | failCount++; |
| | | continue; |
| | | } |
| | | sysUser.setDeptId(dept.getId().toString()); |
| | | userService.insertUser(sysUser); |
| | | successCount++; |
| | | |
| | | } catch (Exception e) { |
| | | errorMessages.add("第" + (successCount + failCount + 1) + "行:" + e.getMessage()); |
| | | failCount++; |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | JSONObject result = new JSONObject(); |
| | | List<SysUser> sysUsers = new ArrayList<>(); |
| | | for (TSysUserImportExcel locationExcel : locationExcelList) { |
| | | System.err.println(locationExcel); |
| | | SysUser sysUser = new SysUser(); |
| | | sysUser.setStatus("0"); |
| | | sysUser.setDelFlag("0"); |
| | | sysUser.setCreateBy(getUsername()); |
| | | userService.insertUser(sysUser); |
| | | result.put("successCount", successCount); |
| | | result.put("failCount", failCount); |
| | | result.put("totalCount", successCount + failCount); |
| | | if (!errorMessages.isEmpty()) { |
| | | result.put("errorMessages", errorMessages); |
| | | return R.fail(result.get("errorMessages")); |
| | | } |
| | | if (!result.isEmpty()) { |
| | | return R.ok(result.toString()); |
| | | |
| | | return R.ok("导入完成", result.toString()); |
| | | } |
| | | |
| | | /** |
| | | * 解析日期字符串为Date对象 |
| | | * @param dateStr 日期字符串,格式如:2022.09 |
| | | * @return Date对象 |
| | | */ |
| | | private Date parseDate(String dateStr) { |
| | | if (StringUtils.isEmpty(dateStr)) { |
| | | return null; |
| | | } |
| | | return R.ok(); |
| | | try { |
| | | // 支持 YYYY.MM 格式 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); |
| | | return sdf.parse(dateStr); |
| | | } catch (ParseException e) { |
| | | try { |
| | | // 支持 YYYY-MM 格式 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); |
| | | return sdf.parse(dateStr); |
| | | } catch (ParseException ex) { |
| | | try { |
| | | // 支持 YYYY-MM-DD 格式 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | return sdf.parse(dateStr); |
| | | } catch (ParseException exc) { |
| | | return null; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将EmployeeImportExcel转换为SysUser |
| | | * @param employeeExcel 员工导入Excel数据 |
| | | * @return SysUser对象 |
| | | */ |
| | | private SysUser convertToSysUser(EmployeeImportExcel employeeExcel) { |
| | | SysUser sysUser = new SysUser(); |
| | | sysUser.setPassword(SecurityUtils.encryptPassword("123456")); |
| | | |
| | | // 基本信息 |
| | | sysUser.setNickName(employeeExcel.getNickName()); |
| | | sysUser.setUserName(employeeExcel.getPhoneNumber()); |
| | | sysUser.setPhonenumber(employeeExcel.getPhoneNumber()); |
| | | // 性别转换 |
| | | if ("男".equals(employeeExcel.getSex())) { |
| | | sysUser.setSex("0"); |
| | | } else if ("女".equals(employeeExcel.getSex())) { |
| | | sysUser.setSex("1"); |
| | | } else { |
| | | sysUser.setSex("0"); |
| | | } |
| | | |
| | | // 身份证 |
| | | sysUser.setIdCard(employeeExcel.getIdCard()); |
| | | |
| | | // 政治面貌 |
| | | sysUser.setPoliticalOutlook(employeeExcel.getPoliticalOutlook()); |
| | | |
| | | // 时间字段 |
| | | sysUser.setParticipationTime(parseDate(employeeExcel.getParticipationTime())); |
| | | sysUser.setCompanyTime(parseDate(employeeExcel.getCompanyTime())); |
| | | sysUser.setPositionTime(parseDate(employeeExcel.getPositionTime())); |
| | | // 学历信息 |
| | | sysUser.setFirstDegree(employeeExcel.getFirstDegree()); |
| | | sysUser.setFirstCollege(employeeExcel.getFirstCollege()); |
| | | sysUser.setFirstMajor(employeeExcel.getFirstMajor()); |
| | | sysUser.setHighestDegree(employeeExcel.getHighestDegree()); |
| | | sysUser.setHighestCollege(employeeExcel.getHighestCollege()); |
| | | sysUser.setHighestMajor(employeeExcel.getHighestMajor()); |
| | | |
| | | // 职务和部门 |
| | | sysUser.setPosition(employeeExcel.getPosition()); |
| | | sysUser.setOrganization(employeeExcel.getOrganization()); |
| | | |
| | | // 系统字段 |
| | | sysUser.setStatus("0"); // 正常状态 |
| | | sysUser.setDelFlag("0"); // 未删除 |
| | | sysUser.setCreateBy(getUsername()); |
| | | |
| | | return sysUser; |
| | | } |
| | | |
| | | @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); |
| | | // 自定义表头,构建两行表头并合并"学历"大标题 |
| | | Workbook workbook = new HSSFWorkbook(); |
| | | Sheet sheet = workbook.createSheet("员工信息"); |
| | | |
| | | // 列宽适配 |
| | | int col = 0; |
| | | String[] firstRowHeads = new String[]{ |
| | | "序号","姓名","性别","身份证号码","年龄","政治面貌","参工时间","进入公司时间", |
| | | "学历","所在股室","职务","任职时间","电话号码","编制" |
| | | }; |
| | | String[] secondRowHeads = new String[]{ |
| | | "","","","","","","","", |
| | | "第一","学院","专业","最高","学院","专业", |
| | | "","","","","","" |
| | | }; |
| | | |
| | | // 创建样式 |
| | | CellStyle headStyle = workbook.createCellStyle(); |
| | | headStyle.setAlignment(HorizontalAlignment.CENTER); |
| | | headStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | headStyle.setBorderBottom(BorderStyle.THIN); |
| | | headStyle.setBorderTop(BorderStyle.THIN); |
| | | headStyle.setBorderLeft(BorderStyle.THIN); |
| | | headStyle.setBorderRight(BorderStyle.THIN); |
| | | Font font = workbook.createFont(); |
| | | font.setBold(true); |
| | | headStyle.setFont(font); |
| | | |
| | | Row row1 = sheet.createRow(0); |
| | | Row row2 = sheet.createRow(1); |
| | | |
| | | // 先填充第一行 |
| | | for (int i = 0; i < firstRowHeads.length; i++) { |
| | | Cell cell = row1.createCell(i); |
| | | cell.setCellValue(firstRowHeads[i]); |
| | | cell.setCellStyle(headStyle); |
| | | sheet.setColumnWidth(i, 16 * 256); |
| | | } |
| | | // 第二行 |
| | | for (int i = 0; i < secondRowHeads.length; i++) { |
| | | Cell cell = row2.createCell(i); |
| | | cell.setCellValue(secondRowHeads[i]); |
| | | cell.setCellStyle(headStyle); |
| | | } |
| | | |
| | | // 合并除"学历"外的单列表头(两行合并): 0-7 列,以及 9-13 列 |
| | | int[][] singleMerge = new int[][]{ |
| | | {0,0},{1,1},{2,2},{3,3},{4,4},{5,5},{6,6},{7,7}, |
| | | {9,9},{10,10},{11,11},{12,12},{13,13} |
| | | }; |
| | | for (int[] m : singleMerge) { |
| | | CellRangeAddress region = new CellRangeAddress(0,1,m[0],m[1]); |
| | | sheet.addMergedRegion(region); |
| | | RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet); |
| | | RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet); |
| | | RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet); |
| | | RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet); |
| | | } |
| | | |
| | | // 合并"学历"大标题区域:第8列到第13列(共6列)第一行 |
| | | CellRangeAddress xlMerge = new CellRangeAddress(0,0,8,13); |
| | | sheet.addMergedRegion(xlMerge); |
| | | RegionUtil.setBorderBottom(BorderStyle.THIN, xlMerge, sheet); |
| | | RegionUtil.setBorderTop(BorderStyle.THIN, xlMerge, sheet); |
| | | RegionUtil.setBorderLeft(BorderStyle.THIN, xlMerge, sheet); |
| | | RegionUtil.setBorderRight(BorderStyle.THIN, xlMerge, sheet); |
| | | |
| | | HttpServletResponse response = WebUtils.response(); |
| | | ServletOutputStream outputStream = null; |
| | | try { |
| | | String fileName = URLEncoder.encode("员工导入模板.xls", "utf-8"); |
| | | 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"); |
| | |
| | | e.printStackTrace(); |
| | | System.out.println("员工导入模板下载失败!"); |
| | | } finally { |
| | | try { |
| | | outputStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | if (outputStream != null) { |
| | | try { |
| | | outputStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) { |
| | | return error("修改用户'" + user.getUserName() + "'失败,手机号码已存在"); |
| | | } |
| | | |
| | | user.setUserName(user.getPhonenumber()); |
| | | user.setUpdateBy(getUsername()); |
| | | if (StringUtils.isNotEmpty(user.getPassword())) { |
| | | user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); |