Files
37duAutomation/plugins/XQ0001-0100/XQ0038 定性评价推送/report/__init__.py
T

154 lines
7.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""XQ0038 定性评价推送 插件 - 报告层:导出当天定性评价单并核对当前处理人。
导出按门户查询方案(管理/专业序列,searchId 固化),以 创建时间=EXPORT_DATE(空=当天)
为条件,走 search.do 结构化导出接口;导出后按「当前处理人」含登录账号姓名的行判为
「异常」,生成 *_核对.xlsx(末列「异常标记」)。
"""
import os
import re
import time
import traceback
from .. import config
from ..data import export_plan_once, get_login_user
def audit_xls(path, user_name):
"""核对导出 xls:「当前处理人」含登录账号姓名 -> 异常,生成 *_核对.xlsx。
返回异常行数(user_name 为空时跳过核对返回 -1)。
导出表只有姓名文本、无处理人工号列(字段字典无此字段),故按姓名匹配。
"""
import openpyxl
import xlrd
wb = xlrd.open_workbook(path)
sh = wb.sheet_by_index(0)
head = [str(sh.cell_value(0, c)) for c in range(sh.ncols)]
if '当前处理人' not in head:
config.log(' 核对跳过:导出表无「当前处理人」列')
return -1
proc_idx = head.index('当前处理人')
key_idx = {n: (head.index(n) if n in head else -1)
for n in ('员工工号', '被评价人', '当前环节')}
rows_out, bad = [], []
for r in range(sh.nrows):
vals = []
for c in range(sh.ncols):
v = sh.cell_value(r, c)
if isinstance(v, float) and '时间' in head[c]:
try:
v = xlrd.xldate.xldate_as_datetime(v, wb.datemode).strftime(
'%Y-%m-%d %H:%M')
except Exception:
pass
vals.append('' if v is None else str(v))
if r == 0:
vals.append('异常标记')
else:
handlers = [x.strip() for x in re.split(r'[;;,,]', vals[proc_idx])
if x.strip()]
mark = '异常:当前处理人含登录账号' if user_name and user_name in handlers else ''
if mark:
bad.append(vals)
vals.append(mark)
rows_out.append(vals)
out_path = re.sub(r'\.xls$', '_核对.xlsx', path)
owb = openpyxl.Workbook()
osh = owb.active
osh.title = '核对结果'
for vals in rows_out:
osh.append(vals)
owb.save(out_path)
config.log(' sheet=%s 行=%d(含表头) 列=%d -> %s'
% (sh.name, sh.nrows, sh.ncols, os.path.basename(out_path)))
config.log(' 异常 %d 行(当前处理人=%s)' % (len(bad), user_name or '未知,未核对'))
for vals in bad[:20]:
config.log(' %s | %s | %s | 处理人=%s' % (
vals[key_idx['员工工号']], vals[key_idx['被评价人']],
vals[key_idx['当前环节']], vals[proc_idx]))
return len(bad)
def export_and_audit(s, name, sid, day, out_path, user_name):
"""单方案导出 + 核对,含全局重试。返回 (行数, 异常行数, 错误信息);错误信息为空即成功。
异常行数:-1=未核对(导出表无「当前处理人」列或未取到登录账号),0=无异常。
"""
last_err = ''
for attempt in range(1, config.MAX_RETRY + 1):
try:
total, err = export_plan_once(s, sid, day, out_path)
except Exception:
total, err = None, '异常: ' + traceback.format_exc(limit=1).replace('\n', ' ')
if not err:
bad = audit_xls(out_path, user_name) if total else 0
return total, bad, ''
last_err = err
config.log('%s -> 第%d次失败:%s' % (name, attempt, err))
time.sleep(1)
return None, 0, last_err
def run_export():
"""导出编排:逐方案导出 + 核对(含索引刷新等待与异常行二次复核)。
单据刚提交时 OA 搜索索引的「当前环节/当前处理人」尚未刷新,立即导出会读成
「起草节点 + 发起人」,从而把刚提交的单据误判为异常。因此:
1) 导出前等待 EXPORT_SETTLE_WAIT 秒让索引落定;
2) 首次导出仍检出异常行时,等待 EXPORT_RECHECK_WAIT 秒重新导出复核,
以复核结果为准(同名 xls 与 *_核对.xlsx 覆盖更新)。
返回 {summary, detail, path}。
"""
day = config.get_export_date() or time.strftime('%Y-%m-%d')
out_dir = config.export_output_dir()
s = config.get_oa_session()
config.log('导出模式:创建时间=%s,方案 %d 个' % (day, len(config.SEARCH_PLANS)))
if config.EXPORT_SETTLE_WAIT > 0:
config.log('等待 %d 秒让 OA 搜索索引刷新(当前环节/当前处理人)后再导出'
% config.EXPORT_SETTLE_WAIT)
time.sleep(config.EXPORT_SETTLE_WAIT)
user_name, login_no = get_login_user(s)
if user_name:
config.log('登录账号:%s(%s),将核对当前处理人' % (user_name, login_no))
else:
config.log('警告:未取得门户登录账号,跳过当前处理人核对')
fail = []
detail = {}
for idx, (name, sid) in enumerate(config.SEARCH_PLANS.items()):
config.progress(int(100 * idx / len(config.SEARCH_PLANS)))
label = name.split('(')[0]
out_path = os.path.join(out_dir, '定性评价结果_%s_%s.xls'
% (label, day.replace('-', '')))
total, bad, err = export_and_audit(s, name, sid, day, out_path, user_name)
if err:
fail.append((name, err))
detail[label] = {'success': False, 'error': err}
config.progress(int(100 * (idx + 1) / len(config.SEARCH_PLANS)))
continue
config.log('%s -> %d 条 -> %s' % (name, total, out_path))
if bad > 0 and user_name and config.EXPORT_RECHECK_WAIT >= 0:
config.log('%s 检出 %d 行异常,等待 %d 秒后重新导出复核(首次多为索引延迟误判)'
% (name, bad, config.EXPORT_RECHECK_WAIT))
if config.EXPORT_RECHECK_WAIT > 0:
time.sleep(config.EXPORT_RECHECK_WAIT)
total2, bad2, err2 = export_and_audit(s, name, sid, day, out_path, user_name)
if err2:
config.log('%s 复核导出失败(沿用首次结果):%s' % (name, err2))
else:
config.log('%s 复核:%d 条,异常 %d 行(首次 %d 行)'
% (name, total2, max(bad2, 0), bad))
total, bad = total2, bad2
detail[label] = {'total': total, 'path': out_path,
'audit': bad if total else 0}
config.progress(int(100 * (idx + 1) / len(config.SEARCH_PLANS)))
config.progress(100)
config.log('==== 导出完成:成功 %d / 失败 %d ====' % (len(config.SEARCH_PLANS) - len(fail),
len(fail)))
for name, err in fail:
config.log('失败 %s:%s' % (name, err))
summary = '定性评价导出核对完成:成功 %d 个方案 / 失败 %d 个%s' % (
len(config.SEARCH_PLANS) - len(fail), len(fail),
'(%s)' % '、'.join(f[0] for f in fail) if fail else '')
return {'summary': summary, 'detail': detail, 'path': out_dir}