<?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.yushu.system.mapper.SysNotificationMapper">
    
    <resultMap type="SysNotification" id="SysNotificationResult">
        <result property="notificationId"    column="notification_id"    />
        <result property="title"    column="title"    />
        <result property="content"    column="content"    />
        <result property="notificationType"    column="notification_type"    />
        <result property="priority"    column="priority"    />
        <result property="senderId"    column="sender_id"    />
        <result property="senderName"    column="sender_name"    />
        <result property="targetType"    column="target_type"    />
        <result property="targetUsers"    column="target_users"    />
        <result property="targetRoles"    column="target_roles"    />
        <result property="status"    column="status"    />
        <result property="publishTime"    column="publish_time"    />
        <result property="expireTime"    column="expire_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectSysNotificationVo">
        select notification_id, title, content, notification_type, priority, sender_id, sender_name, target_type, target_users, target_roles, status, publish_time, expire_time, create_time, update_time from sys_notification
    </sql>

    <select id="selectSysNotificationList" parameterType="SysNotification" resultMap="SysNotificationResult">
        <include refid="selectSysNotificationVo"/>
        <where>  
            <if test="title != null  and title != ''"> and title like concat('%', #{title}, '%')</if>
            <if test="notificationType != null  and notificationType != ''"> and notification_type = #{notificationType}</if>
            <if test="priority != null  and priority != ''"> and priority = #{priority}</if>
            <if test="senderId != null "> and sender_id = #{senderId}</if>
            <if test="senderName != null  and senderName != ''"> and sender_name like concat('%', #{senderName}, '%')</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectSysNotificationByNotificationId" parameterType="Long" resultMap="SysNotificationResult">
        <include refid="selectSysNotificationVo"/>
        where notification_id = #{notificationId}
    </select>

    <!-- 查询用户的通知列表 -->
    <select id="selectUserNotificationList" parameterType="Long" resultType="java.util.Map">
        SELECT 
            n.notification_id,
            n.title,
            n.content,
            n.notification_type,
            n.priority,
            n.sender_name,
            n.publish_time,
            nr.is_read,
            nr.read_time,
            nr.create_time as receive_time
        FROM sys_notification n
        INNER JOIN sys_notification_receiver nr ON n.notification_id = nr.notification_id
        WHERE nr.user_id = #{userId} 
        AND nr.is_deleted = '0'
        AND n.status = '1'
        AND (n.expire_time IS NULL OR n.expire_time > NOW())
        ORDER BY nr.create_time DESC
    </select>

    <!-- 统计用户未读通知数 -->
    <select id="countUnreadNotifications" parameterType="Long" resultType="int">
        SELECT COUNT(DISTINCT n.notification_id)
        FROM sys_notification n
        LEFT JOIN sys_notification_receiver nr ON n.notification_id = nr.notification_id AND nr.user_id = #{userId}
        WHERE n.status = '1' 
        AND (nr.is_read IS NULL OR nr.is_read = '0')
        AND (n.expire_time IS NULL OR n.expire_time > NOW())
    </select>
        
    <insert id="insertSysNotification" parameterType="SysNotification" useGeneratedKeys="true" keyProperty="notificationId">
        insert into sys_notification
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">title,</if>
            <if test="content != null and content != ''">content,</if>
            <if test="notificationType != null">notification_type,</if>
            <if test="priority != null">priority,</if>
            <if test="senderId != null">sender_id,</if>
            <if test="senderName != null">sender_name,</if>
            <if test="targetType != null">target_type,</if>
            <if test="targetUsers != null">target_users,</if>
            <if test="targetRoles != null">target_roles,</if>
            <if test="status != null">status,</if>
            <if test="publishTime != null">publish_time,</if>
            <if test="expireTime != null">expire_time,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">#{title},</if>
            <if test="content != null and content != ''">#{content},</if>
            <if test="notificationType != null">#{notificationType},</if>
            <if test="priority != null">#{priority},</if>
            <if test="senderId != null">#{senderId},</if>
            <if test="senderName != null">#{senderName},</if>
            <if test="targetType != null">#{targetType},</if>
            <if test="targetUsers != null">#{targetUsers},</if>
            <if test="targetRoles != null">#{targetRoles},</if>
            <if test="status != null">#{status},</if>
            <if test="publishTime != null">#{publishTime},</if>
            <if test="expireTime != null">#{expireTime},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateSysNotification" parameterType="SysNotification">
        update sys_notification
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="content != null and content != ''">content = #{content},</if>
            <if test="notificationType != null">notification_type = #{notificationType},</if>
            <if test="priority != null">priority = #{priority},</if>
            <if test="senderId != null">sender_id = #{senderId},</if>
            <if test="senderName != null">sender_name = #{senderName},</if>
            <if test="targetType != null">target_type = #{targetType},</if>
            <if test="targetUsers != null">target_users = #{targetUsers},</if>
            <if test="targetRoles != null">target_roles = #{targetRoles},</if>
            <if test="status != null">status = #{status},</if>
            <if test="publishTime != null">publish_time = #{publishTime},</if>
            <if test="expireTime != null">expire_time = #{expireTime},</if>
            update_time = sysdate()
        </trim>
        where notification_id = #{notificationId}
    </update>

    <delete id="deleteSysNotificationByNotificationId" parameterType="Long">
        delete from sys_notification where notification_id = #{notificationId}
    </delete>

    <delete id="deleteSysNotificationByNotificationIds" parameterType="String">
        delete from sys_notification where notification_id in 
        <foreach item="notificationId" collection="array" open="(" separator="," close=")">
            #{notificationId}
        </foreach>
    </delete>

    <!-- 发布通知给用户 -->
    <insert id="publishNotificationToUsers">
        INSERT IGNORE INTO sys_notification_receiver (notification_id, user_id, is_read, create_time)
        VALUES
        <foreach collection="userIds" item="userId" separator=",">
            (#{notificationId}, #{userId}, '0', NOW())
        </foreach>
    </insert>

    <!-- 标记通知为已读 -->
    <update id="markNotificationAsRead">
        UPDATE sys_notification_receiver 
        SET is_read = '1', read_time = NOW()
        WHERE notification_id = #{notificationId} AND user_id = #{userId}
    </update>

    <!-- 删除用户通知 -->
    <update id="deleteUserNotification">
        UPDATE sys_notification_receiver 
        SET is_deleted = '1', delete_time = NOW()
        WHERE notification_id = #{notificationId} AND user_id = #{userId}
    </update>

    <!-- 查询所有有效的系统通知（简化版） -->
    <select id="selectAllNotifications" resultType="java.util.Map">
        SELECT 
            n.notification_id,
            n.title,
            n.content,
            n.notification_type as type,
            n.create_time,
            COALESCE(nr.is_read, '0') as is_read,
            nr.read_time
        FROM sys_notification n
        LEFT JOIN sys_notification_receiver nr ON n.notification_id = nr.notification_id AND nr.user_id = #{userId}
        WHERE n.status = '1'
        ORDER BY n.create_time DESC
    </select>

</mapper>
