From 8dcf643db71859b13513dadbb76ed2e79a5cd192 Mon Sep 17 00:00:00 2001 From: zzlgreat Date: Fri, 24 Oct 2025 11:27:45 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=8F=96=E6=B6=88levels=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E9=99=90=E5=88=B6classifications=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BB=85=E4=B8=BA=E7=94=B3=E4=B8=87=E8=A1=8C=E4=B8=9A=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 296 ++++++++++++++++++++++----------------------------------- 1 file changed, 111 insertions(+), 185 deletions(-) diff --git a/app.py b/app.py index 73e2006e..71abe46d 100755 --- a/app.py +++ b/app.py @@ -6324,196 +6324,100 @@ def parse_json_field(field_value): # ==================== 行业API ==================== @app.route('/api/classifications', methods=['GET']) def get_classifications(): - """获取所有行业分类系统""" + """获取申银万国行业分类树形结构""" try: + # 查询申银万国行业分类的所有数据 sql = """ - SELECT DISTINCT f002v as classification_name + SELECT f003v as code, f004v as level1, f005v as level2, f006v as level3,f007v as level4 FROM ea_sector - WHERE f002v NOT IN ('指数成份股', '市场分类', '概念板块', '地区省市分类', '中上协行业分类') - ORDER BY classification_name \ + WHERE f002v = '申银万国行业分类' + AND f003v IS NOT NULL + AND f004v IS NOT NULL + ORDER BY f003v """ result = db.session.execute(text(sql)).all() - classifications = [{'name': row.classification_name} for row in result] + # 构建树形结构 + tree_dict = {} - return jsonify({ - 'success': True, - 'data': classifications - }) + for row in result: + code = row.code + level1 = row.level1 + level2 = row.level2 + level3 = row.level3 - except Exception as e: - return jsonify({ - 'success': False, - 'error': str(e) - }), 500 + # 跳过空数据 + if not level1: + continue - -@app.route('/api/levels', methods=['GET']) -def get_industry_levels(): - """获取行业层级数据""" - try: - classification = request.args.get('classification') - level = request.args.get('level', type=int) - level1_name = request.args.get('level1_name', '') - level2_name = request.args.get('level2_name', '') - level3_name = request.args.get('level3_name', '') - - if not classification or not level or level < 1 or level > 4: - return jsonify({ - 'success': False, - 'error': 'Invalid parameters' - }), 400 - - # 层级到字段的映射 - level_fields = { - 1: "f004v", - 2: "f005v", - 3: "f006v", - 4: "f007v" - } - - field_name = level_fields[level] - - # 构建查询 - if level == 1: - sql = f""" - SELECT DISTINCT {field_name} as name, - MIN(f003v) as code - FROM ea_sector - WHERE f002v = :classification - AND {field_name} IS NOT NULL - GROUP BY name - ORDER BY name - """ - params = {"classification": classification} - - elif level == 2: - sql = f""" - SELECT DISTINCT {field_name} as name, - MIN(f003v) as code - FROM ea_sector - WHERE f002v = :classification - AND f004v = :level1_name - AND {field_name} IS NOT NULL - GROUP BY name - ORDER BY name - """ - params = {"classification": classification, "level1_name": level1_name} - - elif level == 3: - sql = f""" - SELECT DISTINCT {field_name} as name, - MIN(f003v) as code - FROM ea_sector - WHERE f002v = :classification - AND f004v = :level1_name - AND f005v = :level2_name - AND {field_name} IS NOT NULL - GROUP BY name - ORDER BY name - """ - params = { - "classification": classification, - "level1_name": level1_name, - "level2_name": level2_name - } - - elif level == 4: - sql = f""" - SELECT DISTINCT f003v as code, - {field_name} as name - FROM ea_sector - WHERE f002v = :classification - AND f004v = :level1_name - AND f005v = :level2_name - AND f006v = :level3_name - AND {field_name} IS NOT NULL - ORDER BY name - """ - params = { - "classification": classification, - "level1_name": level1_name, - "level2_name": level2_name, - "level3_name": level3_name - } - - results = db.session.execute(text(sql), params).all() - - industries = [{"code": row.code, "name": row.name} for row in results if row.name] - - return jsonify({ - 'success': True, - 'data': industries - }) - - except Exception as e: - return jsonify({ - 'success': False, - 'error': str(e) - }), 500 - - -@app.route('/api/info', methods=['GET']) -def get_industry_info(): - """获取行业详细信息""" - try: - classification = request.args.get('classification') - code = request.args.get('code') - - if not classification or not code: - return jsonify({ - 'success': False, - 'error': 'Missing parameters' - }), 400 - - # 根据代码长度确定字段 - if len(code) >= 8: - field_name = "f007v" - elif len(code) >= 6: - field_name = "f006v" - elif len(code) >= 4: - field_name = "f005v" - else: - field_name = "f004v" - - sql = f""" - SELECT {field_name} as name, - f004v as level1_name, - f005v as level2_name, - f006v as level3_name, - f007v as level4_name - FROM ea_sector - WHERE f002v = :classification - AND f003v = :code - AND {field_name} IS NOT NULL - LIMIT 1 - """ - - result = db.session.execute(text(sql), { - "classification": classification, - "code": code - }).first() - - if not result: - return jsonify({ - 'success': False, - 'error': 'Industry not found' - }), 404 - - return jsonify({ - 'success': True, - 'data': { - 'name': result.name, - 'code': code, - 'classification': classification, - 'hierarchy': { - 'level1': result.level1_name, - 'level2': result.level2_name, - 'level3': result.level3_name, - 'level4': result.level4_name + # 第一层 + if level1 not in tree_dict: + # 获取第一层的code(取前3位或前缀) + level1_code = code[:3] if len(code) >= 3 else code + tree_dict[level1] = { + 'value': level1_code, + 'label': level1, + 'children_dict': {} } + + # 第二层 + if level2: + if level2 not in tree_dict[level1]['children_dict']: + # 获取第二层的code(取前6位) + level2_code = code[:6] if len(code) >= 6 else code + tree_dict[level1]['children_dict'][level2] = { + 'value': level2_code, + 'label': level2, + 'children_dict': {} + } + + # 第三层 + if level3: + if level3 not in tree_dict[level1]['children_dict'][level2]['children_dict']: + tree_dict[level1]['children_dict'][level2]['children_dict'][level3] = { + 'value': code, + 'label': level3 + } + + # 转换为最终格式 + result_list = [] + for level1_name, level1_data in tree_dict.items(): + level1_node = { + 'value': level1_data['value'], + 'label': level1_data['label'] } + + # 处理第二层 + if level1_data['children_dict']: + level1_children = [] + for level2_name, level2_data in level1_data['children_dict'].items(): + level2_node = { + 'value': level2_data['value'], + 'label': level2_data['label'] + } + + # 处理第三层 + if level2_data['children_dict']: + level2_children = [] + for level3_name, level3_data in level2_data['children_dict'].items(): + level2_children.append({ + 'value': level3_data['value'], + 'label': level3_data['label'] + }) + if level2_children: + level2_node['children'] = level2_children + + level1_children.append(level2_node) + + if level1_children: + level1_node['children'] = level1_children + + result_list.append(level1_node) + + return jsonify({ + 'success': True, + 'data': result_list }) except Exception as e: @@ -6523,6 +6427,29 @@ def get_industry_info(): }), 500 +@app.route('/api/stocklist', methods=['GET']) +def get_stock_list(): + """获取股票列表""" + try: + sql = """ + SELECT DISTINCT SECCODE as code, SECNAME as name + FROM ea_stocklist + ORDER BY SECCODE + """ + + result = db.session.execute(text(sql)).all() + + stocks = [{'code': row.code, 'name': row.name} for row in result] + + return jsonify(stocks) + + except Exception as e: + return jsonify({ + 'success': False, + 'error': str(e) + }), 500 + + @app.route('/api/events', methods=['GET'], strict_slashes=False) def api_get_events(): """ @@ -6544,10 +6471,8 @@ def api_get_events(): date_range = request.args.get('date_range') recent_days = request.args.get('recent_days', type=int) - # 行业筛选参数 - industry_classification = request.args.get('industry_classification') - industry_code = request.args.get('industry_code') - industry_level = request.args.get('industry_level', type=int) + # 行业筛选参数(只支持申银万国行业分类) + industry_code = request.args.get('industry_code') # 申万行业代码,如 "S370502" # 概念/标签筛选参数 tag = request.args.get('tag') @@ -6593,9 +6518,10 @@ def api_get_events(): query = query.filter_by(importance=importance) if creator_id: query = query.filter_by(creator_id=creator_id) - # 新增:行业代码过滤(MySQL JSON,对象数组模式) - if industry_classification and industry_code: - json_path = f'$[*]."{industry_classification}"' + # 新增:行业代码过滤(申银万国行业分类) + if industry_code: + # related_industries 格式: [{"申银万国行业分类": "S370502"}, ...] + json_path = '$[*]."申银万国行业分类"' query = query.filter( text("JSON_CONTAINS(JSON_EXTRACT(related_industries, :json_path), :industry_code)") ).params(json_path=json_path, industry_code=json.dumps(industry_code)) From 246adf4538f9c24d2717359196751583a1d835d1 Mon Sep 17 00:00:00 2001 From: zzlgreat Date: Fri, 24 Oct 2025 11:33:27 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=8F=96=E6=B6=88levels=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E9=99=90=E5=88=B6classifications=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BB=85=E4=B8=BA=E7=94=B3=E4=B8=87=E8=A1=8C=E4=B8=9A=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/app.py b/app.py index 71abe46d..b98b378d 100755 --- a/app.py +++ b/app.py @@ -6645,8 +6645,6 @@ def api_get_events(): applied_filters['start_date'] = start_date if end_date: applied_filters['end_date'] = end_date - if industry_classification: - applied_filters['industry_classification'] = industry_classification if industry_code: applied_filters['industry_code'] = industry_code if tag: From 62d6487cbb1505073441e5dc08d6717b3b806906 Mon Sep 17 00:00:00 2001 From: zzlgreat Date: Fri, 24 Oct 2025 11:47:48 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=8F=96=E6=B6=88levels=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E9=99=90=E5=88=B6classifications=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BB=85=E4=B8=BA=E7=94=B3=E4=B8=87=E8=A1=8C=E4=B8=9A=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 21 +- .../Community/components/EventFilters.js | 212 ++++++------------ 2 files changed, 90 insertions(+), 143 deletions(-) diff --git a/app.py b/app.py index b98b378d..b5b31b6b 100755 --- a/app.py +++ b/app.py @@ -6521,10 +6521,25 @@ def api_get_events(): # 新增:行业代码过滤(申银万国行业分类) if industry_code: # related_industries 格式: [{"申银万国行业分类": "S370502"}, ...] + # 支持多个行业代码,用逗号分隔 json_path = '$[*]."申银万国行业分类"' - query = query.filter( - text("JSON_CONTAINS(JSON_EXTRACT(related_industries, :json_path), :industry_code)") - ).params(json_path=json_path, industry_code=json.dumps(industry_code)) + + # 如果包含逗号,说明是多个行业代码 + if ',' in industry_code: + codes = [code.strip() for code in industry_code.split(',') if code.strip()] + # 使用 OR 条件匹配任意一个行业代码 + conditions = [] + for code in codes: + conditions.append( + text("JSON_CONTAINS(JSON_EXTRACT(related_industries, :json_path), :code)") + .bindparams(json_path=json_path, code=json.dumps(code)) + ) + query = query.filter(db.or_(*conditions)) + else: + # 单个行业代码 + query = query.filter( + text("JSON_CONTAINS(JSON_EXTRACT(related_industries, :json_path), :industry_code)") + ).params(json_path=json_path, industry_code=json.dumps(industry_code)) # 新增:关键词/全文搜索过滤(MySQL JSON) if search_query: like_pattern = f"%{search_query}%" diff --git a/src/views/Community/components/EventFilters.js b/src/views/Community/components/EventFilters.js index dd40adfc..76861277 100644 --- a/src/views/Community/components/EventFilters.js +++ b/src/views/Community/components/EventFilters.js @@ -1,6 +1,6 @@ // src/views/Community/components/EventFilters.js import React, { useState, useEffect } from 'react'; -import { Card, Row, Col, DatePicker, Button, Select, Form, Input } from 'antd'; +import { Card, Row, Col, DatePicker, Button, Select, Form, Input, Cascader } from 'antd'; import { FilterOutlined } from '@ant-design/icons'; import moment from 'moment'; import locale from 'antd/es/date-picker/locale/zh_CN'; @@ -12,13 +12,7 @@ const { Option } = Select; const EventFilters = ({ filters, onFilterChange, loading }) => { const [form] = Form.useForm(); - const [industryData, setIndustryData] = useState({ - classifications: [], - level1: [], - level2: [], - level3: [], - level4: [] - }); + const [industryOptions, setIndustryOptions] = useState([]); // 初始化表单值 useEffect(() => { @@ -26,43 +20,26 @@ const EventFilters = ({ filters, onFilterChange, loading }) => { date_range: filters.date_range ? filters.date_range.split(' 至 ').map(d => moment(d)) : null, sort: filters.sort, importance: filters.importance, - industry_classification: filters.industry_classification, - industry_code: filters.industry_code + industry_code: filters.industry_code ? filters.industry_code.split(',') : [] }; form.setFieldsValue(initialValues); }, [filters, form]); - // 加载行业分类数据 + // 加载申银万国行业分类树形数据 const loadIndustryClassifications = async () => { try { const response = await industryService.getClassifications(); - setIndustryData(prev => ({ ...prev, classifications: response.data })); - logger.debug('EventFilters', '行业分类加载成功', { - count: response.data?.length || 0 - }); + if (response.success && response.data) { + setIndustryOptions(response.data); + logger.debug('EventFilters', '申银万国行业分类加载成功', { + count: response.data?.length || 0 + }); + } } catch (error) { logger.error('EventFilters', 'loadIndustryClassifications', error); } }; - // 加载行业层级数据 - const loadIndustryLevels = async (level, params) => { - try { - const response = await industryService.getLevels(params); - setIndustryData(prev => ({ ...prev, [`level${level}`]: response.data })); - // 清空下级 - for (let l = level + 1; l <= 4; l++) { - setIndustryData(prev => ({ ...prev, [`level${l}`]: [] })); - } - logger.debug('EventFilters', '行业层级数据加载成功', { - level, - count: response.data?.length || 0 - }); - } catch (error) { - logger.error('EventFilters', 'loadIndustryLevels', error, { level, params }); - } - }; - useEffect(() => { loadIndustryClassifications(); }, []); @@ -84,54 +61,61 @@ const EventFilters = ({ filters, onFilterChange, loading }) => { onFilterChange('importance', value); }; - // 行业分类体系变化时,加载一级行业 - const handleIndustryClassificationChange = (value) => { - form.setFieldsValue({ industry_code: '' }); - onFilterChange('industry_classification', value); - setIndustryData(prev => ({ ...prev, level1: [], level2: [], level3: [], level4: [] })); - if (value) { - loadIndustryLevels(1, { classification: value, level: 1 }); + // 收集所有叶子节点的 value(递归) + const collectLeafValues = (node) => { + // 如果没有子节点,说明是叶子节点 + if (!node.children || node.children.length === 0) { + return [node.value]; } + + // 有子节点,递归收集所有子节点的叶子节点 + let leafValues = []; + node.children.forEach(child => { + leafValues = leafValues.concat(collectLeafValues(child)); + }); + return leafValues; }; - // 级联选择行业 - const handleLevelChange = (level, value) => { - // 直接从state里查找name - let name = ''; - if (level === 1) { - const found = industryData.level1.find(item => item.code === value); - name = found ? found.name : ''; - } else if (level === 2) { - const found = industryData.level2.find(item => item.code === value); - name = found ? found.name : ''; - } else if (level === 3) { - const found = industryData.level3.find(item => item.code === value); - name = found ? found.name : ''; - } else if (level === 4) { - const found = industryData.level4.find(item => item.code === value); - name = found ? found.name : ''; + // 根据级联路径找到对应的节点 + const findNodeByPath = (options, path) => { + let current = options; + let node = null; + + for (let i = 0; i < path.length; i++) { + node = current.find(item => item.value === path[i]); + if (!node) return null; + if (i < path.length - 1) { + current = node.children || []; + } } - form.setFieldsValue({ [`level${level}`]: value }); - form.setFieldsValue({ industry_code: value }); - onFilterChange('industry_code', value); - for (let l = level + 1; l <= 4; l++) { - form.setFieldsValue({ [`level${l}`]: undefined }); + return node; + }; + + // 行业级联选择变化 + const handleIndustryChange = (value, selectedOptions) => { + if (!value || value.length === 0) { + onFilterChange('industry_code', ''); + return; } - const params = { classification: form.getFieldValue('industry_classification'), level: level + 1 }; - if (level === 1) params.level1_name = name; - if (level === 2) { - params.level1_name = form.getFieldValue('level1_name'); - params.level2_name = name; + + // 获取选中的节点 + const selectedNode = findNodeByPath(industryOptions, value); + + if (!selectedNode) { + // 如果找不到节点,使用最后一个值 + onFilterChange('industry_code', value[value.length - 1]); + return; } - if (level === 3) { - params.level1_name = form.getFieldValue('level1_name'); - params.level2_name = form.getFieldValue('level2_name'); - params.level3_name = name; + + // 如果选中的节点有子节点,收集所有叶子节点的 value + // 这样可以匹配该级别下的所有事件 + if (selectedNode.children && selectedNode.children.length > 0) { + const leafValues = collectLeafValues(selectedNode); + onFilterChange('industry_code', leafValues.join(',')); + } else { + // 叶子节点,直接使用该 value + onFilterChange('industry_code', selectedNode.value); } - if (level < 4 && name) { - loadIndustryLevels(level + 1, params); - } - form.setFieldsValue({ [`level${level}_name`]: name }); }; return ( @@ -177,74 +161,22 @@ const EventFilters = ({ filters, onFilterChange, loading }) => { - - - - - - - - - - - - - - - - - - - - - - - + style={{ width: '100%' }} + displayRender={(labels) => labels.join(' / ')} + />