<?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.TpTicketMapper" >
  <resultMap id="BaseResultMap" type="com.community.huiju.model.TpTicket" >
    <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="ticket_title" property="ticketTitle" jdbcType="VARCHAR" />
    <result column="ticket_content" property="ticketContent" jdbcType="VARCHAR" />
    <result column="status" property="status" jdbcType="CHAR" />
    <result column="type" property="type" jdbcType="CHAR" />
    <result column="repair_type" property="repairType" jdbcType="CHAR" />
    <result column="repair_name" property="repairName" jdbcType="VARCHAR" />
    <result column="tp_user_id" property="tpUserId" jdbcType="INTEGER" />
    <result column="score" property="score" jdbcType="VARCHAR" />
    <result column="comment" property="comment" jdbcType="VARCHAR" />
    <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" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, community_id, ta_user_id, ticket_title, ticket_content, status, type, repair_type,
    tp_user_id, score, comment, create_user, create_date, update_user, update_date
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from tp_ticket
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from tp_ticket
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.community.huiju.model.TpTicket" >
    insert into tp_ticket (id, community_id, ta_user_id, 
      ticket_title, ticket_content, status, 
      type, repair_type, tp_user_id, score,
      comment, create_user, create_date, 
      update_user, update_date)
    values (#{id,jdbcType=INTEGER}, #{communityId,jdbcType=INTEGER}, #{taUserId,jdbcType=INTEGER}, 
      #{ticketTitle,jdbcType=VARCHAR}, #{ticketContent,jdbcType=VARCHAR}, #{status,jdbcType=CHAR}, 
      #{type,jdbcType=CHAR}, #{repairType,jdbcType=CHAR}, #{tpUserId,jdbcType=INTEGER}, #{score,jdbcType=VARCHAR},
      #{comment,jdbcType=VARCHAR}, #{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.TpTicket" >
    insert into tp_ticket
    <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="ticketTitle != null" >
        ticket_title,
      </if>
      <if test="ticketContent != null" >
        ticket_content,
      </if>
      <if test="status != null" >
        status,
      </if>
      <if test="type != null" >
        type,
      </if>
      <if test="repairType != null" >
        repair_type,
      </if>
      <if test="tpUserId != null" >
        tp_user_id,
      </if>
      <if test="score != null" >
        score,
      </if>
      <if test="comment != null" >
        comment,
      </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="ticketTitle != null" >
        #{ticketTitle,jdbcType=VARCHAR},
      </if>
      <if test="ticketContent != null" >
        #{ticketContent,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        #{status,jdbcType=CHAR},
      </if>
      <if test="type != null" >
        #{type,jdbcType=CHAR},
      </if>
      <if test="repairType != null" >
        #{repairType,jdbcType=CHAR},
      </if>
      <if test="tpUserId != null" >
        #{tpUserId,jdbcType=INTEGER},
      </if>
      <if test="score != null" >
        #{score,jdbcType=VARCHAR},
      </if>
      <if test="comment != null" >
        #{comment,jdbcType=VARCHAR},
      </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.TpTicket" >
    update tp_ticket
    <set >
      <if test="communityId != null" >
        community_id = #{communityId,jdbcType=INTEGER},
      </if>
      <if test="taUserId != null" >
        ta_user_id = #{taUserId,jdbcType=INTEGER},
      </if>
      <if test="ticketTitle != null" >
        ticket_title = #{ticketTitle,jdbcType=VARCHAR},
      </if>
      <if test="ticketContent != null" >
        ticket_content = #{ticketContent,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        status = #{status,jdbcType=CHAR},
      </if>
      <if test="type != null" >
        type = #{type,jdbcType=CHAR},
      </if>
      <if test="repairType != null" >
        repair_type = #{repairType,jdbcType=CHAR},
      </if>
      <if test="tpUserId != null" >
        tp_user_id = #{tpUserId,jdbcType=INTEGER},
      </if>
      <if test="score != null" >
        score = #{score,jdbcType=VARCHAR},
      </if>
      <if test="comment != null" >
        comment = #{comment,jdbcType=VARCHAR},
      </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.TpTicket" >
    update tp_ticket
    set community_id = #{communityId,jdbcType=INTEGER},
      ta_user_id = #{taUserId,jdbcType=INTEGER},
      ticket_title = #{ticketTitle,jdbcType=VARCHAR},
      ticket_content = #{ticketContent,jdbcType=VARCHAR},
      status = #{status,jdbcType=CHAR},
      type = #{type,jdbcType=CHAR},
      repair_type = #{repairType,jdbcType=CHAR},
      tp_user_id = #{tpUserId,jdbcType=INTEGER},
      score = #{score,jdbcType=VARCHAR},
      comment = #{comment,jdbcType=VARCHAR},
      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="selectByCommuniytIdAndByTaUserIdAndByType" parameterType="map" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List" />
    from tp_ticket
      <trim prefix="where" prefixOverrides="and">
        <if test="communityId != null" >
          and community_id = #{communityId,jdbcType=INTEGER}
        </if>
        <if test="taUserId != null" >
          and ta_user_id = #{taUserId,jdbcType=INTEGER}
        </if>
        <if test="type != null" >
          and type = #{type,jdbcType=CHAR}
        </if>
      </trim>
    order by create_date DESC
  </select>

  <select id="selectByTicketId" resultMap="BaseResultMap" >
     SELECT
        t.id,
        t.community_id,
        t.ticket_title,
        t.ticket_content,
        t.STATUS,
        t.type,
        t.repair_type,
        d.name as repair_name,
        t.score,
        t.COMMENT,
        t.create_date
    FROM
        tp_ticket t
        LEFT JOIN sys_dictionary d ON t.repair_type = d.CODE
        AND d.group_id = ( SELECT id FROM sys_dictionary WHERE CODE = "ticket_repair_type" )
    WHERE
        t.id = #{ticketId,jdbcType=INTEGER}
        AND t.community_id = #{communityId,jdbcType=INTEGER}
</select>
  <update id="updateTpUserId" parameterType="int" >
    update tp_ticket
    set
      tp_user_id =null
    where id = #{ticketId,jdbcType=INTEGER}
  </update>
  <select id="getTicketTpUserId" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List" />
    from tp_ticket
    where id = #{ticketId,jdbcType=INTEGER} and community_id= #{communityId,jdbcType=INTEGER}
  </select>
  <select id="selectRecordStatus" resultType="int">
    select
    create_user
    from tp_ticket_record
    where ticket_id = #{ticketId,jdbcType=INTEGER}
     and community_id= #{communityId,jdbcType=INTEGER}
     and status = 4
  </select>
</mapper>