diff --git a/src/main/java/com/zhgd/xmgl/modules/worker/controller/WorkerAttendanceController.java b/src/main/java/com/zhgd/xmgl/modules/worker/controller/WorkerAttendanceController.java index 885598786..08b036acc 100644 --- a/src/main/java/com/zhgd/xmgl/modules/worker/controller/WorkerAttendanceController.java +++ b/src/main/java/com/zhgd/xmgl/modules/worker/controller/WorkerAttendanceController.java @@ -1,5 +1,7 @@ package com.zhgd.xmgl.modules.worker.controller; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.metadata.IPage; import com.zhgd.annotation.OperLog; import com.zhgd.jeecg.common.api.vo.Result; @@ -16,11 +18,24 @@ import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.MapUtils; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.util.IOUtils; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ClassPathResource; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import springfox.documentation.annotations.ApiIgnore; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URLEncoder; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -282,8 +297,109 @@ public class WorkerAttendanceController { @ApiImplicitParam(name = "projectSn", value = "项目sn", paramType = "body", required = true, dataType = "String"), }) @PostMapping(value = "/safetyPerformanceAnalysis") - public Result safetyPerformanceAnalysis(@ApiIgnore @RequestBody Map paramMap) { + public Result safetyPerformanceAnalysis(@ApiIgnore @RequestBody Map paramMap) { return Result.success(workerAttendanceService.safetyPerformanceAnalysis(paramMap)); } + @ApiOperation(value = "模板导入下载", notes = "模板导入下载") + @GetMapping("/downloadExcelTemplate") + public void downloadExcelTemplate(HttpServletResponse response) { + try { + OutputStream out = response.getOutputStream(); + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/vnd.ms-excel;charset=gb2312"); + response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("风险清册导入模板.xlsx", "UTF-8")); + InputStream fis = new ClassPathResource("excel/人员考勤导入模板(详细).xlsx").getInputStream(); + IOUtils.copy(fis, out); + out.flush(); + out.close(); + } catch (IOException e) { + log.error("error:", e); + } + } + + @ApiOperation(value = "通过excel导入人员考勤信息", notes = "通过excel导入人员考勤信息", httpMethod = "POST") + @RequestMapping(value = "/importExcel", method = RequestMethod.POST) + public Result importData(@RequestParam(value = "file") MultipartFile file) { + try { + if (file == null) { + return Result.error("请上传文件"); + } + Workbook wb = null; + //判断类型 + if (file.getOriginalFilename().endsWith(".xls")) { + wb = new HSSFWorkbook(file.getInputStream()); + } + if (file.getOriginalFilename().endsWith(".xlsx")) { + wb = new XSSFWorkbook(file.getInputStream()); + } + int sheets = wb.getNumberOfSheets(); + List> importExcelVoList = new ArrayList<>(); + //循环表格sheet表单数量 + for (int i = 0; i < sheets; i++) { + Sheet sheet = wb.getSheetAt(i); + if (null == sheet) { + continue; + } + //舍弃第一行表头数据 + for (int j = sheet.getFirstRowNum() + 1; j <= sheet.getLastRowNum(); j++) { + //获取当前行 + Row row = sheet.getRow(j); + if (null == row) { + continue; + } + //遍历行所有的列 + int firstCellNum = row.getFirstCellNum(); + int lastCellNum = row.getLastCellNum(); + //列数 + Map map = new HashMap<>(); + for (int k = firstCellNum; k < lastCellNum; k++) { + //获取单元格的值,不管是否合并的单元格,都可以获取 + String value = ExcelUtils.getSheelValue(sheet, j, k); + //把当前列的值赋值给对应的属性字段 + if (k == 0) { + map.put("projectCode", value); + } else if (k == 1) { + map.put("idCard", value); + } else if (k == 2) { + map.put("passTime", value); + } else if (k == 3) { + map.put("direction", getDirection(value)); + } else if (k == 4) { + map.put("devCode", value); + } + } + map.put("passType", 2); + importExcelVoList.add(map); + } + } + if (CollUtil.isNotEmpty(importExcelVoList)) { + for (Map map : importExcelVoList) { + try { + workerAttendanceService.saveExternalPassRecord(map); + } catch (Exception e) { + log.error("批量导入考勤异常:", e); + } + } + } + return Result.ok(); + } catch (Exception e) { + log.error("", e); + return Result.error("excel解析数据异常......"); + } + } + + private Integer getDirection(String value) { + if (StrUtil.isBlank(value)) { + return null; + } + if (value.trim().equals("进")) { + return 1; + } else if (value.trim().equals("出")) { + return 2; + } + return null; + } + + } diff --git a/src/main/resources/excel/人员考勤导入模板(详细).xlsx b/src/main/resources/excel/人员考勤导入模板(详细).xlsx new file mode 100644 index 000000000..ef6761a46 Binary files /dev/null and b/src/main/resources/excel/人员考勤导入模板(详细).xlsx differ