refactor: 替换排班/打卡接口为考勤逐日接口,清理旧依赖

1. 重构考勤相关接口:将get_attendance_html、get_attendance_records替换为get_attendance_daily,使用新的ATTENDANCE_DAILY_URL接口替代旧接口
2. 更新请假校验逻辑:使用考勤逐日接口统计上班天数,替换原跨月排班HTML请求逻辑
3. 升级测试用例:同步更新test_03_attendance.py和test_06_leave_online.py适配新接口
4. 新增/完善文档:新增多份流程与需求文档,删除旧版冗余文档,整理docs目录结构
5. 清理废弃文件:删除原周考勤异常、门店请休假异常相关旧需求文档
6. 配置更新:更新config.py中的考勤相关URL常量,补充注释说明
7. 待办优化:更新aa.md内容,同步加班审批规则说明
This commit is contained in:
2026-08-18 12:00:53 +08:00
parent 2fafd9af18
commit c95720514b
53 changed files with 6119 additions and 1064 deletions
+17 -43
View File
@@ -20,54 +20,28 @@ TEST_STAFF_ID = "572e6cce-7a9f-11ec-b449-000c296f9469"
TEST_DATE = "2026-08-02"
def test_attendance_html(session, staff_id: str, year: int, month: int):
"""测试:排班 HTML 接口 + 解析"""
from app.service.hrbp.oa.common.oa_api import get_attendance_html
from app.service.hrbp.oa.common.parser import parse_attendance_html
def test_attendance_daily(session, staff_id: str, start_date: str, end_date: str):
"""测试:考勤逐日接口(班次名 + 上下班打卡)"""
from app.service.hrbp.oa.common.oa_api import get_attendance_daily
html = get_attendance_html(session, staff_id, year, month)
assert html, "排班 HTML 为空"
attendance = parse_attendance_html(html)
assert isinstance(attendance, dict), "排班解析结果非字典"
logger.info("[通过] 排班接口正常,当月解析到 %d 天班次", len(attendance))
for date, shift in list(attendance.items())[:5]:
logger.info(" %s: %s", date, shift)
return attendance
def test_punch_records(session, staff_id: str, start_date: str, end_date: str):
"""测试:打卡明细接口 + 按日汇总(迁移自原 test.py)"""
from app.service.hrbp.oa.common.oa_api import get_attendance_records
from app.service.hrbp.oa.common.parser import summarize_attendance_records
# 1. 拉取原始打卡明细
records = get_attendance_records(session, staff_id, start_date, end_date)
assert isinstance(records, list), "打卡明细格式异常"
logger.info("[通过] 打卡明细接口正常,返回 %d 条原始记录", len(records))
# 2. 按日汇总:每日最早上班 / 最晚下班
summary = summarize_attendance_records(records)
logger.info("[通过] 打卡汇总完成,共 %d 天", len(summary))
for row in summary:
logger.info(" %s | %s | 最早:%s | 最晚:%s",
row.get("attend_date"),
row.get("staff_name"),
row.get("first_check_in") or "无",
row.get("last_check_out") or "无")
return summary
records = get_attendance_daily(session, staff_id, start_date, end_date)
assert isinstance(records, list), "考勤逐日格式异常"
logger.info("[通过] 考勤逐日接口正常,返回 %d 天数据", len(records))
for r in records[:10]:
logger.info(" %s | %s | %s~%s | 上班:%s 下班:%s",
r.get("work_day"),
r.get("bc_name"),
r.get("bc_begin_time") or "-",
r.get("bc_end_time") or "-",
r.get("sb_dk_time") or r.get("sb_dk_time2") or "无",
r.get("xb_dk_time") or r.get("xb_dk_time2") or "无")
return records
if __name__ == "__main__":
logger.info("========== 测试③:排班 + 打卡明细 ==========")
logger.info("========== 测试③:考勤逐日接口 ==========")
browser, session = get_logged_in_session(subject="加班")
year = int(TEST_DATE[:4])
month = int(TEST_DATE[5:7])
test_attendance_html(session, TEST_STAFF_ID, year, month)
# 打卡查询范围:当天 + 次日(覆盖跨天凌晨下班)
from datetime import datetime, timedelta
end = (datetime.strptime(TEST_DATE, "%Y-%m-%d") + timedelta(days=1)).strftime("%Y-%m-%d")
test_punch_records(session, TEST_STAFF_ID, TEST_DATE, end)
test_attendance_daily(session, TEST_STAFF_ID, TEST_DATE, TEST_DATE)
logger.info("========== 测试③结束 ==========")
+59 -42
View File
@@ -15,48 +15,65 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
from tests.hrbp._helpers import logger # noqa: E402
def test_attendance_html_parse():
"""测试:排班 HTML 解析 + 工作日计算"""
from app.service.hrbp.oa.common.parser import parse_attendance_html, get_work_days_in_range
def test_attendance_daily_work_days():
"""测试:考勤逐日数据 → 实际上班天数统计(休息日/无记录不计)"""
from unittest.mock import MagicMock, patch
from app.service.hrbp.oa.leave.validator import _get_work_days_count
html = """
<a class='cale_a' data-temp="2026-08-01">休息</a>
<a class='cale_a' data-temp="2026-08-02">正常</a>
<a class='cale_a' data-temp="2026-08-03">休息</a>
"""
result = parse_attendance_html(html)
assert result == {"2026-08-01": "休息", "2026-08-02": "正常", "2026-08-03": "休息"}
work_days = get_work_days_in_range(result, "2026-08-01", "2026-08-03")
assert work_days == ["2026-08-02"], f"工作日计算错误: {work_days}"
logger.info("[通过] 排班 HTML 解析 + 工作日计算")
def test_attendance_summary():
"""测试:打卡明细按日汇总(含跨天凌晨下班)"""
from app.service.hrbp.oa.common.parser import summarize_attendance_records
records = [
{"staff_id": "S001", "staff_name": "张三", "bc_date": "2026-08-02",
"kq_time": "2026-08-02 08:50:00", "status": "1", "time_order": "1",
"DEPT_ORG_NAME": "技术部", "POST_ALIAS_NAME": "工程师"},
{"staff_id": "S001", "staff_name": "张三", "bc_date": "2026-08-02",
"kq_time": "2026-08-02 12:00:00", "status": "5", "time_order": "2",
"DEPT_ORG_NAME": "技术部", "POST_ALIAS_NAME": "工程师"},
{"staff_id": "S001", "staff_name": "张三", "bc_date": "2026-08-02",
"kq_time": "2026-08-03 00:30:00", "status": "5", "time_order": "2",
"DEPT_ORG_NAME": "技术部", "POST_ALIAS_NAME": "工程师"},
{"staff_id": "S001", "staff_name": "张三", "bc_date": "2026-08-02",
"kq_time": "2026-08-02 19:00:00", "status": "6", "time_order": "1",
"DEPT_ORG_NAME": "技术部", "POST_ALIAS_NAME": "工程师"}, # 重复打卡,应过滤
daily = [
{"work_day": "2026-08-10", "bc_name": "经营正班A"},
{"work_day": "2026-08-11", "bc_name": "经营正班A"},
{"work_day": "2026-08-12", "bc_name": "休息"},
{"work_day": "2026-08-13", "bc_name": "经营正班A"},
]
summary = summarize_attendance_records(records)
assert len(summary) == 1, f"汇总天数错误: {len(summary)}"
row = summary[0]
assert row["first_check_in"] == "2026-08-02 08:50:00"
assert row["last_check_out"] == "2026-08-03 00:30:00" # 跨天最晚
logger.info("[通过] 打卡汇总(含跨天 + 过滤重复): 最早=%s, 最晚=%s",
row["first_check_in"], row["last_check_out"])
session = MagicMock()
with patch("app.service.hrbp.oa.leave.validator.get_attendance_daily", return_value=daily):
count = _get_work_days_count(session, "S001", "2026-08-10", "2026-08-13")
assert count == 3, f"实际上班天数错误: {count}"
# 区间内无记录的日期(如休息日未生成记录)也视为休息
with patch("app.service.hrbp.oa.leave.validator.get_attendance_daily", return_value=daily):
count2 = _get_work_days_count(session, "S001", "2026-08-10", "2026-08-16")
assert count2 == 3, f"含无记录日期的实际上班天数错误: {count2}"
logger.info("[通过] 考勤逐日上班天数统计: 休息日/无记录均不计入")
def test_attendance_daily_merge():
"""测试:考勤逐日四种状态(休息/正常/未打卡/异常)各查一次后合并去重"""
from unittest.mock import MagicMock, patch
from app.service.hrbp.oa.common.oa_api.attendance import get_attendance_daily
session = MagicMock()
normal = [{"work_day": "2026-08-14", "bc_name": "经营正班A", "xb_dk_time": "18:05:58"}]
abnormal = [{"work_day": "2026-08-15", "bc_name": "维护早班", "bc_begin_time": "08:00",
"bc_end_time": "17:00", "xb_dk_time2": "19:49:49"}]
def fake_request(session, method, url, params, desc):
# 按 kq_status_total 参数返回对应状态的数据
status = params.get("kq_status_total")
if status == "1":
return {"data": normal}
if status == "11":
return {"data": abnormal}
return {"data": []}
with patch("app.service.hrbp.oa.common.oa_api.attendance.oa_request_json",
side_effect=fake_request) as m:
records = get_attendance_daily(session, "S001", "2026-08-14", "2026-08-15")
# 四种状态各请求一次
assert m.call_count == 4, f"应请求4次,实际{m.call_count}"
# 合并去重后按日期升序
assert [r["work_day"] for r in records] == ["2026-08-14", "2026-08-15"], \
f"合并顺序错误: {records}"
# 异常班记录保留班次与打卡
ab_rec = records[1]
assert ab_rec["bc_name"] == "维护早班"
assert ab_rec["bc_end_time"] == "17:00"
assert ab_rec["xb_dk_time2"] == "19:49:49"
logger.info("[通过] 考勤逐日状态合并: 休息/正常/未打卡/异常各查一次,合并去重保留班次与打卡")
def test_overtime_form_parse():
@@ -88,7 +105,7 @@ def test_overtime_form_parse():
if __name__ == "__main__":
logger.info("========== 测试④:纯解析函数(离线) ==========")
test_attendance_html_parse()
test_attendance_summary()
test_attendance_daily_work_days()
test_attendance_daily_merge()
test_overtime_form_parse()
logger.info("========== 测试④结束 ==========")
+42 -71
View File
@@ -18,23 +18,25 @@ from tests.hrbp._helpers import logger # noqa: E402
# ---------- 测试数据 ----------
SAMPLE_ATTENDANCE_HTML = """
<a class='cale_a' data-temp="2026-08-10">正常</a>
<a class='cale_a' data-temp="2026-08-11">正常</a>
<a class='cale_a' data-temp="2026-08-12">休息</a>
<a class='cale_a' data-temp="2026-08-13">正常</a>
<a class='cale_a' data-temp="2026-08-14">正常</a>
<a class='cale_a' data-temp="2026-08-15">休息</a>
<a class='cale_a' data-temp="2026-08-16">休息</a>
<a class='cale_a' data-temp="2026-08-17">正常</a>
"""
# 测试用考勤逐日数据(bc_name 为"休息"表示休息日,数据源 rf_s_kq_count_leave_yc_day_all)
SAMPLE_ATTENDANCE_DAILY = [
{"work_day": "2026-08-10", "bc_name": "经营正班A"},
{"work_day": "2026-08-11", "bc_name": "经营正班A"},
{"work_day": "2026-08-12", "bc_name": "休息"},
{"work_day": "2026-08-13", "bc_name": "经营正班A"},
{"work_day": "2026-08-14", "bc_name": "经营正班A"},
{"work_day": "2026-08-15", "bc_name": "休息"},
{"work_day": "2026-08-16", "bc_name": "休息"},
{"work_day": "2026-08-17", "bc_name": "经营正班A"},
]
# 跨月排班 HTML(9月)
SAMPLE_ATTENDANCE_HTML_SEP = """
<a class='cale_a' data-temp="2026-09-01">正常</a>
<a class='cale_a' data-temp="2026-09-02">正常</a>
<a class='cale_a' data-temp="2026-09-03">休息</a>
"""
# 跨月考勤逐日数据(8月底休息 + 9月上班,按日期范围一次返回)
SAMPLE_ATTENDANCE_DAILY_CROSS_MONTH = [
{"work_day": "2026-08-30", "bc_name": "休息"},
{"work_day": "2026-08-31", "bc_name": "休息"},
{"work_day": "2026-09-01", "bc_name": "经营正班A"},
{"work_day": "2026-09-02", "bc_name": "经营正班A"},
]
# ---------- 测试:假期类型编码映射 ----------
@@ -158,37 +160,14 @@ def test_get_date_range():
logger.info("[通过] 日期范围提取: start=%s, end=%s", start, end)
def test_get_months_in_range():
"""测试:获取日期范围内所有月份(跨月场景)"""
from app.service.hrbp.oa.leave.validator import _get_months_in_range
# 单月
months = _get_months_in_range("2026-08-10", "2026-08-20")
assert months == [(2026, 8)], f"单月范围错误: {months}"
# 跨月
months = _get_months_in_range("2026-08-10", "2026-10-05")
assert months == [(2026, 8), (2026, 9), (2026, 10)], f"跨月范围错误: {months}"
# 跨年
months = _get_months_in_range("2026-12-25", "2027-01-05")
assert months == [(2026, 12), (2027, 1)], f"跨年范围错误: {months}"
# 同一天
months = _get_months_in_range("2026-08-10", "2026-08-10")
assert months == [(2026, 8)], f"同天范围错误: {months}"
logger.info("[通过] 月份范围计算: 单月/跨月/跨年均正确")
# ---------- 测试:validate_doc 审批决策逻辑 ----------
@patch("app.service.hrbp.oa.leave.validator.get_attendance_html")
def test_validate_doc_group1_pass(mock_get_attendance_html):
@patch("app.service.hrbp.oa.leave.validator.get_attendance_daily")
def test_validate_doc_group1_pass(mock_get_attendance_daily):
"""测试:Group 1(事假)天数一致 → 通过"""
from app.service.hrbp.oa.leave.validator import validate_doc
mock_get_attendance_html.return_value = SAMPLE_ATTENDANCE_HTML
mock_get_attendance_daily.return_value = SAMPLE_ATTENDANCE_DAILY
doc = {
"title": "张三的请假申请",
@@ -212,12 +191,12 @@ def test_validate_doc_group1_pass(mock_get_attendance_html):
logger.info("[通过] Group1(事假)天数一致→通过: %s", result["note"])
@patch("app.service.hrbp.oa.leave.validator.get_attendance_html")
def test_validate_doc_group1_reject(mock_get_attendance_html):
@patch("app.service.hrbp.oa.leave.validator.get_attendance_daily")
def test_validate_doc_group1_reject(mock_get_attendance_daily):
"""测试:Group 1(事假)天数不一致 → 驳回"""
from app.service.hrbp.oa.leave.validator import validate_doc
mock_get_attendance_html.return_value = SAMPLE_ATTENDANCE_HTML
mock_get_attendance_daily.return_value = SAMPLE_ATTENDANCE_DAILY
doc = {
"title": "张三的请假申请",
@@ -242,12 +221,12 @@ def test_validate_doc_group1_reject(mock_get_attendance_html):
logger.info("[通过] Group1(事假)天数不一致→驳回: %s", result["note"])
@patch("app.service.hrbp.oa.leave.validator.get_attendance_html")
def test_validate_doc_group2_pass(mock_get_attendance_html):
@patch("app.service.hrbp.oa.leave.validator.get_attendance_daily")
def test_validate_doc_group2_pass(mock_get_attendance_daily):
"""测试:Group 2(病假)天数一致 + 有附件 → 通过"""
from app.service.hrbp.oa.leave.validator import validate_doc
mock_get_attendance_html.return_value = SAMPLE_ATTENDANCE_HTML
mock_get_attendance_daily.return_value = SAMPLE_ATTENDANCE_DAILY
doc = {
"title": "李四的病假申请",
@@ -271,12 +250,12 @@ def test_validate_doc_group2_pass(mock_get_attendance_html):
logger.info("[通过] Group2(病假)天数一致+有附件→通过: %s", result["note"])
@patch("app.service.hrbp.oa.leave.validator.get_attendance_html")
def test_validate_doc_group2_reject_no_attachment(mock_get_attendance_html):
@patch("app.service.hrbp.oa.leave.validator.get_attendance_daily")
def test_validate_doc_group2_reject_no_attachment(mock_get_attendance_daily):
"""测试:Group 2(病假)天数一致但无附件 → 驳回"""
from app.service.hrbp.oa.leave.validator import validate_doc
mock_get_attendance_html.return_value = SAMPLE_ATTENDANCE_HTML
mock_get_attendance_daily.return_value = SAMPLE_ATTENDANCE_DAILY
doc = {
"title": "李四的病假申请",
@@ -300,12 +279,12 @@ def test_validate_doc_group2_reject_no_attachment(mock_get_attendance_html):
logger.info("[通过] Group2(病假)无附件→驳回: %s", result["note"])
@patch("app.service.hrbp.oa.leave.validator.get_attendance_html")
def test_validate_doc_group2_reject_both(mock_get_attendance_html):
@patch("app.service.hrbp.oa.leave.validator.get_attendance_daily")
def test_validate_doc_group2_reject_both(mock_get_attendance_daily):
"""测试:Group 2(病假)天数不一致 + 无附件 → 驳回(双原因)"""
from app.service.hrbp.oa.leave.validator import validate_doc
mock_get_attendance_html.return_value = SAMPLE_ATTENDANCE_HTML
mock_get_attendance_daily.return_value = SAMPLE_ATTENDANCE_DAILY
doc = {
"title": "李四的病假申请",
@@ -330,12 +309,12 @@ def test_validate_doc_group2_reject_both(mock_get_attendance_html):
logger.info("[通过] Group2(病假)天数不一致+无附件→驳回: %s", result["note"])
@patch("app.service.hrbp.oa.leave.validator.get_attendance_html")
def test_validate_doc_unknown_type(mock_get_attendance_html):
@patch("app.service.hrbp.oa.leave.validator.get_attendance_daily")
def test_validate_doc_unknown_type(mock_get_attendance_daily):
"""测试:未识别的假期类型 → 驳回"""
from app.service.hrbp.oa.leave.validator import validate_doc
mock_get_attendance_html.return_value = SAMPLE_ATTENDANCE_HTML
mock_get_attendance_daily.return_value = SAMPLE_ATTENDANCE_DAILY
doc = {
"title": "未知假期申请",
@@ -358,8 +337,8 @@ def test_validate_doc_unknown_type(mock_get_attendance_html):
logger.info("[通过] 未识别假期类型→驳回: %s", result["note"])
@patch("app.service.hrbp.oa.leave.validator.get_attendance_html")
def test_validate_doc_missing_fields(mock_get_attendance_html):
@patch("app.service.hrbp.oa.leave.validator.get_attendance_daily")
def test_validate_doc_missing_fields(mock_get_attendance_daily):
"""测试:缺失必要字段 → 驳回"""
from app.service.hrbp.oa.leave.validator import validate_doc
@@ -387,20 +366,13 @@ def test_validate_doc_missing_fields(mock_get_attendance_html):
logger.info("[通过] 缺失必要字段→驳回: 3种场景均正确")
@patch("app.service.hrbp.oa.leave.validator.get_attendance_html")
def test_validate_doc_cross_month(mock_get_attendance_html):
@patch("app.service.hrbp.oa.leave.validator.get_attendance_daily")
def test_validate_doc_cross_month(mock_get_attendance_daily):
"""测试:跨月请假排班获取"""
from app.service.hrbp.oa.leave.validator import validate_doc
# 模拟跨月请求:8月返回部分数据,9月返回部分数据
def side_effect(session, staff_id, year, month):
if month == 8:
return SAMPLE_ATTENDANCE_HTML
elif month == 9:
return SAMPLE_ATTENDANCE_HTML_SEP
return ""
mock_get_attendance_html.side_effect = side_effect
# 跨月按日期范围一次请求,返回覆盖整个区间的逐日数据
mock_get_attendance_daily.return_value = SAMPLE_ATTENDANCE_DAILY_CROSS_MONTH
doc = {
"title": "跨月请假",
@@ -488,7 +460,6 @@ if __name__ == "__main__":
test_parse_leave_form()
test_parse_leave_form_fallback_fields()
test_get_date_range()
test_get_months_in_range()
test_validate_doc_group1_pass()
test_validate_doc_group1_reject()
test_validate_doc_group2_pass()
+13 -18
View File
@@ -61,23 +61,18 @@ def test_leave_todo_list(session):
return docs
def test_leave_attendance_html(session, staff_id: str):
"""测试:排班 HTML 接口 + 解析(请假场景)"""
from app.service.hrbp.oa.common.oa_api import get_attendance_html
from app.service.hrbp.oa.common.parser import parse_attendance_html, get_work_days_in_range
def test_leave_attendance_daily(session, staff_id: str):
"""测试:考勤逐日接口(请假场景,班次名 + 休息日识别)"""
from app.service.hrbp.oa.common.oa_api import get_attendance_daily
year = int(TEST_DATE[:4])
month = int(TEST_DATE[5:7])
html = get_attendance_html(session, staff_id, year, month)
assert html, "排班 HTML 为空"
attendance = parse_attendance_html(html)
assert isinstance(attendance, dict), "排班解析结果非字典"
logger.info("[通过] 请假排班接口正常,当月解析到 %d 天班次", len(attendance))
# 验证 get_work_days_in_range 可用
work_days = get_work_days_in_range(attendance, TEST_DATE, TEST_DATE)
logger.info(" %s 排班: %s", TEST_DATE, attendance.get(TEST_DATE, "无排班"))
return attendance
start_date = TEST_DATE
end_date = TEST_DATE
records = get_attendance_daily(session, staff_id, start_date, end_date)
assert isinstance(records, list), "考勤逐日格式异常"
logger.info("[通过] 请假考勤逐日接口正常,返回 %d 天数据", len(records))
for r in records[:5]:
logger.info(" %s | %s", r.get("work_day"), r.get("bc_name"))
return records
def test_leave_full_flow(session, browser):
@@ -149,8 +144,8 @@ if __name__ == "__main__":
# 2. 请假待办列表
docs = test_leave_todo_list(session)
# 3. 请假排班接口
test_leave_attendance_html(session, TEST_STAFF_ID)
# 3. 请假考勤逐日接口
test_leave_attendance_daily(session, TEST_STAFF_ID)
# 4. 请假全流程(如有待办)
test_leave_full_flow(session, browser)