<?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.ReviewScoreMapper">
	<!-- 通用查询映射结果 -->
	<resultMap id="BaseResultMap"
		type="cn.com.countrygarden.bi.main.model.review.ReviewScore">
		<id column="id" property="id" />
		<result column="judge_id" property="judgeId" />
		<result column="problem_id" property="problemId" />
		<result column="questionnaire_problem_option_id"
			property="questionnaireproblemoptionId" />
		<result column="score" property="score" />
		<result column="answer" property="answer" />
		<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>



	<select id="scoredtos"
		resultType="cn.com.countrygarden.bi.main.dto.ReviewScoreDTO"
		parameterType="java.lang.Integer">

		select rg.name as gradename,sum(rs.score) as summ,sum(rs.score)/(select count(1) from review_judge rjj where
		 rjj.grade_id=#{gradeid} and rjj.investigated_id=#{investigatedid}) as
		avgs,pqpc.name as described from review_grade rg,review_judge
		rj,review_score rs,
		review_questionnaire_problem rqp,review_questionnaire_problem_classification pqpc
		where rs.judge_id=rj.id and rg.id=rj.grade_id and rqp.id=rs.problem_id and
		rqp.questionnaire_problem_classification_id= pqpc.id and
		rqp.question_type_id <![CDATA[<>]]>
		2
		and rj.investigated_id=#{investigatedid} and rs.flag=1
		group by rg.name,pqpc.name

	</select>

	<select id="scoreproblem"
		resultType="cn.com.countrygarden.bi.main.dto.ReviewScoreProblemDTO"
		parameterType="cn.com.countrygarden.bi.main.dto.ReviewScoreProblemDTO">
		 <!-- select rqp.title_description as titledescription,pqpc.name as
		described,rs.score as score,rs.answer as answer,rj.name as
		name,rg.name as rgname,rs.create_time as createtime from review_grade
		rg,review_judge rj,review_score rs,
		review_questionnaire_problem rqp,review_questionnaire_problem_classification pqpc
		where rs.judge_id=rj.id and rg.id=rj.grade_id and rqp.id=rs.problem_id and
		rqp.questionnaire_problem_classification_id= pqpc.id
		and rj.investigated_id=#{investigatedid} and rg.name=#{rgname}
		order by rs.create_time desc -->

		select rqp.title_description as titledescription,
        rs.score as score,rj.id ,rj.grade_id as rgname,rqp.id as questionnaireId,rqp.questionnaire_problem_classification_id as described,
        rj.`name`, rs.answer as answer,rs.create_time as createtime
       from review_judge rj
      left join review_questionnaire_problem rqp on rj.questionnaire_id=rqp.questionnaire_id
      left join review_score rs on rs.problem_id=rqp.id and rs.judge_id=rj.id and rs.judge_id=rj.id
      where rj.investigated_id = #{investigatedid} and rj.grade_id=#{rgname}  
       order by rs.create_time desc
	</select>

    <select id="scoreraid"
		resultType="java.lang.Integer"
		parameterType="cn.com.countrygarden.bi.main.dto.ReviewScoreProblemDTO">
		select count(1) from review_judge rj,review_investigated ri where rj.investigated_id=ri.id and
        ri.id=#{investigatedid} and rj.grade_id=#{rgname}

	</select>


	<!-- <select id="scoreproblemweiti"
		resultType="cn.com.countrygarden.bi.main.dto.ReviewScoreProblemDTO"
		parameterType="cn.com.countrygarden.bi.main.dto.ReviewScoreProblemDTO">
		select  rqp.title_description  as titledescription,rqpc.name as described ,
        rj.name as name,rg.name as rgname 
        from review_judge rj,review_investigated ri,review_questionnaire rq,review_grade rg,review_questionnaire_problem rqp,
        review_questionnaire_problem_classification rqpc
        where ri.id=rj.investigated_id and ri.questionnaire_id=rq.id and rg.id=rj.grade_id and rq.id=rqp.questionnaire_id and
        rqpc.id=rqp.questionnaire_problem_classification_id
        and ri.id=#{investigatedid} and rg.name=#{rgname} and rj.is_submission=0

	</select> -->

	<update id="updatesource"
		parameterType="cn.com.countrygarden.bi.main.model.review.ReviewScore">
		update review_score set score = #{score} where
		questionnaire_problem_option_id = #{questionnaireproblemoptionId};

	</update>

	<select id="evaluationproblem"
		resultType="cn.com.countrygarden.bi.main.dto.ReviewScoreProblemDTO"
		parameterType="java.lang.Integer">
		
		select rj.name as name,rqp.title_description as titledescription,pqpc.id as
		described,rs.score as score,rs.answer as answer,rg.id as rgname,rs.create_time as createtime from review_grade
		rg,review_judge rj,review_score rs,
		review_questionnaire_problem rqp,review_questionnaire_problem_classification pqpc
		where rs.judge_id=rj.id and rg.id=rj.grade_id and rqp.id=rs.problem_id and
		rqp.questionnaire_problem_classification_id= pqpc.id and rqp.question_type_id=2
		and rj.investigated_id=#{investigatedid} order by rs.create_time desc
	</select>

	<select id="reviewevaluate" resultType="java.lang.String" parameterType="java.lang.Integer">
	      select re.described from review_evaluate re,review_investigated ri
        where re.investigated_id=ri.id and ri.questionnaire_id=re.questionnaire_id 
       and re.investigated_id=#{investigatedid} and re.questionnaire_id=#{questionnaireid}
	</select>

	<update id="updateedit"
		parameterType="cn.com.countrygarden.bi.main.model.review.ReviewEvaluate">
		update review_evaluate set described = #{described},update_by=#{updateBy},
		update_time=#{updateTime} 
		WHERE investigated_id = #{investigatedId} AND questionnaire_id = #{questionnaireId}

	</update>

	<update id="updataflag" parameterType="java.lang.Integer">
		update review_score rs,review_judge rj set rs.flag=0
		where rs.judge_id=rj.id and rj.questionnaire_id=#{questionnaireId} and
		rj.investigated_id=#{id} and rs.judge_id=#{judgeId};

	</update>

     <delete id="delsoce" parameterType="java.lang.Integer">
          delete from review_score where judge_id = #{judgeId};

     </delete>

	 <select id="reviewrinqu" parameterType="java.lang.Integer" resultType="cn.com.countrygarden.bi.main.dto.ReviewExcel.ReviewRinquDTO">
              select ri.id as riid,ri.name as riname,rq.id as rqid,rq.name as rqname from review_investigated ri,review_questionnaire rq
               where rq.id=ri.questionnaire_id and ri.id=#{investigatedid}
	 </select>

	 <select id="scoreproblemexcl" parameterType="java.lang.Integer" resultType="cn.com.countrygarden.bi.main.dto.ReviewScoreProblemDTO">

        select rqp.title_description as titledescription,
        rs.score as score,rj.id ,rj.grade_id as rgname,rqp.id as questionnaireId,rqp.questionnaire_problem_classification_id as described,
        rj.`name`, rs.answer as answer,rs.create_time as createtime
       from review_judge rj
      left join review_questionnaire_problem rqp on rj.questionnaire_id=rqp.questionnaire_id
      left join review_score rs on rs.problem_id=rqp.id and rs.judge_id=rj.id and rs.judge_id=rj.id 
      where rj.investigated_id=#{investigatedid} and rj.questionnaire_id=#{questionnaireid} and rqp.question_type_id<![CDATA[<>]]>2
      order by described

	 </select>

	 <select id="scoreproblemwenexcl" parameterType="java.lang.Integer" resultType="cn.com.countrygarden.bi.main.dto.ReviewScoreProblemDTO">

        select rqp.title_description as titledescription,
        rs.score as score,rj.id ,rj.grade_id as rgname,rqp.id as questionnaireId,rqp.questionnaire_problem_classification_id as described,
        rj.`name`, rs.answer as answer,rs.create_time as createtime
       from review_judge rj
      left join review_questionnaire_problem rqp on rj.questionnaire_id=rqp.questionnaire_id
      left join review_score rs on rs.problem_id=rqp.id and rs.judge_id=rj.id and rs.judge_id=rj.id 
      where rj.investigated_id=#{investigatedid} and rj.questionnaire_id=#{questionnaireid} and rqp.question_type_id<![CDATA[<>]]>1 and rqp.question_type_id<![CDATA[<>]]>3
      order by described

	 </select>


</mapper>