package com.ruoyi.ai.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.ai.domain.MapValue;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ai.mapper.AiStatisticsMapper;
import com.ruoyi.ai.domain.AiStatistics;
import com.ruoyi.ai.service.IAiStatisticsService;

/**
 * ai-统计Service业务层处理
 * 
 * @author shi
 * @date 2025-11-29
 */
@Service
public class AiStatisticsServiceImpl implements IAiStatisticsService {

    @Autowired
    private AiStatisticsMapper aiStatisticsMapper;

    /**
     * 查询ai-统计
     * 
     * @param dateKey ai-统计主键
     * @return ai-统计
     */
    @Override
    public AiStatistics selectAiStatisticsByDateKey(String dateKey) {
        return aiStatisticsMapper.selectById(dateKey);
    }

    /**
     * 查询ai-统计列表
     * 
     * @param aiStatistics ai-统计
     * @return ai-统计
     */
    @Override
    public List<AiStatistics> selectAiStatisticsList(AiStatistics aiStatistics) {
        LambdaQueryWrapper<AiStatistics> query = Wrappers.lambdaQuery(aiStatistics);
        return aiStatisticsMapper.selectList(query);
    }

    /**
     * 分页查询ai-统计列表
     *
     * @param aiStatistics ai-统计
     * @return ai-统计
     */
    @Override
    public IPage<AiStatistics> selectAiStatisticsPage(Page page, AiStatistics aiStatistics) {
        LambdaQueryWrapper<AiStatistics> query = Wrappers.lambdaQuery(aiStatistics);
        return aiStatisticsMapper.selectPage(page, query);
    }

    /**
     * 新增ai-统计
     * 
     * @param aiStatistics ai-统计
     * @return 结果
     */
    @Override
    public int insertAiStatistics(AiStatistics aiStatistics) {
        return aiStatisticsMapper.insert(aiStatistics);
    }

    /**
     * 修改ai-统计
     * 
     * @param aiStatistics ai-统计
     * @return 结果
     */
    @Override
    public int updateAiStatistics(AiStatistics aiStatistics) {
        return aiStatisticsMapper.updateById(aiStatistics);
    }

    /**
     * 批量删除ai-统计
     * 
     * @param dateKeys 需要删除的ai-统计主键
     * @return 结果
     */
    @Override
    public int deleteAiStatisticsByDateKeys(String[] dateKeys)
    {
        return aiStatisticsMapper.deleteAiStatisticsByDateKeys(dateKeys);
    }

    /**
     * 删除ai-统计信息
     * 
     * @param dateKey ai-统计主键
     * @return 结果
     */
    @Override
    public int deleteAiStatisticsByDateKey(String dateKey)
    {
        return aiStatisticsMapper.deleteAiStatisticsByDateKey(dateKey);
    }

    @Override
    public void saveOrUpdateData(AiStatistics aiStatistics) {
        // 设置时间 为 yyyy-MM-dd HH:00:00
        aiStatistics.setDateKey(DateUtils.getCurrentHourStart());
        aiStatisticsMapper.saveOrUpdateData(aiStatistics);
    }

    @Override
    public AiStatistics getTitleTotal() {
        return aiStatisticsMapper.getTitleTotal();
    }

    @Override
    public List<Long> getTodayHourlyAddUserWithZero(String source) {
        return aiStatisticsMapper.getTodayHourlyAddUserWithZero(source);
    }

    @Override
    public List<Long> getWeeklyAddUserCount(String source) {
        return aiStatisticsMapper.getWeeklyAddUserCount(source);
    }

    @Override
    public List<Long> getMonthlyAddUserCount(String source) {
        return aiStatisticsMapper.getMonthlyAddUserCount(source);
    }

    @Override
    public List<Long> getMonthlyAddUserCountByRange(String source, Date startDate, Date endDate) {
        return aiStatisticsMapper.getMonthlyAddUserCountByRange(source, startDate, endDate);
    }

    @Override
    public List<Long> getTodayHourlyRechargeWithZero(String source) {
        return aiStatisticsMapper.getTodayHourlyRechargeWithZero(source);
    }

    @Override
    public List<Long> getWeeklyRecharge(String source) {
        return aiStatisticsMapper.getWeeklyRecharge(source);
    }

    @Override
    public List<Long> getMonthlyRecharge(String source) {
        return aiStatisticsMapper.getMonthlyRecharge(source);
    }

    @Override
    public List<Long> getMonthlyRechargeByRange(String source, Date startDate, Date endDate) {
        return aiStatisticsMapper.getMonthlyRechargeByRange(source, startDate, endDate);
    }

    @Override
    public List<Long> getTodayHourlyVisitorWithZero(String source) {
        return aiStatisticsMapper.getTodayHourlyVisitorWithZero(source);
    }

    @Override
    public List<Long> getWeeklyVisitor(String source) {
        return aiStatisticsMapper.getWeeklyVisitor(source);
    }

    @Override
    public List<Long> getMonthlyVisitor(String source) {
        return aiStatisticsMapper.getMonthlyVisitor(source);
    }

    @Override
    public List<Long> getMonthlyVisitorByRange(String source, Date startDate, Date endDate) {
        return aiStatisticsMapper.getMonthlyVisitorByRange(source, startDate, endDate);
    }

    @Override
    public List<Long> getTodayHourlyGenerateWithZero(String source) {
        return aiStatisticsMapper.getTodayHourlyGenerateWithZero(source);
    }

    @Override
    public List<Long> getWeeklyGenerate(String source) {
        return aiStatisticsMapper.getWeeklyGenerate(source);
    }

    @Override
    public List<Long> getMonthlyGenerate(String source) {
        return aiStatisticsMapper.getMonthlyGenerate(source);
    }

    @Override
    public List<Long> getMonthlyVisitorGenerate(String source, Date startDate, Date endDate) {
        return aiStatisticsMapper.getMonthlyVisitorGenerate(source, startDate, endDate);
    }

    @Override
    public  List<MapValue> getMapStatistics() {
        return aiStatisticsMapper.getMapStatistics();
    }

    @Override
    public List<String> getAllSource() {
        return aiStatisticsMapper.getAllSource();
    }
}
