# -*- coding: utf-8 -*- """票 04 终清理:collab owner 还原(heavy F-13 交割副作用)+ 非 seed e2c- 残留正门 archive""" import sys, io, json sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') import requests import pymysql BASE = 'http://localhost:8080' ADMIN = '739564171091247104' BUDDY, BUDDY_NAME, BUDDY_DEPT, BUDDY_DEPT_NAME = ('744842318024015872', '曾偲青', '744841292483133440', '职员') COLLAB_ID = '750844482391375872' ids = json.load(open('.scratch/customer-e2e/seed-ids.json', encoding='utf-8')) seed_ids = {str(v['id']) for v in ids['customers'].values() if isinstance(v, dict) and v.get('id')} conn = pymysql.connect(host='8.129.84.155', user='root', password='Itc@123456', database='crm', charset='utf8mb4', autocommit=True, cursorclass=pymysql.cursors.DictCursor) cur = conn.cursor() # 1. collab owner 还原(heavy 第二跑 F-13 把它当 BUDDY 名下唯一客户交割转走了) cur.execute("UPDATE customer SET owner_user_id=%s, owner_user_name_snapshot=%s, " "owner_dept_id=%s, owner_dept_name_snapshot=%s WHERE id=%s", (BUDDY, BUDDY_NAME, BUDDY_DEPT, BUDDY_DEPT_NAME, COLLAB_ID)) cur.execute("SELECT owner_user_name_snapshot n, owner_dept_name_snapshot dn " "FROM customer WHERE id=%s", (COLLAB_ID,)) r = cur.fetchone() print('1. collab owner 还原 ->', r['n'], '/', r['dn']) # 2. 非 seed 的活跃 e2c- 残留清点 cur.execute("SELECT id, customer_name, archive_status FROM customer " "WHERE customer_name LIKE 'e2c-%' AND archive_status=1") targets = [x for x in cur.fetchall() if str(x['id']) not in seed_ids] print('2. 非 seed 活跃 e2c- 残留 %d 条:' % len(targets)) for x in targets: print(' ', x['id'], x['customer_name']) # 3. 正门 archive(POST /api/customer/archive?id=) tok = None for _ in range(5): try: d = requests.get(f'{BASE}/api/auth/debug/token', params={'userId': ADMIN}, timeout=15).json().get('data') if d: tok = d if isinstance(d, str) else d.get('token') if tok: break except Exception as e: print(' token retry:', e) import time time.sleep(2) assert tok, 'debug/token 重试 5 次仍失败' H = {'Authorization': f'Bearer {tok}'} for x in targets: resp = requests.post(f'{BASE}/api/customer/archive', params={'id': str(x['id'])}, headers=H, timeout=30).json() print(' archive', x['customer_name'], '->', resp.get('code'), resp.get('message')) if resp.get('code') == 0: cur.execute("SELECT archive_status s FROM customer WHERE id=%s", (str(x['id']),)) print(' DB 复核 arch =', cur.fetchone()['s']) cur.execute("SELECT COUNT(*) c FROM customer WHERE customer_name LIKE 'e2c-%' " "AND archive_status=1") print('3. 清理后 e2c-* 活跃数:', cur.fetchone()['c'], '(应=seed 样例数)') conn.close() print('FINAL CLEAN DONE')