删除无用文件
This commit is contained in:
@@ -244,32 +244,101 @@ class WeChatMessageHandler:
|
||||
|
||||
try:
|
||||
from ..storage.postgresql import get_storage_manager
|
||||
storage = get_storage_manager()
|
||||
stats = storage.get_statistics()
|
||||
from ..core.database import get_db_cursor
|
||||
from datetime import date
|
||||
|
||||
if stats:
|
||||
response = f"""今日公告统计
|
||||
# 获取今日关键词命中公告数(从auto_announcements表)
|
||||
today = date.today()
|
||||
with get_db_cursor() as cursor:
|
||||
# 今日关键词命中总数
|
||||
cursor.execute("""
|
||||
SELECT COUNT(*) as today_keyword_hits
|
||||
FROM auto_announcements
|
||||
WHERE DATE(publish_date) = %s
|
||||
""", (today,))
|
||||
today_keyword_hits = cursor.fetchone()['today_keyword_hits']
|
||||
|
||||
# 各类型今日关键词命中数
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
announcement_type,
|
||||
COUNT(*) as count
|
||||
FROM auto_announcements
|
||||
WHERE DATE(publish_date) = %s
|
||||
GROUP BY announcement_type
|
||||
ORDER BY count DESC
|
||||
""", (today,))
|
||||
type_stats = {row['announcement_type']: row['count'] for row in cursor.fetchall()}
|
||||
|
||||
# 历史累计关键词命中数
|
||||
cursor.execute("""
|
||||
SELECT COUNT(*) as total_keyword_hits
|
||||
FROM auto_announcements
|
||||
""")
|
||||
total_keyword_hits = cursor.fetchone()['total_keyword_hits']
|
||||
|
||||
# 今日各来源关键词命中数
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
source_name,
|
||||
COUNT(*) as count
|
||||
FROM auto_announcements
|
||||
WHERE DATE(publish_date) = %s
|
||||
GROUP BY source_name
|
||||
ORDER BY count DESC
|
||||
LIMIT 5
|
||||
""", (today,))
|
||||
source_stats = cursor.fetchall()
|
||||
|
||||
# 类型名称映射
|
||||
type_name_map = {
|
||||
'purchase': '采购公告',
|
||||
'result': '结果公告',
|
||||
'correction': '更正公告',
|
||||
'contract': '合同公告',
|
||||
'pre_announcement': '预公示',
|
||||
'single_source': '单一来源',
|
||||
'electronic_market': '电子卖场',
|
||||
'acceptance': '履约验收',
|
||||
'engineering': '工程公告',
|
||||
'intention': '采购意向'
|
||||
}
|
||||
|
||||
if today_keyword_hits > 0:
|
||||
response = f"""📊 今日关键词命中统计
|
||||
|
||||
统计时间: {datetime.now().strftime('%Y-%m-%d %H:%M')}
|
||||
|
||||
数据概览:
|
||||
- 今日新增: {stats.get('today_count', 0)} 条
|
||||
- 累计总数: {stats.get('total_count', 0)} 条
|
||||
- 活跃关键词: {stats.get('active_keywords', 0)} 个
|
||||
🎯 今日关键词命中: {today_keyword_hits} 条
|
||||
📈 历史累计命中: {total_keyword_hits} 条
|
||||
|
||||
分类统计:
|
||||
- 采购公告: {stats.get('purchase_count', 0)} 条
|
||||
- 结果公告: {stats.get('result_count', 0)} 条
|
||||
- 更正公告: {stats.get('correction_count', 0)} 条
|
||||
- 其他类型: {stats.get('other_count', 0)} 条
|
||||
📋 今日命中分类:
|
||||
"""
|
||||
|
||||
提示: 数据每小时更新,点击"立即搜索"可获取最新数据."""
|
||||
# 添加各类型统计
|
||||
for ann_type, count in type_stats.items():
|
||||
type_name = type_name_map.get(ann_type, ann_type)
|
||||
response += f"- {type_name}: {count} 条\n"
|
||||
|
||||
response += "\n🏢 今日命中来源TOP5:\n"
|
||||
for i, source in enumerate(source_stats, 1):
|
||||
response += f"{i}. {source['source_name']}: {source['count']} 条\n"
|
||||
|
||||
response += "\n💡 提示: 这些是关键词自动匹配成功的公告"
|
||||
else:
|
||||
response = """今日公告统计
|
||||
response = f"""📊 今日关键词命中统计
|
||||
|
||||
暂无统计数据,请先执行"立即搜索"获取最新公告。
|
||||
统计时间: {datetime.now().strftime('%Y-%m-%d %H:%M')}
|
||||
|
||||
建议: 系统会在每日定时搜索,您也可以手动触发更新."""
|
||||
🎯 今日关键词命中: 0 条
|
||||
📈 历史累计命中: {total_keyword_hits} 条
|
||||
|
||||
暂无今日关键词命中公告。
|
||||
|
||||
💡 可能原因:
|
||||
- 今日暂无匹配关键词的公告发布
|
||||
- 系统定时搜索还未执行
|
||||
- 点击"立即搜索"可手动触发更新"""
|
||||
|
||||
except Exception as db_error:
|
||||
logger.warning(f"数据库查询失败,使用默认统计: {str(db_error)}")
|
||||
@@ -347,22 +416,22 @@ class WeChatMessageHandler:
|
||||
return self._create_text_response("获取最新公告失败,请稍后重试", from_user)
|
||||
|
||||
def _handle_latest_news_by_source(self, source_choice: str, from_user: str) -> Optional[str]:
|
||||
"""处理按来源查看最新公告"""
|
||||
"""处理按来源查看最新公告 - 直接从指定来源爬取最新的10条公告"""
|
||||
try:
|
||||
logger.info(f"用户 {from_user} 选择公告来源: {source_choice}")
|
||||
|
||||
# 来源映射
|
||||
# 来源映射 - 公告类型到来源代码的映射
|
||||
source_mapping = {
|
||||
"1": ("purchase", "采购公告"),
|
||||
"2": ("result", "结果公告"),
|
||||
"3": ("correction", "更正公告"),
|
||||
"4": ("contract", "合同公告"),
|
||||
"5": ("pre_announcement", "预公示"),
|
||||
"6": ("single_source", "单一来源"),
|
||||
"7": ("electronic_market", "电子卖场"),
|
||||
"8": ("acceptance", "履约验收"),
|
||||
"9": ("engineering", "工程公告"),
|
||||
"10": ("intention", "采购意向")
|
||||
"1": ("ZcyAnnouncement1", "采购公告"),
|
||||
"2": ("ZcyAnnouncement2", "结果公告"),
|
||||
"3": ("ZcyAnnouncement4", "更正公告"),
|
||||
"4": ("ZcyAnnouncement3", "合同公告"),
|
||||
"5": ("ZcyAnnouncement5", "预公示"),
|
||||
"6": ("ZcyAnnouncement6", "单一来源"),
|
||||
"7": ("ZcyAnnouncement7", "电子卖场"),
|
||||
"8": ("ZcyAnnouncement10", "履约验收"),
|
||||
"9": ("ZcyAnnouncement11", "工程公告"),
|
||||
"10": ("61-266648", "采购意向")
|
||||
}
|
||||
|
||||
if source_choice == "全部" or source_choice == "all":
|
||||
@@ -377,48 +446,91 @@ class WeChatMessageHandler:
|
||||
返回公告查询菜单,请点击"最新公告"重新选择。"""
|
||||
return self._create_text_response(response, from_user)
|
||||
|
||||
ann_type, type_name = source_mapping[source_choice]
|
||||
source_code, type_name = source_mapping[source_choice]
|
||||
|
||||
# 查询该类型的最新公告
|
||||
# 直接从指定来源爬取最新公告
|
||||
try:
|
||||
from ..storage.postgresql import get_storage_manager
|
||||
storage = get_storage_manager()
|
||||
# 获取所有最近公告,然后过滤类型
|
||||
all_announcements = storage.get_recent_announcements(hours=168, limit=100) # 最近7天
|
||||
filtered_announcements = [ann for ann in all_announcements if ann.announcement_type.value == ann_type][:10]
|
||||
from ..crawler.spider import crawl_announcements
|
||||
from ..filters.filters import DateFilter
|
||||
from datetime import date
|
||||
|
||||
if filtered_announcements:
|
||||
response = f"""📋 {type_name} - 最新10条
|
||||
# 只爬取指定来源的公告
|
||||
logger.info(f"开始爬取 {type_name} 来源的公告")
|
||||
crawl_results = crawl_announcements(sources=[source_code])
|
||||
|
||||
🕒 更新时间:{datetime.now().strftime('%Y-%m-%d %H:%M')}
|
||||
if not crawl_results or not crawl_results[0].announcements:
|
||||
response = f"❌ 暂无 {type_name} 相关公告"
|
||||
return self._create_text_response(response, from_user)
|
||||
|
||||
# 获取该来源的所有公告
|
||||
source_announcements = crawl_results[0].announcements
|
||||
|
||||
# 按日期筛选(今天的数据)
|
||||
date_filter = DateFilter()
|
||||
today_announcements = date_filter.filter_announcements(
|
||||
source_announcements,
|
||||
start_date=date.today(),
|
||||
end_date=date.today()
|
||||
)
|
||||
|
||||
# 按发布时间排序,取最新的10条
|
||||
sorted_announcements = sorted(
|
||||
today_announcements,
|
||||
key=lambda x: x.publish_date or x.crawled_at,
|
||||
reverse=True
|
||||
)[:10]
|
||||
|
||||
if not sorted_announcements:
|
||||
response = f"❌ 今天暂无 {type_name} 相关公告"
|
||||
return self._create_text_response(response, from_user)
|
||||
|
||||
# 生成Markdown格式的结果并发送
|
||||
markdown_content = self._generate_latest_news_markdown(sorted_announcements, type_name)
|
||||
|
||||
try:
|
||||
from ..notification.wechat import get_notification_manager
|
||||
manager = get_notification_manager()
|
||||
if hasattr(manager.wechat, 'send_system_notification'):
|
||||
notify_success = manager.wechat.send_system_notification(
|
||||
title=f"📋 {type_name} - 最新公告",
|
||||
content=markdown_content,
|
||||
message_type="markdown"
|
||||
)
|
||||
if notify_success:
|
||||
# 返回简短确认
|
||||
return self._create_text_response(f"✅ 已发送 {type_name} 最新5条公告到聊天窗口。", from_user)
|
||||
else:
|
||||
# 如果Markdown发送失败,返回文本格式
|
||||
return self._create_text_response(f"发送失败,已获取 {len(sorted_announcements)} 条 {type_name} 公告。", from_user)
|
||||
else:
|
||||
# 如果不支持markdown,返回文本格式
|
||||
response = f"""📋 {type_name} - 最新公告
|
||||
|
||||
共找到 {len(sorted_announcements)} 条公告:
|
||||
|
||||
"""
|
||||
for i, announcement in enumerate(sorted_announcements[:5], 1): # 只显示前5条
|
||||
title = announcement.title[:25] + "..." if len(announcement.title) > 25 else announcement.title
|
||||
time_str = announcement.publish_date.strftime('%m-%d %H:%M') if announcement.publish_date else "未知"
|
||||
response += f"{i}. {title}\n 🕒 {time_str}\n"
|
||||
|
||||
for i, announcement in enumerate(filtered_announcements, 1):
|
||||
title = announcement.title[:25] + "..." if len(announcement.title) > 25 else announcement.title
|
||||
time_str = announcement.publish_date.strftime('%m-%d %H:%M') if announcement.publish_date else "未知"
|
||||
response += f"{i}. {title}\n 🕒 {time_str} | 🏷️ {announcement.purchase_name or '未知'}\n\n"
|
||||
if len(sorted_announcements) > 5:
|
||||
response += f"\n... 还有 {len(sorted_announcements) - 5} 条公告"
|
||||
|
||||
response += "💡 发送关键词可进一步筛选,点击菜单可查看更多功能。"
|
||||
else:
|
||||
response = f"""📋 {type_name}
|
||||
return self._create_text_response(response, from_user)
|
||||
|
||||
暂无该类型的最新公告数据。
|
||||
except Exception as notify_error:
|
||||
logger.warning(f"发送Markdown通知失败: {str(notify_error)}")
|
||||
# 返回文本格式的结果
|
||||
response = f"""📋 {type_name} - 最新公告
|
||||
|
||||
💡 建议:
|
||||
• 点击"立即搜索"更新数据
|
||||
• 该类型公告可能较少出现
|
||||
• 返回重新选择其他类型"""
|
||||
共找到 {len(sorted_announcements)} 条公告,请查看详细结果。"""
|
||||
return self._create_text_response(response, from_user)
|
||||
|
||||
except Exception as db_error:
|
||||
logger.warning(f"数据库查询失败: {str(db_error)}")
|
||||
response = f"""📋 {type_name}
|
||||
|
||||
暂时无法获取数据,请稍后重试。
|
||||
|
||||
您可以返回重新选择其他类型。"""
|
||||
|
||||
return self._create_text_response(response, from_user)
|
||||
except Exception as e:
|
||||
logger.error(f"爬取公告失败: {str(e)}")
|
||||
response = f"❌ 获取 {type_name} 公告失败,请稍后重试"
|
||||
return self._create_text_response(response, from_user)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"按来源查看最新公告异常: {str(e)}")
|
||||
@@ -1005,6 +1117,34 @@ class WeChatMessageHandler:
|
||||
|
||||
return "".join(lines)
|
||||
|
||||
def _generate_latest_news_markdown(self, announcements: List, source_type_name: str) -> str:
|
||||
"""生成最新公告的markdown格式"""
|
||||
import datetime
|
||||
|
||||
# 只显示最新的5条公告
|
||||
display_announcements = announcements[:5]
|
||||
|
||||
# 生成markdown内容
|
||||
lines = [
|
||||
f"总公告数: {len(announcements)}\n\n",
|
||||
f"更新时间: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
|
||||
]
|
||||
|
||||
# 逐条列出公告(最多5条)
|
||||
for i, ann in enumerate(display_announcements, 1):
|
||||
title = ann.title
|
||||
if len(title) > 50:
|
||||
title = title[:50] + "..."
|
||||
|
||||
url = ann.content_url or "#"
|
||||
date_str = ann.publish_date.strftime('%Y-%m-%d') if ann.publish_date else "未知"
|
||||
purchaser = ann.purchase_name or "未知"
|
||||
|
||||
lines.append(f"{i}. [{title}]({url})\n\n")
|
||||
lines.append(f" {date_str} | {purchaser}\n\n")
|
||||
|
||||
return "".join(lines)
|
||||
|
||||
def handle_other_message(self, msg_type: str, from_user: str) -> Optional[str]:
|
||||
"""处理其他类型的消息"""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user