package cn.iocoder.yudao.module.im.service.statistics; import cn.hutool.core.convert.Convert; import cn.iocoder.yudao.module.im.dal.mysql.statistics.ImStatisticsManagerMapper; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; import java.util.Map; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; /** * IM 数据看板 Service 实现类 * * @author 芋道源码 */ @Service @Validated public class ImStatisticsManagerServiceImpl implements ImStatisticsManagerService { @Resource private ImStatisticsManagerMapper statisticsMapper; // ==================== 用户 ==================== @Override public Long getTotalUserCount() { return statisticsMapper.selectTotalUserCount(); } @Override public Long getNewUserCount(LocalDateTime beginTime, LocalDateTime endTime) { return statisticsMapper.selectNewUserCount(beginTime, endTime); } @Override public Long getActiveUserCount(LocalDateTime beginTime, LocalDateTime endTime) { return statisticsMapper.selectActiveUserCount(beginTime, endTime); } @Override public Map getNewUserDailyCountMap(LocalDateTime beginTime, LocalDateTime endTime) { List> rows = statisticsMapper.selectNewUserDailyCount(beginTime, endTime); return toDailyCountMap(rows); } @Override public Map getActiveUserDailyCountMap(LocalDateTime beginTime, LocalDateTime endTime) { List> rows = statisticsMapper.selectActiveUserDailyCount(beginTime, endTime); return toDailyCountMap(rows); } // ==================== 群 ==================== @Override public Long getTotalGroupCount() { return statisticsMapper.selectTotalGroupCount(); } @Override public Long getNewGroupCount(LocalDateTime beginTime, LocalDateTime endTime) { return statisticsMapper.selectNewGroupCount(beginTime, endTime); } @Override public Map getGroupSizeCountMap() { List> rows = statisticsMapper.selectGroupSizeDistribution(); return convertMap(rows, row -> (String) row.get("range"), row -> Convert.toLong(row.get("count"))); } // ==================== 消息 ==================== @Override public Long getPrivateMessageCount(LocalDateTime beginTime, LocalDateTime endTime) { return statisticsMapper.selectPrivateMessageCount(beginTime, endTime); } @Override public Long getGroupMessageCount(LocalDateTime beginTime, LocalDateTime endTime) { return statisticsMapper.selectGroupMessageCount(beginTime, endTime); } @Override public Map getPrivateMessageDailyCountMap(LocalDateTime beginTime, LocalDateTime endTime) { List> rows = statisticsMapper.selectPrivateMessageDailyCount(beginTime, endTime); return toDailyCountMap(rows); } @Override public Map getGroupMessageDailyCountMap(LocalDateTime beginTime, LocalDateTime endTime) { List> rows = statisticsMapper.selectGroupMessageDailyCount(beginTime, endTime); return toDailyCountMap(rows); } @Override public Map getMessageTypeCountMap(LocalDateTime beginTime, LocalDateTime endTime) { List> rows = statisticsMapper.selectMessageTypeDistribution(beginTime, endTime); return convertMap(rows, row -> Convert.toInt(row.get("type")), row -> Convert.toLong(row.get("count"))); } @Override public Map getTopSenderCountMap(LocalDateTime beginTime, LocalDateTime endTime, int limit) { List> rows = statisticsMapper.selectTopSenders(beginTime, endTime, limit); return convertMap(rows, row -> Convert.toLong(row.get("userId")), row -> Convert.toLong(row.get("messageCount"))); } /** * 把 [{date, count}] 行映射为 {LocalDateTime -> Long}; */ private static Map toDailyCountMap(List> rows) { return convertMap(rows, row -> Convert.convert(LocalDate.class, row.get("date")).atStartOfDay(), row -> Convert.toLong(row.get("count"))); } }