package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.entity.Activity;
import com.zhentao.entity.Users;
import com.zhentao.mapper.ActivityMapper;
import com.zhentao.mapper.UsersMapper;
import com.zhentao.service.DashboardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据面板服务实现类
 */
@Slf4j
@Service
public class DashboardServiceImpl implements DashboardService {
    
    private final UsersMapper usersMapper;
    private final ActivityMapper activityMapper;
    
    public DashboardServiceImpl(UsersMapper usersMapper, ActivityMapper activityMapper) {
        this.usersMapper = usersMapper;
        this.activityMapper = activityMapper;
    }
    
    @Override
    public Map<String, Object> getStats() {
        Map<String, Object> stats = new HashMap<>();
        
        try {
            // 总用户数
            Long totalUsers = usersMapper.selectCount(null);
            stats.put("totalUsers", totalUsers != null ? totalUsers : 0);
            
            // 总活动数
            Long totalActivities = activityMapper.selectCount(null);
            stats.put("totalActivities", totalActivities != null ? totalActivities : 0);
            
            // 成功案例数（示例数据）
            stats.put("totalCases", 89);
            
            // 课程数量（示例数据）
            stats.put("totalCourses", 34);
            
            // VIP用户数
            QueryWrapper<Users> vipWrapper = new QueryWrapper<>();
            vipWrapper.eq("is_vip", 1);
            Long vipUsers = usersMapper.selectCount(vipWrapper);
            stats.put("vipUsers", vipUsers != null ? vipUsers : 0);
            
            // 今日新增用户（示例数据）
            stats.put("todayNewUsers", 12);
            
        } catch (Exception e) {
            log.error("获取统计数据失败", e);
            // 返回默认值
            stats.put("totalUsers", 0);
            stats.put("totalActivities", 0);
            stats.put("totalCases", 0);
            stats.put("totalCourses", 0);
            stats.put("vipUsers", 0);
            stats.put("todayNewUsers", 0);
        }
        
        return stats;
    }
    
    @Override
    public Map<String, Object> getRecentActivities(Integer limit) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            if (limit == null || limit <= 0) {
                limit = 10;
            }
            
            QueryWrapper<Activity> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("created_at");
            wrapper.last("LIMIT " + limit);
            
            List<Activity> activities = activityMapper.selectList(wrapper);
            result.put("list", activities != null ? activities : new ArrayList<>());
            result.put("total", activities != null ? activities.size() : 0);
            
        } catch (Exception e) {
            log.error("获取最近活动失败", e);
            result.put("list", new ArrayList<>());
            result.put("total", 0);
        }
        
        return result;
    }
    
    @Override
    public Map<String, Object> getUserTrend(Integer days) {
        Map<String, Object> result = new HashMap<>();
        
        // 这里可以实现真实的趋势统计
        // 示例数据
        List<Map<String, Object>> trend = new ArrayList<>();
        for (int i = days - 1; i >= 0; i--) {
            Map<String, Object> dayData = new HashMap<>();
            dayData.put("date", "2025-10-" + (22 - i));
            dayData.put("count", 10 + (int)(Math.random() * 20));
            trend.add(dayData);
        }
        
        result.put("trend", trend);
        return result;
    }
    
    @Override
    public Map<String, Object> getActivityStats(Integer days) {
        Map<String, Object> result = new HashMap<>();
        
        // 这里可以实现真实的活动统计
        // 示例数据
        List<Map<String, Object>> stats = new ArrayList<>();
        for (int i = days - 1; i >= 0; i--) {
            Map<String, Object> dayData = new HashMap<>();
            dayData.put("date", "2025-10-" + (22 - i));
            dayData.put("count", 5 + (int)(Math.random() * 15));
            stats.add(dayData);
        }
        
        result.put("stats", stats);
        return result;
    }
}

