<?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.frequency.ordermessage.mapper.OrderMessageMapper">
    
    <resultMap type="com.ruoyi.frequency.ordermessage.entity.OrderMessage" id="OrderMessageResult">
        <result property="id"    column="id"    />
        <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="delFlag"    column="del_flag"    />
        <result property="orderId"    column="order_id"    />
        <result property="orderType"    column="order_type"    />
        <result property="title"    column="title"    />
        <result property="content"    column="content"    />
        <result property="isRead"    column="is_read"    />
        <result property="userId"    column="user_id"    />
        <result property="userType"    column="user_type"    />
        <result property="isShow"    column="is_show"    />
        <result property="messageType"    column="message_type"    />
    </resultMap>

    <sql id="selectOrderMessageVo">
        select id, create_by, create_time, update_by, update_time, del_flag, order_id, order_type, title, content, is_read, user_id, user_type, is_show, message_type from t_order_message
    </sql>

    <select id="selectOrderMessageList" parameterType="com.ruoyi.frequency.ordermessage.entity.OrderMessage" resultMap="OrderMessageResult">
        <include refid="selectOrderMessageVo"/>
        <where>  
            <if test="orderId != null "> and order_id = #{orderId}</if>
            <if test="orderType != null "> and order_type = #{orderType}</if>
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="isRead != null "> and is_read = #{isRead}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="userType != null "> and user_type = #{userType}</if>
            <if test="isShow != null "> and is_show = #{isShow}</if>
            <if test="messageType != null "> and message_type = #{messageType}</if>
        </where>
    </select>
    
    <select id="selectOrderMessageById" parameterType="Long" resultMap="OrderMessageResult">
        <include refid="selectOrderMessageVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertOrderMessage" parameterType="com.ruoyi.frequency.ordermessage.entity.OrderMessage">
        insert into t_order_message
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="orderId != null">order_id,</if>
            <if test="orderType != null">order_type,</if>
            <if test="title != null">title,</if>
            <if test="content != null">content,</if>
            <if test="isRead != null">is_read,</if>
            <if test="userId != null">user_id,</if>
            <if test="userType != null">user_type,</if>
            <if test="isShow != null">is_show,</if>
            <if test="messageType != null">message_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="orderType != null">#{orderType},</if>
            <if test="title != null">#{title},</if>
            <if test="content != null">#{content},</if>
            <if test="isRead != null">#{isRead},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userType != null">#{userType},</if>
            <if test="isShow != null">#{isShow},</if>
            <if test="messageType != null">#{messageType},</if>
         </trim>
    </insert>

    <update id="updateOrderMessage" parameterType="com.ruoyi.frequency.ordermessage.entity.OrderMessage">
        update t_order_message
        <trim prefix="SET" suffixOverrides=",">
            <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="delFlag != null">del_flag = #{delFlag},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="orderType != null">order_type = #{orderType},</if>
            <if test="title != null">title = #{title},</if>
            <if test="content != null">content = #{content},</if>
            <if test="isRead != null">is_read = #{isRead},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="isShow != null">is_show = #{isShow},</if>
            <if test="messageType != null">message_type = #{messageType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOrderMessageById" parameterType="Long">
        delete from t_order_message where id = #{id}
    </delete>

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

    <select id="messageOrderPage" resultType="com.ruoyi.controller.resp.MessageOrderPageResp">
        SELECT
            order_id,
            order_type
        FROM
            t_order_message t
        WHERE
            t.user_id =#{userVo.userId}
          AND t.user_type =#{userVo.userType}
          AND t.is_show = 1
          AND t.del_flag = 1
        GROUP BY
            t.order_id,
            t.order_type
        ORDER BY
            MAX(t.create_time) desc
    </select>


</mapper>