# -*- coding: utf-8 -*-
import openpyxl, io, sys
from collections import defaultdict
wb=openpyxl.load_workbook("한국지역난방공사.xlsx")
ws=wb.active
out=io.open("_dupscan_out.txt","w",encoding="utf-8")
def w(*a): out.write(" ".join(str(x) for x in a)+"\n")

rows=[]
for r in range(3, ws.max_row+1):
    B=ws.cell(r,2).value
    D2J=[ws.cell(r,c).value for c in range(4,11)]  # D..J
    K=ws.cell(r,11).value
    L=ws.cell(r,12).value; M=ws.cell(r,13).value; N=ws.cell(r,14).value
    rows.append((r,B,D2J,K,L,M,N))

w("max_row",ws.max_row,"data rows",len(rows))

# 1) exact same K URL appearing on multiple rows
byK=defaultdict(list)
for r,B,d,K,L,M,N in rows:
    if K: byK[str(K).strip()].append(r)
w("\n=== DUP by identical K URL ===")
dupK=0
for K,rs in byK.items():
    if len(rs)>1:
        dupK+=1
        w("URL:",K)
        for rr in rs:
            row=next(x for x in rows if x[0]==rr)
            lbl=" > ".join([str(v) for v in row[2] if v])
            w("   row",rr,"B="+str(row[1]),"L="+str(row[4]),"M="+str(row[5]),"|",lbl)
w("total identical-URL groups:",dupK)

# 2) same full D..J label path AND same K (true dup row)
byPath=defaultdict(list)
for r,B,d,K,L,M,N in rows:
    key=(tuple(str(x).strip() if x else "" for x in d), str(K).strip() if K else "")
    byPath[key].append(r)
w("\n=== DUP by identical (D..J path + K) ===")
for key,rs in byPath.items():
    if len(rs)>1:
        w("rows",rs,"path=", " > ".join([x for x in key[0] if x]), "| K=",key[1])

out.close()
print("done")
