# -*- coding: utf-8 -*-
"""Dọn CSDL B sau chuyển: bỏ dòng trùng (YB2087 vs Ferrispec FP YB-2087), đặt tên/hãng/tài liệu đầy đủ cho 14 mã mục 8,
chuẩn hoá tên Carbon Black MA 100. Kiểm tra không còn mã trùng."""
import sqlite3

B = r'C:\Claw 2\Nghien cuu\Du-an-B-He-mau-Stain-Glaze\CSDLB.db'
cb = sqlite3.connect(B)
cb.row_factory = sqlite3.Row

# 1. bỏ dòng trùng YB2087 (giữ Ferrispec FP YB-2087 đã có đủ thông tin)
dup = list(cb.execute("select id, ma, ten, coalesce(ci_cas,'') ci from mau where ma in ('YB2087')"))
for r in dup:
    cb.execute('delete from mau where id=?', (r['id'],))
    print('đã xoá dòng trùng:', dict(r))

# 2. đặt lại tên/hãng/tài liệu cho các mã mục 8
M8 = {
    'C5': ('Chromaflo Blue C5 (8727225)', 'Chromaflo', 'SDS bản Việt/EN + TDS Blue C5', 'Màu pha máy (tint machine), chưa vào 10 mã ưu tiên', 'MÀU PHA MÁY (Chromaflo)'),
    'C6': ('Chromaflo Blue C6 (8727027)', 'Chromaflo', 'SDS bản Việt/EN', 'Màu pha máy, chưa vào ưu tiên', 'MÀU PHA MÁY (Chromaflo)'),
    'C4': ('Chromaflo Red C4 (8720801)', 'Chromaflo', 'MSDS VN + TDS Red C4', 'Màu pha máy, chưa vào ưu tiên', 'MÀU PHA MÁY (Chromaflo)'),
    'C2': ('Chromaflo Yellow C2 (8722040)', 'Chromaflo', 'TDS 8722040 YELLOW C2', 'Màu pha máy, chưa vào ưu tiên', 'MÀU PHA MÁY (Chromaflo)'),
    'C26': ('Chromaflo Yellow Oxide C26 (8721826)', 'Chromaflo', 'TDS 8721826 YELLOW OXIDE C26', 'Màu pha máy, chưa vào ưu tiên', 'MÀU PHA MÁY (Chromaflo)'),
    'C27': ('Chromaflo Red Oxide C27 (8721027)', 'Chromaflo', 'MSDS VN (8721027) + TDS Red Oxide C27', 'Màu pha máy, chưa vào ưu tiên', 'MÀU PHA MÁY (Chromaflo)'),
    'BLACK 20': ('MH Black 20 (NL.001471 — Solvent Black 27)', 'Matapel/Eckart – nội bộ MH', 'TDS + MSDS (kèm công thức pha trong bảng tính 02/10)', 'Dye đen nội bộ', 'MÀU NỘI BỘ MH'),
    'BLACK 56': ('MH Black 56 (NL.001475 — Solvent Black 28)', 'Matapel/Eckart – nội bộ MH', 'TDS + MSDS', 'Dye đen nội bộ', 'MÀU NỘI BỘ MH'),
    'Y82': ('MH Yellow 57 (Y82)', 'Matapel/Eckart – nội bộ MH', 'TDS 2024 — thiếu MSDS', 'Dye vàng nội bộ', 'MÀU NỘI BỘ MH'),
    'CN09': ('Novoperm Red F3RK 70-CN09', 'Clariant', 'MSDS bản Việt', 'Pigment hữu cơ đỏ', 'NCC KHÁC (Clariant)'),
    'BG 100': ('Hostatint Blue A-BG 100', 'Hostatint (BASF/Clariant)', 'MSDS', 'Pigment paste xanh dương', 'NCC KHÁC (Hostatint)'),
    'G007': ('Paste màu xanh lá TS-G007', 'Tri Mộc', 'MSDS', 'Paste màu nội địa', 'NCC KHÁC (Tri Mộc)'),
    'NS 29': ('UC General Red NS 29', 'Matapel / Eckart', 'MSDS bản Việt', 'Pigment đỏ', 'NCC KHÁC (Matapel/Eckart)'),
    'NS 40': ('UC Giant Blue NS 40', 'Matapel / Eckart', 'MSDS bản Việt + SDS 02/10', 'Pigment xanh', 'NCC KHÁC (Matapel/Eckart)'),
}
n = 0
for ma, (ten, hang, tl, gc, nhom) in M8.items():
    cur = cb.execute('update mau set ten=?, hang_ncc=?, tai_lieu=?, ghi_chu=?, nhom=? where ma=?', (ten, hang, tl, gc, nhom, ma))
    n += cur.rowcount
print('đã đặt tên/tài liệu cho %d mã mục 8' % n)

# 3. chuẩn hoá MA 100 + Carbon 30L
cb.execute("update mau set ten='Carbon Black MA 100 (Mitsubishi)', nhom='ĐEN', hang_ncc='Mitsubishi - Tín Phú' where ma='MA 100'")
cb.execute("update mau set ten='Carbon 30L (nghi HIBLACK 30L – Tín Phú)' where ma='30L'")
cb.commit()

print('\n=== KIỂM TRA TRÙNG MÃ ===')
dups = list(cb.execute("select ma, count(*) c from mau group by ma having c > 1"))
print('  mã bị trùng:', dups if dups else 'KHÔNG có ✔')
print('  tổng mã:', cb.execute('select count(*) from mau').fetchone()[0])
print('  C.I./CAS: %d | tồn kho: %d | ưu tiên: %d | tài liệu: %d' % (
    cb.execute('select count(*) from mau where ci_cas is not null').fetchone()[0],
    cb.execute('select count(*) from mau where ton_kg is not null').fetchone()[0],
    cb.execute('select count(*) from mau where uu_tien is not null').fetchone()[0],
    cb.execute('select count(*) from mau where tai_lieu is not null').fetchone()[0]))
print('\n=== 13 MÃ ƯU TIÊN (theo thứ tự chạy mẫu trong tài liệu) ===')
for r in cb.execute('select uu_tien, ma, ten, ci_cas, ton_kg from mau where uu_tien is not null order by uu_tien, ma'):
    print('  %-3s %-10s %-42s tồn %s kg' % (r['uu_tien'], r['ma'], r['ten'][:42], r['ton_kg']))
print('\nB — integrity:', cb.execute('pragma integrity_check').fetchone()[0])
cb.close()
