<?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.zcloud.mapper.datasource.task.StageStudentRelationMapper">

	<!--表名 -->
	<sql id="tableName">
		BUS_STAGESTUDENTRELATION
	</sql>

	<!--数据字典表名 -->
	<sql id="dicTableName">
		SYS_DICTIONARIES
	</sql>

	<!-- 字段 -->
	<sql id="Field">
		f.STAGEEXAMPAPER_ID,
		f.USER_ID,
		f.STAGEEXAMSTATE,
		f.STAGEEXAMSCORE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.STUDYTASK_ID,
		f.COMPLETE_COURSEWARE,
		f.COMPLETE_CLASSHOUR,
		f.STAGESTUDENTRELATION_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		STAGEEXAMPAPER_ID,
		USER_ID,
		STAGEEXAMSTATE,
		STAGEEXAMSCORE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		CORPINFO_ID,
		STUDYTASK_ID,
		COMPLETE_COURSEWARE,
		COMPLETE_CLASSHOUR,
		STAGESTUDENTRELATION_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{STAGEEXAMPAPER_ID},
		#{USER_ID},
		#{STAGEEXAMSTATE},
		#{STAGEEXAMSCORE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{CORPINFO_ID},
		#{STUDYTASK_ID},
		#{COMPLETE_COURSEWARE},
		#{COMPLETE_CLASSHOUR},
		#{STAGESTUDENTRELATION_ID}
	</sql>

	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into
	<include refid="tableName"></include>
		(
	<include refid="Field2"></include>
		) values (
	<include refid="FieldValue"></include>
		)
	</insert>

	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1',
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME}
		where
			STAGESTUDENTRELATION_ID = #{STAGESTUDENTRELATION_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			STAGEEXAMPAPER_ID = #{STAGEEXAMPAPER_ID},
			USER_ID = #{USER_ID},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			STAGESTUDENTRELATION_ID = STAGESTUDENTRELATION_ID
		where
			STAGESTUDENTRELATION_ID = #{STAGESTUDENTRELATION_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.STAGESTUDENTRELATION_ID = #{STAGESTUDENTRELATION_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		u.NAME USERNAME,
		d.NAME LEARNERCATEGORYNAME,
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		 LEFT JOIN SYS_USER u on f.USER_ID = u.USER_ID
		 LEFT JOIN <include refid="dicTableName"></include> d on d.DICTIONARIES_ID = u.LEARNERCATEGORY AND d.parent_id = '532282f9c48346b8b646531909c739df'
		where f.ISDELETE = '0' and f.STAGEEXAMPAPER_ID = #{pd.STAGEEXAMPAPER_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and u.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
		</if>
		ORDER BY f.OPERATTIME DESC
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		f.STAGESTUDENTRELATION_ID,
		f.STAGEEXAMPAPER_ID,
		f.USER_ID,
		f.STAGEEXAMSTATE,
		f.STAGEEXAMSCORE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.STUDYTASK_ID,
		f.COMPLETE_COURSEWARE,
		u.NAME AS USER_NAME,
		u.USERNAME as PHONE,
		d.NAME AS DEPARTMENT_NAME,
		x.NAME AS SEX_NAME,
		i.SEX,
		i.USER_ID_CARD,
		p.NAME POSTNAME,
		u.USERNAME,
		u.NAME,
		TRUNCATE((IF(cr.RESOURCETIME IS NULL,0,(cr.RESOURCETIME DIV 60))+f.COMPLETE_CLASSHOUR),0) COMPLETE_CLASSHOUR,
		stu.SUM_CLASSHOUR as SUM_CLASSHOUR
		from
		<include refid="tableName"></include> f
		left join SYS_USER u on u.USER_ID = f.USER_ID
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID=u.DEPARTMENT_ID
		left join SYS_USERINFO i on i.USER_ID = f.USER_ID
		left join SYS_DICTIONARIES x on x.DICTIONARIES_ID = i.SEX
		left join sys_post p on p.POST_ID = u.POST_ID and p.ISDELETE = '0' and p.STATUS = '0'
		left join bus_studytask stu on stu.STUDYTASK_ID = f.STUDYTASK_ID
		left join (select sum(co.RESOURCETIME) RESOURCETIME,co.STUDYTASK_ID,co.USER_ID from BUS_COURSESTUDYVIDEORECORD co where co.PLAYCOUNT = 0
		    group by co.STUDYTASK_ID,co.USER_ID) cr ON cr.STUDYTASK_ID = f.STUDYTASK_ID and cr.USER_ID = f.USER_ID
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{CORPINFO_ID}
		<if test="STUDYTASK_ID!= null and STUDYTASK_ID != ''"><!-- 关键词检索 -->
			and f.STUDYTASK_ID =#{STUDYTASK_ID}
		</if>
		<if test="USER_ID!= null and USER_ID != ''"><!-- 关键词检索 -->
			and f.USER_ID =#{USER_ID}
		</if>
		<if test="userkey!= null and userkey != ''"><!-- 关键词检索 -->
			and
			(
			u.USERNAME LIKE CONCAT(CONCAT('%', #{userkey}),'%')
			or
			u.EMAIL LIKE CONCAT(CONCAT('%', #{userkey}),'%')
			or
			u.NUMBER LIKE CONCAT(CONCAT('%', #{userkey}),'%')
			or
			u.NAME LIKE CONCAT(CONCAT('%', #{userkey}),'%')
			or
			u.PHONE LIKE CONCAT(CONCAT('%', #{userkey}),'%')
			)
		</if>
		<if test="DEPARTMENT_ID!= null and DEPARTMENT_ID != ''"><!-- 用户部门 -->
			and u.DEPARTMENT_ID = #{DEPARTMENT_ID}
		</if>
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1',
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME}
		where
			STAGESTUDENTRELATION_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

	<!-- 列表(根据所选ID获取数据) -->
	<select id="findByIds" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			STAGESTUDENTRELATION_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
				#{item}
		</foreach>
	</select>

	<!-- 列表(全部) -->
	<select id="findByExamPaper" parameterType="pd" resultType="pd">
		select
		f.USER_ID
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0' and f.STAGEEXAMPAPER_ID = #{STAGEEXAMPAPER_ID}
	</select>

	<!-- 删除(根据试卷ID删除考试人员)-->
	<delete id="deleteByExamPaper" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		ISDELETE = '1',
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME}
		where ISDELETE = '0'
		AND STAGEEXAMPAPER_ID = #{STAGEEXAMPAPER_ID}
	</delete>

	<!-- 列表【我的考试】（获取当前用户的所属阶段考试信息） -->
	<select id="pageDataByUserlistPage" parameterType="page" resultType="pd">
		select
		p.STAGEEXAMPAPER_ID,
		p.EXAMNAME,
		p.EXAMTIME,
		p.ANSWERSHEETTIME,
		p.EXAMSCORE,
		p.QUESTIONNUM,
		p.EXAMSTATE,
		f.STAGESTUDENTRELATION_ID,
		f.STAGEEXAMSTATE,
		f.STAGEEXAMSCORE
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_STAGEEXAMPAPER p ON p.STAGEEXAMPAPER_ID = f.STAGEEXAMPAPER_ID AND p.ISDELETE = '0'
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
				p.EXAMNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			<!--	根据需求自己加检索条件
                字段1 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
                 or
                字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            -->
			)
		</if>
		<if test="pd.EXAMSTATE != null and pd.EXAMSTATE != ''">
			<choose>
				<when test="pd.EXAMSTATE == 1">	<!-- 未开始 -->
					and f.STAGEEXAMSTATE = #{pd.EXAMSTATE} and p.EXAMTIME &gt;= NOW()
				</when>
				<when test="pd.EXAMSTATE == 2">	<!-- 进行中 -->
					and f.STAGEEXAMSTATE = '1' and p.EXAMTIME &lt;= NOW()
				</when>
				<when test="pd.EXAMSTATE == 3">	<!-- 已通过 -->
					and f.STAGEEXAMSTATE = #{pd.EXAMSTATE}
				</when>
				<when test="pd.EXAMSTATE == 4">	<!-- 已结束 -->
					and f.STAGEEXAMSTATE in ('3','4')
				</when>
			</choose>
		</if>
		AND f.USER_ID = #{pd.USER_ID}
		GROUP BY f.STAGEEXAMPAPER_ID ORDER BY p.EXAMTIME DESC
	</select>

	<!-- 修改（更新学员考试状态） -->
	<update id="updateStudentState" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		<if test="USER_SIGN_PATH!= null and USER_SIGN_PATH != ''"><!-- 关键词检索 -->
			USER_SIGN_PATH = #{USER_SIGN_PATH},
		</if>
		STAGEEXAMSTATE = #{STAGEEXAMSTATE},
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME}
		where
		STAGESTUDENTRELATION_ID = #{STAGESTUDENTRELATION_ID}
	</update>

	<!-- 修改（更新学员考试成绩及状态） -->
	<update id="updateStudentScore" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		<if test="STAGEEXAMSCORE != null and STAGEEXAMSCORE != ''">
			STAGEEXAMSCORE = #{STAGEEXAMSCORE},
		</if>
		<if test="STAGEEXAMSTATE != null and STAGEEXAMSTATE != ''">
			STAGEEXAMSTATE = #{STAGEEXAMSTATE},
		</if>
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME}
		where
		STAGESTUDENTRELATION_ID = #{STAGESTUDENTRELATION_ID}
	</update>

	<!-- 处理没有参加考试的学员阶考，考试未考STAGEEXAMSTATE = '4' -->
	<update id="overStage" parameterType="pd">
		update
		<include refid="tableName"></include> f
		 LEFT JOIN BUS_STAGEEXAMPAPER p ON p.STAGEEXAMPAPER_ID = f.STAGEEXAMPAPER_ID AND p.ISDELETE = '0'
		set
		f.STAGEEXAMSTATE = '4',
		f.OPERATTIME = #{OPERATTIME}
		where f.STAGEEXAMSTATE = '1'
		 AND SUBSTR(DATE_ADD(CONCAT_WS(':', p.EXAMTIME, '00'), INTERVAL p.ANSWERSHEETTIME MINUTE),1,10) = #{ENDTIME}
	</update>

	<!-- 列表【app我的任务】（获取当前用户的所属任务信息列表） -->
	<select id="pageTaskByUserlistPage" parameterType="page" resultType="pd">
		select
		t.STUDYTASK_ID,
		t.STUDY_NAME,
		t.PEIXUE_START_TIME,
		t.PEIXUE_END_TIME,
		t.STATE,
		t.COURSEWARE_COUNT,
		f.COMPLETE_COURSEWARE,
		dic1.name POSTTYPENAME,
		dic2.name TRAINTYPENAME,
		p.STAGEEXAMPAPER_ID,
		p.EXAMNAME,
		p.EXAMTIME,
		p.ANSWERSHEETTIME,
		p.EXAMSCORE,
		p.QUESTIONNUM,
		p.EXAMSTATE,
		p.PASSSCORE,
		f.STAGESTUDENTRELATION_ID,
		f.STAGEEXAMSTATE,
		(select count(*) from BUS_STAGEEXAM stag where stag.STUDYTASK_ID = f.STUDYTASK_ID and stag.USER_ID =#{pd.USER_ID} ) as ksCount ,
		p.NUMBER_OF_EXAMINATIONS,
		f.STAGEEXAMSCORE
		from
		<include refid="tableName"></include> f
		INNER JOIN BUS_STUDYTASK t ON t.STUDYTASK_ID = f.STUDYTASK_ID AND t.ISDELETE = '0'
		LEFT JOIN BUS_STAGEEXAMPAPER p ON p.STAGEEXAMPAPER_ID = f.STAGEEXAMPAPER_ID AND p.ISDELETE = '0'
		LEFT JOIN SYS_DICTIONARIES dic1 ON dic1.DICTIONARIES_ID = t.POSTTYPE
		LEFT JOIN SYS_DICTIONARIES dic2 ON dic2.DICTIONARIES_ID = t.TRAINTYPE
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		AND f.USER_ID = #{pd.USER_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			<!--	根据需求自己加检索条件-->
                t.STUDY_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		<if test="pd.STATUS != null and pd.STATUS != ''"><!-- 关键词检索 -->
			<if test="pd.STATUS != null and pd.STATUS == 1">
				and DATE_FORMAT( t.PEIXUE_START_TIME, '%Y-%m-%d' ) &gt; DATE_FORMAT( NOW(), '%Y-%m-%d' )
			</if>
			<if test="pd.STATUS != null and pd.STATUS == 2">
				and DATE_FORMAT( t.PEIXUE_START_TIME, '%Y-%m-%d' ) &lt;= DATE_FORMAT( NOW(), '%Y-%m-%d' )
				and DATE_FORMAT( t.PEIXUE_END_TIME, '%Y-%m-%d' ) &gt;= DATE_FORMAT( NOW(), '%Y-%m-%d' )
			</if>
			<if test="pd.STATUS != null and pd.STATUS == 3">
				and DATE_FORMAT( t.PEIXUE_END_TIME, '%Y-%m-%d' ) &lt; DATE_FORMAT( NOW(), '%Y-%m-%d' )
			</if>
		</if>
		<if test="pd.examinationState != null and pd.examinationState != ''"><!-- 关键词检索 -->
				and f.STAGEEXAMSTATE =#{pd.examinationState}
		</if>
		GROUP BY f.STAGEEXAMPAPER_ID ORDER BY p.EXAMTIME DESC
	</select>

	<!-- 列表【app成绩查询】（获取当前用户的所属考试信息列表） -->
	<select id="pageTaskScoreByUserlistPage" parameterType="page" resultType="pd">
		select
		t.STUDYTASK_ID,
		t.STUDY_NAME,
		p.STAGEEXAMPAPER_ID,
		p.EXAMNAME,
		p.EXAMTIME,
		p.ANSWERSHEETTIME,
		p.EXAMSCORE,
		p.QUESTIONNUM,
		p.EXAMSTATE,
		p.PASSSCORE,
		f.STAGESTUDENTRELATION_ID,
		f.STAGEEXAMSTATE,
		f.STAGEEXAMSCORE
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_STUDYTASK t ON t.STUDYTASK_ID = f.STUDYTASK_ID AND t.ISDELETE = '0'
		LEFT JOIN BUS_STAGEEXAMPAPER p ON p.STAGEEXAMPAPER_ID = f.STAGEEXAMPAPER_ID AND p.ISDELETE = '0'
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		and f.STAGEEXAMSTATE in ('2','3')
		and f.STAGEEXAMSCORE != '-1'   -- 未参加考试
		AND f.USER_ID = #{pd.USER_ID}
		GROUP BY f.STAGEEXAMPAPER_ID ORDER BY p.EXAMTIME DESC
	</select>


	<!-- 列表【app成绩查询】（获取当前用户的所属考试所有信息列表） -->
	<select id="pageTaskScoreByUserAll" parameterType="page" resultType="pd">
		select
		t.STUDYTASK_ID,
		t.STUDY_NAME,
		p.STAGEEXAMPAPER_ID,
		p.EXAMNAME,
		p.EXAMTIME,
		p.ANSWERSHEETTIME,
		p.EXAMSCORE,
		p.QUESTIONNUM,
		p.EXAMSTATE,
		p.PASSSCORE,
		f.STAGESTUDENTRELATION_ID,
		f.STAGEEXAMSTATE,
		f.STAGEEXAMSCORE
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_STUDYTASK t ON t.STUDYTASK_ID = f.STUDYTASK_ID AND t.ISDELETE = '0'
		LEFT JOIN BUS_STAGEEXAMPAPER p ON p.STAGEEXAMPAPER_ID = f.STAGEEXAMPAPER_ID AND p.ISDELETE = '0'
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		and f.STAGEEXAMSTATE in ('2','3')
		and f.STAGEEXAMSCORE != '-1'   -- 未参加考试
		AND f.USER_ID = #{pd.USER_ID}
		GROUP BY f.STAGEEXAMPAPER_ID ORDER BY p.EXAMTIME DESC
	</select>

	<!-- 列表【app学习详细】（根据STAGESTUDENTRELATION_ID获取当前的修改任务） -->
	<select id="getMyTask" parameterType="pd" resultType="pd">
		select
		t.STUDYTASK_ID,
		t.STUDY_NAME,
		t.COURSEWARE_COUNT,
		p.STAGEEXAMPAPER_ID,
		p.EXAMNAME,
		p.EXAMTIME,
		p.ANSWERSHEETTIME,
		p.EXAMSCORE,
		p.QUESTIONNUM,
		p.EXAMSTATE,
		p.PASSSCORE,
		f.STAGESTUDENTRELATION_ID,
		f.STAGEEXAMSTATE,
		f.STAGEEXAMSCORE,
		f.COMPLETE_COURSEWARE,
		( SELECT count(stag.USER_ID) FROM BUS_STAGEEXAM stag WHERE stag.STUDYTASK_ID = f.STUDYTASK_ID AND stag.USER_ID = #{USER_ID} ) AS ksCount,
		p.NUMBER_OF_EXAMINATIONS
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_STUDYTASK t ON t.STUDYTASK_ID = f.STUDYTASK_ID AND t.ISDELETE = '0'
		LEFT JOIN BUS_STAGEEXAMPAPER p ON p.STAGEEXAMPAPER_ID = f.STAGEEXAMPAPER_ID AND p.ISDELETE = '0'
		where  f.STAGESTUDENTRELATION_ID = #{STAGESTUDENTRELATION_ID}
	</select>
	<!-- 修改 -->
	<update id="updateClassHourAndCount" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		COMPLETE_COURSEWARE = #{COMPLETE_COURSEWARE},
		COMPLETE_CLASSHOUR = #{COMPLETE_CLASSHOUR}
		where
		USER_ID = #{USER_ID}
		and STUDYTASK_ID =#{STUDYTASK_ID}
	</update>

</mapper>
