<?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="cn.com.countrygarden.bi.main.dao.review.ReviewQuestionnaireProblemMapper">
	<!-- 通用查询映射结果 -->
	<resultMap id="BaseResultMap"
		type="cn.com.countrygarden.bi.main.model.review.ReviewQuestionnaireProblem">
		<id column="id" property="id" />
		<result column="questionnaire_id" property="questionnaireId" />
		<result column="questionnaire_problem_classification_id"
			property="questionnaireProblemClassificationId" />
		<result column="question_type_id" property="questionTypeId" />
		<result column="score" property="score" />
		<result column="title_description" property="titleDescription" />
		<result column="answer_description"
			property="answerDescription" />
		<result column="flag" property="flag" />
		<result column="create_by" property="createBy" />
		<result column="create_time" property="createTime" />
		<result column="update_by" property="updateBy" />
		<result column="update_time" property="updateTime" />
	</resultMap>

	<!-- 自定义查询映射结果，再根据id查询对应的List<option> -->
	<resultMap id="ProblemVoMap"
		type="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemVo">
		<id column="id" property="id" />
		<result column="questionnaire_problem_classification_id"
			property="classification" />
		<result column="question_type_id" property="type" />
		<result column="name" property="name" />
		
		<result column="title_description" property="title" />
		<result column="score" property="score" />

		<result column="answer_description"
			property="answerDescription" />
		<collection property="option"
			ofType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemOptionVo"
			select="optionVoList" column="id">
		</collection>
	</resultMap>




	<select id="selectProblemId" resultType="int">
		SELECT id FROM review_questionnaire_problem WHERE
		questionnaire_id=#{questionnaireId}

	</select>

	<select id="optionVoList"
		resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemOptionVo">
		select id optionId, questionnaire_problem_id problemId,name name,option_score optionScore
		from review_questionnaire_problem_option where
		questionnaire_problem_id=#{id}
	</select>

	<select id="queryQuestionnaireProblem"
		resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemEditVo"
		parameterType="int">
		select id classificationId,(select c.name from
		review_questionnaire_problem_classification c where c.id=
		classificationId ) from review_questionnaire_problem where
		questionnaire_id=#{questionnaireId}
	</select>
	<!-- <select id="queryQuestionnaireProblemVo" resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemVo" 
		parameterType="int"> select id id,questionnaire_problem_classification_id 
		classification, question_type_id type, title_description title,score score,answer_description 
		answerDescription, create_by createBy,create_time createTime from review_questionnaire_problem 
		where questionnaire_id=#{questionnaireId} </select> -->

   <select id="queryQuestionnaireProblemdanVo" resultMap="ProblemVoMap" parameterType="int">
             select id,questionnaire_problem_classification_id,
		question_type_id , title_description ,score ,answer_description
		from review_questionnaire_problem where
		questionnaire_id=#{questionnaireId}
   </select>
    
<!-- resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemVo" -->

	<select id="queryQuestionnaireProblemVo" resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemVo" parameterType="int">

		<!-- select rqp.id as id,rqp.questionnaire_problem_classification_id as classification,rqpc.name as `name`,
        rqp.question_type_id as `type`,rqp.title_description as title,rqp.score as score,rqp.answer_description as answerDescription
        from review_questionnaire_problem_classification rqpc,review_questionnaire_problem rqp,review_questionnaire rq 
        where rq.id=rqp.questionnaire_id and rqpc.id=rqp.questionnaire_problem_classification_id and
        rqp.questionnaire_id=#{questionnaireId} and rq.investigationmanagement_id=1 and rqp.question_type_id<![CDATA[<>]]>2
        order by classification -->

			   select rqp.id as id,rqpc.name as `name`,rqp.title_description as title,rqp.questionnaire_problem_classification_id as classification,rqp.question_type_id as TYPE,rqpt.orderxu as orderxu,rqp.score as score
                 FROM review_questionnaire_problem rqp
                 left join review_questionnaire rq on rq.id=rqp.questionnaire_id
                 left join review_questionnaire_problem_type rqpt on rqpt.id=rqp.question_type_id
                 left join review_questionnaire_problem_classification rqpc on rqpc.id=rqp.questionnaire_problem_classification_id
                 where rq.investigationmanagement_id=1 and rq.id=#{questionnaireId} and rqp.question_type_id<![CDATA[<>]]>2
                  order by classification 
	</select>

	     <select id="option" resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemOptionVo">
            select rqpo.id as optionId,rqpo.questionnaire_problem_id as problemId,rqpo.name as name,rqpo.option_score as optionScore
            from review_questionnaire_problem_option rqpo
            where rqpo.questionnaire_problem_id=#{problemid}  

     </select>

    <select id="queryQuestionnaireProblemwendaVo" resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemVo" parameterType="int">
	      select rqp.id as id,rqp.questionnaire_problem_classification_id as classification,rqpc.name as `name`,
        rqp.question_type_id as `type`,rqp.title_description as title,rqp.score as score,rqp.answer_description as answerDescription
        from review_questionnaire_problem_classification rqpc,review_questionnaire_problem rqp,review_questionnaire rq 
        where rq.id=rqp.questionnaire_id and rqpc.id=rqp.questionnaire_problem_classification_id and
        rqp.questionnaire_id=#{questionnaireId} and rq.investigationmanagement_id=1 and rqp.question_type_id<![CDATA[<>]]>1 and rqp.question_type_id<![CDATA[<>]]>3
        order by classification

     </select>

	<delete id="deleteQuestionnaireProblem" parameterType="int">
		delete from review_questionnaire_problem where questionnaire_id
		=#{questionnaireId}


	</delete>

	<update id="updateProblemFlag" parameterType="int">
		update review_questionnaire_problem set flag=0 where
		questionnaire_id=#{questionnaireId}
	</update>
	
	<!-- 
	<select id="selectProblemClassificationIdsYesDelete" parameterType="int" resultType="int">
	
	select distinct(questionnaire_problem_classification_id) from `review_questionnaire_problem` where flag=1 and questionnaire_id !=#{questionnaireId}  and questionnaire_problem_classification_id not in
	
	<foreach collection="classificationIds" index="index" item="classificationIds"
			open="(" separator="," close=")">
			#{classificationIds}
		</foreach>	
	</select>
	 -->
	
	<select id="selectProblemClassificationIds" parameterType="int" resultType="int">
	
	select distinct(questionnaire_problem_classification_id) from `review_questionnaire_problem` where flag=1 and questionnaire_id !=#{questionnaireId}  and questionnaire_problem_classification_id in
	
	<foreach collection="classificationIds" index="index" item="classificationIds"
			open="(" separator="," close=")">
			#{classificationIds}
		</foreach>	
	</select>


	<select id="questionnaireProblems" resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemDTO" parameterType="java.lang.Integer">
              select rqp.id as id,rqp.questionnaire_id as questionnaireId,rqp.questionnaire_problem_classification_id as questionnaireProblemClassificationId,rqpc.name as `name`,
              rqp.question_type_id as questionTypeId,rqp.score as score,rqp.title_description as titleDescription,rqp.answer_description as answerDescription,rqp.flag as flag
              from review_questionnaire_problem rqp,review_questionnaire_problem_classification rqpc,review_questionnaire rq
              where rqp.questionnaire_problem_classification_id=rqpc.id and rq.id=rqp.questionnaire_id
              and rqp.questionnaire_id=#{questionnaireId} and rq.investigationmanagement_id=#{investigationmanagementId}
               
	</select>

		<select id="questionoblemsex" resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemDTO" parameterType="java.lang.Integer">
              select rqp.id as id,rqp.questionnaire_id as questionnaireId,rqp.questionnaire_problem_classification_id as questionnaireProblemClassificationId,rqpc.name as `name`,
              rqp.question_type_id as questionTypeId,rqp.score as score,rqp.title_description as titleDescription,rqp.answer_description as answerDescription,rqp.flag as flag
              from review_questionnaire_problem rqp,review_questionnaire_problem_classification rqpc,review_questionnaire rq
              where rqp.questionnaire_problem_classification_id=rqpc.id and rq.id=rqp.questionnaire_id and rqp.question_type_id<![CDATA[<>]]>2
              and rqp.questionnaire_id=#{questionnaireId} and rq.investigationmanagement_id=#{investigationmanagementId}
               
			   order by questionnaireProblemClassificationId
	   </select>

	   <select id="questionoblemswenex" resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemDTO" parameterType="java.lang.Integer">
              select rqp.id as id,rqp.questionnaire_id as questionnaireId,rqp.questionnaire_problem_classification_id as questionnaireProblemClassificationId,rqpc.name as `name`,
              rqp.question_type_id as questionTypeId,rqp.score as score,rqp.title_description as titleDescription,rqp.answer_description as answerDescription,rqp.flag as flag
              from review_questionnaire_problem rqp,review_questionnaire_problem_classification rqpc,review_questionnaire rq
              where rqp.questionnaire_problem_classification_id=rqpc.id and rq.id=rqp.questionnaire_id and rqp.question_type_id<![CDATA[<>]]>1 and rqp.question_type_id<![CDATA[<>]]>3
              and rqp.questionnaire_id=#{questionnaireId} and rq.investigationmanagement_id=#{investigationmanagementId}
               
	   </select>
	   
	   <select id="selectrqclassiid" resultType="java.lang.Integer">
            select id from review_questionnaire_problem_classification where `name`='无' and investigationmanagement_id=2	   
	   </select>


</mapper>