G41_TAPD_BUG_SYNC/src3/message_formatter.py

73 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""消息格式化器"""
from typing import List, Dict
from datetime import datetime
class MessageFormatter:
"""消息格式化器"""
def format_message(self, items: List[dict], user_mapping: Dict[str, str], date: str = None) -> tuple:
"""格式化消息
返回: (markdown_content, mentioned_list)
"""
if not items:
return None, []
if date is None:
date = datetime.now().strftime('%Y-%m-%d')
# 按处理人分组
grouped = self._group_by_owner(items)
# 排序
sorted_groups = self._sort_groups(grouped)
# 生成Markdown
lines = [f"⏰ TAPD 过期单提醒({date}\n\n"]
mentioned_list = []
total_count = 0
item_index = 1
for owner, owner_items in sorted_groups:
wework_id = user_mapping.get(owner, owner)
mentioned_list.append(wework_id)
lines.append(f"<@{wework_id}>{len(owner_items)} 条过期)")
for item in owner_items:
type_label = "需求" if item['type'] == 'story' else "缺陷"
title = item.get('name') or item.get('title', '未命名')
lines.append(f"{item_index}.【{type_label}{title} | 过期 {item['overdue_days']} 天 | [查看]({item['url']})")
item_index += 1
total_count += 1
lines.append("\n\n========================")
lines.append(f"{total_count} 条过期单,请今日内更新状态 🙏")
return "\n".join(lines), mentioned_list
def _group_by_owner(self, items: List[dict]) -> Dict[str, List[dict]]:
"""按处理人分组"""
grouped = {}
for item in items:
owner = item['owner']
if owner not in grouped:
grouped[owner] = []
grouped[owner].append(item)
return grouped
def _sort_groups(self, grouped: Dict[str, List[dict]]) -> List[tuple]:
"""排序:组内按过期天数降序,组间按最大过期天数降序"""
sorted_groups = []
for owner, items in grouped.items():
# 组内排序
items.sort(key=lambda x: x['overdue_days'], reverse=True)
sorted_groups.append((owner, items))
# 组间排序
sorted_groups.sort(key=lambda x: max(item['overdue_days'] for item in x[1]), reverse=True)
return sorted_groups