package com.storlead.trade.mapper;

import com.storlead.trade.entity.CustomerAnalysisResultEntity;
import com.storlead.framework.mybatis.mapper.MyBaseMapper;
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;

@Mapper
public interface CustomerAnalysisResultEntityMapper extends MyBaseMapper<CustomerAnalysisResultEntity> {

    /**
     * 按客户等级分组统计
     * @return 每个客户等级的统计信息（等级、数量、平均潜力分、平均置信度）
     */
    @Select("SELECT customer_level as customerLevel, " +
            "COUNT(*) as count, " +
            "AVG(potential_score) as avgPotentialScore, " +
            "AVG(confidence) as avgConfidence " +
            "FROM customer_analysis_result " +
            "WHERE customer_level IS NOT NULL AND is_delete = 0 " +
            "GROUP BY customer_level " +
            "ORDER BY customer_level")
    List<Map<String, Object>> selectStatisticsByCustomerLevel();

    /**
     * 按客户等级分组统计（带筛选条件）
     * @param analysisScene 分析场景
     * @param analysisStatus 分析状态
     * @return 每个客户等级的统计信息
     */
    @Select("SELECT customer_level as customerLevel, " +
            "COUNT(*) as count, " +
            "AVG(potential_score) as avgPotentialScore, " +
            "AVG(confidence) as avgConfidence " +
            "FROM customer_analysis_result " +
            "WHERE customer_level IS NOT NULL AND is_delete = 0 " +
            "<if test='analysisScene != null and analysisScene != &quot;&quot;'> AND analysis_scene = #{analysisScene} </if>" +
            "<if test='analysisStatus != null'> AND analysis_status = #{analysisStatus} </if>" +
            "GROUP BY customer_level " +
            "ORDER BY customer_level")
    List<Map<String, Object>> selectStatisticsByCustomerLevelWithFilter(@Param("analysisScene") String analysisScene,
                                                                        @Param("analysisStatus") Integer analysisStatus);
}
