| | |
| | | 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.ApiModelProperty; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import io.swagger.annotations.ApiParam; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | |
| | | 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; |
| | | |
| | |
| | | @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); //表头行数 |
| | | 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 { |
| | | if (inputStream != null) { |
| | | try { |
| | | inputStream.close(); |
| | | } catch (IOException e) { |
| | | throw new ServiceException(e.getMessage()); |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | if (CollectionUtils.isEmpty(locationExcelList)) { |
| | | throw new ServiceException("员工数据为空!"); |
| | | } |
| | | |
| | | if (CollectionUtils.isEmpty(employeeExcelList)) { |
| | | throw new ServiceException("员工数据为空!"); |
| | | } |
| | | 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()); |
| | | |
| | | 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(); |
| | | } |
| | | if (!result.isEmpty()) { |
| | | return R.ok(result.toString()); |
| | | } |
| | | return R.ok(); |
| | | |
| | | JSONObject result = new JSONObject(); |
| | | 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")); |
| | | } |
| | | |
| | | return R.ok("导入完成", result.toString()); |
| | | } |
| | | |
| | | /** |
| | | * 解析日期字符串为Date对象 |
| | | * @param dateStr 日期字符串,格式如:2022.09 |
| | | * @return Date对象 |
| | | */ |
| | | private Date parseDate(String dateStr) { |
| | | if (StringUtils.isEmpty(dateStr)) { |
| | | return null; |
| | | } |
| | | 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); |
| | | List<EmployeeImportExcel> employeeImportExcels = new ArrayList<>(); |
| | | EmployeeImportExcel employeeImportExcel = new EmployeeImportExcel(); |
| | | employeeImportExcels.add(employeeImportExcel); |
| | | ExportParams exportParams = new ExportParams(); |
| | | exportParams.setTitle("员工信息导入模板"); |
| | | exportParams.setSheetName("员工信息"); |
| | | Workbook workbook = ExcelExportUtil.exportExcel(exportParams, EmployeeImportExcel.class, employeeImportExcels); |
| | | 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 { |
| | | if (outputStream != null) { |
| | | try { |
| | | outputStream.close(); |
| | | } catch (IOException e) { |
| | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 修改用户 |