#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""探针：课题组维度与预存款线索。"""
import io
import json
import os
from collections import Counter

import pandas as pd

ROOT = '/Users/liuxinyuan/Desktop/Hermes输出-工作类'
BASE = '/Users/liuxinyuan/Desktop/hermas输入-工作台数据库'
D = json.load(io.open(ROOT + '/销售分析/科研销售_按人与区域_20260820-20260930.json', encoding='utf-8'))

print('=== ① 科研 37 家客户名（看是否含课题组标识）===')
for c in D['customers']:
    print('  %-46s %8.4f 万' % (c['客户'][:44], c['金额']))
kw = sum(1 for c in D['customers'] if any(k in c['客户'] for k in ('课题组', '实验室', 'Lab ', '组')))
print('  含「课题组/实验室/组」字样的：%d / %d' % (kw, len(D['customers'])))

print('\n=== ② MCP 客户数据的字段 ===')
for f in ('数据/mcp_customers.json', '数据/customers.json'):
    p = os.path.join(ROOT, f)
    if not os.path.exists(p):
        print('  %s 不存在' % f)
        continue
    j = json.load(io.open(p, encoding='utf-8'))
    items = j.get('customers') or j.get('items') or []
    print('  %s ｜ %d 条' % (f, len(items)))
    if items:
        ks = set()
        for it in items[:200]:
            ks.update(it.keys())
        print('     字段:', sorted(ks))
        print('     样例:', json.dumps(items[0], ensure_ascii=False)[:520])

print('\n=== ③ ERP 单据里有无预存款线索 ===')
df = pd.read_excel(BASE + '/月度销售额数据/销售订单_20260101-20260930.xlsx')
print('  单据类型:', dict(df['单据类型'].value_counts()))
for c in df.columns:
    if '预' in str(c) or '备注' in str(c) or '收款' in str(c):
        print('  可疑列:', c)
# 备注列里找「预存」
for c in df.columns:
    if df[c].dtype == object:
        hit = df[df[c].astype(str).str.contains('预存|预收|预付', na=False)]
        if len(hit):
            print('  列「%s」含预存字样 %d 行' % (c, len(hit)))
            print('     样例:', str(hit[c].iloc[0])[:150])

print('\n=== ④ 飞书端有无预存/收款类工作项 ===')
print('  （销售管理工作项类型已知清单中没有「预存/收款」类；客户字段见 ②）')
