package com.company.agentfoundation.agent.capability; import com.company.agentfoundation.agent.AgentCapabilityResponse; import com.company.agentfoundation.agent.AgentIntent; import com.company.agentfoundation.agent.AgentRequestContext; import com.company.agentfoundation.integration.wecom.WecomStudentContactStats; import com.company.agentfoundation.integration.wecom.WecomStudentContactStatsService; import org.springframework.stereotype.Component; import java.time.Instant; import java.util.List; import java.util.Map; @Component public class CustomerDashboardDailyCapabilityHandler implements AgentCapabilityHandler { private final WecomStudentContactStatsService studentContactStatsService; public CustomerDashboardDailyCapabilityHandler(WecomStudentContactStatsService studentContactStatsService) { this.studentContactStatsService = studentContactStatsService; } @Override public AgentIntent intent() { return AgentIntent.CUSTOMER_DASHBOARD_DAILY; } @Override public AgentCapabilityResponse handle(AgentRequestContext context) { WecomStudentContactStats stats; try { stats = studentContactStatsService.fetchTodayStats(); } catch (Exception e) { return new AgentCapabilityResponse( intent().code(), intent().title(), intent().mcpGroup(), "degraded", """ 学员联系数据暂时无法获取,不影响身份信息查询。 学员总数:暂未获取 今日新增:暂未获取 跟进成员数:暂未获取 统计管理:暂未获取 今日收款:暂未接入 """.trim(), List.of("企业微信学员联系 API", "企业微信统计管理 API"), Map.of("error", e.getMessage() == null ? "api forbidden" : e.getMessage()), Instant.now() ); } return new AgentCapabilityResponse( intent().code(), intent().title(), intent().mcpGroup(), "ready", """ 学员联系数据已从企业微信实时获取。 学员总数:%d 今日新增:%d 跟进成员数:%d 今日咨询数:%d 今日沟通客户数:%d 今日沟通消息条数:%d 今日回复率:%s 平均首次回复时长:%s 今日负反馈数:%d 今日收款:暂未接入 """.formatted( stats.totalStudents(), stats.todayNewStudents(), stats.followUserCount(), stats.todayNewApplyCount(), stats.todayChatCount(), stats.todayMessageCount(), percent(stats.todayReplyPercentage()), seconds(stats.todayAvgReplySeconds()), stats.todayNegativeFeedbackCount() ).trim(), List.of("企业微信学员联系 API", "企业微信统计管理 API"), Map.of( "totalStudents", stats.totalStudents(), "todayNewStudents", stats.todayNewStudents(), "followUserCount", stats.followUserCount(), "todayNewApplyCount", stats.todayNewApplyCount(), "todayChatCount", stats.todayChatCount(), "todayMessageCount", stats.todayMessageCount(), "todayNegativeFeedbackCount", stats.todayNegativeFeedbackCount() ), Instant.now() ); } private String percent(Integer value) { return value == null ? "暂无数据" : value + "%"; } private String seconds(Integer value) { return value == null ? "暂无数据" : value + "秒"; } }