<?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.community.huiju.dao.MessageMapper">
    <select id="messageLiset" resultType="com.community.huiju.model.Message">
        SELECT
        id,
        ticket_id,
        community_id,
        message_type,
        advice_type,
        model_type,
        uuid,
        uuid_type,
        source,
        message_content,
        STATUS,
        result,
        meaasge_type_id,
        read_status,
        create_user,
        create_date,
        update_user,
        update_date,
        bill_id,
        activity_id,
        announcement_id
    FROM
      tp_message
      <where>
          <if test='messageType == "-1"'>
              and  message_type in(5,8,9)
          </if>
          <if test='messageType == "-2"'>
              and  message_type in(1,2,3)
          </if>
          <if test='messageType == "-3"'>
              and  message_type=7
          </if>
          <if test="status != null and status !=''">
              and  status=#{status,jdbcType=VARCHAR}
          </if>
          <if test="createDate != null">
              and  date_format(create_date,'%Y%m%d')=date_format(#{createDate},'%Y%m%d')
          </if>
          <if test="messageContent != null and messageContent !=''">
              and message_content like concat('%',#{ messageContent,jdbcType=VARCHAR},'%')
          </if>
          and uuid =#{uuid,jdbcType=INTEGER}
          and uuid_type = 2
          and model_type= 2
          order by create_date desc
      </where>
    </select>
    <update id="updateMessage">
    update tp_message
    set
        <if test="message.messageContent!= null" >
            message_content = #{message.messageContent,jdbcType=VARCHAR},
        </if>
        <if test="message.updateUser != null" >
            update_user = #{message.updateUser,jdbcType=INTEGER},
        </if>
        <if test="message.updateDate != null" >
            update_date = #{message.updateDate,jdbcType=TIMESTAMP},
        </if>
        <if test="message.status != null" >
            status = #{message.status,jdbcType=CHAR}
        </if>
        where announcement_id = #{message.announcementId,jdbcType=INTEGER}
  </update>

    <update id="updateTicketMessage" >
        update tp_message
        set
        <if test="message.messageContent!= null" >
            message_content = #{message.messageContent,jdbcType=VARCHAR},
        </if>
        <if test="message.updateUser != null" >
            update_user = #{message.updateUser,jdbcType=INTEGER},
        </if>
        <if test="message.updateDate != null" >
            update_date = #{message.updateDate,jdbcType=TIMESTAMP},
        </if>
        <if test="message.status != null" >
            status = #{message.status,jdbcType=CHAR}
        </if>
        where ticket_id = #{message.ticketId,jdbcType=INTEGER}
    </update>

    <select id="myMessageList" resultType="com.community.huiju.model.Message">
        SELECT
        id,
        ticket_id,
        community_id,
        message_type,
        advice_type,
        model_type,
        uuid,
        uuid_type,
        source,
        message_content,
        STATUS,
        result,
        meaasge_type_id,
        read_status,
        create_user,
        create_date,
        update_user,
        update_date,
        bill_id,
        activity_id,
        announcement_id,
        transaction_id
        FROM
        tp_message
        <where>
            <if test='messageType == "-1"'>
                and  message_type in(5,8,9)
            </if>
            <if test='messageType == "-2"'>
                and  message_type in(1,2,3)
            </if>
            <if test='messageType == "-3"'>
                and  message_type=7
            </if>
            <if test="readStatus != null and readStatus !=''">
                and  read_status=#{readStatus,jdbcType=VARCHAR}
            </if>
            <if test="createDate != null">
                and  date_format(create_date,'%Y%m%d')=date_format(#{createDate},'%Y%m%d')
            </if>
            <if test="messageContent != null and messageContent !=''">
                and message_content like concat('%',#{ messageContent,jdbcType=VARCHAR},'%')
            </if>
            and uuid =#{uuid,jdbcType=INTEGER}
            and uuid_type = 2
            and model_type= 1
            order by create_date desc
        </where>
    </select>

    <update id="updateTickMessageStatus" >
        update tp_message
        set
            status = 0
        where
        ticket_id = #{ticketId,jdbcType=INTEGER}
        and model_type=2
        order by create_date ASC
        LIMIT 1
    </update>
</mapper>
