feat: 补卡识别(supplementDate归属被补日+仅计已审批), 抓取窗口放宽5天
This commit is contained in:
+56
-15
@@ -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]
|
||||
|
||||
+16
-7
@@ -14,6 +14,9 @@ import csv
|
||||
from datetime import date, datetime, timedelta
|
||||
|
||||
from .analyze import daily_summaries, today_status, problems, STATUS_OK
|
||||
|
||||
# 补卡最多只能在被补当天后 5 天内提交,超过则无法补卡
|
||||
SUPPLEMENT_WINDOW_DAYS = 5
|
||||
from .config import config_from_env
|
||||
from .iepms import IepmsClient
|
||||
|
||||
@@ -39,14 +42,15 @@ def _memo_oneline(rec: dict) -> str:
|
||||
|
||||
|
||||
def print_table(summaries) -> None:
|
||||
print(f"{'日期':<12}{'次数':>4} {'最早':<9}{'最晚':<9}{'时长h':>6} {'状态':<18}站点 / 打卡内容")
|
||||
print("-" * 110)
|
||||
print(f"{'日期':<12}{'次数':>4} {'补卡':<4} {'最早':<9}{'最晚':<9}{'时长h':>6} {'状态':<18}站点 / 打卡内容")
|
||||
print("-" * 114)
|
||||
for s in summaries:
|
||||
rec = s.records[-1] if s.records else {}
|
||||
site = (rec.get("siteName") or "").strip()
|
||||
memo = _memo_oneline(rec)
|
||||
extra = (site[:16] + "… " if site else "") + memo
|
||||
print(f"{s.day.isoformat():<12}{s.count:>4} {_fmt_t(s.first):<9}{_fmt_t(s.last):<9}"
|
||||
supp = "含补" if s.has_supplement else " "
|
||||
print(f"{s.day.isoformat():<12}{s.count:>4} {supp:<4} {_fmt_t(s.first):<9}{_fmt_t(s.last):<9}"
|
||||
f"{s.hours:>6.1f} {s.status:<18}{extra[:60]}")
|
||||
|
||||
|
||||
@@ -78,10 +82,15 @@ def main(argv=None) -> int:
|
||||
|
||||
cfg = config_from_env(args.env)
|
||||
client = IepmsClient(cfg)
|
||||
rows = client.fetch_all(start, end)
|
||||
start_d = date.fromisoformat(start)
|
||||
end_d = date.fromisoformat(end) if end else None
|
||||
# 补卡只能在 5 天内补(signDate=提交日,可能晚于被补当天 5 天内),故抓取窗口放宽到 end+5 天,
|
||||
# 再按有效归属日过滤到所选区间;不必拉到今天。
|
||||
fetch_end = (end_d or date.today()) + timedelta(days=SUPPLEMENT_WINDOW_DAYS)
|
||||
rows = client.fetch_all(start, fetch_end.isoformat())
|
||||
|
||||
summaries = daily_summaries(rows)
|
||||
print(f"\n== 打卡记录 {start} ~ {end or '今'}({len(rows)} 条, {len(summaries)} 天)==")
|
||||
summaries = daily_summaries(rows, start_d, end_d)
|
||||
print(f"\n== 打卡记录 {start} ~ {end or '今'}({len(summaries)} 天)==")
|
||||
print_table(summaries)
|
||||
|
||||
# 今日提醒
|
||||
@@ -100,7 +109,7 @@ def main(argv=None) -> int:
|
||||
print(f"⚠ 今日打卡 {ts.count} 次,{_fmt_t(ts.first)}~{_fmt_t(ts.last)}({ts.hours:.1f}h),{ts.status}")
|
||||
|
||||
# 缺卡统计
|
||||
probs = problems(rows)
|
||||
probs = problems(rows, start_d, end_d)
|
||||
print(f"\n== 缺卡/不足统计({len(probs)} 天)==")
|
||||
if not probs:
|
||||
print("✓ 区间内无缺卡/时间不足。")
|
||||
|
||||
Reference in New Issue
Block a user