<?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.RecordInfoMapper">
    
    <resultMap type="RecordInfo" id="RecordInfoResult">
        <result property="id"    column="id"    />
        <result property="title"    column="title"    />
        <result property="content"    column="content"    />

        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="topDept"    column="top_dept"    />

        <result property="senderId"    column="sender_id"    />
        <result property="senderName"    column="sender_name"    />
        <result property="addresseeId"    column="addressee_id"    />
        <result property="addresseeName"    column="addressee_name"    />
        <result property="isRead"    column="is_read"    />
        <result property="infoType"    column="info_type"    />
        <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="selectRecordInfoVo">
        select id, title, content,dept_id,dept_name,top_dept, sender_id, sender_name, addressee_id, addressee_name, is_read, info_type, create_by, create_time, update_by, update_time, remark from record_info
    </sql>

    <select id="selectRecordInfoList" parameterType="RecordInfo" resultMap="RecordInfoResult">
        <include refid="selectRecordInfoVo"/>
        <where>  
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="senderName != null  and senderName != ''"> and sender_name like concat('%', #{senderName}, '%')</if>
            <if test="addresseeName != null  and addresseeName != ''"> and addressee_name like concat('%', #{addresseeName}, '%')</if>
            <if test="isRead != null  and isRead != ''"> and is_read = #{isRead}</if>
            <if test="infoType != null  and infoType != ''"> and info_type = #{infoType}</if>
            <if test="createTime != null "> and create_time = #{createTime}</if>
        </where>
    </select>
    
    <select id="selectRecordInfoById" parameterType="Long" resultMap="RecordInfoResult">
        <include refid="selectRecordInfoVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertRecordInfo" parameterType="RecordInfo" useGeneratedKeys="true" keyProperty="id">
        insert into record_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null">title,</if>
            <if test="content != null">content,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="deptName != null">dept_name,</if>
            <if test="topDept != null">top_dept,</if>
            <if test="senderId != null">sender_id,</if>
            <if test="senderName != null">sender_name,</if>
            <if test="addresseeId != null">addressee_id,</if>
            <if test="addresseeName != null">addressee_name,</if>
            <if test="isRead != null">is_read,</if>
            <if test="infoType != null">info_type,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
            create_time,update_time
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null">#{title},</if>
            <if test="content != null">#{content},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="topDept != null">#{topDept},</if>
            <if test="senderId != null">#{senderId},</if>
            <if test="senderName != null">#{senderName},</if>
            <if test="addresseeId != null">#{addresseeId},</if>
            <if test="addresseeName != null">#{addresseeName},</if>
            <if test="isRead != null">#{isRead},</if>
            <if test="infoType != null">#{infoType},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
            sysdate(),sysdate()
         </trim>
    </insert>

    <update id="updateRecordInfo" parameterType="RecordInfo">
        update record_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null">title = #{title},</if>
            <if test="content != null">content = #{content},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="topDept != null">top_dept = #{topDept},</if>
            <if test="senderId != null">sender_id = #{senderId},</if>
            <if test="senderName != null">sender_name = #{senderName},</if>
            <if test="addresseeId != null">addressee_id = #{addresseeId},</if>
            <if test="addresseeName != null">addressee_name = #{addresseeName},</if>
            <if test="isRead != null">is_read = #{isRead},</if>
            <if test="infoType != null">info_type = #{infoType},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRecordInfoById" parameterType="Long">
        delete from record_info where id = #{id}
    </delete>

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

    <select id="selectNewApplyFor" parameterType="RecordInfo" resultMap="RecordInfoResult">
        <include refid="selectRecordInfoVo"/>
        where addressee_id = #{addresseeId}
        and info_type = #{infoType}
        and is_read = '0'
        order by create_time desc limit 1
    </select>

    <update id="updateRecordInfoIsChange" parameterType="Long">
        update record_info set is_read = '1' where id in
        <foreach collection="array" item="array" open="(" separator="," close=")">
            #{array}
        </foreach>
    </update>

    <update id="updateIsReadByAddresseeId" parameterType="String">
        update record_info set is_read = '1' where addressee_id = #{userId} and is_read = '0'
    </update>

</mapper>