Excel上传接口优化

This commit is contained in:
yxz 2024-01-25 16:45:48 +08:00
parent 0f39654327
commit b8a4ca52dc
3 changed files with 206 additions and 62 deletions

View File

@ -30,127 +30,127 @@ public class EmployeeAttendanceBo {
@ExcelProperty("UserId") @ExcelProperty("UserId")
@JsonProperty("UserId") @JsonProperty("UserId")
private String userId; private String userId;
@ExcelProperty("day1") @ExcelProperty("1")
@JsonProperty("1") @JsonProperty("1")
private String day1; private String day1;
@ExcelProperty("day2") @ExcelProperty("2")
@JsonProperty("2") @JsonProperty("2")
private String day2; private String day2;
@ExcelProperty("day3") @ExcelProperty("3")
@JsonProperty("3") @JsonProperty("3")
private String day3; private String day3;
@ExcelProperty("day4") @ExcelProperty("4")
@JsonProperty("4") @JsonProperty("4")
private String day4; private String day4;
@ExcelProperty("day5") @ExcelProperty("5")
@JsonProperty("5") @JsonProperty("5")
private String day5; private String day5;
@ExcelProperty("day6") @ExcelProperty("6")
@JsonProperty("6") @JsonProperty("6")
private String day6; private String day6;
@ExcelProperty("day7") @ExcelProperty("7")
@JsonProperty("7") @JsonProperty("7")
private String day7; private String day7;
@ExcelProperty("day8") @ExcelProperty("8")
@JsonProperty("8") @JsonProperty("8")
private String day8; private String day8;
@ExcelProperty("day9") @ExcelProperty("9")
@JsonProperty("9") @JsonProperty("9")
private String day9; private String day9;
@ExcelProperty("day10") @ExcelProperty("10")
@JsonProperty("10") @JsonProperty("10")
private String day10; private String day10;
@ExcelProperty("day11") @ExcelProperty("11")
@JsonProperty("11") @JsonProperty("11")
private String day11; private String day11;
@ExcelProperty("day12") @ExcelProperty("12")
@JsonProperty("12") @JsonProperty("12")
private String day12; private String day12;
@ExcelProperty("day13") @ExcelProperty("13")
@JsonProperty("13") @JsonProperty("13")
private String day13; private String day13;
@ExcelProperty("day14") @ExcelProperty("14")
@JsonProperty("14") @JsonProperty("14")
private String day14; private String day14;
@ExcelProperty("day15") @ExcelProperty("15")
@JsonProperty("15") @JsonProperty("15")
private String day15; private String day15;
@ExcelProperty("day16") @ExcelProperty("16")
@JsonProperty("16") @JsonProperty("16")
private String day16; private String day16;
@ExcelProperty("day17") @ExcelProperty("17")
@JsonProperty("17") @JsonProperty("17")
private String day17; private String day17;
@ExcelProperty("day18") @ExcelProperty("18")
@JsonProperty("18") @JsonProperty("18")
private String day18; private String day18;
@ExcelProperty("day19") @ExcelProperty("19")
@JsonProperty("19") @JsonProperty("19")
private String day19; private String day19;
@ExcelProperty("day20") @ExcelProperty("20")
@JsonProperty("20") @JsonProperty("20")
private String day20; private String day20;
@ExcelProperty("day21") @ExcelProperty("21")
@JsonProperty("21") @JsonProperty("21")
private String day21; private String day21;
@ExcelProperty("day22") @ExcelProperty("22")
@JsonProperty("22") @JsonProperty("22")
private String day22; private String day22;
@ExcelProperty("day23") @ExcelProperty("23")
@JsonProperty("23") @JsonProperty("23")
private String day23; private String day23;
@ExcelProperty("day24") @ExcelProperty("24")
@JsonProperty("24") @JsonProperty("24")
private String day24; private String day24;
@ExcelProperty("day25") @ExcelProperty("25")
@JsonProperty("25") @JsonProperty("25")
private String day25; private String day25;
@ExcelProperty("day26") @ExcelProperty("26")
@JsonProperty("26") @JsonProperty("26")
private String day26; private String day26;
@ExcelProperty("day27") @ExcelProperty("27")
@JsonProperty("27") @JsonProperty("27")
private String day27; private String day27;
@ExcelProperty("day28") @ExcelProperty("28")
@JsonProperty("28") @JsonProperty("28")
private String day28; private String day28;
@ExcelProperty("day29") @ExcelProperty("29")
@JsonProperty("29") @JsonProperty("29")
private String day29; private String day29;
@ExcelProperty("day30") @ExcelProperty("30")
@JsonProperty("30") @JsonProperty("30")
private String day30; private String day30;
@ExcelProperty("day31") @ExcelProperty("31")
@JsonProperty("31") @JsonProperty("31")
private String day31; private String day31;

View File

@ -58,12 +58,10 @@ public class ClockInRecordServiceImpl implements ClockInRecordService {
**/ **/
@Override @Override
public String uploadRecord(MultipartFile file) throws IOException, ParseException { public String uploadRecord(MultipartFile file) throws IOException, ParseException {
List<Map<Integer, String>> dataList = new ArrayList<>();
EasyExcel.read(file.getInputStream()) EasyExcel.read(file.getInputStream())
.sheet() .sheet()
.registerReadListener(new AnalysisEventListener<Map<Integer, String>>() { .registerReadListener(new AnalysisEventListener<Map<Integer, String>>() {
List<Map<Integer, String>> dataList = new ArrayList<>();
@Override @Override
public void invoke(Map<Integer, String> data, AnalysisContext context) { public void invoke(Map<Integer, String> data, AnalysisContext context) {
@ -83,43 +81,31 @@ public class ClockInRecordServiceImpl implements ClockInRecordService {
@Override @Override
public void doAfterAllAnalysed(AnalysisContext context) { public void doAfterAllAnalysed(AnalysisContext context) {
// 处理数据完成后的操作
String jsonData = JSON.toJSONString(dataList);
// 存入Redis中
redisService.setCacheObject(ClockInConstant.CLOCKIN_EXCEL_JSON,jsonData,5, TimeUnit.MINUTES);
System.out.println(jsonData);
} }
}) })
.doRead(); .doRead();
String cacheObject = redisService.getCacheObject(ClockInConstant.CLOCKIN_EXCEL_JSON);
JSONArray jsonArray = new JSONArray(cacheObject);
Map<Integer, String> sortedJsonObject = dataList.get(1);
List<Map> mergedJsonList = new ArrayList<>(); List<Map> mergedJsonList = new ArrayList<>();
// 依据键从小到大排序
JSONObject titleObject = jsonArray.getJSONObject(1);
Map<String, String> sortedJsonObject = new LinkedHashMap<>();
titleObject.keySet().stream()
.sorted(Comparator.comparingInt(Integer::parseInt))
.forEach(key -> {
// 将排序后的键和对应的值加入新的JSONObject
System.out.println(key + ": " + titleObject.getString(key));
sortedJsonObject.put(key, titleObject.getString(key));
});
// 合并键值对 // 合并键值对
for (int i = 2; i < jsonArray.length(); i++) { for (int i = 2; i < dataList.size(); i++) {
Map<String, String> mergedJson = new LinkedHashMap<>(); Map<String, String> mergedJson = new LinkedHashMap<>();
Map<Integer, String> element = dataList.get(i);
String name = element.get(0);
ClockInStaff clockInStaff = clockInStaffService.getOne(QueryWrapper.create().eq("name", name));
List<StaffClasses> staffClasses = new ArrayList<>();
if (ObjectUtil.isNotNull(clockInStaff)) staffClasses = staffClassesService.list(QueryWrapper.create().eq("staff_id", clockInStaff.getId()));
for (String key : sortedJsonObject.keySet()) {
String newKey = titleObject.getString(key); for (Integer key : sortedJsonObject.keySet()) {
String newKey = sortedJsonObject.get(key);
// 获取当前元素 // 获取当前元素
JSONObject element = jsonArray.getJSONObject(i); String value = element.get(key);
String value = element.getString(key);
// 时间列 // 时间列
if (newKey.matches("-?\\d+(\\.\\d+)?") || newKey.equals("") || newKey.equals("")){ if (newKey.matches("-?\\d+(\\.\\d+)?") || newKey.equals("") || newKey.equals("")){
// 工时 // 工时
@ -127,12 +113,9 @@ public class ClockInRecordServiceImpl implements ClockInRecordService {
// 加班时间 // 加班时间
double overtime = 0.0; double overtime = 0.0;
String name = element.getString("0");
// 匹配员工 // 匹配员工
ClockInStaff clockInStaff = clockInStaffService.getOne(QueryWrapper.create().eq("name", name));
// 查找班次 // 查找班次
if (ObjectUtil.isNotNull( clockInStaff)) { if (ObjectUtil.isNotNull( clockInStaff)) {
List<StaffClasses> staffClasses = staffClassesService.list(QueryWrapper.create().eq("staff_id", clockInStaff.getId()));
for (StaffClasses staffClass : staffClasses) { for (StaffClasses staffClass : staffClasses) {
Classes classes = classesService.getById(staffClass.getClassId()); Classes classes = classesService.getById(staffClass.getClassId());
if (ObjectUtil.isNotNull(classes) && ObjectUtil.isNotEmpty(value)){ if (ObjectUtil.isNotNull(classes) && ObjectUtil.isNotEmpty(value)){
@ -180,7 +163,6 @@ public class ClockInRecordServiceImpl implements ClockInRecordService {
overtime = (double) timeDifferenceInMillis / (1000 * 60 * 60); overtime = (double) timeDifferenceInMillis / (1000 * 60 * 60);
} }
System.out.println("lastTime==========="+ lastTime);
} }
} }
} }
@ -210,4 +192,138 @@ public class ClockInRecordServiceImpl implements ClockInRecordService {
return jsonString; return jsonString;
} }
/*@Override
public String uploadRecord(MultipartFile file) throws IOException, ParseException {
*//*EasyExcel.read(file.getInputStream())
.sheet()
.registerReadListener(new AnalysisEventListener<EmployeeAttendanceBo>() {
@Override
public void invoke(EmployeeAttendanceBo bo, AnalysisContext context) {
dataList.add(bo);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// 处理数据完成后的操作
}
})
.doRead();*//*
ExcelListener excelListener = new ExcelListener();
EasyExcel.read(file.getInputStream(), EmployeeAttendanceBo.class, excelListener)
.headRowNumber(3) // 从第三行开始读取数据
.sheet().doReadSync();
List<EmployeeAttendanceBo> dataList = excelListener.getDataList();
for (EmployeeAttendanceBo employeeAttendanceBo : dataList) {
Class<?> clazz = employeeAttendanceBo.getClass();
Field[] fields = clazz.getDeclaredFields();
ClockInStaff clockInStaff = clockInStaffService.getOne(QueryWrapper.create().eq("name", employeeAttendanceBo.getName()));
if (ObjectUtil.isNotNull(clockInStaff)){
List<StaffClasses> staffClasses = staffClassesService.list(QueryWrapper.create().eq("staff_id", clockInStaff.getId()));
for (Field field : fields) {
if (field.getName().startsWith("day")) {
try {
field.setAccessible(true);
String dayValue = (String) field.get(employeeAttendanceBo);
// 进行后续操作例如输出或其他处理
// 工时
double manHour = 0.0;
// 加班时间
double overtime = 0.0;
// 匹配员工
// 查找班次
if (ObjectUtil.isNotNull( clockInStaff)) {
for (StaffClasses staffClass : staffClasses) {
Classes classes = classesService.getById(staffClass.getClassId());
if (ObjectUtil.isNotNull(classes) && ObjectUtil.isNotEmpty(dayValue)){
// 计算工时
String firstTime = dayValue.substring(0, 5);
// String lastTime = value.substring( value.length() - 5,value.length());
String lastTime = dayValue.substring(dayValue.length() - 5).trim();
String pattern = "HH:mm";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
Date firstTimeDate = dateFormat.parse(firstTime);
Date lastTimeDate = dateFormat.parse(lastTime);
Date overTimeDate = dateFormat.parse("21:00");
if (firstTimeDate.before(classes.getStartTime())){
// 正常上班卡
if (lastTimeDate.before(classes.getEndTime())){
// 早退
long timeDifferenceInMillis = lastTimeDate.getTime() - classes.getStartTime().getTime();
manHour = manHour + (double) timeDifferenceInMillis / (1000 * 60 * 60);
}else {
// 正常下班
long timeDifferenceInMillis = classes.getEndTime().getTime() - classes.getStartTime().getTime();
manHour = manHour + (double) timeDifferenceInMillis / (1000 * 60 * 60);
}
}else {
// 迟到
if (lastTimeDate.before(classes.getEndTime())){
// 早退
long timeDifferenceInMillis = lastTimeDate.getTime() - firstTimeDate.getTime();
manHour = manHour + (double) timeDifferenceInMillis / (1000 * 60 * 60);
}else {
// 正常下班
long timeDifferenceInMillis = classes.getEndTime().getTime() - firstTimeDate.getTime();
manHour = manHour + (double) timeDifferenceInMillis / (1000 * 60 * 60);
}
}
if (lastTimeDate.after(overTimeDate)){
// 加班
long timeDifferenceInMillis = lastTimeDate.getTime() - overTimeDate.getTime();
overtime = (double) timeDifferenceInMillis / (1000 * 60 * 60);
}
}
}
}
// 保留两位小数
DecimalFormat decimalFormat = new DecimalFormat("#.##");
// 使用format方法进行格式化
String formatManHour = decimalFormat.format(manHour + overtime);
String formatOverTime = decimalFormat.format(overtime);
dayValue = "工时:" + formatManHour;
if (overtime != 0.0){
//加班
dayValue = dayValue + " " + "加班工时:" +formatOverTime;
}
field.set(employeeAttendanceBo, dayValue);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
String jsonString = JSON.toJSONString(dataList);
redisService.setCacheObject(ClockInConstant.MAN_TIME_JSON,jsonString,5,TimeUnit.MINUTES);
return jsonString;
}*/
} }

View File

@ -0,0 +1,28 @@
package com.cpop.clockin.framework.constant.utils;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cpop.clockin.business.bo.EmployeeAttendanceBo;
import org.apache.poi.ss.formula.functions.T;
import java.util.ArrayList;
import java.util.List;
public class ExcelListener extends AnalysisEventListener<EmployeeAttendanceBo> {
private final List<EmployeeAttendanceBo> dataList = new ArrayList<>();
@Override
public void invoke(EmployeeAttendanceBo bo, AnalysisContext context) {
dataList.add(bo);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// 数据读取完毕后的处理
}
public List<EmployeeAttendanceBo> getDataList() {
return dataList;
}
}