<?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="uuid" property="uuid" jdbcType="INTEGER"/>
        <result column="uuid_name" property="uuidName" jdbcType="VARCHAR"/>
        <result column="uuid_type" property="uuidType" jdbcType="VARCHAR"/>
        <result column="transaction_title" property="transactionTitle" jdbcType="VARCHAR"/>
        <result column="transaction_content" property="transactionContent" jdbcType="VARCHAR"/>
        <result column="sort" property="sort" 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"/>
        <result column="release_identity" property="releaseIdentity" jdbcType="VARCHAR"/>
        <result column="room_no_name" property="roomNoName" jdbcType="VARCHAR"/>

    </resultMap>
    <sql id="Base_Column_List">
    id, community_id, transaction_title, transaction_content, sort,uuid,uuid_type,uuid_name,
    view_count, status, type, create_user, create_date, update_user, update_date, release_identity,room_no_name
  </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="com.community.huiju.model.TpTransaction">
    select
    <include refid="Base_Column_List"/>
    from tp_transaction ttr
    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,release_identity,
      ta_user_verify_id,
      ta_user_id,
      transaction_title, transaction_content,
      sort,
      view_count, status,
      type, create_user, create_date, 
      update_user, update_date,room_no_name)
    values (#{id,jdbcType=INTEGER}, #{communityId,jdbcType=INTEGER},#{releaseIdentity,jdbcType=VARCHAR},#{taUserVerifyId,jdbcType=INTEGER}, #{taUserId,jdbcType=INTEGER},
      #{transactionTitle,jdbcType=VARCHAR}, #{transactionContent,jdbcType=VARCHAR}, #{sort,jdbcType=VARCHAR},
       #{viewCount,jdbcType=INTEGER}, #{status,jdbcType=CHAR},
      #{type,jdbcType=CHAR}, #{createUser,jdbcType=INTEGER}, #{createDate,jdbcType=TIMESTAMP}, 
      #{updateUser,jdbcType=INTEGER}, #{updateDate,jdbcType=TIMESTAMP},#{roomNoName,jdbcType=VARCHAR})
  </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="uuid != null">
                uuid,
            </if>
            <if test="uuidType != null">
                uuid_type,
            </if>
            <if test="uuidName != null">
                uuid_Name,
            </if>
            <if test="transactionTitle != null">
                transaction_title,
            </if>
            <if test="transactionContent != null">
                transaction_content,
            </if>
            <if test="sort != null">
                sort,
            </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>
            <if test="releaseIdentity != null">
                release_identity,
            </if>
            <if test="roomNoName != null">
                room_no_name
            </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="uuid != null">
                #{uuid,jdbcType=INTEGER},
            </if>
            <if test="uuidType != null">
                #{uuidType,jdbcType=INTEGER},
            </if>
            <if test="uuidName != null">
                #{uuidName,jdbcType=INTEGER},
            </if>
            <if test="transactionTitle != null">
                #{transactionTitle,jdbcType=VARCHAR},
            </if>
            <if test="transactionContent != null">
                #{transactionContent,jdbcType=VARCHAR},
            </if>
            <if test="sort != null">
                #{sort,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>
            <if test="releaseIdentity != null">
                #{releaseIdentity,jdbcType=VARCHAR},
            </if>
            <if test="roomNoName != null">
                #{roomNoName,jdbcType=VARCHAR}
            </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="transactionTitle != null">
                transaction_title = #{transactionTitle,jdbcType=VARCHAR},
            </if>
            <if test="transactionContent != null">
                transaction_content = #{transactionContent,jdbcType=VARCHAR},
            </if>
            <if test="sort != null">
                sort = #{sort,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>
            <if test="roomNoName != null">
                room_no_name = #{roomNoName,jdbcType=VARCHAR},
            </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},
      sort = #{sort,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},
      room_no_name = #{roomNoName,jdbcType=VARCHAR}
    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.community_id=#{communityId,jdbcType=INTEGER}
        AND t.uuid = #{id} and t.uuid_type = 'app'
        <if test="type != null and type != ''">
            and t.type = #{type,jdbcType=INTEGER}
        </if>
        order by create_date DESC
    </select>

    <update id="updateTransaction" parameterType="com.community.huiju.model.TpTransaction">
    update tp_transaction
    set
    uuid = #{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,create_date)
    values (#{communityId,jdbcType=INTEGER}, #{transactionId,jdbcType=INTEGER},
      #{reportReasonId,jdbcType=VARCHAR}, #{userId,jdbcType=INTEGER},#{createDate,jdbcType=TIMESTAMP})
  </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>