<?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.QuestionnaireQuestionItemDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.QuestionnaireQuestionItem" id="QuestionnaireQuestionItem">
		<result column="id_" property="id" />
		<result column="questionnaire_topic_id_" property="questionnaireTopicId" />
		<result column="questionnaire_question_id_" property="questionnaireQuestionId" />
		<result column="answer_value_" property="answerValue" />
		<result column="sort_" property="sort" />
		<result column="is_over_" property="isOver"/>
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
        <result column="tenant_id_" property="tenantId"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="QuestionnaireQuestionItem" >
		SELECT * FROM questionnaire_question_item WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="QuestionnaireQuestionItem">
		SELECT * FROM questionnaire_question_item 
		<where>
			tenant_id_ = #{tenantId}
			<if test="questionIdList != null and questionIdList.size > 0">
				AND questionnaire_question_id_ IN
				<foreach collection="questionIdList" open="(" close=")" separator="," item="item">
					#{item}
				</foreach>
			</if>
		</where>
		ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.QuestionnaireQuestionItem" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO questionnaire_question_item (questionnaire_topic_id_,questionnaire_question_id_,answer_value_,sort_,is_over_,create_time_,update_time_,tenant_id_)
		VALUES(#{questionnaireTopicId},#{questionnaireQuestionId},#{answerValue},#{sort},#{isOver},NOW(),NOW(),#{tenantId})
	</insert>

    <insert id="batchInsert" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO questionnaire_question_item (questionnaire_topic_id_,questionnaire_question_id_,answer_value_,sort_,is_over_,create_time_,update_time_,tenant_id_)
		VALUES
		<foreach collection="questionItemList" item="item" separator=",">
			(#{topicId},#{questionId},#{item.answerValue},#{item.sort},#{item.isOver},NOW(),NOW(),#{tenantId})
		</foreach>
	</insert>

    <!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.QuestionnaireQuestionItem">
		UPDATE questionnaire_question_item <set>
		<if test="updateTime != null">
			update_time_ = #{updateTime},
		</if>
		<if test="sort != null">
			sort_ = #{sort},
		</if>
		<if test="isOver != null">
			is_over_ = #{isOver},
		</if>
		<if test="answerValue != null">
			answer_value_ = #{answerValue},
		</if>
		</set> WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<update id="delete" >
		DELETE FROM questionnaire_question_item WHERE id_ = #{id}
	</update>
	<update id="batchDelByQuestionIdList">
		DELETE FROM questionnaire_question_item WHERE questionnaire_question_id_ IN
		<foreach collection="questionIdList" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
	</update>
	<update id="batchDel">
		DELETE FROM questionnaire_question_item WHERE id_ IN
		<foreach collection="itemIdList" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
	</update>
	<update id="batchUpdate">
		<foreach collection="updateItemList" item="item" separator=";">
			UPDATE questionnaire_question_item
			<set>
				<if test="item.sort != null">
					sort_ = #{item.sort},
				</if>
				<if test="item.isOver != null">
					is_over_ = #{item.isOver},
				</if>
				<if test="item.answerValue != null">
					answer_value_ = #{item.answerValue},
				</if>
					update_time_ = NOW()
			</set>
			WHERE id_ = #{item.id}
		</foreach>
	</update>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="QuestionnaireQuestionItem" parameterType="map">
		SELECT * FROM questionnaire_question_item where tenant_id_ = #{tenantId} ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM questionnaire_question_item where tenant_id_ = #{tenantId}
	</select>
    <select id="findByQuestionId" resultMap="QuestionnaireQuestionItem">
		SELECT * FROM questionnaire_question_item WHERE questionnaire_question_id_ = #{questionId} LIMIT 1
	</select>
	<select id="queryUserName" resultType="java.util.Map">
		SELECT user_id_ `key`,user_name_ `value` FROM replacement_instrument_activity
		WHERE replacement_instrument_cooperation_id_ = #{cooperationId}
	</select>
</mapper>
