<?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">
<!-- 命名，每一个映射对象不一样
	namespace:必须与对应的接口全类名一致
 -->
<mapper namespace="com.iamberry.wechat.service.mapper.QuestionnairePaperMapper">
 
	<sql id="allField">
 		 paper_id paperId,
 		 paper_name paperName,
 		 paper_pic paperPic,
 		 paper_number paperNumber,
 		 paper_share_number paperShareNumber,
 		 paper_create_date paperCreateDate,
 		 paper_status paperStatus,
 		 page_start_month pageStartMonth,
 		 page_end_month pageEndMonth
	</sql>
 
 	<!-- 查询试卷列表 分页 -->
 	<select id="getQuestionnairePaperList" resultType="QuestionnairePaper" parameterType="QuestionnairePaper">
 		SELECT 
 			<include refid="allField"></include>
 		FROM tb_iamberry_questionnaire_paper
 		<where>
 			<if test="paperName !=null and paperName != '' ">
				paper_name LIKE CONCAT('%',#{paperName},'%')
		    </if>
		    <if test="paperStatus !=null and paperStatus != '' ">
				AND paper_status = #{paperStatus}
		    </if>
 		</where>
		ORDER BY paper_create_date DESC
		<if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
	     LIMIT ${page.recordBegin},${page.pageSize}
	    </if>
	     <if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
	     LIMIT ${page.pageSize}
	    </if>
 	</select>
 	<!-- 查询试卷总数 -->
 	<select id="getQuestionnairePaperCount" resultType="Integer">
 		SELECT COUNT(1) FROM tb_iamberry_questionnaire_paper
 	</select>
 
 
 <!-- 根据试卷id获取，试卷信息 -->
 <select id="getQuestionnairePaperQuesOptiList" resultType="QuestionnairePaperDto" parameterType="Integer">		
	<!-- SELECT 
	paper.paper_id   paperId,
	paper.paper_name paperName,
	question.question_id questionId,
	question.question_name questionName,
	op.option_id optionId,
	op.option_name optionName,
	testpaper.test_id testId,
	testpaper.test_score testScore,
	testpaper.test_is_chekcbox testIsChekcbox
	 FROM 
	(SELECT * FROM tb_iamberry_questionnaire_test_paper WHERE test_paper_id = #{paperId}) testpaper 
	LEFT JOIN tb_iamberry_questionnaire_paper paper ON  paper.paper_id = testpaper.test_paper_id 
	LEFT JOIN tb_iamberry_questionnaire_question question ON question.question_id = testpaper.test_question_id
	LEFT JOIN tb_iamberry_questionnaire_option op ON op.option_id = testpaper.test_option_id -->
	SELECT
	p.paper_id  paperId,
	p.paper_name  paperName,
	Q.question_id  questionId,
	Q.question_name  questionName,
	O.option_id  optionId,
	O.option_name  optionName,
	O.option_score  score,
	Q.question_is_chekcbox  isChekcbox,
	O.option_id  testId,
	Q.question_order questionOrder
FROM
	tb_iamberry_questionnaire_paper P
LEFT JOIN tb_iamberry_questionnaire_question Q ON p.paper_id = Q.question_paper_id
LEFT JOIN tb_iamberry_questionnaire_option O ON Q.question_id = O.option_quesion_id
WHERE
P.PAPER_ID = #{paperId}
ORDER BY Q.QUESTION_ORDER,O.OPTION_ORDER
		   
</select>
 
 
 
 <update id="updateQuestionnairePaperByType" parameterType="QuestionnairePaperDto">
 		
 	
		<if test="type == 1 ">
			update tb_iamberry_questionnaire_question
			set question_name = #{content}
			where question_id = #{id}
		</if>
		<if test="type == 2 ">
			update tb_iamberry_questionnaire_option
			set option_name = #{content}
			where option_id = #{id}
		</if>
		<if test="type == 3 ">
			update tb_iamberry_questionnaire_option
			set option_score = #{content}
			where option_id = #{id}
		</if>
 		<if test="type == 4 ">
			update tb_iamberry_questionnaire_question
			set question_is_chekcbox = #{content}
			where question_id = #{id}
		</if>
 	</update>
 	
 	<!-- 添加一种试卷 -->
 	<insert id="insertQuestionnairePaper" parameterType="QuestionnairePaper"
 		useGeneratedKeys="true" keyProperty="paperId">
 		INSERT INTO tb_iamberry_questionnaire_paper
		(
			paper_name ,
			paper_pic,
			page_start_month ,
			page_end_month,
			paper_status
			
		)
		VALUES 
		(
			#{paperName},#{paperPic},#{pageStartMonth},#{pageEndMonth},#{paperStatus}
		)
 	</insert>
 	
 	
 	
 	
 	
 	
 	
 	<!-- 根据id查询单个试卷 -->
 	<select id="QuestionnairePaper" resultType="QuestionnairePaper" parameterType="Integer">
 		SELECT 
 			<include refid="allField"></include>
 		FROM tb_iamberry_questionnaire_paper
 		WHERE paper_id=#{paperId}
 	</select>
 	
 	
 	<update id="updateQuestionnairePaperById" parameterType="QuestionnairePaper">
 		UPDATE tb_iamberry_questionnaire_paper
 		<set>
 			<if test="paperName !=null and paperName !=''">
	 			paper_name=#{paperName},
 			</if>
 			<if test="paperPic !=null and paperPic !=''">
	 			paper_pic=#{paperPic} ,
 			</if>
 		</set>
 		WHERE paper_id=#{paperId}
 	</update>
 	
 	<delete id="deleteQuestionnairePaperById" parameterType="Integer">
 		DELETE FROM tb_iamberry_questionnaire_paper 
 		WHERE paper_id=#{paperId}
 	</delete>
 	<!-- 根据试卷id分配题目 -->
	<update id="updatePaperQuestionById" parameterType="QuestionnaireQuestion">
		UPDATE 
			TB_IAMBERRY_QUESTIONNAIRE_QUESTION
		SET  
			question_paper_id = #{questionId},
			question_is_chekcbox = #{questionIsChekcbox}
		WHERE
			question_id = #{questionPaperId}
	</update>
</mapper>