<?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.ytpm.middle.dao.MessageMapper">

    <insert id="insertMessage">
        insert into yt_platform_message
        (
         message_id,
         message_type,
         message_title,
         message_content,
         message_status,
         send_time,
         send_type,
         create_time,
         create_user_id,
         available
        )
        values
        (
         #{messageId},
         #{messageType},
         #{messageTitle},
         #{messageContent},
         #{messageStatus},
         #{sendTime},
         #{sendType},
         #{createTime},
         #{createUserId},
         #{available}
        );
    </insert>
    <insert id="batchInsertMessageRecipient">
        insert into yt_platform_message_record
        (
         record_id,
         message_id,
         recipient,
         recipient_status,
         recipient_time,
         create_user_id,
         create_time,
         available
        )
        values
        <foreach collection="recipient.split(',')" separator="," item="item">
           (
            replace(uuid(),'-',''),
            #{messageId},
            #{item},
            #{recipientStatus},
            #{recipientTime},
            #{createUserId},
            #{createTime},
            #{available}
           )
        </foreach>
    </insert>
    <update id="updateByPrimaryKeySelective">
        update yt_platform_message
        <set>
            <if test="messageType != null">
                message_type = #{messageType},
            </if>
            <if test="messageTitle != null">
                message_title = #{messageTitle},
            </if>
            <if test="messageContent != null">
                message_content = #{messageContent},
            </if>
            <if test="messageStatus != null">
                message_status = #{messageStatus},
            </if>
            <if test="sendTime != null">
                send_time = #{sendTime},
            </if>
            <if test="sendType != null">
                send_type = #{sendType},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="updateUserId != null">
                update_user_id = #{updateUserId},
            </if>
            <if test="available != null">
                available = #{available}
            </if>
        </set>
        where message_id = #{messageId}
    </update>
    <resultMap id="MessageVOMap" type="com.ytpm.middle.view.MessageVO">
        <id column="message_id" property="messageId" />
        <result column="message_type" property="messageType" />
        <result column="message_title" property="messageTitle" />
        <result column="message_content" property="messageContent" />
        <result column="message_status" property="messageStatus" />
        <result column="send_time" property="sendTime" />
        <result column="send_type" property="sendType" />
        <result column="create_time" property="createTime" />
        <result column="available" property="available" />
        <collection property="recipient" ofType="com.ytpm.middle.view.MessageRecordVO">
            <result column="record_id" property="recordId" />
            <result column="recipient" property="recipient" />
            <result column="recipientName" property="recipientName" />
            <result column="recipient_status" property="recipientStatus" />
            <result column="recipient_time" property="recipientTime" />
        </collection>
    </resultMap>
    <select id="queryAll" resultMap="MessageVOMap">
        select
        m.message_id,
        m.message_type,
        m.message_title,
        m.message_content,
        m.message_status,
        m.send_time,
        m.send_type,
        m.create_time,
        m.create_user_id,
        m.update_time,
        m.update_user_id,
        m.available,
        ypmr.record_id,
        ypmr.recipient,
        (select nick_name from yt_platform_user where user_id = ypmr.recipient) recipientName,
        ypmr.recipient_status,
        ypmr.recipient_time
        from yt_platform_message m
        left join yt_platform.yt_platform_message_record ypmr on m.message_id = ypmr.message_id
        <where>
            <if test="messageTitle != null and messageTitle != ''">
                and m.message_title like concat('%',#{messageTitle},'%')
            </if>
            <if test="messageType != null ">
                and m.message_type = #{messageType}
            </if>
            <if test="messageStatus != null ">
                and m.message_status = #{messageStatus}
            </if>
        </where>
    </select>
</mapper>