<?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="cn.cordys.crm.system.mapper.ExtNotificationMapper">

    <select id="listNotification" parameterType="cn.cordys.crm.system.dto.request.NotificationRequest"
            resultType="cn.cordys.crm.system.dto.response.NotificationDTO">
        SELECT * FROM sys_notification
        WHERE sys_notification.receiver = #{request.receiver} and sys_notification.organization_id = #{organizationId}
        <if test='request.subject != null and request.subject != ""'>
            AND ( sys_notification.subject LIKE CONCAT('%', #{request.subject},'%') OR sys_notification.content LIKE CONCAT('%', #{request.subject},'%') )
        </if>
        <if test='request.keyword != null and request.keyword != ""'>
            AND ( (sys_notification.subject LIKE  CONCAT('%', #{request.keyword},'%') and sys_notification.type = 'ANNOUNCEMENT_NOTICE') OR (sys_notification.resource_name LIKE  CONCAT('%', #{request.keyword},'%') and sys_notification.type = 'SYSTEM_NOTICE'))
        </if>
        <if test='request.type != null and request.type != ""'>
            AND sys_notification.type = #{request.type}
        </if>
        <if test='request.status != null and request.status != ""'>
            AND sys_notification.status = #{request.status}
        </if>
        <if test='request.resourceType != null and request.resourceType != ""'>
            AND sys_notification.resource_type LIKE CONCAT('%', #{request.resourceType},'%')
        </if>
        <if test='request.createTime != null and request.endTime == null'>
            AND sys_notification.create_time &gt;= #{request.createTime}
        </if>
        <if test='request.createTime != null and request.endTime != null'>
            AND sys_notification.create_time &gt;= #{request.createTime} AND sys_notification.create_time &lt;= #{request.endTime} 
        </if>
        ORDER BY sys_notification.status DESC, sys_notification.create_time DESC
    </select>

    <delete id="deleteByTime" parameterType="java.lang.Long">
        DELETE
        FROM sys_notification
        WHERE create_time &lt; #{timestamp}
    </delete>

    <delete id="deleteByResourceId" parameterType="java.lang.String">
        DELETE
        FROM sys_notification
        WHERE resource_id = #{resourceId}
    </delete>
    <delete id="deleteByReceivers">
        DELETE
        FROM sys_notification
        WHERE receiver IN
        <foreach collection="receivers" item="receiver" open="(" separator="," close=")">
            #{receiver}
        </foreach>
        and organization_id = #{organizationId}
    </delete>

    <update id="updateByReceiver" parameterType="cn.cordys.crm.system.domain.Notification">
        UPDATE sys_notification SET status = #{request.status} WHERE sys_notification.receiver = #{request.receiver}
        <if test='request.resourceType != null and request.resourceType != ""'>
            AND sys_notification.resource_type LIKE #{request.resourceType}
        </if>
        <if test='request.subject != null and request.subject != ""'>
            AND sys_notification.subject LIKE #{request.subject}
        </if>
        AND sys_notification.organization_id = #{request.organizationId}
    </update>

    <select id="countByReceiver" parameterType="cn.cordys.crm.system.domain.Notification"
            resultType="java.lang.Integer">
        SELECT COUNT(*) FROM sys_notification WHERE sys_notification.organization_id = #{request.organizationId}
        <if test='request.resourceType != null and request.resourceType != ""'>
            AND sys_notification.resource_type LIKE #{request.resourceType}
        </if>
        <if test='request.status != null and request.status != ""'>
            AND sys_notification.status = #{request.status}
        </if>
        AND sys_notification.receiver = #{request.receiver}
    </select>

    <select id="selectByAnyOne" parameterType="cn.cordys.crm.system.domain.Notification"
            resultType="cn.cordys.crm.system.dto.response.NotificationDTO">
        SELECT * FROM sys_notification WHERE sys_notification.organization_id = #{request.organizationId}
        <if test='request.resourceType != null and request.resourceType != ""'>
            AND sys_notification.resource_type LIKE #{request.resourceType}
        </if>
        <if test='request.subject != null and request.subject != ""'>
            AND sys_notification.subject LIKE #{request.subject}
        </if>
        <if test='request.status != null and request.status != ""'>
            AND sys_notification.status = #{request.status}
        </if>
        <if test='request.receiver != null and request.receiver != ""'>
            AND sys_notification.receiver = #{request.receiver}
        </if>
        <if test='request.resourceId != null and request.resourceId != ""'>
            AND sys_notification.resource_id = #{request.resourceId}
        </if>
    </select>
    <select id="selectLastList" resultType="cn.cordys.crm.system.dto.response.NotificationDTO">
        SELECT * FROM sys_notification
        WHERE sys_notification.receiver = #{userId} and sys_notification.organization_id = #{organizationId}
        and sys_notification.status='UNREAD'
        and sys_notification.resource_type in
        <foreach collection="modules" item="module" open="(" separator="," close=")">
            #{module}
        </foreach>
        order by sys_notification.create_time DESC limit 5
    </select>
    <select id="selectLastAnnouncementList" resultType="cn.cordys.crm.system.dto.response.NotificationDTO">
        SELECT * FROM sys_notification
        WHERE sys_notification.receiver = #{userId} and sys_notification.organization_id = #{organizationId}
        and sys_notification.status='UNREAD'
        and sys_notification.resource_type = 'ANNOUNCEMENT_NOTICE'
        order by sys_notification.create_time DESC


    </select>

</mapper>
