package com.erp.report.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.erp.report.entity.PullRecordEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * API调用记录Repository
 */
@Mapper
public interface PullRecordRepository extends BaseMapper<PullRecordEntity> {

    /**
     * 按日期+平台分组统计
     */
    @Select("""
        SELECT 
            DATE_FORMAT(start_time, '%Y-%m-%d') as dateKey,
            platform_type,
            COUNT(*) as pullCount,
            COALESCE(SUM(pulled_count), 0) as totalOrders,
            SUM(CASE WHEN pull_status = 'success' THEN 1 ELSE 0 END) as successCount,
            SUM(CASE WHEN pull_status = 'failed' THEN 1 ELSE 0 END) as failedCount
        FROM pull_records
        WHERE deleted_at IS NULL
        AND (:platform IS NULL OR platform_type = :platform)
        AND (:startDate IS NULL OR start_time >= :startDate)
        AND (:endDate IS NULL OR start_time <= :endDate)
        GROUP BY DATE_FORMAT(start_time, '%Y-%m-%d'), platform_type
        ORDER BY dateKey ${trend}
        """)
    List<Map<String, Object>> selectGroupByDate(
            @Param("platform") String platform,
            @Param("startDate") String startDate,
            @Param("endDate") String endDate,
            @Param("trend") String trend);

    /**
     * 按平台分组统计
     */
    @Select("""
        SELECT 
            platform_type,
            COUNT(*) as pullCount,
            COALESCE(SUM(pulled_count), 0) as totalOrders,
            SUM(CASE WHEN pull_status = 'success' THEN 1 ELSE 0 END) as successCount,
            SUM(CASE WHEN pull_status = 'failed' THEN 1 ELSE 0 END) as failedCount,
            ROUND(SUM(CASE WHEN pull_status = 'success' THEN 1 ELSE 0 END) * 100.0 / NULLIF(COUNT(*), 0), 2) as successRate
        FROM pull_records
        WHERE deleted_at IS NULL
        AND (:platform IS NULL OR platform_type = :platform)
        AND (:startDate IS NULL OR start_time >= :startDate)
        AND (:endDate IS NULL OR start_time <= :endDate)
        GROUP BY platform_type
        ORDER BY pullCount ${trend}
        """)
    List<Map<String, Object>> selectGroupByPlatform(
            @Param("platform") String platform,
            @Param("startDate") String startDate,
            @Param("endDate") String endDate,
            @Param("trend") String trend);

    /**
     * 按拉取类型+平台分组统计
     */
    @Select("""
        SELECT 
            pull_type,
            platform_type,
            COUNT(*) as pullCount,
            COALESCE(SUM(pulled_count), 0) as totalOrders,
            SUM(CASE WHEN pull_status = 'success' THEN 1 ELSE 0 END) as successCount,
            SUM(CASE WHEN pull_status = 'failed' THEN 1 ELSE 0 END) as failedCount
        FROM pull_records
        WHERE deleted_at IS NULL
        AND (:platform IS NULL OR platform_type = :platform)
        AND (:startDate IS NULL OR start_time >= :startDate)
        AND (:endDate IS NULL OR start_time <= :endDate)
        GROUP BY pull_type, platform_type
        ORDER BY pullCount ${trend}
        """)
    List<Map<String, Object>> selectGroupByPullType(
            @Param("platform") String platform,
            @Param("startDate") String startDate,
            @Param("endDate") String endDate,
            @Param("trend") String trend);
}
