员工考勤对接前端,后端未实装
This commit is contained in:
@ -0,0 +1,194 @@
|
||||
package com.sipai.controller.administration;
|
||||
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/administration/attendance")
|
||||
public class AttendanceController {
|
||||
private static final DateTimeFormatter DAY_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
|
||||
@RequestMapping("/showList.do")
|
||||
public String showList(HttpServletRequest request, Model model) {
|
||||
return "/administration/attendanceRecordList";
|
||||
}
|
||||
|
||||
@RequestMapping("/getList.do")
|
||||
public ModelAndView getList(HttpServletRequest request, Model model,
|
||||
@RequestParam(value = "page") Integer page,
|
||||
@RequestParam(value = "rows") Integer rows,
|
||||
@RequestParam(value = "sort", required = false) String sort,
|
||||
@RequestParam(value = "order", required = false) String order) {
|
||||
String name = safe(request.getParameter("search_name"));
|
||||
String dept = safe(request.getParameter("search_dept"));
|
||||
String status = safe(request.getParameter("search_status"));
|
||||
String start = safe(request.getParameter("search_start"));
|
||||
String end = safe(request.getParameter("search_end"));
|
||||
|
||||
LocalDate startDate = parseDay(start);
|
||||
LocalDate endDate = parseDay(end);
|
||||
List<Map<String, Object>> allRows = buildMockRows();
|
||||
List<Map<String, Object>> filtered = new ArrayList<Map<String, Object>>();
|
||||
|
||||
for (Map<String, Object> item : allRows) {
|
||||
String employeeNo = str(item.get("employeeNo"));
|
||||
String employeeName = str(item.get("employeeName"));
|
||||
String deptName = str(item.get("deptName"));
|
||||
String st = str(item.get("status"));
|
||||
LocalDate day = parseDay(str(item.get("attendanceDate")));
|
||||
|
||||
if (!name.isEmpty()) {
|
||||
String q = name.toLowerCase();
|
||||
if (!employeeNo.toLowerCase().contains(q) && !employeeName.toLowerCase().contains(q)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (!dept.isEmpty() && !dept.equals(deptName)) {
|
||||
continue;
|
||||
}
|
||||
if (!status.isEmpty() && !status.equals(st)) {
|
||||
continue;
|
||||
}
|
||||
if (startDate != null && day != null && day.isBefore(startDate)) {
|
||||
continue;
|
||||
}
|
||||
if (endDate != null && day != null && day.isAfter(endDate)) {
|
||||
continue;
|
||||
}
|
||||
filtered.add(item);
|
||||
}
|
||||
|
||||
sortRows(filtered, sort, order);
|
||||
int total = filtered.size();
|
||||
int from = Math.max(0, (page - 1) * rows);
|
||||
int to = Math.min(total, from + rows);
|
||||
List<Map<String, Object>> pageRows = from >= to ? Collections.<Map<String, Object>>emptyList() : filtered.subList(from, to);
|
||||
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("total", total);
|
||||
result.put("rows", JSONArray.fromObject(pageRows));
|
||||
model.addAttribute("result", result.toString());
|
||||
return new ModelAndView("result");
|
||||
}
|
||||
|
||||
private static void sortRows(List<Map<String, Object>> rows, String sort, String order) {
|
||||
final boolean asc = "asc".equalsIgnoreCase(order);
|
||||
final String sortField = (sort == null || sort.trim().isEmpty() || "id".equals(sort)) ? "attendanceDate" : sort;
|
||||
Collections.sort(rows, new Comparator<Map<String, Object>>() {
|
||||
@Override
|
||||
public int compare(Map<String, Object> a, Map<String, Object> b) {
|
||||
int cmp;
|
||||
if ("employeeNo".equals(sortField)) {
|
||||
cmp = str(a.get("employeeNo")).compareTo(str(b.get("employeeNo")));
|
||||
} else if ("employeeName".equals(sortField)) {
|
||||
cmp = str(a.get("employeeName")).compareTo(str(b.get("employeeName")));
|
||||
} else if ("deptName".equals(sortField)) {
|
||||
cmp = str(a.get("deptName")).compareTo(str(b.get("deptName")));
|
||||
} else if ("status".equals(sortField)) {
|
||||
cmp = str(a.get("status")).compareTo(str(b.get("status")));
|
||||
} else if ("checkInTime".equals(sortField)) {
|
||||
cmp = str(a.get("checkInTime")).compareTo(str(b.get("checkInTime")));
|
||||
} else {
|
||||
cmp = str(a.get("attendanceDate")).compareTo(str(b.get("attendanceDate")));
|
||||
}
|
||||
if (!asc) {
|
||||
cmp = -cmp;
|
||||
}
|
||||
if (cmp == 0) {
|
||||
return str(a.get("employeeNo")).compareTo(str(b.get("employeeNo")));
|
||||
}
|
||||
return cmp;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static List<Map<String, Object>> buildMockRows() {
|
||||
String[][] employees = {
|
||||
{"E0001", "张三", "生产部"},
|
||||
{"E0002", "李四", "设备部"},
|
||||
{"E0003", "王五", "品控部"},
|
||||
{"E0004", "赵六", "仓储部"},
|
||||
{"E0005", "钱七", "行政部"}
|
||||
};
|
||||
|
||||
List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
|
||||
LocalDate today = LocalDate.now();
|
||||
int idSeq = 1;
|
||||
|
||||
for (int d = 0; d < 45; d++) {
|
||||
LocalDate day = today.minusDays(d);
|
||||
String dayStr = DAY_FMT.format(day);
|
||||
for (int i = 0; i < employees.length; i++) {
|
||||
String[] emp = employees[i];
|
||||
int flag = d + i;
|
||||
|
||||
String status = "正常";
|
||||
String checkIn = "08:55";
|
||||
String checkOut = "18:05";
|
||||
String workHours = "8.5";
|
||||
|
||||
if (flag % 19 == 0) {
|
||||
status = "缺卡";
|
||||
checkOut = "--";
|
||||
workHours = "4.2";
|
||||
} else if (flag % 11 == 0) {
|
||||
status = "迟到";
|
||||
checkIn = "09:" + (10 + (flag % 20));
|
||||
workHours = "7.8";
|
||||
} else if (flag % 13 == 0) {
|
||||
status = "早退";
|
||||
checkOut = "17:" + (20 + (flag % 30));
|
||||
workHours = "7.1";
|
||||
}
|
||||
|
||||
Map<String, Object> row = new LinkedHashMap<String, Object>();
|
||||
row.put("id", "mock-" + idSeq++);
|
||||
row.put("employeeNo", emp[0]);
|
||||
row.put("employeeName", emp[1]);
|
||||
row.put("deptName", emp[2]);
|
||||
row.put("attendanceDate", dayStr);
|
||||
row.put("checkInTime", checkIn);
|
||||
row.put("checkOutTime", checkOut);
|
||||
row.put("workHours", workHours);
|
||||
row.put("status", status);
|
||||
row.put("source", "第三方接口(MOCK)");
|
||||
rows.add(row);
|
||||
}
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
private static String safe(String s) {
|
||||
return s == null ? "" : s.trim();
|
||||
}
|
||||
|
||||
private static String str(Object o) {
|
||||
return o == null ? "" : String.valueOf(o);
|
||||
}
|
||||
|
||||
private static LocalDate parseDay(String day) {
|
||||
if (day == null || day.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return LocalDate.parse(day.trim(), DAY_FMT);
|
||||
} catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user