feat: 补卡识别(supplementDate归属被补日+仅计已审批), 抓取窗口放宽5天

This commit is contained in:
2026-11-06 17:59:59 +08:00
parent 87af63a9c0
commit a350816bfe
3 changed files with 518 additions and 22 deletions
+56 -15
View File
@@ -4,12 +4,19 @@ Rule (user-defined): for each day, take the earliest and the latest punch of tha
- If the day has fewer than 2 punches -> 缺卡 (insufficient count)
- Else if (latest - earliest) < 8 hours -> 打卡时间不足 (insufficient duration)
- Else -> 正常 (OK)
补卡 (supplementary punch) handling:
- A supplement record has ``supplementType`` set and ``signType == 4``. Its ``signDate``
is when the supplement was submitted, NOT the real punch time. The real punch time is
``supplementDate``, and it only counts when ``approveStatus == 1`` (approved).
- So each record is attributed to its *effective* day/time: normal punches use signDate;
approved supplements use supplementDate; unapproved supplements are ignored.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, date, timedelta
from datetime import datetime, date
MIN_WORK_HOURS = 8.0
_DT_FMT = "%Y-%m-%d %H:%M:%S"
@@ -18,9 +25,33 @@ STATUS_OK = "正常"
STATUS_FEW = "缺卡(不足2次)"
STATUS_SHORT = "打卡时间不足(<8h)"
APPROVE_PASSED = 1
def _parse_dt(s: str) -> datetime:
return datetime.strptime(s[:19], _DT_FMT)
def _parse_dt(s: str | None) -> datetime | None:
if not s:
return None
try:
return datetime.strptime(s[:19], _DT_FMT)
except ValueError:
return None
def effective_punch(r: dict) -> datetime | None:
"""Effective punch datetime for a record, or None if it should not count.
- 补卡 (supplementType not null): use supplementDate, only if approved; else ignore.
- 普通打卡: use signDate.
"""
if r.get("supplementType") is not None or r.get("signType") == 4:
if r.get("approveStatus") != APPROVE_PASSED:
return None
return _parse_dt(r.get("supplementDate"))
return _parse_dt(r.get("signDate"))
def is_supplement(r: dict) -> bool:
return r.get("supplementType") is not None or r.get("signType") == 4
@dataclass
@@ -31,6 +62,7 @@ class DaySummary:
last: datetime | None
hours: float
status: str
has_supplement: bool = False
records: list = field(default_factory=list)
@property
@@ -38,21 +70,28 @@ class DaySummary:
return self.status != STATUS_OK
def daily_summaries(rows: list[dict]) -> list[DaySummary]:
def daily_summaries(rows: list[dict], start: date | None = None,
end: date | None = None) -> list[DaySummary]:
"""Group records by their effective day and classify each day.
Supplements are attributed to the day they supplement (supplementDate), so callers
should fetch a wide-enough window (supplements may be submitted days later). If
``start``/``end`` are given, only days within [start, end] are returned.
"""
by_day: dict[date, list[dict]] = {}
for r in rows:
sd = r.get("signDate")
if not sd:
dt = effective_punch(r)
if dt is None:
continue
try:
dt = _parse_dt(sd)
except ValueError:
continue
r = {**r, "_dt": dt}
by_day.setdefault(dt.date(), []).append(r)
rec = {**r, "_dt": dt}
by_day.setdefault(dt.date(), []).append(rec)
out: list[DaySummary] = []
for day in sorted(by_day):
if start and day < start:
continue
if end and day > end:
continue
recs = sorted(by_day[day], key=lambda x: x["_dt"])
first = recs[0]["_dt"]
last = recs[-1]["_dt"]
@@ -63,7 +102,8 @@ def daily_summaries(rows: list[dict]) -> list[DaySummary]:
status = STATUS_SHORT
else:
status = STATUS_OK
out.append(DaySummary(day, len(recs), first, last, hours, status, recs))
out.append(DaySummary(day, len(recs), first, last, hours, status,
any(is_supplement(r) for r in recs), recs))
return out
@@ -75,5 +115,6 @@ def today_status(rows: list[dict], today: date | None = None) -> DaySummary | No
return None
def problems(rows: list[dict]) -> list[DaySummary]:
return [s for s in daily_summaries(rows) if s.is_problem]
def problems(rows: list[dict], start: date | None = None,
end: date | None = None) -> list[DaySummary]:
return [s for s in daily_summaries(rows, start, end) if s.is_problem]