121 lines
5.3 KiB
Python
121 lines
5.3 KiB
Python
# -*- 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 run_export():
|
||
"""导出编排:逐方案导出 + 核对。返回 {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)))
|
||
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('-', '')))
|
||
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:
|
||
config.log('%s -> %d 条 -> %s' % (name, total, out_path))
|
||
bad = 0
|
||
if total:
|
||
bad = audit_xls(out_path, user_name)
|
||
detail[label] = {'total': total, 'path': out_path,
|
||
'audit': bad if total else 0}
|
||
break
|
||
last_err = err
|
||
config.log('%s -> 第%d次失败:%s' % (name, attempt, err))
|
||
time.sleep(1)
|
||
else:
|
||
fail.append((name, last_err))
|
||
detail[label] = {'success': False, 'error': last_err}
|
||
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}
|