diff --git a/app/service/hrbp/ehr/absence/absence_analysis.py b/app/service/hrbp/ehr/absence/absence_analysis.py index 8ad13bf..6bff31d 100644 --- a/app/service/hrbp/ehr/absence/absence_analysis.py +++ b/app/service/hrbp/ehr/absence/absence_analysis.py @@ -254,6 +254,7 @@ def _fetch_month_kuang(session, user_id: str, treecode: str, m_str: str) -> Dict def _fetch_month_leave_candidates(session, user_id: str, m_str: str) -> set: """批量发现当月有请假记录的员工工号集合(base_code 置空,拉取全集团请假)。 + 仅纳入请假天数 >= 1 天的记录(过滤 0 天/0.5 天的短时请假)。 若接口不支持空工号查询(返回空),则回退为不增量请假候选,仅靠旷工候选。 """ mb, me = _shift_range_bounds(m_str) @@ -263,9 +264,18 @@ def _fetch_month_leave_candidates(session, user_id: str, m_str: str) -> set: except Exception as e: # noqa: BLE001 logger.warning("批量拉取请假候选失败,仅按旷工候选梳理:%s", e) return set() - codes = {_clean(r.get("base_code")) for r in rows} - codes.discard("") - logger.info("当月有请假记录的员工:%d 人", len(codes)) + codes: set = set() + for r in rows: + days = r.get("leave_days", 0) + try: + days = float(days) if days else 0 + except (ValueError, TypeError): + days = 0 + if days >= 1: + code = _clean(r.get("base_code")) + if code: + codes.add(code) + logger.info("当月请假天数>=1天的员工:%d 人(过滤后)", len(codes)) return codes diff --git a/app/service/hrbp/ehr/employer_insurance/employer_insurance.py b/app/service/hrbp/ehr/employer_insurance/employer_insurance.py index 69ca965..d216bed 100644 --- a/app/service/hrbp/ehr/employer_insurance/employer_insurance.py +++ b/app/service/hrbp/ehr/employer_insurance/employer_insurance.py @@ -392,12 +392,89 @@ def output_results(final_records, change_log, reused_source_employees, biz_df, emp_ids_to_clear.add(rec["证件号"]) emp_ids_to_clear.update(reused_source_employees) + # 构建初始参保映射:在参名单中存在原投保方案(M列)的员工视为"原已参保" + # (不依赖R列方案处理值,避免老员工R列为空/其它值时漏掉"删除原方案"记录, + # 例如 A:20→50 与 B:50→20 交换时需输出 A删20+A增50+B删50+B增20 共4条) + initial_employee_map = {} + for _, row in in_param_df.iterrows(): + emp_id = row.iloc[10] + if pd.isna(emp_id): + continue + orig_scheme = row.iloc[12] + if pd.notna(orig_scheme) and str(orig_scheme).strip() != "": + initial_employee_map[emp_id] = True + + final_emp_ids = set(rec["证件号"] for rec in final_records) + + # 槽位复用网络:来源员工ID -> 接收记录(用于链条追踪) + slot_network = {} + for _rec in final_records: + _sid = _rec.get("来源员工ID") + if _sid is not None and not pd.isna(_sid) and str(_sid).strip(): + slot_network[str(_sid).strip()] = _rec + + # 链条追踪:离职/参保减少释放的名额被老员工复用后,沿"来源员工ID"网络一路追踪, + # 老员工每次"删除自己旧方案"即释放一个旧名额,若该名额被下一人复用则继续,直到: + # - 接收者是全新新员工(无旧方案可删)→ 链条自然结束,增删本就平衡; + # - 末端老员工释放的旧名额无人复用 → 该旧名额即"空闲名额"(X档)。 + # 此时把最初源员工的名额改为该空闲名额保留在参名单,并补一条"增加[X档]"记录: + # 既保证 增加>=删除,又把空闲名额放回池中供下次替换使用(避免同人两条名单)。 + special_slots = [] + for rec in final_records: + if rec.get("名额来源") not in ("离职减少", "参保减少"): + continue + source_id = rec.get("来源员工ID") + # 源员工自身也参保则跳过,避免同一个人出现两条名单 + if source_id is None or pd.isna(source_id) or source_id in final_emp_ids: + continue + source_id = str(source_id).strip() + # 沿链条追踪,找到末端"未被复用的旧方案"(空闲名额 X档) + current_owner = source_id + visited = set() + loose_end_scheme = None + while current_owner not in visited: + visited.add(current_owner) + next_rec = slot_network.get(current_owner) + if next_rec is None: + break # 链条断裂 + receiver_id = str(next_rec["证件号"]).strip() + # 接收者不是原已参保老员工(全新新员工)-> 无旧方案可释放,链条自然结束 + if receiver_id not in initial_employee_map: + break + # 接收者是老员工:释放自己的旧方案(X档);若该旧名额无人复用则即为空闲名额 + if receiver_id not in slot_network: + row_match = in_param_df[in_param_df.iloc[:, 10] == receiver_id] + if not row_match.empty: + loose_end_scheme = row_match.iloc[0].iloc[12] + break + current_owner = receiver_id # 旧名额被复用,继续追踪下一环 + if not loose_end_scheme: + continue + src_match = in_param_df[in_param_df.iloc[:, 10] == source_id] + if src_match.empty: + continue + src_row = src_match.iloc[0] + special_slots.append({ + "source_id": source_id, + "source_name": src_row.iloc[9], + "source_category": src_row.iloc[11], + "new_scheme": loose_end_scheme, + }) + special_source_ids = set(s["source_id"] for s in special_slots) + special_src_scheme = {s["source_id"]: s["new_scheme"] for s in special_slots} + if special_slots: + logger.info("特殊情况:离职/参保减少名额被老员工复用,链条末端空闲名额 %d 例(源员工改为空闲方案保留)", + len(special_slots)) + for s in special_slots: + logger.info(" 源 %s -> 改为方案 %s", s["source_id"], s["new_scheme"]) + # ---------- 2. 保留的行 ---------- handle_col = column_names[17] emp_id_col = column_names[10] keep_mask = ( ((in_param_df[handle_col] == "无需") & (~in_param_df[emp_id_col].isin(emp_ids_to_clear))) | - ((in_param_df[handle_col].isin(["离职减少", "参保减少"])) & (~in_param_df[emp_id_col].isin(reused_source_employees))) + ((in_param_df[handle_col].isin(["离职减少", "参保减少"])) & (~in_param_df[emp_id_col].isin(reused_source_employees))) | + (in_param_df[emp_id_col].isin(special_source_ids)) # 特殊情况源员工保留(改M列为老员工旧方案) ) kept_df = in_param_df[keep_mask].copy() kept_emp_ids = set(kept_df[emp_id_col].tolist()) @@ -458,17 +535,22 @@ def output_results(final_records, change_log, reused_source_employees, biz_df, for row_info in existing_rows: emp_id = row_info["emp_id"] if emp_id in kept_emp_ids: + row_data = row_info["data"] + # 特殊情况源员工:M列改为老员工释放的旧方案(X档),下次可继续做替换 + if emp_id in special_src_scheme: + row_data[12] = special_src_scheme[emp_id] new_rows.append({ "row_idx": row_info["row_idx"], - "data": row_info["data"], + "data": row_data, "emp_id": emp_id, "action": "keep" }) # 7.2 清空需要替换的行(仅清空B~N列,R/S列不动) + # 注意:特殊情况源员工已保留(改M列),此处不清空 for row_info in existing_rows: emp_id = row_info["emp_id"] - if emp_id in emp_ids_to_clear or emp_id is None: + if (emp_id in emp_ids_to_clear and emp_id not in special_source_ids) or emp_id is None: row_data = row_info["data"].copy() # 清空B~N列(索引1-13) for i in range(1, 14): @@ -646,22 +728,10 @@ def output_results(final_records, change_log, reused_source_employees, biz_df, for col in range(16, 20): # P~S列 ws_change.cell(row=row, column=col).value = None - # 构建初始参保映射 - initial_employee_map = {} - for _, row in in_param_df.iterrows(): - emp_id = row.iloc[10] - if pd.isna(emp_id): - continue - handle = str(row.iloc[17]).strip() - if handle in ["无需", "档次替换"]: - initial_employee_map[emp_id] = True - - final_emp_ids = set(rec["证件号"] for rec in final_records) - # 收集变更记录 records = [] - # 1. 删除记录 + # 1. 删除记录(特殊情况源员工一并输出"增加老员工旧方案",保证增减平衡) for emp_id in emp_ids_to_clear: if emp_id in final_emp_ids: continue @@ -672,6 +742,9 @@ def output_results(final_records, change_log, reused_source_employees, biz_df, category = row.iloc[11] scheme = row.iloc[12] records.append(("删除", name, emp_id, category, scheme)) + # 特殊情况:源员工名额被老员工复用且老员工旧名额无人复用 -> 源员工改为该旧方案 + if emp_id in special_src_scheme: + records.append(("增加", name, emp_id, category, special_src_scheme[emp_id])) else: records.append(("删除", "未知", emp_id, "未知", "未知")) diff --git a/app/service/hrbp/oa/common/config.py b/app/service/hrbp/oa/common/config.py index 523c51a..c65fc72 100644 --- a/app/service/hrbp/oa/common/config.py +++ b/app/service/hrbp/oa/common/config.py @@ -83,4 +83,6 @@ OT_PRE_SHIFT_ARRIVE_BEFORE_HOURS = 1 OT_PRE_SHIFT_ARRIVE_AFTER_MINUTES = 0 # ---------- 分页 ---------- -DEFAULT_PAGE_SIZE = 15 +# 待办接口分页参数名正确应为 pageno/rowsize/pagingtype(见 common/oa_api/todo.py); +# 每页调大至 100 作为兜底:即便个别场景分页仍异常,一次请求也能尽量拉全,避免丢单 +DEFAULT_PAGE_SIZE = 100 diff --git a/app/service/hrbp/oa/common/oa_api/todo.py b/app/service/hrbp/oa/common/oa_api/todo.py index 717af90..7004baa 100644 --- a/app/service/hrbp/oa/common/oa_api/todo.py +++ b/app/service/hrbp/oa/common/oa_api/todo.py @@ -6,8 +6,8 @@ @Version: 1.0.0 """ -import time import re +import random from typing import List, Dict, Any from app.utils.logger import get_logger @@ -48,12 +48,15 @@ def fetch_todo_page( "dataType": "todo", "fdType": FD_TYPE, # 待办类型(请假/加班) "fdCateId": "", + "q.s_raq": str(random.random()), # 防缓存随机数,每次刷新 "q.fdSubject": subject, # 按标题关键字过滤(请假/加班) + # 分页参数必须用 pageno/rowsize/pagingtype: + # 用 page.pageNo/page.pageSize 会被服务端忽略,翻页返回重复数据 + "pageno": page_no, + "rowsize": page_size, + "pagingtype": "default", "orderby": "fdCreateTime", "ordertype": "down", - "page.pageNo": page_no, - "page.pageSize": page_size, - "__seq": int(time.time() * 1000), # 防缓存 } logger.debug("请求第 %d 页待办[%s]", page_no, subject) return oa_request_json(session, "GET", TODO_LIST_URL, params, "待办列表") diff --git a/yingdao/employer_insurance/employer_insurance_schedule.py b/yingdao/employer_insurance/employer_insurance_schedule.py index 9e317a7..05fbe0a 100644 --- a/yingdao/employer_insurance/employer_insurance_schedule.py +++ b/yingdao/employer_insurance/employer_insurance_schedule.py @@ -589,12 +589,93 @@ def output_results(final_records, change_log, reused_source_employees, biz_df, emp_ids_to_clear.add(rec["证件号"]) emp_ids_to_clear.update(reused_source_employees) + # 构建初始参保映射:在参名单中存在原投保方案(M列)的员工视为"原已参保" + # (不依赖R列方案处理值,避免老员工R列为空/其它值时漏掉"删除原方案"记录, + # 例如 A:20→50 与 B:50→20 交换时需输出 A删20+A增50+B删50+B增20 共4条) + initial_employee_map = {} + for _, row in in_param_df.iterrows(): + emp_id = row.iloc[10] + if pd.isna(emp_id): + continue + orig_scheme = row.iloc[12] + if pd.notna(orig_scheme) and str(orig_scheme).strip() != "": + initial_employee_map[emp_id] = True + + final_emp_ids = set(rec["证件号"] for rec in final_records) + + # 特殊情况识别:离职/参保减少名额被老员工复用,且该老员工释放的旧名额无人复用 + # (例如 曹益娟参保减少20万→老员工胡兴复用,胡兴的X档旧名额没人用)。 + # 此时把源员工名额改为老员工旧方案(X档)保留在参名单,变更清单补一条"增加X档", + # 既保证 增加>=删除 平衡,又把空闲名额放回池中供下次替换使用(避免同人两条名单)。 + # 槽位复用网络:来源员工ID -> 接收记录(用于链条追踪) + slot_network = {} + for _rec in final_records: + _sid = _rec.get("来源员工ID") + if _sid is not None and not pd.isna(_sid) and str(_sid).strip(): + slot_network[str(_sid).strip()] = _rec + + # 链条追踪:离职/参保减少释放的名额被老员工复用后,沿"来源员工ID"网络一路追踪, + # 老员工每次"删除自己旧方案"即释放一个旧名额,若该名额被下一人复用则继续,直到: + # - 接收者是全新新员工(无旧方案可删)→ 链条自然结束,增删本就平衡; + # - 末端老员工释放的旧名额无人复用 → 该旧名额即"空闲名额"(X档)。 + # 此时把最初源员工的名额改为该空闲名额保留在参名单,并补一条"增加[X档]"记录: + # 既保证 增加>=删除,又把空闲名额放回池中供下次替换使用(避免同人两条名单)。 + special_slots = [] + for rec in final_records: + if rec.get("名额来源") not in ("离职减少", "参保减少"): + continue + source_id = rec.get("来源员工ID") + # 源员工自身也参保则跳过,避免同一个人出现两条名单 + if source_id is None or pd.isna(source_id) or source_id in final_emp_ids: + continue + source_id = str(source_id).strip() + # 沿链条追踪,找到末端"未被复用的旧方案"(空闲名额 X档) + current_owner = source_id + visited = set() + loose_end_scheme = None + while current_owner not in visited: + visited.add(current_owner) + next_rec = slot_network.get(current_owner) + if next_rec is None: + break # 链条断裂 + receiver_id = str(next_rec["证件号"]).strip() + # 接收者不是原已参保老员工(全新新员工)-> 无旧方案可释放,链条自然结束 + if receiver_id not in initial_employee_map: + break + # 接收者是老员工:释放自己的旧方案(X档);若该旧名额无人复用则即为空闲名额 + if receiver_id not in slot_network: + row_match = in_param_df[in_param_df.iloc[:, 10] == receiver_id] + if not row_match.empty: + loose_end_scheme = row_match.iloc[0].iloc[12] + break + current_owner = receiver_id # 旧名额被复用,继续追踪下一环 + if not loose_end_scheme: + continue + src_match = in_param_df[in_param_df.iloc[:, 10] == source_id] + if src_match.empty: + continue + src_row = src_match.iloc[0] + special_slots.append({ + "source_id": source_id, + "source_name": src_row.iloc[9], + "source_category": src_row.iloc[11], + "new_scheme": loose_end_scheme, + }) + special_source_ids = set(s["source_id"] for s in special_slots) + special_src_scheme = {s["source_id"]: s["new_scheme"] for s in special_slots} + if special_slots: + logger.info("特殊情况:离职/参保减少名额被老员工复用,链条末端空闲名额 %d 例(源员工改为空闲方案保留)", + len(special_slots)) + for s in special_slots: + logger.info(" 源 %s -> 改为方案 %s", s["source_id"], s["new_scheme"]) + # ---------- 2. 保留的行 ---------- handle_col = column_names[17] emp_id_col = column_names[10] keep_mask = ( ((in_param_df[handle_col] == "无需") & (~in_param_df[emp_id_col].isin(emp_ids_to_clear))) | - ((in_param_df[handle_col].isin(["离职减少", "参保减少"])) & (~in_param_df[emp_id_col].isin(reused_source_employees))) + ((in_param_df[handle_col].isin(["离职减少", "参保减少"])) & (~in_param_df[emp_id_col].isin(reused_source_employees))) | + (in_param_df[emp_id_col].isin(special_source_ids)) # 特殊情况源员工保留(改M列为老员工旧方案) ) kept_df = in_param_df[keep_mask].copy() kept_emp_ids = set(kept_df[emp_id_col].tolist()) @@ -655,17 +736,22 @@ def output_results(final_records, change_log, reused_source_employees, biz_df, for row_info in existing_rows: emp_id = row_info["emp_id"] if emp_id in kept_emp_ids: + row_data = row_info["data"] + # 特殊情况源员工:M列改为老员工释放的旧方案(X档),下次可继续做替换 + if emp_id in special_src_scheme: + row_data[12] = special_src_scheme[emp_id] new_rows.append({ "row_idx": row_info["row_idx"], - "data": row_info["data"], + "data": row_data, "emp_id": emp_id, "action": "keep" }) # 7.2 清空需要替换的行(仅清空B~N列,R/S列不动) + # 注意:特殊情况源员工已保留(改M列),此处不清空 for row_info in existing_rows: emp_id = row_info["emp_id"] - if emp_id in emp_ids_to_clear or emp_id is None: + if (emp_id in emp_ids_to_clear and emp_id not in special_source_ids) or emp_id is None: row_data = row_info["data"].copy() # 清空B~N列(索引1-13) for i in range(1, 14): @@ -856,22 +942,10 @@ def output_results(final_records, change_log, reused_source_employees, biz_df, for col in range(16, 20): # P~S列 ws_change.cell(row=row, column=col).value = None - # 构建初始参保映射 - initial_employee_map = {} - for _, row in in_param_df.iterrows(): - emp_id = row.iloc[10] - if pd.isna(emp_id): - continue - handle = str(row.iloc[17]).strip() - if handle in ["无需", "档次替换"]: - initial_employee_map[emp_id] = True - - final_emp_ids = set(rec["证件号"] for rec in final_records) - # 收集变更记录 records = [] - # 1. 删除记录 + # 1. 删除记录(特殊情况源员工一并输出"增加老员工旧方案",保证增减平衡) for emp_id in emp_ids_to_clear: if emp_id in final_emp_ids: continue @@ -882,6 +956,9 @@ def output_results(final_records, change_log, reused_source_employees, biz_df, category = row.iloc[11] scheme = row.iloc[12] records.append(("删除", name, emp_id, category, scheme)) + # 特殊情况:源员工名额被老员工复用且老员工旧名额无人复用 -> 源员工改为该旧方案 + if emp_id in special_src_scheme: + records.append(("增加", name, emp_id, category, special_src_scheme[emp_id])) else: records.append(("删除", "未知", emp_id, "未知", "未知")) diff --git a/yingdao/oa/leave_approval.py b/yingdao/oa/leave_approval.py index a5bf533..54ecef9 100644 --- a/yingdao/oa/leave_approval.py +++ b/yingdao/oa/leave_approval.py @@ -86,7 +86,9 @@ GET_CUR_USER_INFO_URL = f"{HR_BASE_URL}/RedseaPlatform/PtUsers.mc?method=getCurU SHIFT_URL = f"{HR_BASE_URL}/RedseaPlatform/redmagicapi/rf_p_kq_count_SelectBanci/getExecutePageResult.mc" # ---------- 分页 ---------- -DEFAULT_PAGE_SIZE = 15 +# 待办接口分页参数名正确应为 pageno/rowsize/pagingtype(fetch_todo_page 已修正); +# 每页调大至 100 作为兜底:即便个别场景分页仍异常,一次请求也能尽量拉全,避免丢单 +DEFAULT_PAGE_SIZE = 100 PAGE_MAX_SIZE = 500 # 排班查询单页最大条数 @@ -343,12 +345,16 @@ def fetch_todo_page(session, subject, page_no=1, page_size=DEFAULT_PAGE_SIZE): "dataType": "todo", "fdType": FD_TYPE, "fdCateId": "", + # 防缓存随机数:前端请求带 q.s_raq=<随机小数>,每次刷新 + "q.s_raq": str(random.random()), "q.fdSubject": subject, + # 分页参数必须用 pageno/rowsize/pagingtype: + # 用 page.pageNo/page.pageSize 会被服务端忽略,翻页返回重复数据 + "pageno": page_no, + "rowsize": page_size, + "pagingtype": "default", "orderby": "fdCreateTime", "ordertype": "down", - "page.pageNo": page_no, - "page.pageSize": page_size, - "__seq": int(time.time() * 1000), } url = f"{TODO_LIST_URL}?{urlencode(params)}" logger.debug("请求第 %d 页: %s", page_no, url)