<?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.ruoyi.system.mapper.SysUserNotificationMapper">
    
    <resultMap type="SysUserNotification" id="SysUserNotificationResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="senderId"    column="sender_id"    />
        <result property="type"    column="type"    />
        <result property="targetId"    column="target_id"    />
        <result property="targetType"    column="target_type"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="detail"    column="detail"    />
    </resultMap>

    <resultMap id="SysNotificationDtoResult" type="SysNotificationDto">
        <result property="id"    column="id"    />
        <result property="createTime" column="create_time"/>
        <result property="type" column="type"/>
        <result property="status" column="status"/>
        <result property="detail" column="detail"/>
    </resultMap>

    <resultMap id="WorkLikeDtoResult" type="WorkLikeDto">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
        <result property="avatar"    column="avatar"    />
        <result property="workId"    column="work_id"    />
        <result property="workName"    column="work_name"    />
        <result property="workAddress"    column="work_address"    />
        <result property="detail"    column="detail"    />
        <result property="createTime" column="create_time"/>

    </resultMap>

    <sql id="selectSysUserNotificationVo">
        select id, user_id, sender_id, type, target_id, target_type, status, create_time, update_time , detail from sys_user_notification
    </sql>

    <select id="selectSysUserNotificationList" parameterType="SysUserNotification" resultMap="SysUserNotificationResult">
        <include refid="selectSysUserNotificationVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="senderId != null "> and sender_id = #{senderId}</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="targetId != null "> and target_id = #{targetId}</if>
            <if test="targetType != null  and targetType != ''"> and target_type = #{targetType}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>

        </where>
    </select>
    
    <select id="selectSysUserNotificationById" parameterType="Long" resultMap="SysUserNotificationResult">
        <include refid="selectSysUserNotificationVo"/>
        where id = #{id}
    </select>
    <select id="unreadNotifiaction" resultType="com.ruoyi.system.domain.SysUserNotification">
        select *
        from sys_user_notification
        where user_id = #{userId} and status = '0'
        order by create_time desc
    </select>
    <select id="getAddFollowerCount" resultType="java.lang.Long">
        select count(*)
        from sys_user_notification
        where user_id = #{userId} and status = '0' and type = '1'
    </select>
    <select id="getSystemMessage" resultMap="SysNotificationDtoResult">
        select id, type, status, detail, create_time
        from sys_user_notification
        where user_id = #{userId} and type != '1' and type != '2'
        order by create_time desc

    </select>
    <select id="getAddLikeCount" resultType="java.lang.Long">
        select  count(*)
        from sys_user_notification
        where user_id = #{userId} and status = '0' and type = '2'
    </select>
    <select id="getWorkLikeList" resultMap="WorkLikeDtoResult">
        select n.id, n.user_id, u.nick_name, u.avatar, w.work_id, w.work_name, w.work_address, n.create_time, n.detail
        from sys_user_notification n
        left join sys_user u on n.sender_id = u.user_id
        left join user_work w on n.target_id = w.work_id
        where n.user_id = #{userId} and n.type = '2'
        order by n.create_time desc

    </select>

    <insert id="insertSysUserNotification" parameterType="SysUserNotification" useGeneratedKeys="true" keyProperty="id">
        insert into sys_user_notification
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="senderId != null">sender_id,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="targetId != null">target_id,</if>
            <if test="targetType != null">target_type,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="detail != null and detail != ''">detail,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="senderId != null">#{senderId},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="targetId != null">#{targetId},</if>
            <if test="targetType != null">#{targetType},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="detail != null and detail != ''">#{detail},</if>
         </trim>
    </insert>

    <update id="updateSysUserNotification" parameterType="SysUserNotification">
        update sys_user_notification
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="senderId != null">sender_id = #{senderId},</if>
            <if test="type != null and type != ''">type = #{type},</if>
            <if test="targetId != null">target_id = #{targetId},</if>
            <if test="targetType != null">target_type = #{targetType},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="detail != null and detail != ''">detail = #{detail},</if>
        </trim>
        where id = #{id}
    </update>
    <update id="readNotifiaction">
        update sys_user_notification
        set status = #{status}, update_time = #{updateTime}
        where user_id = #{userId} and type = #{type}
    </update>

    <delete id="deleteSysUserNotificationById" parameterType="Long">
        delete from sys_user_notification where id = #{id}
    </delete>

    <delete id="deleteSysUserNotificationByIds" parameterType="String">
        delete from sys_user_notification where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteSysUserNotificationBySenderId">
        delete from sys_user_notification where sender_id = #{userId}
    </delete>
</mapper>