<?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.TpMessageMapper" >
  <resultMap id="BaseResultMap" type="com.community.huiju.model.TpMessage" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <id column="ticket_id" property="ticketId" jdbcType="INTEGER" />
    <result column="community_id" property="communityId" jdbcType="INTEGER" />
    <result column="message_type" property="messageType" jdbcType="CHAR" />
    <result column="advice_type" property="adviceType" jdbcType="CHAR" />
    <result column="model_type" property="modelType" jdbcType="CHAR" />
    <result column="uuid" property="uuid" jdbcType="INTEGER" />
    <result column="uuid_type" property="uuidType" jdbcType="CHAR" />
    <result column="source" property="source" jdbcType="CHAR" />
    <result column="message_content" property="messageContent" jdbcType="VARCHAR" />
    <result column="status" property="status" jdbcType="CHAR" />
    <result column="result" property="result" jdbcType="VARCHAR" />
    <result column="meaasge_type_id" property="meaasgeTypeId" jdbcType="VARCHAR" />
    <result column="read_status" property="readStatus" jdbcType="CHAR" />
    <result column="create_user" property="createUser" jdbcType="INTEGER" />
    <result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
    <result column="update_user" property="updateUser" jdbcType="INTEGER" />
    <result column="update_date" property="updateDate" jdbcType="TIMESTAMP" />
    <result column="ticket_status" property="ticketStatus" jdbcType="VARCHAR" />
    <result column="content" property="content" jdbcType="VARCHAR" />
    <result column="read_message_icon" property="readMessageIcon" jdbcType="VARCHAR" />
    <result column="unread_message_icon" property="unreadMessageIcon" jdbcType="VARCHAR" />
    <result column="message_detail_url" property="messageDetailUrl" jdbcType="VARCHAR" />
    <result column="message_icon_title" property="messageIconTitle" jdbcType="VARCHAR" />
    <result column="bill_id" property="billId" jdbcType="INTEGER" />
    <result column="activity_id" property="activityId" jdbcType="INTEGER" />
    <result column="announcement_id" property="announcementId" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    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
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from tp_message
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from tp_message
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.community.huiju.model.TpMessage" >
    insert into tp_message (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)
    values (#{id,jdbcType=INTEGER}, #{ticketId,jdbcType=INTEGER},#{communityId,jdbcType=INTEGER}, #{messageType,jdbcType=CHAR},
      #{adviceType,jdbcType=CHAR}, #{modelType,jdbcType=CHAR}, #{uuid,jdbcType=INTEGER},
      #{uuidType,jdbcType=CHAR}, #{source,jdbcType=CHAR}, #{messageContent,jdbcType=VARCHAR},
      #{status,jdbcType=CHAR}, #{result,jdbcType=VARCHAR}, #{meaasgeTypeId,jdbcType=VARCHAR},
      #{readStatus,jdbcType=CHAR}, #{createUser,jdbcType=INTEGER}, #{createDate,jdbcType=TIMESTAMP},
      #{updateUser,jdbcType=INTEGER}, #{updateDate,jdbcType=TIMESTAMP}, #{billId,jdbcType=INTEGER},
      #{activityId,jdbcType=INTEGER}, #{announcementId,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.community.huiju.model.TpMessage" useGeneratedKeys="true" keyProperty="id" >
    insert into tp_message
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="ticketId != null">
        ticket_id,
      </if>
      <if test="communityId != null" >
        community_id,
      </if>
      <if test="messageType != null" >
        message_type,
      </if>
      <if test="adviceType != null" >
        advice_type,
      </if>
      <if test="modelType != null" >
        model_type,
      </if>
      <if test="uuid != null" >
        uuid,
      </if>
      <if test="uuidType != null" >
        uuid_type,
      </if>
      <if test="source != null" >
        source,
      </if>
      <if test="messageContent != null" >
        message_content,
      </if>
      <if test="status != null" >
        status,
      </if>
      <if test="result != null" >
        result,
      </if>
      <if test="meaasgeTypeId != null" >
        meaasge_type_id,
      </if>
      <if test="readStatus != null" >
        read_status,
      </if>
      <if test="createUser != null" >
        create_user,
      </if>
      <if test="createDate != null" >
        create_date,
      </if>
      <if test="updateUser != null" >
        update_user,
      </if>
      <if test="updateDate != null" >
        update_date,
      </if>
      <if test="billId != null" >
        bill_id,
      </if>
      <if test="activityId != null" >
        activity_id,
      </if>
      <if test="announcementId != null" >
      announcement_id,
    </if>
      <if test="transactionId != null" >
        transaction_id
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="ticketId != null">
        #{ticketId,jdbcType=INTEGER},
      </if>
      <if test="communityId != null" >
        #{communityId,jdbcType=INTEGER},
      </if>
      <if test="messageType != null" >
        #{messageType,jdbcType=CHAR},
      </if>
      <if test="adviceType != null" >
        #{adviceType,jdbcType=CHAR},
      </if>
      <if test="modelType != null" >
        #{modelType,jdbcType=CHAR},
      </if>
      <if test="uuid != null" >
        #{uuid,jdbcType=INTEGER},
      </if>
      <if test="uuidType != null" >
        #{uuidType,jdbcType=CHAR},
      </if>
      <if test="source != null" >
        #{source,jdbcType=CHAR},
      </if>
      <if test="messageContent != null" >
        #{messageContent,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        #{status,jdbcType=CHAR},
      </if>
      <if test="result != null" >
        #{result,jdbcType=VARCHAR},
      </if>
      <if test="meaasgeTypeId != null" >
        #{meaasgeTypeId,jdbcType=VARCHAR},
      </if>
      <if test="readStatus != null" >
        #{readStatus,jdbcType=CHAR},
      </if>
      <if test="createUser != null" >
        #{createUser,jdbcType=INTEGER},
      </if>
      <if test="createDate != null" >
        #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null" >
        #{updateUser,jdbcType=INTEGER},
      </if>
      <if test="updateDate != null" >
        #{updateDate,jdbcType=TIMESTAMP},
      </if>
      <if test="billId != null" >
        #{billId,jdbcType=TIMESTAMP},
      </if>
      <if test="activityId != null" >
        #{activityId,jdbcType=TIMESTAMP},
      </if>
      <if test="announcementId != null" >
        #{announcementId,jdbcType=TIMESTAMP},
      </if>
      <if test="transactionId != null" >
        #{transactionId,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.community.huiju.model.TpMessage" >
    update tp_message
    <set >
      <if test="ticketId != null">
        ticket_id = #{ticketId,jdbcType=INTEGER},
      </if>
      <if test="communityId != null" >
        community_id = #{communityId,jdbcType=INTEGER},
      </if>
      <if test="messageType != null" >
        message_type = #{messageType,jdbcType=CHAR},
      </if>
      <if test="adviceType != null" >
        advice_type = #{adviceType,jdbcType=CHAR},
      </if>
      <if test="modelType != null" >
        model_type = #{modelType,jdbcType=CHAR},
      </if>
      <if test="uuid != null" >
        uuid = #{uuid,jdbcType=INTEGER},
      </if>
      <if test="uuidType != null" >
        uuid_type = #{uuidType,jdbcType=CHAR},
      </if>
      <if test="source != null" >
        source = #{source,jdbcType=CHAR},
      </if>
      <if test="messageContent != null" >
        message_content = #{messageContent,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        status = #{status,jdbcType=CHAR},
      </if>
      <if test="result != null" >
        result = #{result,jdbcType=VARCHAR},
      </if>
      <if test="meaasgeTypeId != null" >
        meaasge_type_id = #{meaasgeTypeId,jdbcType=VARCHAR},
      </if>
      <if test="readStatus != null" >
        read_status = #{readStatus,jdbcType=CHAR},
      </if>
      <if test="createUser != null" >
        create_user = #{createUser,jdbcType=INTEGER},
      </if>
      <if test="createDate != null" >
        create_date = #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null" >
        update_user = #{updateUser,jdbcType=INTEGER},
      </if>
      <if test="updateDate != null" >
        update_date = #{updateDate,jdbcType=TIMESTAMP},
      </if>
      <if test="billId != null" >
        bill_id = #{billId,jdbcType=INTEGER},
      </if>
      <if test="activityId != null" >
        activity_id = #{activityId,jdbcType=INTEGER},
      </if>
      <if test="announcementId != null" >
        announcement_id = #{announcementId,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.community.huiju.model.TpMessage" >
    update tp_message
    set ticket_id = #{ticketId,jdbcType=INTEGER},
      community_id = #{communityId,jdbcType=INTEGER},
      message_type = #{messageType,jdbcType=CHAR},
      advice_type = #{adviceType,jdbcType=CHAR},
      model_type = #{modelType,jdbcType=CHAR},
      uuid = #{uuid,jdbcType=INTEGER},
      uuid_type = #{uuidType,jdbcType=CHAR},
      source = #{source,jdbcType=CHAR},
      message_content = #{messageContent,jdbcType=VARCHAR},
      status = #{status,jdbcType=CHAR},
      result = #{result,jdbcType=VARCHAR},
      meaasge_type_id = #{meaasgeTypeId,jdbcType=VARCHAR},
      read_status = #{readStatus,jdbcType=CHAR},
      create_user = #{createUser,jdbcType=INTEGER},
      create_date = #{createDate,jdbcType=TIMESTAMP},
      update_user = #{updateUser,jdbcType=INTEGER},
      update_date = #{updateDate,jdbcType=TIMESTAMP},
      bill_id = #{billId,jdbcType=INTEGER},
      activity_id = #{activityId,jdbcType=INTEGER},
      announcement_id = #{announcementId,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="getMessageTotal" resultType="map">
    SELECT
        count( CASE WHEN model_type = 1 AND read_status = 0 THEN 1 ELSE NULL END ) AS messageTotal,
        count( CASE WHEN model_type = 2 AND status = 1 THEN 1 ELSE NULL END ) AS toDoTotal
    FROM
        tp_message
    WHERE
        uuid_type = 1
        AND community_id = #{communityId,jdbcType=INTEGER}
        AND uuid = #{userId,jdbcType=INTEGER}
        AND STATUS = 1
        AND advice_type = 1
  </select>

  <select id="getMessages" resultMap="BaseResultMap">
    SELECT
        t.id,
        t.ticket_id,
        t.community_id,
        t.message_type,
        t.advice_type,
        t.model_type,
        t.uuid,
        t.uuid_type,
        t.source,
        t.message_content,
        t.result,
        t.meaasge_type_id,
        t.read_status ,
        t.create_user,
        t.create_date,
        t.update_user,
        t.update_date,
        t.bill_id,
        t.activity_id,
        t.announcement_id,
        k.status,
        r.content,
        d.name as ticket_status,
        i.read_message_icon,
		i.unread_message_icon,
		i.message_detail_url,
		i.message_icon_title
    FROM
        tp_message t
    LEFT JOIN tp_ticket k on t.ticket_id = k.id  AND k.community_id = #{communityId,jdbcType=INTEGER}
    LEFT JOIN tp_ticket_record r on t.ticket_id = r.ticket_id AND r.community_id = #{communityId,jdbcType=INTEGER}  AND k.status = r.status
    LEFT JOIN sys_dictionary d on k.status = d.code AND d.group_id = ( SELECT id FROM sys_dictionary WHERE CODE = "ticket_status" )
    LEFT JOIN tp_message_icon i on t.message_type = i.message_type
    WHERE
        t.uuid_type = 1
        AND t.community_id = #{communityId,jdbcType=INTEGER}
        AND t.uuid = #{userId,jdbcType=INTEGER}
        AND t.STATUS = 1
        AND t.advice_type = 1
        AND t.uuid_type = 1
        AND t.model_type = #{modelType,jdbcType=INTEGER}
    ORDER BY
    t.create_date DESC , t.id
  </select>

  <update id="updateReadStatus" parameterType="java.lang.Integer" >
    update tp_message
    set
      read_status = 1
    where community_id = #{communityId}
    and advice_type = 1
    and model_type = 1
    and source = 1
    and uuid_type = 1
    and uuid = #{userId}
  </update>

  <select id="selectTicketMessage" resultMap="BaseResultMap" parameterType="com.community.huiju.model.TpMessage">
    select
    <include refid="Base_Column_List"/>
    from tp_message
    where
    message_type=#{messageType}
    and ticket_id=#{ticketId}
    and model_type=#{modelType}
    and source=#{source}
  </select>
  <update id="updateTickMessage" parameterType="com.community.huiju.model.TpMessage" >
    update tp_message
    set
    <trim suffixOverrides=",">
      <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>
    </trim>
    where ticket_id = #{message.ticketId,jdbcType=INTEGER}
  </update>
  <select id="selectTicket" resultType="int">
    select
    ticket_id
    from
    tp_message
    when ticket_id=#{ticketId}
  </select>
  <select id="selectConditionsTpMessage" resultMap="BaseResultMap" parameterType="map">
    SELECT
        *
    FROM
        tp_message
    WHERE
    <trim prefixOverrides="and | or">
      <if test="map.communityId != null">
        and community_id = #{map.communityId}
      </if>
      <if test="map.messageType != null">
        and message_type = #{map.messageType}
      </if>
      <if test="map.adviceType != null">
        and advice_type = #{map.adviceType}
      </if>
      <if test="map.uuidType != null">
        and uuid_type = #{map.uuidType}
      </if>
      <if test="map.source != null">
        and source = #{map.source}
      </if>
      <if test="map.status != null">
        and status = #{map.status}
      </if>
      <if test="map.transactionId != null">
        and transaction_id = #{map.transactionId}
      </if>
      <if test="map.modelType != null">
        and model_type = #{map.modelType}
      </if>
    </trim>
  </select>

  <select id="selectBillinvoiceMessage" resultMap="BaseResultMap" parameterType="map">
    select
    <include refid="Base_Column_List" />
    from tp_message
    where
    community_id = #{map.communityId}
    and bill_id = #{map.billInvoiceId}
    and uuid_type = 1
    and uuid = #{map.userId}
    and message_type = 7
    and advice_type = 1
    and source = 2
  </select>
</mapper>