<?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.curriculum.CourseStudentRelationMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.CURRICULUM_ID,
		f.USER_ID,
		f.STUDYCOUNT,
		f.COURSEEXAMCOUNT,
		f.STAGEEXAMCOUNT,
		f.STUDYSTATE,
		f.COURSEEXAMSTATE,
		f.STAGEEXAMSTATE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.COURSEEXAMSCORE,
		f.STAGEEXAMSCORE,
		f.COURSESTUDENTRELATION_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CURRICULUM_ID,
		USER_ID,
		STUDYCOUNT,
		COURSEEXAMCOUNT,
		STAGEEXAMCOUNT,
		STUDYSTATE,
		COURSEEXAMSTATE,
		STAGEEXAMSTATE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		CORPINFO_ID,
		COURSEEXAMSCORE,
		STAGEEXAMSCORE,
		COURSESTUDENTRELATION_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CURRICULUM_ID},
		#{USER_ID},
		#{STUDYCOUNT},
		#{COURSEEXAMCOUNT},
		#{STAGEEXAMCOUNT},
		#{STUDYSTATE},
		#{COURSEEXAMSTATE},
		#{STAGEEXAMSTATE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{CORPINFO_ID},
		#{COURSEEXAMSCORE},
		#{STAGEEXAMSCORE},
		#{COURSESTUDENTRELATION_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
			COURSESTUDENTRELATION_ID = #{COURSESTUDENTRELATION_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			CURRICULUM_ID = #{CURRICULUM_ID},
			USER_ID = #{USER_ID},
			STUDYCOUNT = #{STUDYCOUNT},
			COURSEEXAMCOUNT = #{COURSEEXAMCOUNT},
			STAGEEXAMCOUNT = #{STAGEEXAMCOUNT},
			STUDYSTATE = #{STUDYSTATE},
			COURSEEXAMSTATE = #{COURSEEXAMSTATE},
			STAGEEXAMSTATE = #{STAGEEXAMSTATE},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			COURSESTUDENTRELATION_ID = COURSESTUDENTRELATION_ID
		where
			COURSESTUDENTRELATION_ID = #{COURSESTUDENTRELATION_ID}
	</update>

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

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		,u.NAME USERNAME
		,(SELECT c.COURSEEXAM_ID FROM BUS_COURSEEXAM c
		WHERE c.CURRICULUM_ID = #{pd.CURRICULUM_ID} AND c.USER_ID = f.USER_ID AND c.ISDELETE='0' ORDER BY c.EXAMTIME DESC LIMIT 1) COURSEEXAM_ID
		from
		<include refid="tableName"></include> f
		LEFT JOIN sys_user u ON u.USER_ID = f.USER_ID
		where f.ISDELETE = '0' and f.CURRICULUM_ID = #{pd.CURRICULUM_ID} and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
				u.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				<!--	根据需求自己加检索条件
					字段1 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				-->
				)
		</if>
		ORDER BY f.OPERATTIME DESC
	</select>

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

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

	<!-- 批量删除（根据所选的课程进行删除课程学员关系表） -->
	<delete id="deleteByCurriculumIds" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		ISDELETE = '1',
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME}
		where ISDELETE = '0'
		AND CURRICULUM_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
			#{item}
		</foreach>
	</delete>

	<!-- 修改（更新学员状态） -->
	<update id="updateStudentState" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		<if test="TYPE != null and TYPE != '' and TYPE == 1"><!-- 习题练习 -->
			STUDYSTATE = #{STUDYSTATE},
		</if>
		<if test="STAGEEXAMSTATE != null and STAGEEXAMSTATE != '' and STAGEEXAMSTATE == 4"><!-- 阶段考试未参加 -->
			STAGEEXAMSCORE = '0',
		</if>
		<if test="TYPE != null and TYPE != '' and TYPE == 3"><!-- 阶段考试 -->
			STAGEEXAMSTATE = #{STAGEEXAMSTATE},
		</if>
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME}
		where
		COURSESTUDENTRELATION_ID = #{COURSESTUDENTRELATION_ID}
	</update>

	<!-- 修改（更新学员考试成绩及状态） -->
	<update id="updateStudentScore" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		<if test="EXAMTYPE != null and EXAMTYPE != '' and EXAMTYPE == 1"><!-- 课程学习 -->
			STUDYCOUNT = CAST(STUDYCOUNT AS SIGNED)+1,
		</if>
		<if test="EXAMTYPE != null and EXAMTYPE != '' and EXAMTYPE == 2"><!-- 课程考试 -->
			COURSEEXAMCOUNT = CAST(COURSEEXAMCOUNT AS SIGNED)+1,
			COURSEEXAMSCORE = #{COURSEEXAMSCORE},
			COURSEEXAMSTATE = #{COURSEEXAMSTATE},
		</if>
		<if test="COURSEEXAMSTATE != null and COURSEEXAMSTATE != '' and COURSEEXAMSTATE == 3"><!-- 课程考试及格学习结束 -->
			STUDYSTATE = #{COURSEEXAMSTATE},
		</if>
		<if test="EXAMTYPE != null and EXAMTYPE != '' and EXAMTYPE == 3"><!-- 阶段考试 -->
			STAGEEXAMCOUNT = CAST(STAGEEXAMCOUNT AS SIGNED)+1,
			STAGEEXAMSCORE = #{STAGEEXAMSCORE},
			STAGEEXAMSTATE = #{STAGEEXAMSTATE},
		</if>
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME}
		where
		COURSESTUDENTRELATION_ID = #{COURSESTUDENTRELATION_ID}
	</update>

	<!-- 课程结束（定时任务，处理没有参加考试的学员课程，课程结束STUDYSTATE = '4'，考试未考COURSEEXAMSTATE = '4'） -->
	<update id="overCourseNotInvolved" parameterType="pd">
		UPDATE
		<include refid="tableName"></include> f
		 LEFT JOIN BUS_CURRICULUM c ON c.CURRICULUM_ID = f.CURRICULUM_ID AND c.ISDELETE = '0'
		SET
		f.STUDYSTATE = '4',
		f.COURSEEXAMSTATE = '4',
		f.COURSEEXAMSTATE = '4',
		f.OPERATTIME = #{OPERATTIME}
		WHERE f.COURSEEXAMSTATE = '1'
		AND c.ENDTIME = #{ENDTIME}
	</update>

	<!-- 课程结束（定时任务，处理参加考试的学员课程，课程结束STUDYSTATE = '4'） -->
	<update id="overCourse" parameterType="pd">
		UPDATE
		<include refid="tableName"></include> f
		LEFT JOIN BUS_CURRICULUM c ON c.CURRICULUM_ID = f.CURRICULUM_ID AND c.ISDELETE = '0'
		SET
		f.STUDYSTATE = '4',
		f.OPERATTIME = #{OPERATTIME}
		WHERE f.COURSEEXAMSTATE IN ('2','3')
		AND c.ENDTIME = #{ENDTIME}
	</update>

</mapper>
