# -*- coding: utf-8 -*-
import openpyxl, io
from collections import defaultdict
wb=openpyxl.load_workbook("한국지역난방공사.xlsx"); ws=wb.active
out=io.open("_efgdup_out.txt","w",encoding="utf-8")
def w(*a): out.write(" ".join(str(x) for x in a)+"\n")
eff={}
for r in range(3,ws.max_row+1):
    for c in range(4,11): eff[(r,c)]=ws.cell(r,c).value
for mr in ws.merged_cells.ranges:
    if mr.min_col>=4 and mr.max_col<=10:
        v=ws.cell(mr.min_row,mr.min_col).value
        for r in range(mr.min_row,mr.max_row+1):
            for c in range(mr.min_col,mr.max_col+1): eff[(r,c)]=v
def cell(r,c): 
    v=eff[(r,c)]; return str(v).strip() if v not in(None,"") else ""
def fullpath(r): return tuple(cell(r,c) for c in range(4,11))   # D..J
def efg(r): return tuple(cell(r,c) for c in range(5,8))         # E,F,G

# full path (D..J) duplicates
fp=defaultdict(list)
for r in range(3,ws.max_row+1):
    p=fullpath(r)
    if any(p): fp[p].append(r)
w("=== EXACT FULL-PATH (D..J) DUPLICATE ROWS ===")
n=0
for p,rs in fp.items():
    if len(rs)>1:
        n+=1
        w("  rows",rs,"K=",[str(ws.cell(r,11).value) for r in rs])
        w("    path:"," > ".join(x for x in p if x))
w("  total exact-full-path dup groups:",n)

# E/F/G triple duplicates (ignore D and H+)
eg=defaultdict(list)
for r in range(3,ws.max_row+1):
    t=efg(r)
    if any(t): eg[t].append(r)
w("\n=== SAME (E,F,G) triple (ignoring D) ===")
n2=0
for t,rs in eg.items():
    if len(rs)>1:
        n2+=1
        for r in rs:
            w("   r%d B%s D=%s | E=%s F=%s G=%s | %s"%(r,ws.cell(r,2).value,cell(r,4),t[0],t[1],t[2],str(ws.cell(r,11).value)[-40:]))
        w("")
w("  total EFG-triple dup groups:",n2)
out.close(); print("done")
