diff --git a/app.py b/app.py index 89709b0b..68e5ddf8 100755 --- a/app.py +++ b/app.py @@ -6278,7 +6278,7 @@ class Event(db.Model): follower_count = db.Column(db.Integer, default=0) # 关联信息 - related_industries = db.Column(db.JSON) + related_industries = db.Column(db.String(20)) # 申万行业代码,如 "S640701" keywords = db.Column(db.JSON) files = db.Column(db.JSON) importance = db.Column(db.String(20)) @@ -10271,10 +10271,8 @@ def api_get_events(): # 新增:行业代码过滤(申银万国行业分类)- 支持前缀匹配 # 申万行业分类层级:一级 Sxx, 二级 Sxxxx, 三级 Sxxxxxx # 搜索 S22 会匹配所有 S22xxxx 的事件(如 S2203, S220309 等) + # related_industries 格式: varchar,如 "S640701" if industry_code: - # related_industries 格式: [{"申银万国行业分类": "S370502"}, ...] - # 支持多个行业代码,用逗号分隔 - # 判断是否需要前缀匹配:一级(3字符)或二级(5字符)行业代码 def is_prefix_code(code): """判断是否为需要前缀匹配的行业代码(一级或二级)""" @@ -10291,48 +10289,20 @@ def api_get_events(): conditions = [] for code in codes: if is_prefix_code(code): - # 前缀匹配:使用 JSON_TABLE + LIKE - # 这个子查询找到所有 related_industries 中申万行业代码以指定前缀开头的事件ID - prefix_sql = text(""" - id IN ( - SELECT DISTINCT e2.id FROM event e2 - CROSS JOIN JSON_TABLE( - e2.related_industries, - '$[*]' COLUMNS (sw_code VARCHAR(20) PATH '$."申银万国行业分类"') - ) AS jt - WHERE jt.sw_code LIKE :prefix - ) - """).bindparams(prefix=f"{code}%") - conditions.append(prefix_sql) + # 前缀匹配:使用 LIKE + conditions.append(Event.related_industries.like(f"{code}%")) else: # 精确匹配(三级行业代码) - json_path = '$[*]."申银万国行业分类"' - conditions.append( - text("JSON_CONTAINS(JSON_EXTRACT(related_industries, :json_path), :code)") - .bindparams(json_path=json_path, code=json.dumps(code)) - ) + conditions.append(Event.related_industries == code) query = query.filter(db.or_(*conditions)) else: # 单个行业代码 if is_prefix_code(industry_code): - # 前缀匹配:使用 JSON_TABLE + LIKE - prefix_sql = text(""" - id IN ( - SELECT DISTINCT e2.id FROM event e2 - CROSS JOIN JSON_TABLE( - e2.related_industries, - '$[*]' COLUMNS (sw_code VARCHAR(20) PATH '$."申银万国行业分类"') - ) AS jt - WHERE jt.sw_code LIKE :prefix - ) - """).bindparams(prefix=f"{industry_code}%") - query = query.filter(prefix_sql) + # 前缀匹配:使用 LIKE + query = query.filter(Event.related_industries.like(f"{industry_code}%")) else: # 精确匹配(三级行业代码) - 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)) + query = query.filter(Event.related_industries == industry_code) # 新增:关键词/全文搜索过滤(MySQL JSON) if search_query: like_pattern = f"%{search_query}%" diff --git a/app_vx.py b/app_vx.py index 575e706d..20a2c2f0 100644 --- a/app_vx.py +++ b/app_vx.py @@ -1204,7 +1204,7 @@ class Event(db.Model): follower_count = db.Column(db.Integer, default=0) # 关联信息 - related_industries = db.Column(db.JSON) + related_industries = db.Column(db.String(20)) # 申万行业代码,如 "S640701" keywords = db.Column(db.JSON) files = db.Column(db.JSON) importance = db.Column(db.String(20)) @@ -2962,37 +2962,14 @@ def api_get_events(): code_prefixes = SYWG_INDUSTRY_CACHE[industry_level].get(industry_classification, []) if code_prefixes: - # 构建查询条件:查找related_industries中包含这些前缀的事件 - if isinstance(db.engine.dialect, MySQLDialect): - # MySQL JSON查询 - conditions = [] - for prefix in code_prefixes: - conditions.append( - text(""" - JSON_SEARCH( - related_industries, - 'one', - CONCAT(:prefix, '%'), - NULL, - '$[*]."申银万国行业分类"' - ) IS NOT NULL - """).params(prefix=prefix) - ) + # 构建查询条件:查找related_industries以这些前缀开头的事件 + # related_industries 现在是 varchar 格式,如 "S640701" + conditions = [] + for prefix in code_prefixes: + conditions.append(Event.related_industries.like(f"{prefix}%")) - if conditions: - query = query.filter(or_(*conditions)) - else: - # 其他数据库 - pattern_conditions = [] - for prefix in code_prefixes: - pattern_conditions.append( - text("related_industries::text LIKE :pattern").params( - pattern=f'%"申银万国行业分类": "{prefix}%' - ) - ) - - if pattern_conditions: - query = query.filter(or_(*pattern_conditions)) + if conditions: + query = query.filter(or_(*conditions)) else: # 没有找到匹配的行业代码,返回空结果 query = query.filter(Event.id == -1) @@ -3019,25 +2996,8 @@ def api_get_events(): if sector_result and sector_result[0]: industry_code_to_search = sector_result[0] - # 在related_industries JSON中查找包含该代码的事件 - if isinstance(db.engine.dialect, MySQLDialect): - query = query.filter( - text(""" - JSON_SEARCH( - related_industries, - 'one', - :industry_code, - NULL, - '$[*]."申银万国行业分类"' - ) IS NOT NULL - """) - ).params(industry_code=industry_code_to_search) - else: - query = query.filter( - text(""" - related_industries::text LIKE :pattern - """) - ).params(pattern=f'%"申银万国行业分类": "{industry_code_to_search}"%') + # related_industries 现在是 varchar 格式,直接匹配 + query = query.filter(Event.related_industries == industry_code_to_search) else: # 如果没有找到对应的行业代码,返回空结果 query = query.filter(Event.id == -1) diff --git a/public/avatars/web.png b/public/avatars/web.png new file mode 100644 index 00000000..09c3d4be Binary files /dev/null and b/public/avatars/web.png differ