<?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.aegis.notification.mapper.NotificationMapper">

    <resultMap type="Notification" id="NotificationResult">
        <id     property="id"                column="id"                 />
        <result property="title"             column="title"              />
        <result property="content"           column="content"            />
        <result property="templateId"        column="template_id"        />
        <result property="channel"           column="channel"            />
        <result property="receiverId"        column="receiver_id"         />
        <result property="receiverName"      column="receiver_name"       />
        <result property="senderId"          column="sender_id"           />
        <result property="senderName"        column="sender_name"         />
        <result property="businessType"      column="business_type"      />
        <result property="businessId"       column="business_id"         />
        <result property="status"            column="status"             />
        <result property="readTime"          column="read_time"          />
        <result property="sendTime"          column="send_time"          />
        <result property="expireTime"        column="expire_time"        />
        <result property="priority"          column="priority"           />
        <result property="extraData"         column="extra_data"         />
        <result property="delFlag"           column="del_flag"           />
        <result property="createBy"          column="create_by"          />
        <result property="createTime"         column="create_time"        />
        <result property="updateBy"          column="update_by"          />
        <result property="updateTime"        column="update_time"        />
        <result property="remark"            column="remark"             />
    </resultMap>

    <sql id="selectNotificationVo">
        select id, title, content, template_id, channel, receiver_id, receiver_name,
               sender_id, sender_name, business_type, business_id, status, read_time,
               send_time, expire_time, priority, extra_data, del_flag, create_by,
               create_time, update_by, update_time, remark
        from sys_notification
    </sql>

    <select id="selectNotificationList" parameterType="Notification" resultMap="NotificationResult">
        <include refid="selectNotificationVo"/>
        <where>
            del_flag = '0'
            <if test="receiverId != null">
                AND receiver_id = #{receiverId}
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
            <if test="channel != null and channel != ''">
                AND channel = #{channel}
            </if>
            <if test="businessType != null and businessType != ''">
                AND business_type = #{businessType}
            </if>
            <if test="title != null and title != ''">
                AND title like concat('%', #{title}, '%')
            </if>
            <if test="params.beginTime != null and params.beginTime != ''">
                AND date_format(create_time,'%Y%m%d') &gt;= date_format(#{params.beginTime},'%Y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                AND date_format(create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectNotificationById" parameterType="Long" resultMap="NotificationResult">
        <include refid="selectNotificationVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <insert id="insertNotification" parameterType="Notification" useGeneratedKeys="true" keyProperty="id">
        insert into sys_notification(
            <if test="title != null and title != ''">title,</if>
            <if test="content != null">content,</if>
            <if test="templateId != null">template_id,</if>
            <if test="channel != null and channel != ''">channel,</if>
            <if test="receiverId != null">receiver_id,</if>
            <if test="receiverName != null">receiver_name,</if>
            <if test="senderId != null">sender_id,</if>
            <if test="senderName != null">sender_name,</if>
            <if test="businessType != null">business_type,</if>
            <if test="businessId != null">business_id,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="sendTime != null">send_time,</if>
            <if test="expireTime != null">expire_time,</if>
            <if test="priority != null">priority,</if>
            <if test="extraData != null">extra_data,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            create_time
        )values(
            <if test="title != null and title != ''">#{title},</if>
            <if test="content != null">#{content},</if>
            <if test="templateId != null">#{templateId},</if>
            <if test="channel != null and channel != ''">#{channel},</if>
            <if test="receiverId != null">#{receiverId},</if>
            <if test="receiverName != null">#{receiverName},</if>
            <if test="senderId != null">#{senderId},</if>
            <if test="senderName != null">#{senderName},</if>
            <if test="businessType != null">#{businessType},</if>
            <if test="businessId != null">#{businessId},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="sendTime != null">#{sendTime},</if>
            <if test="expireTime != null">#{expireTime},</if>
            <if test="priority != null">#{priority},</if>
            <if test="extraData != null">#{extraData},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            sysdate()
        )
    </insert>

    <update id="updateNotification" parameterType="Notification">
        update sys_notification
        <set>
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="content != null">content = #{content},</if>
            <if test="templateId != null">template_id = #{templateId},</if>
            <if test="channel != null and channel != ''">channel = #{channel},</if>
            <if test="receiverId != null">receiver_id = #{receiverId},</if>
            <if test="receiverName != null">receiver_name = #{receiverName},</if>
            <if test="senderId != null">sender_id = #{senderId},</if>
            <if test="senderName != null">sender_name = #{senderName},</if>
            <if test="businessType != null">business_type = #{businessType},</if>
            <if test="businessId != null">business_id = #{businessId},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="readTime != null">read_time = #{readTime},</if>
            <if test="sendTime != null">send_time = #{sendTime},</if>
            <if test="expireTime != null">expire_time = #{expireTime},</if>
            <if test="priority != null">priority = #{priority},</if>
            <if test="extraData != null">extra_data = #{extraData},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where id = #{id}
    </update>

    <update id="deleteNotificationById" parameterType="Long">
        update sys_notification set del_flag = '2' where id = #{id}
    </update>

    <update id="deleteNotificationByIds" parameterType="String">
        update sys_notification set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="countUnreadNotifications" parameterType="Long" resultType="int">
        select count(1) from sys_notification
        where receiver_id = #{receiverId} and status = 'sent' and del_flag = '0'
    </select>

    <update id="markBatchAsRead">
        update sys_notification
        set status = 'read', read_time = sysdate(), update_time = sysdate()
        where receiver_id = #{receiverId} and id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="markAllAsRead" parameterType="Long">
        update sys_notification
        set status = 'read', read_time = sysdate(), update_time = sysdate()
        where receiver_id = #{receiverId} and status = 'sent' and del_flag = '0'
    </update>

</mapper>