<?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.ho.business.mapper.MessageInfoMapper">

    <insert id="addMessageInfo" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.MessageInfoVo">
        insert into message_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="messageDetail.messageId != null">message_id,</if>
            <if test="messageDetail.messageName != null">message_name,</if>
            <if test="messageDetail.messageContent != null">message_content,</if>
            <if test="messageDetail.messageType != null">message_type,</if>
            <if test="messageDetail.messageFrequency != null">message_frequency,</if>
            <if test="messageDetail.frequencyUnit != null">frequency_unit,</if>
            <if test="messageDetail.startTime != null">start_time,</if>
            <if test="messageDetail.endTime != null">end_time,</if>
            <if test="messageDetail.createTime != null">create_time,</if>
            <if test="messageDetail.createUser != null">create_user,</if>
            <if test="messageDetail.createStatus != null">create_status,</if>
            <if test="messageDetail.readTime != null">read_time,</if>
            <if test="messageDetail.sendType != null">send_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="messageDetail.messageId != null">#{messageDetail.messageId},</if>
            <if test="messageDetail.messageName != null">#{messageDetail.messageName},</if>
            <if test="messageDetail.messageContent != null">#{messageDetail.messageContent},</if>
            <if test="messageDetail.messageType != null">#{messageDetail.messageType},</if>
            <if test="messageDetail.messageFrequency != null">#{messageDetail.messageFrequency},</if>
            <if test="messageDetail.frequencyUnit != null">#{messageDetail.frequencyUnit},</if>
            <if test="messageDetail.startTime != null">#{messageDetail.startTime},</if>
            <if test="messageDetail.endTime != null">#{messageDetail.endTime},</if>
            <if test="messageDetail.createTime != null">#{messageDetail.createTime},</if>
            <if test="messageDetail.createUser != null">#{messageDetail.createUser},</if>
            <if test="messageDetail.createStatus != null">#{messageDetail.createStatus},</if>
            <if test="messageDetail.readTime != null">#{messageDetail.readTime},</if>
            <if test="messageDetail.sendType != null">#{messageDetail.sendType},</if>
        </trim>

    </insert>

    <insert id="addMessageReceiver" useGeneratedKeys="true"  keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.MessageReceiverVo">
        insert into message_receiver(message_id,receiving_type,receiving_user,read_status,receiving_time) values
        <foreach collection="messageReceiver"  item="item" separator=",">
            ( #{item.messageId},#{item.receivingType},#{item.receivingUser},#{item.readStatus},#{item.receivingTime})
        </foreach>


    </insert>

    <update id="updateMessageReadStatus">
        update message_receiver set read_status = 1
        where
            message_id = #{condition.messageId}
          and ((receiving_user = #{condition.stationId} and receiving_type = '1')
            or (receiving_user = #{condition.receivingUser} and receiving_type = '2'))
    </update>

    <delete id="deleteMessageInfo">
        delete from message_info where message_id in
        <foreach collection="messageIds" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>


    </delete>

    <delete id="deleteMessageReceivers">
        delete from message_receiver where message_id in
        <foreach collection="messageIds" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </delete>

    <select id="selectMessageInfo" resultType="com.ho.business.entity.MessageInfoVo">
            select * from message_info where 1=1
            <if test ="condition.messageId != null">
                and message_id = #{condition.messageId}
            </if>
            <if test ="condition.startTime != null">
                and create_time &gt;= #{condition.startTime }
            </if>
            <if test ="condition.endTime != null">
                and create_time &lt;= #{condition.endTime}
            </if>
            <if test ="condition.beginTime != null">
                and start_time &lt;= #{condition.beginTime}
            </if>
            <if test ="condition.finishTime != null">
                and end_time &gt;= #{condition.finishTime}
            </if>
            <if test ="condition.messageName != null">
                and message_name like  concat('%',#{condition.messageName},'%')
            </if>
            <if test ="condition.messageType != null">
                and message_type = #{condition.messageType}
            </if>
            <if test ="condition.createUser != null">
                and create_user = #{condition.createUser}
            </if>
            <if test ="condition.createStatus != null">
                and create_status = #{condition.createStatus}
            </if>
    </select>

    <select id="selectMessageReceivers" resultType="com.ho.business.entity.MessageReceiverVo">
        SELECT *,
        (
        CASE
        WHEN receiving_type &lt;= 1 THEN
        (SELECT NAME
        FROM
        station q
        WHERE
        q.id = receiving_user) ELSE (SELECT
        real_name
        FROM
        user_center_db.sys_user a
        WHERE
        a.id = receiving_user )
        END
        ) receivingUserName
        FROM
        message_receiver  where
        message_id = #{condition.messageId}
        <if test="condition.receivingUser != null and  condition.receivingType != null">
             and receiving_type = #{condition.receivingType} and receiving_user in
            <foreach collection="condition.receivingUser" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
        </if>
        <if test="condition.receivingUser != null and condition.stationId != null">
            and ((receiving_user = #{condition.stationId} and receiving_type = '1')
                or (receiving_user = #{condition.receivingUser} and receiving_type = '2'))
        </if>
        <if test="condition.receivingTime != null">
            and receiving_time = #{condition.receivingTime}
        </if>
        <if test="condition.readStatus != null">
            and read_status = #{condition.readStatus}
        </if>

    </select>

    <update id="updateCreateStatus">
        update message_info set create_status = 3
        where message_id in
        <foreach collection="messageId" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </update>

    <update id="updateReceivingTime">
        update message_receiver set receiving_time = now()
        where message_id in
        <foreach collection="messageId" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </update>

    <update id="refreshMessageReadStatus">
        update message_receiver set read_status = 0,receiving_time = now()
        where message_id = #{message.messageId}
        and date_add(receiving_time,INTERVAL ${message.messageFrequency} ${message.frequencyUnit}) &lt;= now()
    </update>


</mapper>
