<?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.ReviewInvestigatedMapper">
	<!-- 通用查询映射结果 -->
	<resultMap id="BaseResultMap"
		type="cn.com.countrygarden.bi.main.model.review.ReviewInvestigated">
		<id column="id" property="id" />
		<result column="questionnaire_id" property="questionnaireId" />
		<result column="name" property="name" />
		<result column="tel" property="tel" />
		<result column="job_number" property="jobNumber" />		
		<result column="is_repeat" property="isRepeat" />
		<result column="flag" property="flag" />
		<result column="sms_template" property="smsTemplate" />
		<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="selectInvestigatedIsRepeatVo"
		resultType="cn.com.countrygarden.bi.main.dto.reviewInvestigatedIsRepeatVo"
		parameterType="int">
		SELECT i.flag flag,i.is_repeat isRepeat,j.is_submission isSubmission FROM
		review_investigated i, review_judge j
		WHERE i.id=j.investigated_id AND i.id=#{investigatedId} AND
		j.id=#{reviewJudgeId}
	</select> -->
	
	 
	 <select id="selectInvestigatedIsRepeatVo" resultType="cn.com.countrygarden.bi.main.dto.reviewInvestigatedIsRepeatVo" parameterType="int">
	select i.flag flag,i.is_repeat isrepeat,j.is_submission issubmission from  review_investigated i, review_judge j 
	where i.id=j.investigated_id and i.id=#{investigatedId} and j.id=#{reviewJudgeId}	
	</select>
	
	<select id="selectAnswerBeforeVo" parameterType="int"
		resultType="cn.com.countrygarden.bi.main.dto.ReviewAnswerBeforeVo">
		select i.id investigatedId,i.questionnaire_id questionnaireId,i.name
		investigatedName,
		(select q.name from review_questionnaire q where q.id=i.questionnaire_id)
		questionnaireName,
		(select q.described from review_questionnaire q where
		q.id=i.questionnaire_id) questionnaireDescribed
		from review_investigated i where i.id=#{investigatedId} and i.questionnaire_id=#{questionnaireId}

	</select>
	

	<select id="selectInvestigationVoList"
		resultType="cn.com.countrygarden.bi.main.dto.ReviewInvestigationVo"
		parameterType="cn.com.countrygarden.bi.main.dto.ReviewInvestigationVo">


		select i.id investigatedid,i.questionnaire_id as questionnaireid,
		i.name, i.flag flag
		,i.create_time launchtime , q.name as
		questionnairename,q.investigationmanagement_id investigationmanagementId,
		(select count(j.id)
		from review_judge j where
		j.investigated_id=i.id)
		total,(select count(
		j.is_submission) from
		review_judge j where
		j.investigated_id=i.id and
		j.is_submission=0)
		underWay,
		(select count(g.id) from review_grade
		g,review_judge j where
		g.name='上级' and
		g.id=j.grade_id and
		j.is_submission=0 and
		j.investigated_id=i.id)
		superiorUnderWay,
		(select
		count(g.id) from
		review_grade g,review_judge j where g.name='上级' and
		g.id=j.grade_id and
		j.investigated_id=i.id) superiortotal,
		(select
		count(g.id) from
		review_grade g,review_judge j where g.name='同级' and
		g.id=j.grade_id and
		j.is_submission=0 and j.investigated_id=i.id)
		equativeunderway,
		(select
		count(g.id) from review_grade g,review_judge
		j where g.name='同级' and
		g.id=j.grade_id and j.investigated_id=i.id)
		equativetotal,
		(select
		count(g.id) from review_grade g,review_judge j
		where g.name='下级' and
		g.id=j.grade_id and j.is_submission=0 and
		j.investigated_id=i.id)
		subordinateunderway,
		(select count(g.id) from
		review_grade
		g,review_judge j where g.name='下级' and
		g.id=j.grade_id and
		j.investigated_id=i.id) subordinatetotal

		from review_investigated
		i,review_questionnaire q where i.questionnaire_id=q.id
		<if test="questionName!=null and questionName!=''">
			and q.name=#{questionName}
		</if>
		
		<if test="name!=null and name!=''">
			and i.name like concat('%',#{name},'%')
		</if>
		<if test="investigationmanagementId!=null">
			and q.investigationmanagement_id =#{investigationmanagementId}
		</if>
		<if test="questionnaireName!=null and questionnaireName!=''">
			and q.name like concat('%',#{questionnaireName},'%')
		</if>
		
		<if test="haseole==false">
		and q.create_by=#{getusercode}
       </if>
		order by i.create_time desc
	</select>






</mapper>