package jnpf.controller; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.TemplateExportParams; import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; import com.alibaba.fastjson.JSON; import feign.Response; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import jnpf.base.ActionResult; import jnpf.base.controller.SuperController; import jnpf.base.vo.DownloadVO; import jnpf.base.vo.PageListVO; import jnpf.base.vo.PaginationVO; import jnpf.config.ConfigValueUtil; import jnpf.constant.FileTypeConstant; import jnpf.constant.MsgCode; import jnpf.entity.EmployeeEntity; import jnpf.exception.DataException; import jnpf.exception.ImportException; import jnpf.file.FileApi; import jnpf.file.FileUploadApi; import jnpf.model.EmployeeModel; import jnpf.model.employee.*; import jnpf.model.upload.UploadFileModel; import jnpf.service.EmployeeService; import jnpf.util.*; import jnpf.util.type.StringNumber; import lombok.Cleanup; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; import org.dromara.x.file.storage.core.FileInfo; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 职员信息 * * @author JNPF开发平台组 * @version V3.1.0 * @copyright 引迈信息技术有限公司 */ @Slf4j @Tag(name = "职员信息", description = "Employee") @RestController @RequestMapping("/Employee") @RequiredArgsConstructor public class EmployeeController extends SuperController { private final EmployeeService employeeService; private final ConfigValueUtil configValueUtil; private final FileApi fileApi; private final FileUploadApi fileUploadApi; private static final String BIRTHDAY = "birthday"; private static final String GRADUATION_ACADEMY = "graduationAcademy"; private static final String EDUCATION = "education"; private static final String XLSX = ".xlsx"; private static final String FULL_NAME = "fullName"; private static final String GENDER = "gender"; private static final String DEPARTMENT_NAME = "departmentName"; private static final String POSITION_NAME = "positionName"; private static final String WORKING_NATURE = "workingNature"; private static final String ID_NUMBER = "idNumber"; private static final String TELEPHONE = "telephone"; private static final String MAJOR = "major"; private static final String ATTEND_WORK_TIME = "attendWorkTime"; private static final String GRADUATION_TIME = "graduationTime"; private static final String TIME = "yyyyMMdd"; private static final String YYYY_MM_DD = "yyyy-MM-dd"; private static final String MESSAGE = "信息导出Excel错误:{}"; private static final String EN_CODE = "enCode"; /** * 列表(忽略验证Token) * * @param paginationEmployee 分页模型 * @return */ @Operation(summary = "获取职员列表") @GetMapping public ActionResult> getList(PaginationEmployee paginationEmployee) { List data = employeeService.getList(paginationEmployee); List list = JsonUtil.getJsonToList(data, EmployeeListVO.class); PaginationVO paginationVO = JsonUtil.getJsonToBean(paginationEmployee, PaginationVO.class); return ActionResult.page(list, paginationVO); } /** * 信息 * * @param id 主键 * @return */ @Operation(summary = "获取职员信息") @GetMapping("/{id}") @Parameter(name = "id", description = "主键", required = true) public ActionResult info(@PathVariable("id") String id) throws DataException { EmployeeEntity entity = employeeService.getInfo(id); EmployeeInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, EmployeeInfoVO.class); return ActionResult.success(vo); } /** * 新建 * * @param employeeCrForm 职工模型 * @return */ @Operation(summary = "app添加职员信息") @PostMapping @Parameter(name = "employeeCrForm", description = "职工模型", required = true) public ActionResult create(@RequestBody @Valid EmployeeCrForm employeeCrForm) { EmployeeEntity entity = JsonUtil.getJsonToBean(employeeCrForm, EmployeeEntity.class); employeeService.create(entity); return ActionResult.success(MsgCode.SU001.get()); } /** * 更新 * * @param id 主键 * @param employeeUpForm 职工模型 * @return */ @Operation(summary = "app修改职员信息") @PutMapping("/{id}") @Parameter(name = "id", description = "主键", required = true) @Parameter(name = "employeeUpForm", description = "职工模型", required = true) public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid EmployeeUpForm employeeUpForm) { EmployeeEntity entity = JsonUtil.getJsonToBean(employeeUpForm, EmployeeEntity.class); employeeService.update(id, entity); return ActionResult.success(MsgCode.SU004.get()); } /** * 删除 * * @param id 主键 * @return */ @Operation(summary = "删除职员信息") @DeleteMapping("/{id}") @Parameter(name = "id", description = "主键", required = true) public ActionResult delete(@PathVariable("id") String id) { EmployeeEntity entity = employeeService.getInfo(id); if (entity != null) { employeeService.delete(entity); return ActionResult.success(MsgCode.SU003.get()); } return ActionResult.fail(MsgCode.FA003.get()); } /** * 模板下载 * * @return */ @Operation(summary = "模板下载") @GetMapping("/TemplateDownload") public ActionResult templateDownload() { String fileName = "职员信息.xlsx"; DownloadVO vo = DownloadVO.builder().build(); try { vo.setName(fileName); vo.setUrl(UploaderUtil.uploaderFile(fileName + "#" + FileTypeConstant.TEMPLATEFILE) + "&name=" + fileName); } catch (Exception e) { log.error(MESSAGE, e.getMessage()); } return ActionResult.success(vo); } /** * 导出Excel * * @return */ @Operation(summary = "导出Excel") @GetMapping("/ExportExcel") public ActionResult exportExcel() { List entityList = employeeService.getList(); List list = JsonUtil.listToJsonField(JsonUtil.getJsonToList(JsonUtilEx.getObjectToStringDateFormat(entityList, YYYY_MM_DD), EmployeeExportVO.class)); List entitys = new ArrayList<>(); entitys.add(new ExcelExportEntity("工号", EN_CODE)); entitys.add(new ExcelExportEntity("姓名", FULL_NAME)); entitys.add(new ExcelExportEntity("性别", GENDER)); entitys.add(new ExcelExportEntity("部门", DEPARTMENT_NAME)); entitys.add(new ExcelExportEntity("职务", POSITION_NAME, 25)); entitys.add(new ExcelExportEntity("用工性质", WORKING_NATURE)); entitys.add(new ExcelExportEntity("身份证号", ID_NUMBER, 25)); entitys.add(new ExcelExportEntity("联系电话", TELEPHONE, 20)); entitys.add(new ExcelExportEntity("出生年月", BIRTHDAY, 20)); entitys.add(new ExcelExportEntity("参加工作", ATTEND_WORK_TIME, 20)); entitys.add(new ExcelExportEntity("最高学历", EDUCATION)); entitys.add(new ExcelExportEntity("所学专业", MAJOR)); entitys.add(new ExcelExportEntity("毕业院校", GRADUATION_ACADEMY)); entitys.add(new ExcelExportEntity("毕业时间", GRADUATION_TIME, 20)); ExportParams exportParams = new ExportParams(null, "职员信息"); exportParams.setType(ExcelType.XSSF); Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entitys, list); String name = "职员信息" + DateUtil.dateNow(TIME) + "_" + RandomUtil.uuId() + XLSX; String fileName = fileApi.getLocalBasePath() + fileApi.getPath(FileTypeConstant.TEMPORARY) + name; DownloadVO vo = DownloadVO.builder().build(); try { @Cleanup FileOutputStream output = new FileOutputStream(fileName); workbook.write(output); vo.setName(name); vo.setUrl(UploaderUtil.uploaderFile(name + "#" + FileTypeConstant.TEMPORARY)); } catch (Exception e) { log.error(MESSAGE, e.getMessage()); } return ActionResult.success(vo); } /** * 导出Word * * @return */ @Operation(summary = "导出Word") @GetMapping("/ExportWord") public ActionResult exportWord() { List list = employeeService.getList(); //模板文件地址 String inputUrl = fileApi.getPath(FileTypeConstant.TEMPLATEFILE) + "employee_export_template.docx"; //新生产的模板文件 String name = "职员信息" + DateUtil.dateNow(TIME) + "_" + RandomUtil.uuId() + ".docx"; String outputUrl = fileApi.getLocalBasePath() + fileApi.getPath(FileTypeConstant.TEMPORARY) + name; List testList = new ArrayList<>(); Map testMap = new HashMap<>(); for (int i = 0; i < list.size(); i++) { String[] employee = new String[13]; EmployeeEntity entity = list.get(i); employee[0] = entity.getFullName(); employee[1] = entity.getGender(); employee[2] = entity.getDepartmentName(); employee[3] = entity.getPositionName(); employee[4] = entity.getWorkingNature(); employee[5] = entity.getIdNumber(); employee[6] = entity.getTelephone(); employee[7] = entity.getBirthday() != null ? DateUtil.daFormat(entity.getBirthday()) : ""; employee[8] = entity.getAttendWorkTime() != null ? DateUtil.daFormat(entity.getAttendWorkTime()) : ""; employee[9] = entity.getEducation(); employee[10] = entity.getMajor(); employee[11] = entity.getGraduationAcademy(); employee[12] = entity.getGraduationTime() != null ? DateUtil.daFormat(entity.getGraduationTime()) : ""; testList.add(employee); } WordUtil.changWord(inputUrl, outputUrl, testMap, testList); if (FileUtil.fileIsFile(outputUrl)) { DownloadVO vo = DownloadVO.builder().name(name).url(UploaderUtil.uploaderFile(name + "#" + FileTypeConstant.TEMPORARY)).build(); return ActionResult.success(vo); } return ActionResult.success(MsgCode.ETD109.get()); } /** * 导出pdf * * @return */ @Operation(summary = "导出pdf") @GetMapping("/ExportPdf") public ActionResult exportPdf() { String name = "职员信息" + DateUtil.dateNow(TIME) + "_" + RandomUtil.uuId() + ".pdf"; String fileName = fileApi.getLocalBasePath() + fileApi.getPath(FileTypeConstant.TEMPORARY) + name; employeeService.exportPdf(employeeService.getList(), fileName); if (FileUtil.fileIsFile(fileName)) { DownloadVO vo = DownloadVO.builder().name(name).url(UploaderUtil.uploaderFile(name + "#" + FileTypeConstant.TEMPORARY)).build(); return ActionResult.success(vo); } return ActionResult.success(MsgCode.ETD109.get()); } /** * 导出Excel * * @return */ @Operation(summary = "导出Excel(备用)") @GetMapping("/Excel") public void excel() { Map map = new HashMap<>(); List list = employeeService.getList(); TemplateExportParams param = new TemplateExportParams(fileApi.getPath(FileTypeConstant.TEMPLATEFILE) + "employee_import_template.xlsx", true); map.put("Employee", JSON.parse(JSON.toJSONStringWithDateFormat(list, YYYY_MM_DD))); Workbook workbook = ExcelExportUtil.exportExcel(param, map); ExcelUtil.dowloadExcel(workbook, "职员信息.xlsx"); } /** * 上传文件(excel) * * @return */ @Operation(summary = "上传文件") @PostMapping("/Uploader") public ActionResult uploader() { List list = UpUtil.getFileAll(); MultipartFile file = list.get(0); if (file.getOriginalFilename().endsWith(XLSX) || file.getOriginalFilename().endsWith(".xls")) { String fileName = RandomUtil.uuId() + "." + UpUtil.getFileType(file); //上传文件 FileInfo fileInfo = fileUploadApi.uploadFile(file, FileTypeConstant.TEMPORARY, fileName); DownloadVO vo = DownloadVO.builder().build(); vo.setName(fileInfo.getFilename()); return ActionResult.success(vo); } else { return ActionResult.fail(MsgCode.ETD110.get()); } } /** * 导入预览 * * @param fileName 文件名称 * @return */ @Operation(summary = "导入预览") @GetMapping("/ImportPreview") @Parameter(name = "fileName", description = "文件名称") public ActionResult importPreview(@RequestParam("fileName") String fileName) throws ImportException { Map map = new HashMap<>(); try { @Cleanup Response response = fileUploadApi.downFile(new UploadFileModel(FileTypeConstant.TEMPORARY, fileName)); @Cleanup InputStream inputStream = response.body().asInputStream(); // 得到数据 List personList = ExcelUtil.importExcelByInputStream(inputStream, 0, 1, EmployeeModel.class); //预览数据 map = employeeService.importPreview(personList); } catch (Exception e) { log.error(e.getMessage()); throw new ImportException(e.getMessage()); } return ActionResult.success(map); } /** * 导入数据 * * @param data 职工模型 * @return */ @Operation(summary = "导入数据") @PostMapping("/ImportData") @Parameter(name = "data", description = "职工模型") public ActionResult importData(@RequestBody EmployeeModel data) throws Exception { List dataList = new ArrayList<>(); if (data.isType()){ ActionResult result = importPreview(data.getFileName()); if (result == null){ throw new IllegalArgumentException(MsgCode.FA018.get()); } if (result.getCode() != 200){ throw new IllegalArgumentException(result.getMsg()); } if (result.getData() instanceof Map){ Map dataMap = (Map) result.getData(); dataList = JsonUtil.getJsonToList(dataMap.get("dataRow"),EmployeeModel.class); } }else { dataList = data.getList(); } //导入数据 EmployeeImportVO result = employeeService.importData(dataList); return ActionResult.success(result); } /** * 导出Excel(可选字段) * * @param paginationEmployee 分页模型 * @return */ @Operation(summary = "导出Excel(可选字段)") @GetMapping("/ExportData") public ActionResult exportExcelData(PaginationEmployee paginationEmployee) { String dataType = paginationEmployee.getDataType(); String selectKey = paginationEmployee.getSelectKey(); List entityList = new ArrayList<>(); if (StringNumber.ZERO.equals(dataType)) { entityList = employeeService.getList(paginationEmployee); } else if (StringNumber.ONE.equals(dataType)) { entityList = employeeService.getList(); } List list = JsonUtil.listToJsonField(JsonUtil.getJsonToList(JsonUtilEx.getObjectToStringDateFormat(entityList, "yyyy-MM-dd HH:mm:ss"), EmployeeExportVO.class)); List entitys = new ArrayList<>(); String[] splitData = selectKey.split(","); for (String splitDatum : splitData) { if (EN_CODE.equals(splitDatum)) { entitys.add(new ExcelExportEntity("工号", EN_CODE)); } if (FULL_NAME.equals(splitDatum)) { entitys.add(new ExcelExportEntity("姓名", FULL_NAME)); } if (GENDER.equals(splitDatum)) { entitys.add(new ExcelExportEntity("性别", GENDER)); } if (DEPARTMENT_NAME.equals(splitDatum)) { entitys.add(new ExcelExportEntity("部门", DEPARTMENT_NAME)); } if (POSITION_NAME.equals(splitDatum)) { entitys.add(new ExcelExportEntity("职务", POSITION_NAME, 25)); } if (WORKING_NATURE.equals(splitDatum)) { entitys.add(new ExcelExportEntity("用工性质", WORKING_NATURE)); } if (ID_NUMBER.equals(splitDatum)) { entitys.add(new ExcelExportEntity("身份证号", ID_NUMBER, 25)); } if (TELEPHONE.equals(splitDatum)) { entitys.add(new ExcelExportEntity("联系电话", TELEPHONE, 20)); } if (BIRTHDAY.equals(splitDatum)) { entitys.add(new ExcelExportEntity("出生年月", BIRTHDAY, 20)); } if (ATTEND_WORK_TIME.equals(splitDatum)) { entitys.add(new ExcelExportEntity("参加工作", ATTEND_WORK_TIME, 20)); } if (EDUCATION.equals(splitDatum)) { entitys.add(new ExcelExportEntity("最高学历", EDUCATION)); } if (MAJOR.equals(splitDatum)) { entitys.add(new ExcelExportEntity("所学专业", MAJOR)); } if (GRADUATION_ACADEMY.equals(splitDatum)) { entitys.add(new ExcelExportEntity("毕业院校", GRADUATION_ACADEMY)); } if (GRADUATION_TIME.equals(splitDatum)) { entitys.add(new ExcelExportEntity("毕业时间", GRADUATION_TIME, 20)); } if ("creatorTime".equals(splitDatum)) { entitys.add(new ExcelExportEntity("创建时间", "creatorTime")); } } ExportParams exportParams = new ExportParams(null, "职员信息"); exportParams.setType(ExcelType.XSSF); DownloadVO vo = DownloadVO.builder().build(); try { @Cleanup Workbook workbook = new HSSFWorkbook(); if (!entitys.isEmpty()) { workbook = ExcelExportUtil.exportExcel(exportParams, entitys, list); } String name = "职员信息" + DateUtil.dateNow(TIME) + "_" + RandomUtil.uuId() + XLSX; //上传文件 MultipartFile multipartFile = ExcelUtil.workbookToCommonsMultipartFile(workbook, name); FileInfo fileInfo = fileUploadApi.uploadFile(multipartFile, FileTypeConstant.TEMPORARY, name); vo.setName(fileInfo.getFilename()); vo.setUrl(UploaderUtil.uploaderFile(fileInfo.getFilename() + "#" + FileTypeConstant.TEMPORARY) + "&name=" + name); } catch (Exception e) { log.error(MESSAGE, e.getMessage()); } return ActionResult.success(vo); } }