2026-11-03 11:37:28 +08:00
|
|
|
|
"""用户补充的候选来源。与计划共用文件锁,先保存,再由修订任务发布。"""
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
import datetime as dt
|
|
|
|
|
|
import json
|
|
|
|
|
|
import re
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
from zoneinfo import ZoneInfo
|
|
|
|
|
|
|
|
|
|
|
|
import plan_versions as pv
|
|
|
|
|
|
|
|
|
|
|
|
BJ = ZoneInfo('Asia/Shanghai')
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-03 12:09:28 +08:00
|
|
|
|
def consumer_view(published, include_user_candidates=False):
|
|
|
|
|
|
"""旧调用方只取系统来源;显式声明支持新来源后才返回人工候选。"""
|
|
|
|
|
|
if include_user_candidates or not published.get('user_candidates'):
|
|
|
|
|
|
return published
|
|
|
|
|
|
out = dict(published)
|
|
|
|
|
|
out.pop('user_candidates', None)
|
|
|
|
|
|
for bucket in ('main', 'observe', 'main_shown', 'observe_shown', 'candidates', 'watch'):
|
|
|
|
|
|
if bucket in out:
|
|
|
|
|
|
out[bucket] = [{k: v for k, v in r.items() if k not in ('user_candidate', 'user_required')}
|
|
|
|
|
|
for r in out[bucket] if not r.get('user_required')]
|
|
|
|
|
|
if 'counts' in out:
|
|
|
|
|
|
out['counts'] = dict(out['counts'], main=len(out.get('main', [])), observe=len(out.get('observe', [])))
|
|
|
|
|
|
if 'card_counts' in out:
|
|
|
|
|
|
rows = out.get('main', []) + out.get('observe', [])
|
|
|
|
|
|
out['card_counts'] = {v: sum(r.get('verdict') == v for r in rows) for v in out['card_counts']}
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-03 11:37:28 +08:00
|
|
|
|
def normalize(value):
|
|
|
|
|
|
value = str(value or '').strip().upper()
|
|
|
|
|
|
if re.fullmatch(r'\d{6}\.(SH|SZ)', value):
|
|
|
|
|
|
value = value[-2:] + value[:6]
|
|
|
|
|
|
elif re.fullmatch(r'\d{6}', value):
|
|
|
|
|
|
value = ('SH' if value.startswith(('60', '68')) else 'SZ') + value
|
|
|
|
|
|
if not re.fullmatch(r'(SH(?:60|68)\d{4}|SZ(?:00|30)\d{4})', value):
|
|
|
|
|
|
raise ValueError('仅支持沪深 A 股,请检查股票代码')
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-03 12:33:10 +08:00
|
|
|
|
def catalog():
|
|
|
|
|
|
"""使用上市证券基础表,不能把部分行业池当作完整可搜索名单。"""
|
|
|
|
|
|
import db
|
|
|
|
|
|
frame = db.read_mysql('heat',
|
|
|
|
|
|
"SELECT gp_code_two,gp_name FROM gp_code_all WHERE list_status='L' "
|
|
|
|
|
|
"AND (gp_code_two LIKE '%.SH' OR gp_code_two LIKE '%.SZ')")
|
|
|
|
|
|
out = {}
|
|
|
|
|
|
for row in frame.to_dict('records'):
|
|
|
|
|
|
try:
|
|
|
|
|
|
code = normalize(row.get('gp_code_two'))
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
continue
|
|
|
|
|
|
name = str(row.get('gp_name') or '').strip()
|
|
|
|
|
|
if name:
|
|
|
|
|
|
out[code] = name
|
|
|
|
|
|
if not out:
|
|
|
|
|
|
raise ValueError('证券名称资料暂不可用,请稍后重试')
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def resolve(raw, names=None):
|
|
|
|
|
|
"""批量名称只按完整简称精确匹配;全部解析成功后才允许保存。"""
|
|
|
|
|
|
if isinstance(raw, str):
|
|
|
|
|
|
raw = [raw]
|
|
|
|
|
|
if not isinstance(raw, list) or any(not isinstance(v, str) for v in raw):
|
|
|
|
|
|
raise ValueError('请输入股票名称或代码')
|
|
|
|
|
|
tokens = [v for item in raw for v in re.split(r'[\s,,;;、]+', item.strip()) if v]
|
|
|
|
|
|
if not 1 <= len(tokens) <= 30:
|
|
|
|
|
|
raise ValueError('每次可添加 1 至 30 只股票')
|
|
|
|
|
|
index = None
|
|
|
|
|
|
codes = []
|
|
|
|
|
|
for value in tokens:
|
|
|
|
|
|
try:
|
|
|
|
|
|
code = normalize(value)
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
# 明确的证券代码继续走原交易所限制,不尝试按名称解释。
|
|
|
|
|
|
if re.fullmatch(r'(?:(?:SH|SZ|BJ)?\d+(?:\.(?:SH|SZ|BJ))?)', value.upper()):
|
|
|
|
|
|
raise
|
|
|
|
|
|
if index is None:
|
|
|
|
|
|
source = names() if callable(names) else names or {}
|
|
|
|
|
|
index = {}
|
|
|
|
|
|
for candidate, name in source.items():
|
|
|
|
|
|
try:
|
|
|
|
|
|
canonical = normalize(candidate)
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
continue
|
|
|
|
|
|
index.setdefault(str(name).strip().upper(), set()).add(canonical)
|
|
|
|
|
|
matches = index.get(value.upper(), set())
|
|
|
|
|
|
if not matches:
|
|
|
|
|
|
raise ValueError(f'未找到沪深 A 股“{value[:40]}”,请检查完整名称或使用代码') from None
|
|
|
|
|
|
if len(matches) != 1:
|
|
|
|
|
|
raise ValueError(f'“{value[:40]}”对应多只股票,请使用代码') from None
|
|
|
|
|
|
code = next(iter(matches))
|
|
|
|
|
|
if code not in codes:
|
|
|
|
|
|
codes.append(code)
|
|
|
|
|
|
return codes
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-03 11:37:28 +08:00
|
|
|
|
def read(root):
|
|
|
|
|
|
path = Path(root) / 'user_candidates.json'
|
|
|
|
|
|
if not path.exists():
|
|
|
|
|
|
return {'revision': 0, 'rows': []}
|
|
|
|
|
|
state = json.loads(path.read_text())
|
|
|
|
|
|
if not isinstance(state.get('rows'), list) or not isinstance(state.get('revision'), int):
|
|
|
|
|
|
raise ValueError('添加记录暂不可用')
|
|
|
|
|
|
return state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def active(root, now=None):
|
|
|
|
|
|
today = (now or dt.datetime.now(BJ)).astimezone(BJ).date().isoformat()
|
|
|
|
|
|
return [dict(r) for r in read(root)['rows']
|
|
|
|
|
|
if not r.get('removed_at') and r['expires_on'] >= today]
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-03 12:33:10 +08:00
|
|
|
|
def save(root, payload, actor, now=None, *, names=None):
|
2026-11-03 11:37:28 +08:00
|
|
|
|
now = now or dt.datetime.now(BJ)
|
2026-11-03 12:33:10 +08:00
|
|
|
|
codes = resolve(payload.get('codes'), names)
|
2026-11-03 11:37:28 +08:00
|
|
|
|
reason = str(payload.get('reason') or '').strip()
|
|
|
|
|
|
if not reason or len(reason) > 300:
|
|
|
|
|
|
raise ValueError('请填写添加理由,最多 300 字')
|
|
|
|
|
|
try:
|
|
|
|
|
|
expires = dt.date.fromisoformat(str(payload.get('expires_on') or ''))
|
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
|
raise ValueError('请选择关注截止日期') from exc
|
|
|
|
|
|
if not now.astimezone(BJ).date() <= expires <= now.astimezone(BJ).date() + dt.timedelta(days=90):
|
|
|
|
|
|
raise ValueError('关注期限应在今天至未来 90 天内')
|
|
|
|
|
|
with pv.writer(root):
|
|
|
|
|
|
state = read(root)
|
|
|
|
|
|
current = {r['code'] for r in active(root, now)}
|
|
|
|
|
|
added = []
|
|
|
|
|
|
for code in codes:
|
|
|
|
|
|
if code in current:
|
|
|
|
|
|
continue
|
|
|
|
|
|
row = {'id': uuid.uuid4().hex, 'code': code, 'reason': reason,
|
|
|
|
|
|
'expires_on': expires.isoformat(), 'created_at': now.isoformat(),
|
|
|
|
|
|
'created_by': str(actor)[:100]}
|
2026-11-03 12:33:10 +08:00
|
|
|
|
if isinstance(names, dict) and names.get(code):
|
|
|
|
|
|
row['name'] = names[code]
|
2026-11-03 11:37:28 +08:00
|
|
|
|
state['rows'].append(row)
|
|
|
|
|
|
added.append(row)
|
|
|
|
|
|
if added:
|
|
|
|
|
|
state['revision'] += 1
|
|
|
|
|
|
pv.atomic(Path(root) / 'user_candidates.json', state)
|
|
|
|
|
|
mark_pending(root, [r['code'] for r in added])
|
|
|
|
|
|
return {'ok': True, 'added': len(added), 'existing': len(codes) - len(added),
|
|
|
|
|
|
'revision': state['revision']}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove(root, identity, actor, now=None):
|
|
|
|
|
|
now = now or dt.datetime.now(BJ)
|
|
|
|
|
|
with pv.writer(root):
|
|
|
|
|
|
state = read(root)
|
|
|
|
|
|
row = next((r for r in state['rows'] if r['id'] == identity), None)
|
|
|
|
|
|
if row is None:
|
|
|
|
|
|
raise ValueError('添加记录不存在,请刷新后重试')
|
|
|
|
|
|
if not row.get('removed_at'):
|
|
|
|
|
|
row.update(removed_at=now.isoformat(), removed_by=str(actor)[:100])
|
|
|
|
|
|
state['revision'] += 1
|
|
|
|
|
|
pv.atomic(Path(root) / 'user_candidates.json', state)
|
|
|
|
|
|
mark_pending(root, [row['code']])
|
|
|
|
|
|
return {'ok': True, 'revision': state['revision']}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def mark_pending(root, codes):
|
|
|
|
|
|
day = pv.latest_date(root)
|
|
|
|
|
|
if day:
|
|
|
|
|
|
state = pv.health(root, day)
|
|
|
|
|
|
pv.health(root, day, pending_codes=sorted(set(state.get('pending_codes') or []) | set(codes)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def live_pending(root, published, now=None):
|
|
|
|
|
|
"""写后未发布、删除或到期时,即使后台失败也不能继续消费旧人工来源。"""
|
|
|
|
|
|
current = {r['code']: r['id'] for r in active(root, now)}
|
|
|
|
|
|
frozen = {r['code']: r['id'] for r in published.get('user_candidates', [])}
|
|
|
|
|
|
return sorted(c for c in current.keys() | frozen.keys() if current.get(c) != frozen.get(c))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def listing(root, now=None):
|
|
|
|
|
|
now = now or dt.datetime.now(BJ)
|
|
|
|
|
|
state = read(root)
|
|
|
|
|
|
try:
|
2026-11-03 12:33:10 +08:00
|
|
|
|
published = pv.load(root) if pv.latest_date(root) else {}
|
2026-11-03 11:37:28 +08:00
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
|
published = {}
|
|
|
|
|
|
material = {r['code']: r for b in ('main', 'observe') for r in published.get(b, [])}
|
|
|
|
|
|
expected = {r['id'] for r in published.get('user_candidates', [])}
|
|
|
|
|
|
today = now.astimezone(BJ).date().isoformat()
|
2026-11-04 10:51:53 +08:00
|
|
|
|
import user_research
|
|
|
|
|
|
research = user_research.views(root, [r['code'] for r in state['rows'][-300:]], now=now)
|
2026-11-03 11:37:28 +08:00
|
|
|
|
rows = []
|
|
|
|
|
|
for source in reversed(state['rows']):
|
|
|
|
|
|
row = material.get(source['code']) or {}
|
|
|
|
|
|
status = ('removed' if source.get('removed_at') else
|
|
|
|
|
|
'expired' if source['expires_on'] < today else
|
|
|
|
|
|
'pending' if source['id'] not in expected else 'evaluated')
|
2026-11-03 12:33:10 +08:00
|
|
|
|
rows.append({**source, 'status': status, 'name': row.get('name') or source.get('name'),
|
2026-11-03 11:37:28 +08:00
|
|
|
|
'verdict': row.get('verdict'), 'basis': row.get('basis'),
|
2026-11-04 10:51:53 +08:00
|
|
|
|
'decision_id': (published.get('decision') or {}).get('id'),
|
|
|
|
|
|
**research.get(source['code'], {})})
|
2026-11-03 11:37:28 +08:00
|
|
|
|
return {'ok': True, 'rows': rows[:300], 'revision': state['revision']}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def refresh_saved():
|
|
|
|
|
|
"""请求结束后尝试发布;失败保留登记和待核验状态,定时修订会重试。"""
|
|
|
|
|
|
import logging
|
|
|
|
|
|
import plan_refresh
|
2026-11-04 10:51:53 +08:00
|
|
|
|
import user_research
|
|
|
|
|
|
try:
|
|
|
|
|
|
user_research.sync()
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
logging.getLogger(__name__).exception('人工关注评析状态同步失败,保留登记等待自然调度')
|
2026-11-03 11:37:28 +08:00
|
|
|
|
try:
|
|
|
|
|
|
return plan_refresh.refresh()
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
logging.getLogger(__name__).exception('用户候选已保存,计划修订待重试')
|
|
|
|
|
|
return {'status': 'pending'}
|