<?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.TpTransactionReplyMapper" >
  <resultMap id="BaseResultMap" type="com.community.huiju.model.TpTransactionReply" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="community_id" property="communityId" jdbcType="INTEGER" />
    <result column="transaction_id" property="transactionId" jdbcType="INTEGER" />
    <result column="ta_user_id" property="taUserId" jdbcType="INTEGER" />
    <result column="ta_user_name" property="taUserName" jdbcType="VARCHAR" />
    <result column="reply_content" property="replyContent" jdbcType="VARCHAR" />
    <result column="reply_ta_user_id" property="replyTaUserId" jdbcType="INTEGER" />
    <result column="reply_ta_user_name" property="replyTaUserName" jdbcType="VARCHAR" />
    <result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, community_id, transaction_id, ta_user_id, ta_user_name, reply_content, reply_ta_user_id, 
    reply_ta_user_name, create_date
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from tp_transaction_reply
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from tp_transaction_reply
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.community.huiju.model.TpTransactionReply" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
    insert into tp_transaction_reply (id, community_id, transaction_id, 
      ta_user_id, ta_user_name, reply_content, 
      reply_ta_user_id, reply_ta_user_name, create_date
      )
    values (#{id,jdbcType=INTEGER}, #{communityId,jdbcType=INTEGER}, #{transactionId,jdbcType=INTEGER}, 
      #{taUserId,jdbcType=INTEGER}, #{taUserName,jdbcType=VARCHAR}, #{replyContent,jdbcType=VARCHAR}, 
      #{replyTaUserId,jdbcType=INTEGER}, #{replyTaUserName,jdbcType=VARCHAR}, #{createDate,jdbcType=TIMESTAMP}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.community.huiju.model.TpTransactionReply" >
    insert into tp_transaction_reply
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="communityId != null" >
        community_id,
      </if>
      <if test="transactionId != null" >
        transaction_id,
      </if>
      <if test="taUserId != null" >
        ta_user_id,
      </if>
      <if test="taUserName != null" >
        ta_user_name,
      </if>
      <if test="replyContent != null" >
        reply_content,
      </if>
      <if test="replyTaUserId != null" >
        reply_ta_user_id,
      </if>
      <if test="replyTaUserName != null" >
        reply_ta_user_name,
      </if>
      <if test="createDate != null" >
        create_date,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="communityId != null" >
        #{communityId,jdbcType=INTEGER},
      </if>
      <if test="transactionId != null" >
        #{transactionId,jdbcType=INTEGER},
      </if>
      <if test="taUserId != null" >
        #{taUserId,jdbcType=INTEGER},
      </if>
      <if test="taUserName != null" >
        #{taUserName,jdbcType=VARCHAR},
      </if>
      <if test="replyContent != null" >
        #{replyContent,jdbcType=VARCHAR},
      </if>
      <if test="replyTaUserId != null" >
        #{replyTaUserId,jdbcType=INTEGER},
      </if>
      <if test="replyTaUserName != null" >
        #{replyTaUserName,jdbcType=VARCHAR},
      </if>
      <if test="createDate != null" >
        #{createDate,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.community.huiju.model.TpTransactionReply" >
    update tp_transaction_reply
    <set >
      <if test="communityId != null" >
        community_id = #{communityId,jdbcType=INTEGER},
      </if>
      <if test="transactionId != null" >
        transaction_id = #{transactionId,jdbcType=INTEGER},
      </if>
      <if test="taUserId != null" >
        ta_user_id = #{taUserId,jdbcType=INTEGER},
      </if>
      <if test="taUserName != null" >
        ta_user_name = #{taUserName,jdbcType=VARCHAR},
      </if>
      <if test="replyContent != null" >
        reply_content = #{replyContent,jdbcType=VARCHAR},
      </if>
      <if test="replyTaUserId != null" >
        reply_ta_user_id = #{replyTaUserId,jdbcType=INTEGER},
      </if>
      <if test="replyTaUserName != null" >
        reply_ta_user_name = #{replyTaUserName,jdbcType=VARCHAR},
      </if>
      <if test="createDate != null" >
        create_date = #{createDate,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.community.huiju.model.TpTransactionReply" >
    update tp_transaction_reply
    set community_id = #{communityId,jdbcType=INTEGER},
      transaction_id = #{transactionId,jdbcType=INTEGER},
      ta_user_id = #{taUserId,jdbcType=INTEGER},
      ta_user_name = #{taUserName,jdbcType=VARCHAR},
      reply_content = #{replyContent,jdbcType=VARCHAR},
      reply_ta_user_id = #{replyTaUserId,jdbcType=INTEGER},
      reply_ta_user_name = #{replyTaUserName,jdbcType=VARCHAR},
      create_date = #{createDate,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <select id="getCommunityIdAndTransactionId" resultMap="BaseResultMap">
    SELECT
	tr.*, (SELECT tsr.role_name FROM ta_sys_user_role tsur LEFT JOIN ta_sys_role tsr ON tsur.role_id = tsr.id WHERE tsur.user_id = tr.ta_user_id) AS taRoleName,
	      (SELECT tsr.role_name FROM ta_sys_user_role tsur LEFT JOIN ta_sys_role tsr ON tsur.role_id = tsr.id WHERE tsur.user_id = tr.reply_ta_user_id) AS replyRoleName
    FROM
        tp_transaction_reply tr
    WHERE
	tr.community_id = 45
      <if test="type != null and type != '' and type == 2" >
      AND (tr.reply_ta_user_id = (SELECT r.id from tp_user r WHERE tr.ta_user_id = r.id )) OR (ta_user_id = (SELECT r.id from tp_user r WHERE tr.ta_user_id = r.id ))
      </if>
      <if test="type != null and type != '' and type == 1" >
      AND (tr.ta_user_id =#{userID}) OR (tr.reply_ta_user_id = #{userID})
      </if>
      <if test="type != null and type != '' and type == 0" >
      AND (tr.ta_user_id =#{founderUserID}) OR (tr.reply_ta_user_id = #{founderUserID})
      </if>
   </select>
   <select id="selectUserTransactionReply" resultMap="BaseResultMap">
     select
     <include refid="Base_Column_List" />
     from tp_transaction_reply
     where  ta_user_id = #{id}
     GROUP BY transaction_id
   </select>
</mapper>