375 lines
16 KiB
Python
375 lines
16 KiB
Python
"""列表导出:把「当前筛选条件」下的数据导成 CSV,外加**流程明细导出**那一整页。
|
||
|
||
两半共用同一套底层:值怎么渲染看 :mod:`app.service.exportfmt`,数据范围与筛选条件走
|
||
:mod:`app.api.runtime` 里流程监控那一份查询。所以界面上看不到的单子一定导不出来,
|
||
不会出现「列表里没有、导出却有」的越权。
|
||
|
||
后半段(``/export/detail/*``)挂的是独立权限码 ``monitor:export``,界面是侧栏里
|
||
「流程明细导出」那条独立菜单,设计稿见 ``docs/process-detail-export-design.md``。
|
||
|
||
只做读路径,不碰流转。查询条件与列表接口逐字复用同一套函数
|
||
(``_created_query`` / ``_task_filter`` / ``_admin_work_page`` / ``_admin_task_stmt``),
|
||
所以界面上看不到的单子一定导不出来,不会出现「列表里没有、导出却有」的越权。
|
||
|
||
三件值得一提的事:
|
||
|
||
- 编码带 UTF-8 BOM:Excel 双击打开中文不乱码,这是导出现场最常见的坑。
|
||
- 表单字段摊不摊成列,取决于本次导出的数据是不是同一张表单。同一张才摊开,
|
||
混在一起摊会多出几十列空值,反而不如固定列好读。
|
||
- 字典值一律翻成标签再落地,导出来的是「差旅费」而不是「CL01」。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import csv
|
||
import io
|
||
from datetime import datetime
|
||
from urllib.parse import quote
|
||
|
||
from fastapi import APIRouter, Depends, Query, Request, Response
|
||
from sqlalchemy import select
|
||
from sqlalchemy.orm import Session
|
||
|
||
from .. import models, schemas
|
||
from ..db import get_db
|
||
from ..deps import CurrentUser, current_user, require_permission
|
||
from ..envelope import bad_request, forbidden, ok
|
||
from ..service import completed as completed_service
|
||
from ..service import detail_export
|
||
from ..service import dicts as dicts_service
|
||
from ..service.exportfmt import ( # noqa: F401 - 值渲染搬进了 service/exportfmt.py,老名字照旧可用
|
||
WORK_STATUS_LABELS,
|
||
cell_text,
|
||
relation_names as _job_names,
|
||
dt as _dt,
|
||
form_export_fields,
|
||
relation_ids as _relation_ids,
|
||
scalar_text as _scalar_text,
|
||
status_label as _status_label,
|
||
table_text as _table_text,
|
||
)
|
||
from .runtime import ( # noqa: PLC2701 - 共用条件与留痕,明细导出走的就是监控那一份查询
|
||
_admin_task_stmt, _admin_work_page, _created_query, _log_operation,
|
||
_task_conditions, _task_filter,
|
||
)
|
||
|
||
router = APIRouter(tags=["export"])
|
||
|
||
#: 一次导出的行数上限。列表接口每页最多 200,导出是给整月报表用的,
|
||
#: 但不能没有上限:一张宽表单导出几万行会把浏览器和数据库连接一起拖垮。
|
||
EXPORT_LIMIT = 5000
|
||
|
||
WORK_COLUMNS = ("单号", "标题", "流程", "分类", "状态", "发起人", "发起部门", "发起时间", "办结时间",
|
||
"当前环节", "当前处理人")
|
||
|
||
TASK_COLUMNS = ("单号", "标题", "流程", "分类", "环节", "处理人", "处理人部门", "发起人",
|
||
"到达时间", "完成时间", "处理动作", "处理意见", "是否超期")
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# 查询与列拼装
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
def _work_lines(rows: list, db: Session, cache: dicts_service.DictCache, with_form: bool):
|
||
"""返回 (表头, 数据行)。只有一张表单时才追加表单字段列。"""
|
||
form_ids = {str(row.form or "") for row in rows if getattr(row, "form", "")}
|
||
fields = form_export_fields(db, next(iter(form_ids))) if (with_form and len(form_ids) == 1) else []
|
||
jobs = _job_names(db, rows, fields)
|
||
headers = list(WORK_COLUMNS) + [str(item.get("label") or item.get("key")) for item in fields]
|
||
lines: list[list[str]] = []
|
||
for row in rows:
|
||
data = dict(row.data or {})
|
||
line = [
|
||
str(row.serial or ""),
|
||
str(row.title or ""),
|
||
str(row.process_name or ""),
|
||
str(row.category_name or ""),
|
||
_status_label(row.status, bool(getattr(row, "paused", False))),
|
||
str(row.creator_person or ""),
|
||
str(row.creator_unit_name or ""),
|
||
_dt(getattr(row, "start_time", None)),
|
||
_dt(getattr(row, "finish_time", None)),
|
||
str(row.current_activity_names or ""),
|
||
str(getattr(row, "manual_identity_text", "") or ""),
|
||
]
|
||
line.extend(
|
||
cell_text(field, data.get(str(field.get("key") or "")), cache, jobs, data=data)
|
||
for field in fields
|
||
)
|
||
lines.append(line)
|
||
return headers, lines
|
||
|
||
|
||
def _task_lines(rows: list) -> list[list[str]]:
|
||
lines: list[list[str]] = []
|
||
for row in rows:
|
||
lines.append(
|
||
[
|
||
str(row.serial or ""),
|
||
str(row.title or ""),
|
||
str(row.process_name or ""),
|
||
str(row.category_name or ""),
|
||
str(row.activity_name or ""),
|
||
str(getattr(row, "identity_name", "") or ""),
|
||
str(getattr(row, "unit_name", "") or ""),
|
||
str(getattr(row, "creator_person", "") or ""),
|
||
_dt(getattr(row, "start_time", None)),
|
||
_dt(getattr(row, "completed_time", None)),
|
||
str(getattr(row, "route_name", "") or ""),
|
||
str(getattr(row, "opinion", "") or ""),
|
||
"是" if bool(getattr(row, "expired", False)) else "否",
|
||
]
|
||
)
|
||
return lines
|
||
|
||
|
||
def _attachment(body: bytes, filename: str, media_type: str) -> Response:
|
||
return Response(
|
||
content=body,
|
||
media_type=media_type,
|
||
headers={
|
||
# 中文文件名走 RFC 5987,另留一个 ASCII 兜底给老版 Excel 和 curl -O。
|
||
"Content-Disposition": f"attachment; filename=\"export\"; filename*=UTF-8''{quote(filename)}",
|
||
"Cache-Control": "no-store",
|
||
},
|
||
)
|
||
|
||
|
||
def _csv(headers: list[str], rows: list[list[str]], filename: str) -> Response:
|
||
buffer = io.StringIO()
|
||
writer = csv.writer(buffer)
|
||
writer.writerow(headers)
|
||
writer.writerows(rows)
|
||
return _attachment(("\ufeff" + buffer.getvalue()).encode("utf-8"), filename,
|
||
"text/csv; charset=utf-8")
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# 接口
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
@router.get("/export/works")
|
||
def export_works(
|
||
scope: str = Query("created", description="created=我发起的 / admin=流程监控范围"),
|
||
keyword: str = Query(""),
|
||
process: str = Query(""),
|
||
status: str = Query(""),
|
||
start_date: str = Query(""),
|
||
end_date: str = Query(""),
|
||
expired: str = Query("", description="admin 范围用:1=只导手里压着超时候办的单子"),
|
||
paused: str = Query("", description="admin 范围用:1=只导被挂起的在途单;0=排除被挂起的"),
|
||
with_form: bool = Query(True, description="导出的是同一张表单时,是否把表单字段摊成列"),
|
||
db: Session = Depends(get_db),
|
||
user: CurrentUser = Depends(current_user),
|
||
) -> Response:
|
||
"""导出「我发起的」,或管理员监控范围内的流程单。"""
|
||
cache = dicts_service.DictCache(db)
|
||
if scope == "admin":
|
||
if not user.can("monitor:work"):
|
||
raise forbidden("无权限导出流程监控数据")
|
||
# 与监控列表同一个取数函数:界面上筛出来是哪些单子,导出来就是哪些单子,
|
||
# 在途的、办结的、终止的都在里面,不会出现「屏上有、CSV 里没有」。
|
||
rows, _total = _admin_work_page(
|
||
db,
|
||
user,
|
||
keyword=keyword,
|
||
process=process,
|
||
status=status,
|
||
start_date=start_date,
|
||
end_date=end_date,
|
||
expired=expired,
|
||
paused=paused,
|
||
limit=EXPORT_LIMIT,
|
||
)
|
||
else:
|
||
rows = []
|
||
if status in ("", "running"):
|
||
rows += _created_query(models.Work, user, keyword, process, status, start_date, end_date, db)
|
||
if status in ("", "completed", "terminated", "canceled"):
|
||
rows += _created_query(models.WorkCompleted, user, keyword, process, status, start_date, end_date, db)
|
||
rows.sort(key=lambda row: row.finish_time or row.start_time or row.create_time, reverse=True)
|
||
rows = rows[:EXPORT_LIMIT]
|
||
|
||
headers, lines = _work_lines(rows, db, cache, with_form)
|
||
return _csv(headers, lines, f"流程数据_{datetime.now():%Y%m%d_%H%M}.csv")
|
||
|
||
|
||
@router.get("/export/tasks")
|
||
def export_tasks(
|
||
scope: str = Query("processing", description="processing=我的待办 / completed=我的已办 / admin=流程监控的全部待办"),
|
||
keyword: str = Query(""),
|
||
process: str = Query(""),
|
||
category: str = Query(""),
|
||
expired: str = Query(""),
|
||
start_date: str = Query(""),
|
||
end_date: str = Query(""),
|
||
db: Session = Depends(get_db),
|
||
user: CurrentUser = Depends(current_user),
|
||
) -> Response:
|
||
"""导出待办 / 已办。
|
||
|
||
``processing`` 与 ``completed`` 的查询条件本身就锁死本人身份,不需要额外权限点;
|
||
``admin`` 是流程监控页「全部待办」那个页签用的,挂 ``monitor:work`` 并走同一套数据范围,
|
||
免得监控页看着一片、导出来却只有自己的几条。
|
||
|
||
已办那一档跟页面一样按单折叠(见 :mod:`app.service.completed`),别导出一张单
|
||
好几行——那份 CSV 十有八九是拿去数"这个月办了几张单"的。
|
||
"""
|
||
if scope == "admin":
|
||
if not user.can("monitor:work"):
|
||
raise forbidden("无权限导出流程监控数据")
|
||
stmt = _admin_task_stmt(
|
||
db,
|
||
user,
|
||
keyword=keyword,
|
||
process=process,
|
||
category=category,
|
||
expired=expired,
|
||
start_date=start_date,
|
||
end_date=end_date,
|
||
)
|
||
rows = list(db.execute(stmt.order_by(models.Task.start_time.desc()).limit(EXPORT_LIMIT)).scalars().all())
|
||
return _csv(list(TASK_COLUMNS), _task_lines(rows), f"监控待办_{datetime.now():%Y%m%d_%H%M}.csv")
|
||
if scope == "completed":
|
||
# 导出跟页面同一个口径:一张单只出一行。界面上合成一条、导出却摊成好几行,
|
||
# 拿 CSV 对账的人会以为自己少算了单,多出来的那一列「处理次数」把几次表态
|
||
# 的账留在同一行上,不靠他记住页面上看过的那个数。
|
||
groups = completed_service.collapse(
|
||
db,
|
||
_task_conditions(
|
||
models.TaskCompleted, user, keyword, process, category, "", "", start_date, end_date,
|
||
date_field="completed_time",
|
||
),
|
||
limit=EXPORT_LIMIT,
|
||
)
|
||
rows = [group.row for group in groups]
|
||
counts = {group.row.id: int(group.handles) for group in groups}
|
||
return _csv(
|
||
[*TASK_COLUMNS, "处理次数"],
|
||
[line + [str(counts.get(row.id, 1))] for line, row in zip(_task_lines(rows), rows)],
|
||
f"我的已办_{datetime.now():%Y%m%d_%H%M}.csv",
|
||
)
|
||
stmt = _task_filter(models.Task, user, keyword, process, category, "", expired, start_date, end_date)
|
||
rows = list(db.execute(stmt.order_by(models.Task.start_time.desc()).limit(EXPORT_LIMIT)).scalars().all())
|
||
return _csv(list(TASK_COLUMNS), _task_lines(rows), f"我的待办_{datetime.now():%Y%m%d_%H%M}.csv")
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# 流程明细导出(独立菜单 / 独立权限码)
|
||
# ---------------------------------------------------------------------------
|
||
|
||
#: 这一页的四条接口挂同一码;与 monitor:work 互不为前置,理由见设计稿 §2。
|
||
DETAIL = require_permission("monitor:export")
|
||
|
||
|
||
def _detail_plan(body: schemas.ExportDetailBody, db: Session, user: CurrentUser):
|
||
"""报文 → (筛选, 列定义),顺带把界面上不该出现的组合挡掉。"""
|
||
filters = detail_export.filters_from_body(body.filters.model_dump())
|
||
detail_export.validate(filters, body.columns, body.layout, body.fmt)
|
||
specs = detail_export.build_columns(
|
||
db, filters.processes, body.columns, body.columns_only, body.activities)
|
||
if not specs:
|
||
raise bad_request("勾选的列在这些流程里都不存在,请重新选择")
|
||
limit = detail_export.max_columns()
|
||
if len(specs) > limit:
|
||
raise bad_request(f"这次要出 {len(specs)} 列,超过单张表上限 {limit} 列:请少勾几列或收起环节列")
|
||
return filters, specs
|
||
|
||
|
||
def _process_names(db: Session, process_ids: list[str]) -> dict[str, str]:
|
||
if not process_ids:
|
||
return {}
|
||
return {
|
||
str(row.id): str(row.name or "")
|
||
for row in db.execute(
|
||
select(models.Process).where(models.Process.id.in_(process_ids))).scalars().all()
|
||
}
|
||
|
||
|
||
@router.get("/export/detail/catalog")
|
||
def export_detail_catalog(
|
||
db: Session = Depends(get_db), user: CurrentUser = Depends(DETAIL)
|
||
) -> dict:
|
||
"""可选流程清单 + 每条流程的字段目录 + 「相关核算项目」那一格在哪。"""
|
||
return ok(detail_export.catalog(db))
|
||
|
||
|
||
@router.post("/export/detail/count")
|
||
def export_detail_count(
|
||
body: schemas.ExportDetailBody,
|
||
db: Session = Depends(get_db),
|
||
user: CurrentUser = Depends(DETAIL),
|
||
) -> dict:
|
||
"""按当前筛选估行数(分流程报),导出前先知道这次有多大。"""
|
||
filters, _specs = _detail_plan(body, db, user)
|
||
counts = detail_export.counts_by_process(db, user, filters)
|
||
names = _process_names(db, filters.processes)
|
||
limit = detail_export.max_rows()
|
||
total = sum(counts.values())
|
||
return ok({
|
||
"total": total,
|
||
"limit": limit,
|
||
"exceeded": total > limit,
|
||
"byProcess": [
|
||
{"process": process_id, "name": names.get(process_id, process_id), "count": counts.get(process_id, 0)}
|
||
for process_id in filters.processes
|
||
],
|
||
})
|
||
|
||
|
||
@router.post("/export/detail/preview")
|
||
def export_detail_preview(
|
||
body: schemas.ExportDetailBody,
|
||
db: Session = Depends(get_db),
|
||
user: CurrentUser = Depends(DETAIL),
|
||
) -> dict:
|
||
"""按最终那份列取一页回来看看:字段勾完就地刷新,不用先导一个文件。"""
|
||
filters, specs = _detail_plan(body, db, user)
|
||
size = max(1, min(int(body.page_size or 20), 100))
|
||
offset = max(0, (int(body.page or 1) - 1) * size)
|
||
rows, total = detail_export.query_page(db, user, filters, limit=size, offset=offset)
|
||
sheets = detail_export.build_sheets(
|
||
db, user, filters, specs, rows, layout=body.layout, summary=body.summary)
|
||
return ok({
|
||
"total": total,
|
||
"page": body.page,
|
||
"pageSize": size,
|
||
"sheets": [{"name": sheet.name, "headers": sheet.headers, "lines": sheet.lines} for sheet in sheets],
|
||
})
|
||
|
||
|
||
@router.post("/export/detail/download")
|
||
def export_detail_download(
|
||
body: schemas.ExportDetailBody,
|
||
db: Session = Depends(get_db),
|
||
user: CurrentUser = Depends(DETAIL),
|
||
request: Request = None, # type: ignore[assignment]
|
||
) -> Response:
|
||
"""出文件:默认一条流程一个 sheet,也可以合并成一张表;CSV 只有合并那一种表头。"""
|
||
filters, specs = _detail_plan(body, db, user)
|
||
limit = detail_export.max_rows()
|
||
rows, total = detail_export.query_page(db, user, filters, limit=limit + 1)
|
||
if total > limit:
|
||
raise bad_request(
|
||
f"这次命中 {total} 行,超过单次上限 {limit} 行:请收窄日期或减少流程条数"
|
||
"(上限在「系统设置」里可调)")
|
||
sheets = detail_export.build_sheets(
|
||
db, user, filters, specs, rows, layout=body.layout, summary=body.summary)
|
||
names = _process_names(db, filters.processes)
|
||
filename = detail_export.file_name(body.fmt)
|
||
_log_operation(
|
||
db, user, "export_detail", "、".join(names.get(item, item) for item in filters.processes)[:255],
|
||
f"导出 {sum(len(sheet.lines) for sheet in sheets)} 行 / {len(body.columns)} 列 / "
|
||
f"{body.layout} / {body.fmt};筛选:状态={','.join(filters.statuses) or '全部'} "
|
||
f"创建={filters.create_start or '不限'}~{filters.create_end or '不限'} "
|
||
f"归档={filters.archive_start or '不限'}~{filters.archive_end or '不限'} "
|
||
f"未操作者={len(filters.unoperator.persons)} 人",
|
||
request,
|
||
)
|
||
db.commit()
|
||
if body.fmt == detail_export.FORMAT_CSV:
|
||
return _attachment(detail_export.to_csv(sheets), filename, "text/csv; charset=utf-8")
|
||
return _attachment(detail_export.to_xlsx(sheets), filename, detail_export.XLSX_TYPE)
|