package com.rj.mapper;

import com.rj.entity.CustomerManagementStatistics;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户统计表 Mapper 接口
 * </p>
 *
 * @author 李中华 ,spllzh
 * @since 2025-08-07
 */
@Mapper
public interface CustomerManagementStatisticsMapper extends BaseMapper<CustomerManagementStatistics> {

    /**
     * 按租户、经销商、项目、销售统计指定日期的客户数据
     * 每天生成一条记录（按租户、经销商、项目、销售分组）
     * 
     * <p>注意：此方法需要跳过多租户拦截器，因为SQL中已经手动通过 cm2.tenant_id = cm.tenant_id 等条件
     * 处理了租户关联，不需要拦截器再自动添加 tenant_id 条件。</p>
     * 
     * @param date 统计日期
     * @return 统计结果列表，包含tenant_id, dealership_id, dealership_name, project_id, project_name, sales_id, sales_name, count_by_dealership, count_by_sales, count_by_project
     */
    @InterceptorIgnore(tenantLine = "true")
    @Select("SELECT " +
            "cm.tenant_id, " +
            "cm.dealership_id, " +
            "cm.dealership_name, " +
            "COALESCE(sm.project_id, '') as project_id, " +
            "COALESCE(sm.project_name, '') as project_name, " +
            "cm.sales_id, " +
            "cm.sales_name, " +
            "(SELECT COUNT(*) FROM customer_management cm2 " +
            " WHERE cm2.dealership_id = cm.dealership_id " +
            " AND DATE(cm2.create_time) = #{date} " +
            " AND cm2.tenant_id = cm.tenant_id) as count_by_dealership, " +
            "(SELECT COUNT(*) FROM customer_management cm3 " +
            " WHERE cm3.sales_id = cm.sales_id " +
            " AND DATE(cm3.create_time) = #{date} " +
            " AND cm3.tenant_id = cm.tenant_id) as count_by_sales, " +
            "(SELECT COUNT(*) FROM customer_management cm4 " +
            " LEFT JOIN sales_management sm4 ON cm4.sales_id = sm4.id " +
            " WHERE sm4.project_id = COALESCE(sm.project_id, '') " +
            " AND DATE(cm4.create_time) = #{date} " +
            " AND cm4.tenant_id = cm.tenant_id " +
            " AND COALESCE(sm.project_id, '') != '') as count_by_project " +
            "FROM customer_management cm " +
            "LEFT JOIN sales_management sm ON cm.sales_id = sm.id " +
            "WHERE DATE(cm.create_time) = #{date} " +
            "AND cm.dealership_id IS NOT NULL " +
            "AND cm.dealership_id != '' " +
            "AND cm.sales_id IS NOT NULL " +
            "AND cm.sales_id != '' " +
            "GROUP BY cm.tenant_id, cm.dealership_id, cm.dealership_name, sm.project_id, sm.project_name, cm.sales_id, cm.sales_name")
    List<Map<String, Object>> getStatisticsByDate(@Param("date") LocalDate date);

    /**
     * 检查指定日期的统计数据是否已存在
     * @param date 统计日期
     * @return 统计记录数量
     */
    @Select("SELECT COUNT(*) FROM customer_management_statistics WHERE statistics_date = #{date}")
    int countByStatisticsDate(@Param("date") LocalDate date);

    /**
     * 删除指定日期的统计数据
     * @param date 统计日期
     * @return 删除的记录数
     */
    int deleteByStatisticsDate(@Param("date") LocalDate date);
}

