# -*- coding: utf-8 -*-
# 공공기관 미검수 사이트 일괄 구조검수(읽기전용). _검수체크.inspect 재사용.
import sys, glob, os
sys.path.insert(0, r'D:\01.프로젝트\DB수집\작업파일\광역_사이트맵')
from _검수체크 import inspect
BASE=os.path.dirname(os.path.abspath(__file__))
import openpyxl
sb=glob.glob(os.path.join(BASE,'공공기관_작업현황.xlsx'))[0]
ws=openpyxl.load_workbook(sb).active
# 미검수 번호
targets=[]
only=[int(a) for a in sys.argv[1:] if a.isdigit()]
for r in range(2,ws.max_row+1):
    no=ws.cell(r,2).value
    if no is None: continue
    ck=str(ws.cell(r,6).value or '')
    if only:
        if no in only: targets.append(no)
    elif '완료' not in ck:
        targets.append(no)
passes=[]; fails=[]
for no in targets:
    ds=[x for x in glob.glob(os.path.join(BASE,'%d.*'%no)) if os.path.isdir(x)]
    if not ds: print('%d: 폴더없음'%no); continue
    xs=[p for p in glob.glob(os.path.join(ds[0],'*.xlsx')) if not os.path.basename(p).startswith(('_','~')) and 'conflict' not in os.path.basename(p) and 'backup' not in p]
    if not xs: print('%d: 파일없음'%no); continue
    n,issues=inspect(xs[0])
    nm=os.path.basename(ds[0])
    if issues:
        fails.append((no,nm,n,issues))
        print('FAIL %-28s %d행 — %d건'%(nm,n,len(issues)))
        for x in issues: print('     %s'%x)
    else:
        passes.append((no,nm,n))
        print('PASS %-28s %d행'%(nm,n))
print('\n=== 요약: PASS %d / FAIL %d (대상 %d) ==='%(len(passes),len(fails),len(targets)))
print('FAIL:', [n for n,_,_,_ in fails])
