<?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.cf.imes.module.system.dal.mysql.notify.NotifyMessageMapper">
    <resultMap id="messageMap" type="com.cf.imes.module.system.dal.dataobject.notify.NotifyMessageDO">
        <result property="templateParams" column="template_params" javaType="java.util.Map" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
    </resultMap>

    <select id="getMyMyNotifyMessage" resultMap="messageMap">
        select distinct m.*,
        coalesce(r.id is not null, true) as readStatus
        , r.read_time readTime
        from system_notify_message m
        left join system_notify_message_readinfo r on r.message_id = m.id and r.user_id = #{reqVo.userId}
        <where>
            <if test="reqVo.readStatus != null and reqVo.readStatus">
                r.id is not null
            </if>
            <if test="reqVo.readStatus != null and !reqVo.readStatus">
                r.id is null
            </if>
            <if test="reqVo.createTime != null">
                and m.create_time between #{reqVo.createTime[0]} and #{reqVo.createTime[1]}
            </if>
            <if test="reqVo.templateCode != null and reqVo.templateCode != ''">
                and m.template_code = #{reqVo.templateCode}
            </if>
            <if test="reqVo.templateType != null">
                and m.template_type = #{reqVo.templateType}
            </if>
        </where>
        order by m.id desc
    </select>

    <select id="getUnreadNotifyMessageCount" resultType="java.lang.Integer">
        select count(distinct m.id)
        from system_notify_message m
                 left join system_notify_message_readinfo r on r.message_id = m.id and r.user_id = #{userId}
        where r.id is null
        and m.user_type = #{userType}
    </select>

    <select id="getUnreadNotifyMessageIds" resultType="java.lang.Long">
        select m.id
        from system_notify_message m
                 left join system_notify_message_readinfo r on r.message_id = m.id and r.user_id = #{userId}
        where r.id is null
        and m.user_type = #{userType}
    </select>
</mapper>