#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""抽取集团同比相关的项目/客诉/活动佐证材料。"""
import json
import os
import re
import sys

ROOT = os.path.expanduser('~/Desktop/Hermes输出-工作类')
KEYS = ['丽珠', '天隆', '华益美', '东方基因', '佰奥莱博', '复星', '迪安', '新产业', '海尔施', '雅为', '安瑞科', '陆军军医']
SKUS = ['BTE-8', '2FM101', 'FMD5104-074901', 'FMD0104-074901', '2FM091', 'CM-8', 'PFM4211', 'M5244-ZJ4',
        'M4211-3', 'E07-NA', 'AS05', 'E13-NA', 'PFHW1007', 'M5261', 'M813-DW2', 'FMD5084-000901', 'FMD2011',
        'FM5144', 'FM513', 'FM507', 'FM0181-4', 'M3161-000403', 'PFM2181-4-NA', 'FM2181-4-NA', 'M5274-073703',
        'FSTR201', 'M8254', 'M181-4-DB']


def walk(o, path=''):
    if isinstance(o, dict):
        for k, v in o.items():
            yield from walk(v, path + '/' + str(k))
    elif isinstance(o, list):
        for i, v in enumerate(o):
            yield from walk(v, path + '[%d]' % i)
    else:
        yield path, o


def txt(o):
    return ' '.join(str(v) for _, v in walk(o))


def main():
    for fn in ['数据/daily_activities_mcp.json', '数据/mcp_complaints.json']:
        p = os.path.join(ROOT, fn)
        if not os.path.exists(p):
            print('!! missing', p)
            continue
        d = json.load(open(p, encoding='utf-8'))
        rows = d if isinstance(d, list) else (d.get('activities') or d.get('data') or d.get('rows') or d.get('complaints') or [])
        print('=' * 90)
        print(fn, 'type=', type(d).__name__, 'rows=', len(rows) if hasattr(rows, '__len__') else '?')
        if isinstance(rows, list) and rows:
            print('sample keys:', list(rows[0].keys())[:25] if isinstance(rows[0], dict) else type(rows[0]))
        hits = []
        for r in (rows if isinstance(rows, list) else []):
            t = txt(r)
            if any(k in t for k in SKUS):
                hits.append(r)
        print('SKU命中条数:', len(hits))
        for r in hits[:40]:
            t = re.sub(r'\s+', ' ', txt(r))
            print('  -', t[:400])


def show_xlsx():
    import pandas as pd
    p = os.path.expanduser('~/.hermes/profiles/zone-b/cache/documents/doc_b2c8a7be2ffa_副本BRSW-营销中心-客户项目推移表V02.xlsx')
    x = pd.ExcelFile(p)
    print('=' * 90)
    print('客户项目推移表 sheets:', x.sheet_names)
    for s in x.sheet_names[:6]:
        d = x.parse(s)
        print('--', s, d.shape, list(d.columns)[:20])
        print(d.head(8).to_string(max_colwidth=25))


if __name__ == '__main__':
    main()
    show_xlsx()
