package org.hzhub.crm.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.hzhub.common.mybatis.core.mapper.BaseMapperPlus;
import org.hzhub.crm.domain.CrmLead;
import org.hzhub.crm.domain.vo.CrmLeadVo;

/**
 * CRM线索 Mapper 接口
 *
 * @author hzhub
 */
public interface CrmLeadMapper extends BaseMapperPlus<CrmLead, CrmLeadVo> {

    /**
     * 统计线索总数
     */
    @Select("SELECT COUNT(*) FROM crm_lead WHERE del_flag = 0 AND tenant_id = #{tenantId}")
    Long countTotal(@Param("tenantId") String tenantId);

    /**
     * 统计高意向线索数量
     */
    @Select("SELECT COUNT(*) FROM crm_lead WHERE del_flag = 0 AND tenant_id = #{tenantId} AND (intent_level = 'high' OR ai_score >= 60)")
    Long countHighIntent(@Param("tenantId") String tenantId);

    /**
     * 统计本月新增线索数量
     */
    @Select("SELECT COUNT(*) FROM crm_lead WHERE del_flag = 0 AND tenant_id = #{tenantId} AND YEAR(create_time) = #{year} AND MONTH(create_time) = #{month}")
    Long countMonthlyNew(@Param("tenantId") String tenantId, @Param("year") int year, @Param("month") int month);

    /**
     * 统计已转化线索数量
     */
    @Select("SELECT COUNT(*) FROM crm_lead WHERE del_flag = 0 AND tenant_id = #{tenantId} AND lead_status = 'converted'")
    Long countConverted(@Param("tenantId") String tenantId);

    /**
     * 统计本月转化线索数量
     */
    @Select("SELECT COUNT(*) FROM crm_lead WHERE del_flag = 0 AND tenant_id = #{tenantId} AND lead_status = 'converted' AND YEAR(create_time) = #{year} AND MONTH(create_time) = #{month}")
    Long countMonthlyConverted(@Param("tenantId") String tenantId, @Param("year") int year, @Param("month") int month);

    /**
     * 统计本月高意向线索数量
     */
    @Select("SELECT COUNT(*) FROM crm_lead WHERE del_flag = 0 AND tenant_id = #{tenantId} AND (intent_level = 'high' OR ai_score >= 60) AND YEAR(create_time) = #{year} AND MONTH(create_time) = #{month}")
    Long countMonthlyHighIntent(@Param("tenantId") String tenantId, @Param("year") int year, @Param("month") int month);
}