package shop.alien.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.store.CommonPushTask;
import shop.alien.entity.store.dto.CommonPushTaskStatsDto;

import java.util.Date;
import java.util.List;

@Mapper
public interface CommonPushTaskMapper extends BaseMapper<CommonPushTask> {

    String NUM_SUM_REAL_SEND = "IFNULL(SUM(CAST(IFNULL(n.real_send, 0) AS UNSIGNED)), 0)";
    String NUM_SUM_REAL_DELIVERED = "IFNULL(SUM(CAST(IFNULL(n.real_delivered, 0) AS UNSIGNED)), 0)";
    String NUM_SUM_CLICK = "IFNULL(SUM(CAST(IFNULL(n.click_sum, 0) AS UNSIGNED)), 0)";
    String NUM_SUM_SHOW = "IFNULL(SUM(CAST(IFNULL(n.show_sum, 0) AS UNSIGNED)), 0)";
    String NUM_JOIN = "LEFT JOIN common_push_task_num n ON n.push_task_id = t.id AND n.delete_flag = 0 ";

    @Select("<script>" +
            "SELECT " +
            NUM_SUM_REAL_SEND + " AS sentCount, " +
            NUM_SUM_REAL_DELIVERED + " AS deliveredCount, " +
            NUM_SUM_CLICK + " AS clickCount " +
            "FROM common_push_task t " +
            NUM_JOIN +
            "WHERE t.delete_flag = 0 " +
            "<if test='taskNo != null and taskNo != \"\"'>AND t.task_no = #{taskNo} </if>" +
            "<if test='keyword != null and keyword != \"\"'>AND (t.title LIKE CONCAT('%', #{keyword}, '%') OR t.task_no LIKE CONCAT('%', #{keyword}, '%')) </if>" +
            "<if test='status != null and status != \"\"'>AND t.status = #{status} </if>" +
            "<if test='pushType != null'>AND t.push_type = #{pushType} </if>" +
            "<if test='channel != null and channel != \"\"'>AND t.channels LIKE CONCAT('%', #{channel}, '%') </if>" +
            "<if test='startTime != null'>AND t.created_time &gt;= #{startTime} </if>" +
            "<if test='endTime != null'>AND t.created_time &lt; #{endTime} </if>" +
            "<if test='startTime != null'>AND n.created_time &gt;= #{startTime} </if>" +
            "<if test='endTime != null'>AND n.created_time &lt; #{endTime} </if>" +
            "</script>")
    CommonPushTaskStatsDto selectStatisticsSummary(@Param("taskNo") String taskNo,
                                                   @Param("keyword") String keyword,
                                                   @Param("status") String status,
                                                   @Param("pushType") Integer pushType,
                                                   @Param("channel") String channel,
                                                   @Param("startTime") Date startTime,
                                                   @Param("endTime") Date endTime);

    @Select("<script>" +
            "SELECT t.id, t.task_no AS taskNo, t.title, t.push_type AS pushType, t.status, " +
            "t.channels, t.estimated_count AS estimatedCount, t.created_time AS createdTime, " +
            NUM_SUM_REAL_SEND + " AS sentCount, " +
            NUM_SUM_REAL_DELIVERED + " AS deliveredCount, " +
            NUM_SUM_SHOW + " AS showCount, " +
            NUM_SUM_CLICK + " AS clickCount " +
            "FROM common_push_task t " +
            NUM_JOIN +
            "WHERE t.delete_flag = 0 " +
            "<if test='taskNo != null and taskNo != \"\"'>AND t.task_no = #{taskNo} </if>" +
            "<if test='keyword != null and keyword != \"\"'>AND (t.title LIKE CONCAT('%', #{keyword}, '%') OR t.task_no LIKE CONCAT('%', #{keyword}, '%')) </if>" +
            "<if test='status != null and status != \"\"'>AND t.status = #{status} </if>" +
            "<if test='pushType != null'>AND t.push_type = #{pushType} </if>" +
            "<if test='channel != null and channel != \"\"'>AND t.channels LIKE CONCAT('%', #{channel}, '%') </if>" +
            "<if test='startTime != null'>AND t.created_time &gt;= #{startTime} </if>" +
            "<if test='endTime != null'>AND t.created_time &lt; #{endTime} </if>" +
            "<if test='startTime != null'>AND n.created_time &gt;= #{startTime} </if>" +
            "<if test='endTime != null'>AND n.created_time &lt; #{endTime} </if>" +
            "GROUP BY t.id, t.task_no, t.title, t.push_type, t.status, t.channels, t.estimated_count, t.created_time " +
            "ORDER BY t.created_time DESC" +
            "</script>")
    IPage<CommonPushTaskStatsDto> selectStatisticsPage(Page<CommonPushTaskStatsDto> page,
                                                       @Param("taskNo") String taskNo,
                                                       @Param("keyword") String keyword,
                                                       @Param("status") String status,
                                                       @Param("pushType") Integer pushType,
                                                       @Param("channel") String channel,
                                                       @Param("startTime") Date startTime,
                                                       @Param("endTime") Date endTime);

    @Select("<script>" +
            "SELECT " +
            NUM_SUM_REAL_SEND + " AS sentCount, " +
            NUM_SUM_REAL_DELIVERED + " AS deliveredCount, " +
            NUM_SUM_SHOW + " AS showCount, " +
            NUM_SUM_CLICK + " AS clickCount " +
            "FROM common_push_task_num n " +
            "WHERE n.delete_flag = 0 " +
            "<if test='startTime != null'>AND n.created_time &gt;= #{startTime} </if>" +
            "<if test='endTime != null'>AND n.created_time &lt; #{endTime} </if>" +
            "<if test='phoneType != null and phoneType != \"\"'>AND n.phone_type = #{phoneType} </if>" +
            "</script>")
    CommonPushTaskStatsDto selectUserStats(@Param("startTime") Date startTime,
                                           @Param("endTime") Date endTime,
                                           @Param("phoneType") String phoneType);

    @Select("<script>" +
            "SELECT t.id, t.title, " +
            NUM_SUM_REAL_SEND + " AS sentCount, " +
            NUM_SUM_REAL_DELIVERED + " AS deliveredCount, " +
            NUM_SUM_CLICK + " AS clickCount " +
            "FROM common_push_task t " +
            "INNER JOIN common_push_task_num n ON n.push_task_id = t.id AND n.delete_flag = 0 " +
            "WHERE t.delete_flag = 0 " +
            "<if test='startTime != null'>AND n.created_time &gt;= #{startTime} </if>" +
            "<if test='endTime != null'>AND n.created_time &lt; #{endTime} </if>" +
            "GROUP BY t.id, t.title " +
            "HAVING deliveredCount &gt; 0 " +
            "ORDER BY (clickCount / deliveredCount) DESC " +
            "LIMIT #{limit}" +
            "</script>")
    List<CommonPushTaskStatsDto> selectTopByClickRate(@Param("startTime") Date startTime,
                                                      @Param("endTime") Date endTime,
                                                      @Param("limit") Integer limit);
}
