fix: 修复加班校验逻辑与OA单据重复问题,新增Excel重算工具

1.  加班校验不通过时直接返回驳回结果,不再执行后续校验
2.  为OA待办查询接口添加去重逻辑,避免重复审批单据
3.  新增WPS COM调用的Excel公式重算测试工具
This commit is contained in:
2026-08-12 17:47:49 +08:00
parent 15f7958f61
commit 3e76d3d234
5 changed files with 117 additions and 7 deletions
+15 -1
View File
@@ -229,7 +229,21 @@ def get_documents(
all_docs.extend(docs)
logger.info("第 %d 页获取 %d 条,累计 %d 条", p, len(docs), len(all_docs))
return all_docs
# 去重:OA 待办接口分页可能返回重复单据(同一条在多次/多页重复出现),
# 以详情链接 detail_url(含唯一 taskId)为键去重,避免同一单重复审批、重复推送
seen = set()
unique_docs = []
for doc in all_docs:
key = doc.get("detail_url") or doc.get("title")
if key in seen:
continue
seen.add(key)
unique_docs.append(doc)
if len(unique_docs) != len(all_docs):
logger.warning("去重前 %d 条,去重后 %d 条(已过滤 %d 条重复单据)",
len(all_docs), len(unique_docs), len(all_docs) - len(unique_docs))
return unique_docs
# ---------- WfActivity 接口:通过 taskId 获取 formId ----------
@@ -230,11 +230,16 @@ def _validate_delayed(
actual_checkout = _parse_dt(punch.get("当日最晚打卡"))
reasons = []
# 1. 日期校验
# 1. 日期校验:加班开始日期与申请加班日期不一致时直接驳回,
# 无需再校验后续的时长、区间合规等内容
if start is None:
reasons.append("加班开始时间缺失")
elif start.strftime("%Y-%m-%d") != ot_date:
reasons.append(f"加班开始日期({start:%Y-%m-%d})与申请加班日期({ot_date})不一致")
info["计算加班时长(小时)"] = None
info["弹性扣减(小时)"] = 0
info["有效加班时长(小时)"] = 0
info["弹性额度D(小时)"] = 0
return "驳回", f"加班开始日期({start:%Y-%m-%d})与申请加班日期({ot_date})不一致"
# 2. 时长校验
hours = None
+57
View File
@@ -0,0 +1,57 @@
import win32com.client
from openpyxl import load_workbook
import os
def recalc_excel_by_wps_com(file_path: str):
"""
使用WPS COM重算表格,保存计算缓存到文件
仅支持 WPS专业版 / MS‑Excel
"""
abs_path = os.path.abspath(file_path)
app = None
try:
# 尝试WPS表格 COM
app = win32com.client.gencache.EnsureDispatch("Ket.Application")
app.Visible = False # 后台不显示窗口
app.DisplayAlerts = False # 关闭弹窗警告
# UpdateLinks=2 更新外部链接;你是本文件公式也带上无妨
wb = app.Workbooks.Open(abs_path, UpdateLinks=2)
# 强制完整重算全部公式
wb.Application.CalculateFullRebuild()
# 关键:保存,把计算结果缓存写入xlsx磁盘
wb.Save()
wb.Close()
except Exception as e:
print(f"COM执行异常: {e}")
raise
finally:
if app is not None:
app.Quit()
del app
if __name__ == "__main__":
file = r"C:\Users\admin\Desktop\test.xlsx"
# ==========1. openpyxl写入测试数据(模拟粘贴数值到目标表)==========
wb = load_workbook(file)
ws = wb.active
ws["A2"] = 10
ws["B2"] = 20
# C2单元格预先在文件内写好公式 =A2+B2
wb.save(file)
wb.close()
# ==========2.调用WPS COM后台重算并保存缓存==========
recalc_excel_by_wps_com(file)
# ==========3. openpyxl读取计算后的值==========
wb_final = load_workbook(file, data_only=True)
val = wb_final.active["C2"].value
print(f"C2计算结果 = {val}")
wb_final.close()
+15 -1
View File
@@ -228,7 +228,21 @@ def get_documents(session, subject, page_size=DEFAULT_PAGE_SIZE):
all_docs.extend(docs)
logger.info("第 %d 页获取 %d 条,累计 %d 条", p, len(docs), len(all_docs))
return all_docs
# 去重:OA 待办接口分页可能返回重复单据(同一条在多次/多页重复出现),
# 以详情链接 detail_url(含唯一 taskId)为键去重,避免同一单重复审批、重复推送
seen = set()
unique_docs = []
for doc in all_docs:
key = doc.get("detail_url") or doc.get("title")
if key in seen:
continue
seen.add(key)
unique_docs.append(doc)
if len(unique_docs) != len(all_docs):
logger.warning("去重前 %d 条,去重后 %d 条(已过滤 %d 条重复单据)",
len(all_docs), len(unique_docs), len(all_docs) - len(unique_docs))
return unique_docs
def get_wf_activity_by_task_id(session, task_id):
+23 -3
View File
@@ -233,7 +233,21 @@ def get_documents(session, subject, page_size=DEFAULT_PAGE_SIZE):
all_docs.extend(docs)
logger.info("第 %d 页获取 %d 条,累计 %d 条", p, len(docs), len(all_docs))
return all_docs
# 去重:OA 待办接口分页可能返回重复单据(同一条在多次/多页重复出现),
# 以详情链接 detail_url(含唯一 taskId)为键去重,避免同一单重复审批、重复推送
seen = set()
unique_docs = []
for doc in all_docs:
key = doc.get("detail_url") or doc.get("title")
if key in seen:
continue
seen.add(key)
unique_docs.append(doc)
if len(unique_docs) != len(all_docs):
logger.warning("去重前 %d 条,去重后 %d 条(已过滤 %d 条重复单据)",
len(all_docs), len(unique_docs), len(all_docs) - len(unique_docs))
return unique_docs
def get_wf_activity_by_task_id(session, task_id):
@@ -1198,11 +1212,17 @@ def _validate_delayed(info, punch, session, staff_id):
actual_checkout = _parse_dt(punch.get("当日最晚打卡"))
reasons = []
# 1. 日期校验
# 1. 日期校验:加班开始日期与申请加班日期不一致时直接驳回,
# 无需再校验后续的时长、区间合规等内容
if start is None:
reasons.append("加班开始时间缺失")
elif start.strftime("%Y-%m-%d") != ot_date:
reasons.append(f"加班开始日期({start:%Y-%m-%d})与申请加班日期({ot_date})不一致")
reason = f"加班开始日期({start:%Y-%m-%d})与申请加班日期({ot_date})不一致"
info["计算加班时长(小时)"] = None
info["弹性扣减(小时)"] = 0
info["有效加班时长(小时)"] = 0
info["弹性额度D(小时)"] = 0
return "驳回", reason
# 2. 时长校验
hours = None