Commit 2d0f5649 by 谢春璐

fix

parent b8745147
...@@ -74,7 +74,7 @@ public class ExcelRead { ...@@ -74,7 +74,7 @@ public class ExcelRead {
} }
public ExcelData parse(InputStream in, String fileName, boolean bool) { public ExcelData parse(InputStream in, String fileName, boolean bool, Integer titleIndex) {
Workbook wb = null; Workbook wb = null;
try { try {
String postfix = fileName.substring(fileName.lastIndexOf("."), fileName.length()); String postfix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
...@@ -91,7 +91,7 @@ public class ExcelRead { ...@@ -91,7 +91,7 @@ public class ExcelRead {
Sheet sheet = wb.getSheetAt(0); Sheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum();// 得到总行数 int rowNum = sheet.getLastRowNum();// 得到总行数
Row row = sheet.getRow(0); Row row = sheet.getRow(titleIndex);
int colNum = row.getPhysicalNumberOfCells(); int colNum = row.getPhysicalNumberOfCells();
String titles[] = readExcelTitle(row); String titles[] = readExcelTitle(row);
...@@ -100,14 +100,14 @@ public class ExcelRead { ...@@ -100,14 +100,14 @@ public class ExcelRead {
// 正文内容应该从第二行开始,第一行为表头的标题 // 正文内容应该从第二行开始,第一行为表头的标题
//bool是否从标题行开始 //bool是否从标题行开始
if (bool) { if (bool) {
for (int i = 0; i <= rowNum; i++) { for (int i = 0 + titleIndex; i <= rowNum; i++) {
int j = 0; int j = 0;
row = sheet.getRow(i); row = sheet.getRow(i);
content = new String[colNum]; content = new String[colNum];
do { do {
if (null != row.getCell(j)) { if (null != row.getCell(j)) {
if (null != getCellFormatValue(row.getCell(j))) { if (null != getCellFormatValue(row.getCell(j))) {
content[j] = getCellFormatValue(row.getCell(j)).trim(); content[j] = getCellFormatValue(row.getCell(j));
} }
} }
j++; j++;
...@@ -115,12 +115,12 @@ public class ExcelRead { ...@@ -115,12 +115,12 @@ public class ExcelRead {
list.add(content); list.add(content);
} }
} else { } else {
for (int i = 1; i <= rowNum; i++) { for (int i = 1 + titleIndex; i <= rowNum; i++) {
int j = 0; int j = 0;
row = sheet.getRow(i); row = sheet.getRow(i);
content = new String[colNum]; content = new String[colNum];
do { do {
content[j] = getCellFormatValue(row.getCell(j)).trim(); content[j] = getCellFormatValue(row.getCell(j));
j++; j++;
} while (j < colNum); } while (j < colNum);
list.add(content); list.add(content);
......
...@@ -2,9 +2,14 @@ package com.zanchina.check.controller; ...@@ -2,9 +2,14 @@ package com.zanchina.check.controller;
import com.zanchina.check.common.ExcelData; import com.zanchina.check.common.ExcelData;
import com.zanchina.check.common.ExcelRead; import com.zanchina.check.common.ExcelRead;
import com.zanchina.check.domain.Staff;
import com.zanchina.check.service.FileService; import com.zanchina.check.service.FileService;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
...@@ -13,6 +18,7 @@ import org.springframework.web.bind.annotation.PostMapping; ...@@ -13,6 +18,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
/** /**
* Created by xiechunlu on 2018-06-15 下午2:40 * Created by xiechunlu on 2018-06-15 下午2:40
...@@ -31,20 +37,49 @@ public class FileController { ...@@ -31,20 +37,49 @@ public class FileController {
@PostMapping("upload") @PostMapping("upload")
@ResponseBody @ResponseBody
public ResponseEntity<byte[]> uploadAndExport( public ResponseEntity<byte[]> uploadAndExport(
@RequestParam MultipartFile file) { HttpServletRequest request) {
try {
if (Objects.isNull(file)) {
return null;
}
InputStream inputStream = file.getInputStream(); try {
ExcelRead inst = ExcelRead.getInst(); ExcelRead inst = ExcelRead.getInst();
ExcelData data = inst.parse(inputStream, file.getOriginalFilename(), false);
return fileService.uploadAndExport(data); List<MultipartFile> files = ((MultipartHttpServletRequest) request)
.getFiles("file");
List<Staff> staffList = new ArrayList<>();
files.forEach(file -> {
try {
InputStream inputStream = file.getInputStream();
String fileName = file.getOriginalFilename();
ExcelData data = null;
List<Staff> staffs = null;
if (fileName.contains("上下班打卡_日报")) {
data = inst.parse(inputStream, fileName, false, 2);
staffs = fileService.wechatWorkInStatistics(data);
} else if (fileName.contains("外出打卡_日报")) {
data = inst.parse(inputStream, fileName, false, 2);
staffs = fileService.wechatWorkOutStatistics(data);
} else if (fileName.contains("考勤导出")) {
data = inst.parse(inputStream, fileName, false, 0);
staffs = fileService.workRecord(data);
}
staffList.addAll(staffs);
} catch (IOException e) {
e.printStackTrace();
}
});
List<Staff> staffList1 = fileService.staffListCollect(staffList);
return fileService.checkExport(staffList1);
} catch (Exception e) { } catch (Exception e) {
return null; e.printStackTrace();
} }
return null;
} }
} }
package com.zanchina.check.service; package com.zanchina.check.service;
import com.zanchina.check.common.ExcelData; import com.zanchina.check.common.ExcelData;
import com.zanchina.check.domain.Staff;
import java.text.ParseException; import java.text.ParseException;
import java.util.List;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
/** /**
...@@ -10,5 +12,13 @@ import org.springframework.http.ResponseEntity; ...@@ -10,5 +12,13 @@ import org.springframework.http.ResponseEntity;
public interface FileService { public interface FileService {
ResponseEntity<byte[]> uploadAndExport(ExcelData data) throws Exception; ResponseEntity<byte[]> checkExport(List<Staff> staffList) throws Exception;
List<Staff> wechatWorkOutStatistics(ExcelData data);
List<Staff> wechatWorkInStatistics(ExcelData data);
List<Staff> workRecord(ExcelData data);
List<Staff> staffListCollect(List<Staff> staffList);
} }
...@@ -9,6 +9,7 @@ import com.zanchina.check.service.FileService; ...@@ -9,6 +9,7 @@ import com.zanchina.check.service.FileService;
import java.io.File; import java.io.File;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
...@@ -16,6 +17,7 @@ import java.util.Map; ...@@ -16,6 +17,7 @@ import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -29,9 +31,86 @@ import org.springframework.stereotype.Service; ...@@ -29,9 +31,86 @@ import org.springframework.stereotype.Service;
@Slf4j @Slf4j
public class FileServiceImpl implements FileService { public class FileServiceImpl implements FileService {
/**
* 企业微信外出打卡统计
*/
@Override @Override
public ResponseEntity<byte[]> uploadAndExport(ExcelData data) throws Exception { public List<Staff> wechatWorkOutStatistics(ExcelData data) {
//1. 根据员工姓名分组
Map<String, List<String[]>> map = data.getDatas().stream().collect(Collectors.groupingBy(d -> d[1]));
//2. 构造数据对象
List<Staff> staffList = new ArrayList<>();
map.entrySet().forEach(entry -> {
Staff staff = new Staff();
staff.setName(entry.getKey());
entry.getValue().forEach(col -> {
WorkCheck workCheck = new WorkCheck();
workCheck.setDate(col[0]);
workCheck.setOnTime(col[0].concat(" ").concat(col[4]));
workCheck.setOffTime(col[0].concat(" ").concat(col[5]));
workCheck.setState(caculateWorkState(workCheck));
staff.getWorkCheckList().add(workCheck);
});
staffList.add(staff);
});
return staffList;
}
/**
* 企业微信上下班打卡统计
*/
@Override
public List<Staff> wechatWorkInStatistics(ExcelData data) {
//1. 根据员工姓名分组
Map<String, List<String[]>> map = data.getDatas().stream()
.filter(d -> !d[11].contains("--") || !d[14].contains("--")).collect(Collectors.groupingBy(d -> d[1]));
//2. 构造数据对象
List<Staff> staffList = new ArrayList<>();
map.entrySet().forEach(entry -> {
Staff staff = new Staff();
staff.setName(entry.getKey());
entry.getValue().forEach(col -> {
WorkCheck workCheck = new WorkCheck();
workCheck.setDate(col[0]);
if (col[11].contains("--")) {
workCheck.setOnTime(col[0].concat(" ").concat(col[14]));
} else {
workCheck.setOnTime(col[0].concat(" ").concat(col[11]));
}
if (col[14].contains("--")) {
workCheck.setOffTime(col[0].concat(" ").concat(col[11]));
} else {
workCheck.setOffTime(col[0].concat(" ").concat(col[14]));
}
workCheck.setState(caculateWorkState(workCheck));
staff.getWorkCheckList().add(workCheck);
});
staffList.add(staff);
});
return staffList;
}
/**
* 打卡机打卡记录统计
*/
@Override
public List<Staff> workRecord(ExcelData data) {
//1. 拿到数据后根据id和日期分组 //1. 拿到数据后根据id和日期分组
Map<String, Map<String, List<String[]>>> map = data.getDatas().stream() Map<String, Map<String, List<String[]>>> map = data.getDatas().stream()
.collect(Collectors.groupingBy(d -> d[2], Collectors.groupingBy( .collect(Collectors.groupingBy(d -> d[2], Collectors.groupingBy(
...@@ -64,45 +143,16 @@ public class FileServiceImpl implements FileService { ...@@ -64,45 +143,16 @@ public class FileServiceImpl implements FileService {
.collect(Collectors.toList()); .collect(Collectors.toList());
WorkCheck workCheck = new WorkCheck(); WorkCheck workCheck = new WorkCheck();
workCheck.setDate(DateUtils.formatDate(dateList.get(0), DateUtils.yyyyMMdd1)); workCheck.setDate(DateUtils.formatDate(dateList.get(0), DateUtils.yyyyMMdd));
// 上班时间算一天当中最小的时间 // 上班时间算一天当中最小的时间
Date minDate = dateList.stream().min(Comparator.comparing(d -> d.getTime())).get(); Date minDate = dateList.stream().min(Comparator.comparing(d -> d.getTime())).get();
workCheck.setOnTime(DateUtils.formatDate(minDate, DateUtils.yyyyMMddHHmm1)); workCheck.setOnTime(DateUtils.formatDate(minDate, DateUtils.yyyyMMddHHmm));
//下班时间算一天当中最大的时间 //下班时间算一天当中最大的时间
Date maxDate = dateList.stream().max(Comparator.comparing(d -> d.getTime())).get(); Date maxDate = dateList.stream().max(Comparator.comparing(d -> d.getTime())).get();
workCheck.setOffTime(DateUtils.formatDate(maxDate, DateUtils.yyyyMMddHHmm1)); workCheck.setOffTime(DateUtils.formatDate(maxDate, DateUtils.yyyyMMddHHmm));
workCheck.setState(caculateWorkState(workCheck));
//计算出勤状态(迟到、早退、迟到和早退、正常)
boolean isLate = false;
boolean isEarly = false;
double hours = workCheck.getDuration();
Date onTime = DateUtils.getDateTime(dateList.get(0), 9, 30);
Date offTime = DateUtils.getDateTime(dateList.get(0), 18, 00);
isLate = DateUtils.parseDate(workCheck.getOnTime()).after(onTime);
isEarly = DateUtils.parseDate(workCheck.getOffTime()).before(offTime);
//早上迟到
if (isLate) {
if (9 <= hours) {
//工作时长大于九个小时
workCheck.setState("迟到");
} else {
workCheck.setState("迟到和早退");
}
} else {
//早上没有迟到
if (isEarly || (!isEarly && 9.0 > hours)) {
//早退了
workCheck.setState("早退");
} else {
workCheck.setState("正常");
}
}
staff.getWorkCheckList().add(workCheck); staff.getWorkCheckList().add(workCheck);
...@@ -113,10 +163,97 @@ public class FileServiceImpl implements FileService { ...@@ -113,10 +163,97 @@ public class FileServiceImpl implements FileService {
}); });
staffList.sort(Comparator.comparing(Staff::getId)); staffList.sort(Comparator.comparing(Staff::getId));
return checkExport(staffList); return staffList;
} }
private ResponseEntity<byte[]> checkExport(List<Staff> staffList) throws Exception { /**
* 计算出勤状态(迟到、早退、迟到和早退、正常)
*/
private String caculateWorkState(WorkCheck workCheck) {
boolean isLate = false;
boolean isEarly = false;
String state = "正常";
double hours = workCheck.getDuration();
Date onTime = DateUtils.getDateTime(DateUtils.parseDate(workCheck.getDate()), 9, 30);
Date offTime = DateUtils.getDateTime(DateUtils.parseDate(workCheck.getDate()), 18, 00);
isLate = DateUtils.parseDate(workCheck.getOnTime()).after(onTime);
isEarly = DateUtils.parseDate(workCheck.getOffTime()).before(offTime);
//早上迟到
if (isLate) {
if (9 <= hours) {
//工作时长大于九个小时
state = "迟到";
} else {
state = "迟到和早退";
}
} else {
//早上没有迟到
if (isEarly || (!isEarly && 9.0 > hours)) {
//早退了
state = "早退";
} else {
state = "正常";
}
}
return state;
}
/**
* 汇总多张表
*/
@Override
public List<Staff> staffListCollect(List<Staff> staffList) {
//1. 按照姓名分组
Map<String, List<Staff>> collect = staffList.stream().collect(Collectors.groupingBy(Staff::getName));
//2. 合并打卡数据
List<Staff> newStaffList = new ArrayList<>();
collect.entrySet().forEach(staffEntry -> {
Staff staff = new Staff();
staff.setName(staffEntry.getKey());
List<Staff> value = staffEntry.getValue();
List<WorkCheck> workChecks = new ArrayList<>();
value.forEach(s -> {
workChecks.addAll(s.getWorkCheckList());
}
);
//3. 通过日期分组,取各个时间最早和最晚的时间
List<WorkCheck> newWorkCheckList = new ArrayList<>();
workChecks.stream().collect(Collectors.groupingBy(WorkCheck::getDate)).entrySet().forEach(entry -> {
List<WorkCheck> singleDateWorkCheck = entry.getValue();
WorkCheck w = new WorkCheck();
String onTime = singleDateWorkCheck.stream().min(Comparator
.comparing(workCheck -> DateUtils.parseDate(workCheck.getOnTime()))).get().getOnTime();
w.setOnTime(onTime);
String offTime = singleDateWorkCheck.stream().max(Comparator
.comparing(workCheck -> DateUtils.parseDate(workCheck.getOffTime()))).get().getOffTime();
w.setOffTime(offTime);
w.setDate(entry.getKey());
w.setState(caculateWorkState(w));
newWorkCheckList.add(w);
});
staff.setWorkCheckList(newWorkCheckList);
newStaffList.add(staff);
});
return newStaffList;
}
@Override
public ResponseEntity<byte[]> checkExport(List<Staff> staffList) throws Exception {
ExcelData data = new ExcelData(); ExcelData data = new ExcelData();
......
...@@ -13,7 +13,9 @@ ...@@ -13,7 +13,9 @@
<div class="container" style="padding-top: 20%;"> <div class="container" style="padding-top: 20%;">
<div class="row align-items-center"> <div class="row align-items-center">
<div class="col"> <div class="col">
<input type="file" name="file"> <p>文件1:<input type="file" name="file"/></p>
<p>文件2:<input type="file" name="file"/></p>
<p>文件3:<input type="file" name="file"/></p>
</div> </div>
<div class="col"> <div class="col">
<button type="submit" class="btn btn-outline-primary btn-lg btn-block">生成考勤记录</button> <button type="submit" class="btn btn-outline-primary btn-lg btn-block">生成考勤记录</button>
......
...@@ -21,7 +21,9 @@ public class CheckApplicationTests { ...@@ -21,7 +21,9 @@ public class CheckApplicationTests {
List<String> allDatesOfTwoDate = DateUtils.getAllDatesOfTwoDate("2018-06-30", "2018-07-15"); List<String> allDatesOfTwoDate = DateUtils.getAllDatesOfTwoDate("2018-06-30", "2018-07-15");
System.out.print(allDatesOfTwoDate); Date date = DateUtils.parseDate("2018-07-31".concat(" ").concat("08:55"), DateUtils.yyyyMMddHHmm);
System.out.print(date);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment