package shop.alien.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.store.CommonPushTaskUser;
import shop.alien.entity.store.dto.CommonPushTaskStatsDto;

public interface CommonPushTaskUserMapper extends BaseMapper<CommonPushTaskUser> {

    @Select("<script>" +
            "SELECT " +
            "IFNULL(SUM(CASE WHEN status IN (1, 2, 3) THEN 1 ELSE 0 END), 0) AS sentCount, " +
            "IFNULL(SUM(CASE WHEN status IN (1, 3) THEN 1 ELSE 0 END), 0) AS deliveredCount, " +
            "IFNULL(SUM(CASE WHEN show_info = 1 THEN 1 ELSE 0 END), 0) AS showCount, " +
            "IFNULL(SUM(CASE WHEN user_add = 1 THEN 1 ELSE 0 END), 0) AS clickCount " +
            "FROM common_push_task_user " +
            "WHERE delete_flag = 0 AND push_task_id = #{pushTaskId} " +
            "<if test='phoneType != null and phoneType != \"\"'>AND phone_type = #{phoneType} </if>" +
            "</script>")
    CommonPushTaskStatsDto selectStatsByTaskAndPhoneType(@Param("pushTaskId") Long pushTaskId,
                                                         @Param("phoneType") String phoneType);
}
