<?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.TpTransactionMapper" >
  <resultMap id="BaseResultMap" type="com.community.huiju.model.TpTransaction" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="community_id" property="communityId" jdbcType="INTEGER" />
    <result column="ta_user_id" property="taUserId" jdbcType="INTEGER" />
    <result column="transaction_title" property="transactionTitle" jdbcType="VARCHAR" />
    <result column="transaction_content" property="transactionContent" jdbcType="VARCHAR" />
    <result column="view_count" property="viewCount" jdbcType="INTEGER" />
    <result column="status" property="status" jdbcType="CHAR" />
    <result column="type" property="type" 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="role_id" property="roleId" jdbcType="INTEGER" />
    <result column="role_name" property="roleName" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, community_id, ta_user_id, transaction_title, transaction_content,
    view_count, status, type, create_user, create_date, update_user, update_date
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="com.community.huiju.model.TpTransaction"  >
    SELECT
	ttr.id,
	ttr.community_id,
	ttr.ta_user_id,
	ttr.transaction_title,
	ttr.transaction_content,
	ttr.view_count,
	ttr.STATUS,
	ttr.type,
	ttr.create_user,
	ttr.create_date,
	ttr.update_user,
	ttr.update_date,
	sr.id AS role_id,
	sr.role_name AS role_name
FROM
	tp_transaction ttr
LEFT JOIN ta_sys_user_role tsur ON ttr.ta_user_id = tsur.user_id
LEFT JOIN ta_sys_role sr ON sr.id = tsur.role_id
    where ttr.id = #{id,jdbcType=INTEGER} and ttr.community_id=#{communityId,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from tp_transaction
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.community.huiju.model.TpTransaction" >
    insert into tp_transaction (id, community_id, ta_user_id, 
      transaction_title, transaction_content,
      view_count, status,
      type, create_user, create_date, 
      update_user, update_date)
    values (#{id,jdbcType=INTEGER}, #{communityId,jdbcType=INTEGER}, #{taUserId,jdbcType=INTEGER}, 
      #{transactionTitle,jdbcType=VARCHAR}, #{transactionContent,jdbcType=VARCHAR},
       #{viewCount,jdbcType=INTEGER}, #{status,jdbcType=CHAR},
      #{type,jdbcType=CHAR}, #{createUser,jdbcType=INTEGER}, #{createDate,jdbcType=TIMESTAMP}, 
      #{updateUser,jdbcType=INTEGER}, #{updateDate,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" useGeneratedKeys="true" keyProperty="id"  parameterType="com.community.huiju.model.TpTransaction" >
    insert into tp_transaction
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="communityId != null" >
        community_id,
      </if>
      <if test="taUserId != null" >
        ta_user_id,
      </if>
      <if test="transactionTitle != null" >
        transaction_title,
      </if>
      <if test="transactionContent != null" >
        transaction_content,
      </if>
      <if test="viewCount != null" >
        view_count,
      </if>
      <if test="status != null" >
        status,
      </if>
      <if test="type != null" >
        type,
      </if>
      <if test="isReported != null" >
        is_reported,
      </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>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="communityId != null" >
        #{communityId,jdbcType=INTEGER},
      </if>
      <if test="taUserId != null" >
        #{taUserId,jdbcType=INTEGER},
      </if>
      <if test="transactionTitle != null" >
        #{transactionTitle,jdbcType=VARCHAR},
      </if>
      <if test="transactionContent != null" >
        #{transactionContent,jdbcType=VARCHAR},
      </if>
      <if test="viewCount != null" >
        #{viewCount,jdbcType=INTEGER},
      </if>
      <if test="status != null" >
        #{status,jdbcType=CHAR},
      </if>
      <if test="type != null" >
        #{type,jdbcType=CHAR},
      </if>
      <if test="isReported != null" >
        #{isReported,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>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.community.huiju.model.TpTransaction" >
    update tp_transaction
    <set >
      <if test="communityId != null" >
        community_id = #{communityId,jdbcType=INTEGER},
      </if>
      <if test="taUserId != null" >
        ta_user_id = #{taUserId,jdbcType=INTEGER},
      </if>
      <if test="transactionTitle != null" >
        transaction_title = #{transactionTitle,jdbcType=VARCHAR},
      </if>
      <if test="transactionContent != null" >
        transaction_content = #{transactionContent,jdbcType=VARCHAR},
      </if>
      <if test="viewCount != null" >
        view_count = #{viewCount,jdbcType=INTEGER},
      </if>
      <if test="isReported != null and isReported !=''" >
        is_reported = #{isReported,jdbcType=CHAR},
      </if>
      <if test="status != null" >
        status = #{status,jdbcType=CHAR},
      </if>
      <if test="type != null" >
        type = #{type,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>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.community.huiju.model.TpTransaction" >
    update tp_transaction
    set community_id = #{communityId,jdbcType=INTEGER},
      ta_user_id = #{taUserId,jdbcType=INTEGER},
      transaction_title = #{transactionTitle,jdbcType=VARCHAR},
      transaction_content = #{transactionContent,jdbcType=VARCHAR},
      view_count = #{viewCount,jdbcType=INTEGER},
      status = #{status,jdbcType=CHAR},
      type = #{type,jdbcType=CHAR},
      create_user = #{createUser,jdbcType=INTEGER},
      create_date = #{createDate,jdbcType=TIMESTAMP},
      update_user = #{updateUser,jdbcType=INTEGER},
      update_date = #{updateDate,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="findAllTransaction" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from tp_transaction
    where community_id = #{communityId,jdbcType=INTEGER}
    <if test="type != null and type != ''">
      and type = #{type,jdbcType=VARCHAR}
    </if>
    <if test="transactionTitle != null and transactionTitle !=''">
      and transaction_title LIKE concat('%',#{transactionTitle,jdbcType=VARCHAR},'%')
    </if>
    and status = 1
    order by update_date DESC, create_date DESC
  </select>

  <select id="selectTransaction" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List" />
    FROM
    tp_transaction t
    WHERE t.create_user=#{id,jdbcType=INTEGER}
    AND t.community_id=#{communityId,jdbcType=INTEGER}
    UNION ALL
    SELECT
    <include refid="Base_Column_List" />
    FROM
    tp_transaction t
    INNER JOIN(SELECT
    y.transaction_id
    FROM
    tp_transaction_reply y
    LEFT JOIN tp_transaction t ON y.transaction_id = t.id
    WHERE
    y.ta_user_id = #{id,jdbcType=INTEGER}
    <if test="type != null and type != ''">
      and  t.type = #{type,jdbcType=INTEGER}
    </if>
    AND y.community_id = #{communityId,jdbcType=INTEGER}
    AND y.transaction_id NOT IN ( SELECT t.id FROM tp_transaction t WHERE t.create_user = #{id,jdbcType=INTEGER} AND t.community_id = #{communityId,jdbcType=INTEGER}  )
    GROUP BY
    y.transaction_id) a ON a.transaction_id = t.id
    order by create_date DESC
  </select>

  <update id="updateTransaction" parameterType="com.community.huiju.model.TpTransaction" >
    update tp_transaction
    set
    ta_user_id = #{userId,jdbcType=INTEGER},
    transaction_title = #{transactionTitle,jdbcType=VARCHAR},
    transaction_content = #{transactionContent,jdbcType=VARCHAR},
    update_user = #{userId,jdbcType=INTEGER},
    update_date = now()
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="getById" resultMap="BaseResultMap" parameterType="com.community.huiju.model.TpTransaction">
    select
    <include refid="Base_Column_List" />
    from tp_transaction
    where id = #{id,jdbcType=INTEGER} and status = 1
    order by create_date DESC
  </select>

  <select id="getReportReasons" resultType="java.util.Map">
    SELECT id,reason FROM sys_report_reason ORDER BY sort
  </select>

  <insert id="saveReportReason">
    insert into tp_transaction_report (community_id, transaction_id,
      report_reason_id, reporter_id)
    values (#{communityId,jdbcType=INTEGER}, #{transactionId,jdbcType=INTEGER},
      #{reportReasonId,jdbcType=VARCHAR}, #{userId,jdbcType=INTEGER})
  </insert>
  <select id="getUserTransactionList" resultMap="BaseResultMap">

SELECT
    <include refid="Base_Column_List" />
    FROM
      tp_transaction t
    WHERE t.create_user=#{id,jdbcType=INTEGER}
    AND t.community_id=#{communityId,jdbcType=INTEGER}
    UNION ALL
    SELECT
        <include refid="Base_Column_List" />
    FROM
      tp_transaction t
    INNER JOIN(SELECT
      y.transaction_id
    FROM
      tp_transaction_reply y
      LEFT JOIN tp_transaction t ON y.transaction_id = t.id
    WHERE
      y.ta_user_id = #{id,jdbcType=INTEGER}
      AND y.community_id = #{communityId,jdbcType=INTEGER}
      AND y.transaction_id NOT IN ( SELECT t.id FROM tp_transaction t WHERE t.create_user = #{id,jdbcType=INTEGER} AND t.community_id = #{communityId,jdbcType=INTEGER}  )
    GROUP BY
      y.transaction_id) a ON a.transaction_id = t.id
    order by create_date DESC
  </select>
</mapper>