153 lines
5.2 KiB
Python
153 lines
5.2 KiB
Python
"""
|
|
backend/app/services/equipment_acceptance/e8_group.py
|
|
|
|
用途:把 E8 验收单联表行收成与 E10 相同的快照草稿。
|
|
依赖:e8_status、snapshot_rules、source_group。
|
|
逻辑:同步服务在写库前调用。流程状态先翻译。人员 ID 仍是 E8 的 HrmResource.id。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Any
|
|
|
|
from app.services.equipment_acceptance.e8_status import e8_flow_status
|
|
from app.services.equipment_acceptance.snapshot_rules import (
|
|
as_datetime,
|
|
as_int,
|
|
can_accept_from_oa,
|
|
is_returned_from_oa,
|
|
)
|
|
from app.services.equipment_acceptance.source_group import (
|
|
AcceptanceDraft,
|
|
NodeDraft,
|
|
OperatorDraft,
|
|
)
|
|
|
|
_TERMINAL_FLOW = {3, 7}
|
|
|
|
|
|
def group_e8_rows(rows: list[dict[str, Any]]) -> tuple[list[AcceptanceDraft], int]:
|
|
"""
|
|
按 requestid 合并 E8 联表行。
|
|
|
|
核心逻辑:没有 requestid 的行跳过。同一请求的未办人收成一张单。
|
|
计时起点读 syddjssj。可以验收读 sfkys。来源标记为 e8。
|
|
Args:
|
|
rows: 查询别名组成的字典行。
|
|
Returns:
|
|
(草稿列表, 因缺少 requestid 而跳过的行数)。
|
|
"""
|
|
skipped = 0
|
|
buckets: dict[int, list[dict[str, Any]]] = {}
|
|
for row in rows:
|
|
request_id = as_int(row.get("requestid"))
|
|
if request_id is None:
|
|
skipped += 1
|
|
continue
|
|
buckets.setdefault(request_id, []).append(row)
|
|
return [_draft_from_rows(request_id, items) for request_id, items in buckets.items()], skipped
|
|
|
|
|
|
def _draft_from_rows(request_id: int, items: list[dict[str, Any]]) -> AcceptanceDraft:
|
|
"""同一流程的多行收成一条 E8 草稿。"""
|
|
header = items[0]
|
|
flow_status = e8_flow_status(header.get("currentnodetype"), header.get("currentstatus"))
|
|
return AcceptanceDraft(
|
|
oa_system="e8",
|
|
request_id=request_id,
|
|
acceptance_no=_text(header.get("ysddh")),
|
|
purchase_order_no=_text(header.get("cgdh")),
|
|
contract_no=_text(header.get("htbh")),
|
|
flow_no=_text(header.get("flow_no")),
|
|
request_title=_text(header.get("request_title")),
|
|
request_created_at=_e8_created_at(header),
|
|
timer_started_at=as_datetime(header.get("syddjssj")),
|
|
can_accept=can_accept_from_oa(as_int(header.get("sfkys"))),
|
|
cannot_accept_note=_text(header.get("ycsm")),
|
|
is_returned=is_returned_from_oa(as_int(header.get("sfzfth"))),
|
|
flow_status_raw=flow_status,
|
|
finished_at=_finished_at(header, flow_status),
|
|
request_match_count=1,
|
|
nodes=_nodes(items),
|
|
operators=_operators(items),
|
|
owner_oa_id=as_int(header.get("sybmdd")),
|
|
owner_name=_text(header.get("owner_name")),
|
|
owner_job_num=_text(header.get("owner_job_num")),
|
|
equip_oa_id=None,
|
|
equip_name=None,
|
|
equip_job_num=None,
|
|
applicant_oa_id=as_int(header.get("creater")),
|
|
applicant_name=_text(header.get("applicant_name")),
|
|
applicant_job_num=_text(header.get("applicant_job_num")),
|
|
)
|
|
|
|
|
|
def _e8_created_at(header: dict[str, Any]) -> datetime | None:
|
|
"""
|
|
拼出 E8 流程创建时间。
|
|
|
|
核心逻辑:createdate 是日期,createtime 是时分秒,两边分开存。
|
|
只有日期时按当天 0 点。都空则没有创建时间。
|
|
"""
|
|
day = _text(header.get("createdate"))
|
|
clock = _text(header.get("createtime"))
|
|
if day is None:
|
|
return None
|
|
if clock is None:
|
|
return as_datetime(day)
|
|
return as_datetime(f"{day} {clock}")
|
|
|
|
|
|
def _finished_at(header: dict[str, Any], flow_status: int | None) -> datetime | None:
|
|
"""归档或撤销时,用最后操作时间作为结束时间。进行中不写。"""
|
|
if flow_status not in _TERMINAL_FLOW:
|
|
return None
|
|
parsed = as_datetime(header.get("lastoperatedate"))
|
|
if parsed is not None:
|
|
return parsed
|
|
return None
|
|
|
|
|
|
def _nodes(rows: list[dict[str, Any]]) -> list[NodeDraft]:
|
|
"""当前节点只取 currentnodeid。没有节点 ID 时不造一行。"""
|
|
found: dict[int, NodeDraft] = {}
|
|
for row in rows:
|
|
node_id = as_int(row.get("currentnodeid"))
|
|
if node_id is None or node_id in found:
|
|
continue
|
|
found[node_id] = NodeDraft(
|
|
node_id=node_id,
|
|
node_name=_text(row.get("nodename")),
|
|
node_type=None,
|
|
)
|
|
return list(found.values())
|
|
|
|
|
|
def _operators(rows: list[dict[str, Any]]) -> list[OperatorDraft]:
|
|
"""当前未办人。查询已限定 isremark=0,这里只按人和节点去重。"""
|
|
found: dict[tuple[int, int], OperatorDraft] = {}
|
|
for row in rows:
|
|
node_id = as_int(row.get("currentnodeid"))
|
|
user_id = as_int(row.get("userid"))
|
|
if node_id is None or user_id is None:
|
|
continue
|
|
key = (node_id, user_id)
|
|
if key in found:
|
|
continue
|
|
found[key] = OperatorDraft(
|
|
node_id=node_id,
|
|
user_id=user_id,
|
|
handler_name=_text(row.get("handler_name")),
|
|
handler_job_num=_text(row.get("handler_job_num")),
|
|
)
|
|
return list(found.values())
|
|
|
|
|
|
def _text(value: Any) -> str | None:
|
|
"""去空白;空串视为没有值。"""
|
|
if value is None:
|
|
return None
|
|
text = str(value).strip()
|
|
return text or None
|