<?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.ym.mec.biz.dal.dao.QuestionnaireQuestionDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.QuestionnaireQuestion" id="QuestionnaireQuestion">
		<result column="id_" property="id" />
		<result column="questionnaire_topic_id_" property="questionnaireTopicId" />
		<result column="content_" property="content" />
		<result column="type_" property="type" />
		<result column="sort_" property="sort" />
		<result column="is_require_" property="isRequire"/>
		<result column="desc_" property="desc" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<collection property="questionnaireQuestionItemList" ofType="com.ym.mec.biz.dal.entity.QuestionnaireQuestionItem">
			<result column="item_id_" property="id" />
			<result column="item_questionnaire_topic_id_" property="questionnaireTopicId" />
			<result column="item_questionnaire_question_id_" property="questionnaireQuestionId" />
			<result column="item_answer_value_" property="answerValue" />
			<result column="item_sort_" property="sort" />
			<result column="item_is_over_" property="isOver"/>
			<result column="item_create_time_" property="createTime" />
			<result column="item_update_time_" property="updateTime" />
		</collection>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="QuestionnaireQuestion" >
		SELECT * FROM questionnaire_question WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="QuestionnaireQuestion">
		SELECT qq.*,qqi.answer_value_ item_answer_value_,qqi.create_time_ item_create_time_,qqi.id_ item_id_,qqi.is_over_ item_is_over_,
			   qqi.questionnaire_question_id_ item_questionnaire_question_id_,qqi.questionnaire_topic_id_ item_questionnaire_topic_id_,
		       qqi.sort_ item_sort_,qqi.update_time_ item_update_time_
		FROM questionnaire_question qq
		LEFT JOIN questionnaire_question_item qqi ON qq.id_ = qqi.questionnaire_question_id_
		<where>
			<if test="topicId != null">
				AND qq.questionnaire_topic_id_ = #{topicId}
			</if>
		</where>
		ORDER BY qq.sort_,qq.id_,qqi.sort_,qqi.id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.QuestionnaireQuestion" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO questionnaire_question (questionnaire_topic_id_,content_,type_,sort_,is_require_,desc_,create_time_,update_time_)
		VALUES(#{questionnaireTopicId},#{content},#{type},#{sort},#{isRequire},#{desc},NOW(),NOW())
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.QuestionnaireQuestion">
		UPDATE questionnaire_question <set>
		<if test="updateTime != null">
			update_time_ = #{updateTime},
		</if>
		<if test="content != null">
			content_ = #{content},
		</if>
		<if test="sort != null">
			sort_ = #{sort},
		</if>
		<if test="type != null">
			type_ = #{type},
		</if>
		<if test="isRequire != null">
			is_require_ = #{isRequire},
		</if>
		<if test="desc != null">
			desc_ = #{desc},
		</if>
		</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<update id="delete" >
		DELETE FROM questionnaire_question WHERE id_ = #{id}
	</update>

    <delete id="batchDel">
		DELETE FROM questionnaire_question WHERE id_ IN
		<foreach collection="questionIdList" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
	</delete>

	<sql id="queryPageSql">
		<where>
			<if test="topicId != null">
				AND questionnaire_topic_id_ = #{topicId}
			</if>
		</where>
	</sql>
    <!-- 分页查询 -->
	<select id="queryPage" resultMap="QuestionnaireQuestion" parameterType="map">
		SELECT * FROM questionnaire_question
		<include refid="queryPageSql"/>
		ORDER BY id_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM questionnaire_question
		<include refid="queryPageSql"/>
	</select>
    <select id="findByTopicId" resultMap="QuestionnaireQuestion">
		SELECT * FROM questionnaire_question WHERE questionnaire_topic_id_ = #{topicId}
	</select>

	<select id="queryQuestionList" resultMap="QuestionnaireQuestion">
		SELECT qq.* FROM replacement_instrument_cooperation ric
		LEFT JOIN questionnaire_question qq ON qq.questionnaire_topic_id_ = ric.topic_id_
		WHERE ric.id_ = #{cooperationId} ORDER BY sort_
	</select>
</mapper>
