Commit 28b58b3d by zhouhaibao

refactor(data-process): 更新解密API并优化人力成本表解析逻辑

- 使用新版msoffcrypto API的outfile参数直接解密到文件对象
- 人力成本表过滤条件从AF列核验改为检核业务线列
- 优化业务类型字段的数据类型转换逻辑
- 添加数据过滤统计日志
- 排除已离职人员的处理逻辑
- 移除临时变量减少内存占用
1 parent 97b9da6f
......@@ -208,12 +208,11 @@ def decrypt_excel_if_needed(file_path):
office_file = msoffcrypto.OfficeFile(f)
if office_file.is_encrypted():
office_file.load_key(password=password)
decrypted = office_file.decrypt()
# 写入临时文件
# 写入临时文件(新版 API: decrypt(outfile=文件对象))
tmp_fd, tmp_path = tempfile.mkstemp(suffix='.xlsx', prefix='decrypted_')
with os.fdopen(tmp_fd, 'wb') as tmp_f:
tmp_f.write(decrypted)
office_file.decrypt(outfile=tmp_f)
logger.info(f"[解密] 成功解密文件,密码: {password}")
return tmp_path, True
......
......@@ -1168,7 +1168,8 @@ def parse_labor_cost(file_path, biz_type='1.1', password='2026'):
"""
解析人力成本表(加密文件密码默认 2026)。
返回 dict[store_name] = headcount (int)
过滤条件:AF列核验=TUER, 人员业务归属=biz_type, 岗位=门店销售/精品内勤/前台/调度质检员
过滤条件:检核列值为 truthy(1.0/True/'TUER'), 人员业务归属=biz_type,
岗位=门店销售/精品内勤/前台/调度质检员, 排除已离职
"""
import tempfile, os, io
logger.info(f"[人力成本表] 解析 {file_path}, biz_type={biz_type}")
......@@ -1182,10 +1183,9 @@ def parse_labor_cost(file_path, biz_type='1.1', password='2026'):
of = msoffcrypto.OfficeFile(f)
if of.is_encrypted():
of.load_key(password=password)
decrypted = of.decrypt()
tmp_fd, tmp_path = tempfile.mkstemp(suffix='.xlsx', prefix='decrypted_labor_')
with os.fdopen(tmp_fd, 'wb') as tmp_f:
tmp_f.write(decrypted)
of.decrypt(outfile=tmp_f) # 新版 API:outfile 需是文件对象
actual_path = tmp_path
is_tmp = True
logger.info(f"[人力成本表] 解密成功(密码={password})")
......@@ -1211,10 +1211,11 @@ def parse_labor_cost(file_path, biz_type='1.1', password='2026'):
df.columns = [str(c).strip() if c else '' for c in df.columns]
col_map = match_columns(df.columns, {
'store_name': ['入驻门店', '门店名称', '门店名'],
'verify': ['核验', 'AF', 'AF列', 'AF列核验', '检核'],
'verify': ['检核业务线'],
'biz_type': ['人员业务归属', '业务类型', '业务线'],
'position': ['岗位', '职位'],
})
logger.info(f"[人力成本表] 列映射: {col_map}")
if 'store_name' not in col_map:
logger.warning(f"[人力成本表] 未找到门店名列 (实际列={list(df.columns)[:15]})")
return {}
......@@ -1225,27 +1226,44 @@ def parse_labor_cost(file_path, biz_type='1.1', password='2026'):
pos_col = col_map.get('position')
result = {}
skip_verify = skip_biz = skip_pos = 0
for _, row in df.iterrows():
# AF列核验=TUER
# 检核业务线 = True
if verify_col:
verify_val = str(row.get(verify_col, '')).strip()
if verify_val.upper() != 'TUER':
v = row.get(verify_col)
if v is not True:
skip_verify += 1
continue
# 人员业务归属=1.1
# 人员业务归属 = biz_type
if biz_col:
bt = str(row.get(biz_col, '')).strip()
bt_raw = row.get(biz_col)
bt = str(bt_raw).strip() if bt_raw is not None else ''
# pandas 可能把 '1.1' 读成 float 1.1
try:
fbt = float(bt)
bt = str(fbt) if fbt == int(fbt) else str(fbt)
except (ValueError, TypeError):
pass
if bt != biz_type:
skip_biz += 1
continue
# 岗位筛选
if pos_col:
pos = str(row.get(pos_col, '')).strip()
if pos and not any(t in pos for t in target_positions):
skip_pos += 1
continue
sn = str(row.get(col_map['store_name'], '')).strip()
if not sn or sn in ('nan', 'None', ''):
continue
result[sn] = result.get(sn, 0) + 1
logger.info(f"[人力成本表] {len(result)} 个门店, biz_type={biz_type}, headcount样本={list(result.items())[:5]}")
logger.info(f"[人力成本表] {len(result)} 个门店, biz_type={biz_type}, "
f"skip_verify={skip_verify}, skip_biz={skip_biz}, skip_pos={skip_pos}, "
f"headcount样本={list(result.items())[:10]}")
return result
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!