<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.storlead.message.mapper.InsideMessageSendLogMapper">

        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="com.storlead.message.pojo.entity.InsideMessageSendLogEntity">
                    <id column="id" property="id" />
                    <result column="message_id" property="messageId" />
                    <result column="receiver_user_id" property="receiverUserId" />
                    <result column="is_read" property="isRead" />
                    <result column="status" property="status" />
                    <result column="create_by" property="createBy" />
                    <result column="create_time" property="createTime" />
                    <result column="update_by" property="updateBy" />
                    <result column="update_time" property="updateTime" />
                    <result column="enabled" property="enabled" />
                    <result column="is_delete" property="isDelete" />
                    <result column="sort" property="sort" />
        </resultMap>

        <!-- 通用查询结果列 -->
        <sql id="Base_Column_List">
            id, message_id, receiver_user_id, is_read, status, create_by, create_time, update_by, update_time, enabled, is_delete, sort
        </sql>


    <select id="pageListMessageLog" resultType="com.storlead.message.pojo.vo.MessageDetailVO">
        SELECT
            mlog.* ,mr.title,mr.content,mr.message_icon,mr.message_tag,mr.callback_url,send_user_id,us.avatar as photo,us.real_name as send_user_name
        FROM
            inside_message_send_log AS mlog
                LEFT JOIN inside_message_record AS mr ON mlog.message_id = mr.id
                LEFT JOIN `user` as us on us.id = mr.send_user_id
            ${ew.customSqlSegment}
    </select>

    <select id="countNoReadNumber" resultType="com.storlead.message.pojo.vo.MessageNoReadTotalVO">
        SELECT
            ifnull(sum(case when mlog.is_read = 0 then 1 else 0 end),0) as no_read_num,
            ifnull(sum(case when mlog.is_read = 0 and  mlog.message_type = 1 then 1 else 0 end),0) as at_no_read_num,
            ifnull(sum(case when mlog.is_read = 0 and  mlog.message_type = 2 then 1 else 0 end),0) as approve_no_read_num
        FROM
            inside_message_send_log AS mlog
                LEFT JOIN inside_message_record AS mr ON mlog.message_id = mr.id
                LEFT JOIN `user` as us on us.id = mr.send_user_id
            ${ew.customSqlSegment}
    </select>

    <select id="countNoReadCount" resultType="com.storlead.message.pojo.vo.MessageTypeReadStateVO">
        SELECT
            mr.message_type as messageType,ifnull(sum(case when mlog.is_read = 0 then 1 else 0 end),0) as stateNumber
        FROM
            inside_message_send_log AS mlog
                LEFT JOIN inside_message_record AS mr ON mlog.message_id = mr.id
                LEFT JOIN `user` as us on us.id = mr.send_user_id
            ${ew.customSqlSegment}
        group by mr.message_type
    </select>

    <select id="countNoReadCountByApp" resultType="com.storlead.message.pojo.vo.MessageAppNoReadVO">
        SELECT
            mr.app_id AS appId,
            ifnull(sum(case when mlog.is_read = 0 then 1 else 0 end), 0) AS stateNumber
        FROM
            inside_message_send_log AS mlog
                LEFT JOIN inside_message_record AS mr ON mlog.message_id = mr.id
                LEFT JOIN `user` AS us ON us.id = mr.send_user_id
            ${ew.customSqlSegment}
        GROUP BY mr.app_id
    </select>
</mapper>
