"""从已冻结的任职依据取岗位,回补历史工资不改当前人员档案。""" from sqlalchemy import select from app.core.exceptions import BadRequestError from app.models import PayrollInput def role_on(person, day): if not person: return None month = str(day)[:7] for period in person.get("role_history", []): if period["start_month"] <= month <= period["end_month"]: return period["role_code"] return person.get("role_code") def input_role(values, personnel_id, day): people = values.get("facts", {}).get("personnel", {}) codes = { role_on(people[source_id], day) for source_id, target_id in values.get("maps", {}).get("personnel", {}).items() if target_id == personnel_id and source_id in people } if len(codes) > 1: raise BadRequestError("同一人员的历史岗位依据冲突,请先核实任职资料") return next(iter(codes), None) def frozen_roles(session, rows, day): """按入结台账引用的资料版本解析任职,冲突时拒绝猜测。""" inputs = {} for row in rows: snapshot = row.snapshot_json or {} if snapshot.get("engine") == "FIFO" and snapshot.get("input_id"): inputs.setdefault(snapshot["input_id"], set()).add(row.personnel_id) result = {} for record in session.scalars(select(PayrollInput).where(PayrollInput.id.in_(inputs))): for pid in inputs[record.id]: role = input_role(record.data_json, pid, day) if role is None: raise BadRequestError("月结人员的历史岗位依据缺失,请先核实任职资料") if pid in result and result[pid] != role: raise BadRequestError("同一人员的历史岗位依据冲突,请先核实任职资料") result[pid] = role return result