package com.fs.company.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fs.company.domain.CompanySmsCommonDailyStatistics;
import com.fs.company.domain.CompanySmsCommonLogs;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 短信通用发送记录Mapper接口
 * 
 * @author fs
 * @date 2025-12-17
 */
public interface CompanySmsCommonLogsMapper extends BaseMapper<CompanySmsCommonLogs>{
    /**
     * 查询短信通用发送记录
     * 
     * @param logsId 短信通用发送记录主键
     * @return 短信通用发送记录
     */
    CompanySmsCommonLogs selectCompanySmsCommonLogsByLogsId(Long logsId);

    /**
     * 查询短信通用发送记录列表
     * 
     * @param companySmsCommonLogs 短信通用发送记录
     * @return 短信通用发送记录集合
     */
    List<CompanySmsCommonLogs> selectCompanySmsCommonLogsList(CompanySmsCommonLogs companySmsCommonLogs);

    /**
     * 新增短信通用发送记录
     * 
     * @param companySmsCommonLogs 短信通用发送记录
     * @return 结果
     */
    int insertCompanySmsCommonLogs(CompanySmsCommonLogs companySmsCommonLogs);

    /**
     * 修改短信通用发送记录
     * 
     * @param companySmsCommonLogs 短信通用发送记录
     * @return 结果
     */
    int updateCompanySmsCommonLogs(CompanySmsCommonLogs companySmsCommonLogs);

    /**
     * 删除短信通用发送记录
     * 
     * @param logsId 短信通用发送记录主键
     * @return 结果
     */
    int deleteCompanySmsCommonLogsByLogsId(Long logsId);

    /**
     * 批量删除短信通用发送记录
     * 
     * @param logsIds 需要删除的数据主键集合
     * @return 结果
     */
    int deleteCompanySmsCommonLogsByLogsIds(Long[] logsIds);

    @Select("select * from company_sms_common_logs where mid=#{msgid} ")
    CompanySmsCommonLogs selectCompanySmsCommonLogsByMid(@Param("msgid") String msgid);
    @Select("select * from company_sms_common_logs where phone=#{phone} order by logs_id desc limit 1")
    CompanySmsCommonLogs selectCompanySmsCommonLogsByMobile(@Param("phone") String phone);

    @Select("<script>" +
            "SELECT " +
            "    company_id, " +
            "    company_user_id, " +
            "    company_user_name, " +
            "    send_type, " +
            "    MAX(send_time) AS last_send_time, " +
            "    COUNT(*) AS total_count, " +
            "    SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) AS pendingCount, " +
            "    SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS successCount, " +
            "    SUM(CASE WHEN status = -1 THEN 1 ELSE 0 END) AS failCount " +
            "FROM company_sms_common_logs " +
            "WHERE DATE(send_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY) " +
            "GROUP BY company_user_id, send_type" +
            "</script>")
    List<CompanySmsCommonDailyStatistics> yesterdayStatistics();
}
