<?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.dgtis.dorm.manager.db.dao.QuestionsAnswersMapper" >
  <resultMap id="BaseResultMap" type="com.dgtis.dorm.manager.db.domain.QuestionsAnswers" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="questions" property="questions" jdbcType="VARCHAR" />
    <result column="answers" property="answers" jdbcType="VARCHAR" />
    <result column="creator_id" property="creatorId" jdbcType="INTEGER" />
    <result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
    <result column="creator" property="creator" jdbcType="VARCHAR" />
    <result column="is_answer" property="isAnswer" jdbcType="BIT" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, questions, answers, creator_id, create_date, creator, is_answer
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
     select 
    <include refid="Base_Column_List" />
    ,au.user_name as creator
    from zhll_questions_answers
    left join zhll_user au
    on zhll_questions_answers.creator_id = au.id
    where zhll_questions_answers.id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from zhll_questions_answers
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.dgtis.dorm.manager.db.domain.QuestionsAnswers" >
    insert into zhll_questions_answers (id, questions, answers,
      creator_id, create_date, is_answer
      )
    values (#{id,jdbcType=INTEGER}, #{questions,jdbcType=VARCHAR}, #{answers,jdbcType=VARCHAR}, 
      #{creatorId,jdbcType=INTEGER}, #{createDate,jdbcType=TIMESTAMP}, #{isAnswer,jdbcType=BIT}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.dgtis.dorm.manager.db.domain.QuestionsAnswers" >
    insert into zhll_questions_answers
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="questions != null" >
        questions,
      </if>
      <if test="answers != null" >
        answers,
      </if>
      <if test="creatorId != null" >
        creator_id,
      </if>
      <if test="createDate != null" >
        create_date,
      </if>
      <if test="isAnswer != null" >
        is_answer,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="questions != null" >
        #{questions,jdbcType=VARCHAR},
      </if>
      <if test="answers != null" >
        #{answers,jdbcType=VARCHAR},
      </if>
      <if test="creatorId != null" >
        #{creatorId,jdbcType=INTEGER},
      </if>
      <if test="createDate != null" >
        #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="isAnswer != null" >
        #{isAnswer,jdbcType=BIT},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.dgtis.dorm.manager.db.domain.QuestionsAnswers" >
    update zhll_questions_answers
    <set >
      <if test="questions != null" >
        questions = #{questions,jdbcType=VARCHAR},
      </if>
      <if test="answers != null" >
        answers = #{answers,jdbcType=VARCHAR},
      </if>
      <if test="creatorId != null" >
        creator_id = #{creatorId,jdbcType=INTEGER},
      </if>
      <if test="createDate != null" >
        create_date = #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="isAnswer != null" >
        is_answer = #{isAnswer,jdbcType=BIT},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.dgtis.dorm.manager.db.domain.QuestionsAnswers" >
    update zhll_questions_answers
    set questions = #{questions,jdbcType=VARCHAR},
      answers = #{answers,jdbcType=VARCHAR},
      creator_id = #{creatorId,jdbcType=INTEGER},
      create_date = #{createDate,jdbcType=TIMESTAMP},
      is_answer = #{isAnswer,jdbcType=BIT}
    where id = #{id,jdbcType=INTEGER}
  </update>
  
  <select id="selectByExample" parameterType="com.dgtis.dorm.manager.db.domain.QuestionsAnswers" resultMap="BaseResultMap">
    select distinct
    aqa.*, au.user_name as creator
    from zhll_questions_answers aqa
      left join zhll_user au
      on aqa.creator_id = au.id 
      where 1=1 
      <if test="questions != null and questions != '' ">
        and aqa.questions like concat('%',#{questions}, '%')
      </if>
      <if test="isAnswer != null">
        and aqa.is_answer = #{isAnswer}
      </if>
      order by aqa.create_date DESC
  </select>
  
  <select id="countByExample" parameterType="com.dgtis.dorm.manager.db.domain.QuestionsAnswers" resultType="java.lang.Integer">
    select count(*) from zhll_questions_answers
    <where>
      <if test="questions != null and questions != ''">
        and questions like concat('%',#{questions}, '%')
      </if>
      <if test="isAnswer != null">
        and is_answer = #{isAnswer}
      </if>
    </where>
  </select>
  
    
  <select id="selectBy" parameterType="com.dgtis.dorm.manager.db.domain.QuestionsAnswers" resultType="java.util.Map">
  	select 
	aqa.id,
	aqa.questions,
	aqa.answers,
	DATE_FORMAT(aqa.create_date,"%Y-%m-%d %H:%i:%s") AS createDate, 
	aqa.is_answer as isAnswer
	from zhll_questions_answers aqa where 1=1
	<if test="creatorId != null and creatorId != ''">
        and aqa.creator_id = #{creatorId}
    </if>
    <if test="isAnswer != null">
      and aqa.is_answer = #{isAnswer}
    </if>
	ORDER BY aqa.create_date DESC
  </select>
</mapper>