<?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.iamberry.wechat.service.mapper.QuestionnaireMapper">
	<sql id="questionnaireQuestion">
		Q.QUESTION_ID questionId,
		Q.QUESTION_NAME questionName,
		Q.QUESTION_DESC questionDesc,
		Q.QUESTION_UPDATE_DATE questionUpdateDate,
		Q.QUESTION_CREATE_DATE questionCreateDate,
		Q.QUESTION_STATU questionStatu,
		Q.QUESTION_PIC questionPic,
		Q.QUESTION_CALCULATION questionCalculation,
		Q.QUESTION_ORDER questionOrder,
		Q.QUESTION_TYPE questionType
	</sql>
	<!-- 分页查询所有题目 -->
	<select id="getAllProblem" parameterType="QuestionnaireQuestionDto" resultType="QuestionnaireQuestionDto">
		SELECT 
			<include refid="questionnaireQuestion" />
		FROM 
			TB_IAMBERRY_QUESTIONNAIRE_QUESTION Q
		<where>
			<if test="questionName != null and questionName != ''">
				AND Q.QUESTION_NAME LIKE CONCAT('%',#{questionName},'%')
			</if>
			<if test="questionPaperId == 2">
				AND Q.QUESTION_PAPER_ID IS NULL
			</if>
		</where>
		LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<select id="getAllProblemCount" parameterType="QuestionnaireQuestionDto" resultType="Integer">
		SELECT 
			COUNT(Q.QUESTION_ID) 
		FROM 
			TB_IAMBERRY_QUESTIONNAIRE_QUESTION Q
		<where>
			<if test="questionName != null and questionName != ''">
				AND Q.QUESTION_NAME LIKE CONCAT('%',#{questionName},'%')
			</if>
			<if test="questionPaperId == 2">
				AND Q.QUESTION_PAPER_ID IS NULL
			</if>
		</where>
	</select>
	
	<!-- 修改题目状态 -->
	<update id="updateQuestionStatu" parameterType="QuestionnaireQuestionDto">
		UPDATE 
			TB_IAMBERRY_QUESTIONNAIRE_QUESTION 
		SET  
			QUESTION_STATU = #{questionStatu}
		WHERE
			QUESTION_ID = #{questionId}
	</update>
	
	<!-- 插入题目信息 -->
	<insert id="insertQuestion" parameterType="QuestionnaireQuestionDto"  useGeneratedKeys="true" keyProperty="questionId">
		 INSERT INTO 
		 	TB_IAMBERRY_QUESTIONNAIRE_QUESTION
		 		(
			 		QUESTION_NAME, QUESTION_DESC, QUESTION_CREATE_DATE,
					QUESTION_STATU, QUESTION_PIC,QUESTION_CALCULATION,QUESTION_ORDER,QUESTION_TYPE
				)
		 VALUES 
		  	( 
			  #{questionName},  
			  #{questionDesc}, 
			  #{questionCreateDate},
			  #{questionStatu},
			  #{questionPic},
			  #{questionCalculation},
			  #{questionOrder},
			  #{questionType}
		  	)
	</insert>
	
	<!-- 修改题目信息 -->
	<update id="updateQuestion" parameterType="QuestionnaireQuestionDto">
		UPDATE 
			TB_IAMBERRY_QUESTIONNAIRE_QUESTION 
		SET  
			QUESTION_NAME = #{questionName},
			QUESTION_DESC = #{questionDesc},
			QUESTION_UPDATE_DATE = #{questionUpdateDate},
			QUESTION_CREATE_DATE = #{questionCreateDate},
			QUESTION_STATU = #{questionStatu},
			QUESTION_PIC = #{questionPic},
			QUESTION_CALCULATION = #{questionCalculation},
			QUESTION_ORDER = #{questionOrder},
			QUESTION_TYPE = #{questionType}
			
			
		WHERE
			QUESTION_ID = #{questionId}
	</update>
	
	<!-- 根据id查询题目信息 -->
	<select id="getProblemById" parameterType="Integer" resultType="QuestionnaireQuestionDto">
		SELECT 
			<include refid="questionnaireQuestion" />
		FROM 
			TB_IAMBERRY_QUESTIONNAIRE_QUESTION Q
		WHERE
		Q.QUESTION_ID = #{questionId}
	</select>
	<!-- 根据题目id 删除一个题目 -->
	<delete id="deleteQuestion" parameterType="int">
			delete from TB_IAMBERRY_QUESTIONNAIRE_QUESTION where QUESTION_ID = #{questionId}
	</delete>
	
	<!-- 修改题目信息 -->
	<update id="deletePaperQuestion" parameterType="Integer">
		UPDATE 
			TB_IAMBERRY_QUESTIONNAIRE_QUESTION 
		SET  
			question_paper_id = null
		WHERE
			QUESTION_ID = #{questionId}
	</update>
	
	<!-- 修改题目顺序 -->
	<update id="updatePaperOrder" parameterType="QuestionnaireQuestionDto">
		UPDATE 
			TB_IAMBERRY_QUESTIONNAIRE_QUESTION 
		SET  
			question_order = #{questionOrder}
		WHERE
			QUESTION_ID = #{questionId}
	</update>
</mapper>
