<?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.KcmiddlekjMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.CURRICULUMID,
		f.COURSEWAREID,
		f.COURSEWARETYPE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.CHAPTER_ID,
		f.KCMIDDLEKJ_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CURRICULUMID,
		COURSEWAREID,
		COURSEWARETYPE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		CORPINFO_ID,
		CHAPTER_ID,
		KCMIDDLEKJ_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CURRICULUMID},
		#{COURSEWAREID},
		#{COURSEWARETYPE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{CORPINFO_ID},
		#{CHAPTER_ID},
		#{KCMIDDLEKJ_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
		KCMIDDLEKJ_ID = #{KCMIDDLEKJ_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		CURRICULUMID = #{CURRICULUMID},
		COURSEWAREID = #{COURSEWAREID},
		COURSEWARETYPE = #{COURSEWARETYPE},
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME},
		KCMIDDLEKJ_ID = KCMIDDLEKJ_ID
		where
		KCMIDDLEKJ_ID = #{KCMIDDLEKJ_ID}
	</update>

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

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		,IF(f.COURSEWARETYPE='1',v.VIDEOCOURSEWARE_ID,d.DATACOURSEWARE_ID) COURSEWARE_ID
		,IF(f.COURSEWARETYPE='1',v.COURSEWARENAME,d.COURSEWARENAME) COURSEWARENAME
		,IF(f.COURSEWARETYPE='1',v.SPEAKER,d.SPEAKER) SPEAKER
		,IF(f.COURSEWARETYPE='1',v.COURSEWAREINTRODUCE,d.COURSEWAREINTRODUCE) COURSEWAREINTRODUCE
		,IF(f.COURSEWARETYPE='1',v.VIDEOFILES,d.COURSEWAREFILES) COURSEWAREFILES
		,IF(f.COURSEWARETYPE='1',v.VIDEOCAPTURE,d.COURSEWARECAPTURE) COURSEWARECAPTURE
		,IF(f.COURSEWARETYPE='1',v.CLASSHOUR,d.CLASSHOUR) CLASSHOUR
		,IF(f.COURSEWARETYPE='1',IFNULL(a.PLAYCOUNT,0),IFNULL(b.PLAYCOUNT,0)) PLAYCOUNT
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_VIDEOCOURSEWARE v on f.COURSEWAREID = v.VIDEOCOURSEWARE_ID AND COURSEWARETYPE='1'
		LEFT JOIN BUS_DATACOURSEWARE d on f.COURSEWAREID = d.DATACOURSEWARE_ID AND COURSEWARETYPE='2'
		LEFT JOIN BUS_COURSESTUDYVIDEORECORD a on a.VIDEOCOURSEWARE_ID = v.VIDEOCOURSEWARE_ID AND a.CURRICULUM_ID = f.CURRICULUMID AND a.TYPE = '1' and a.ISDELETE = '0' AND a.USER_ID = #{pd.USER_ID} and a.STUDYTASK_ID = #{pd.STUDYTASK_ID}
		LEFT JOIN BUS_COURSESTUDYVIDEORECORD b on b.VIDEOCOURSEWARE_ID = d.DATACOURSEWARE_ID AND b.CURRICULUM_ID = f.CURRICULUMID AND b.TYPE = '2' and b.ISDELETE = '0' AND b.USER_ID = #{pd.USER_ID} and  b.STUDYTASK_ID = #{pd.STUDYTASK_ID}
		where
		1=1
		<if test="pd.ISDELETE != null and pd.ISDELETE != ''"><!-- 课程ID -->
			AND f.ISDELETE = #{pd.ISDELETE}
		</if>
		and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(

                v.COURSEWARENAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
                 or
                v.SPEAKER LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')

			)
		</if>
		<if test="pd.CURRICULUMID != null and pd.CURRICULUMID != ''"><!-- 课程ID -->
			AND f.CURRICULUMID = #{pd.CURRICULUMID}
		</if>
		<if test="pd.STUDYTASK_ID != null and pd.STUDYTASK_ID != ''"><!-- 课程ID -->
			and CURRICULUMID in (select CURRICULUM_ID from BUS_STUDYTASKCURRICULUM where STUDYTASK_ID = #{pd.STUDYTASK_ID} )
		</if>
		GROUP BY f.COURSEWAREID, f.COURSEWARETYPE
		ORDER BY f.OPERATTIME DESC
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		,IF(f.COURSEWARETYPE='1',v.COURSEWARENAME,d.COURSEWARENAME) COURSEWARENAME
		,IF(f.COURSEWARETYPE='1',v.SPEAKER,d.SPEAKER) SPEAKER
		,IF(f.COURSEWARETYPE='1',v.COURSEWAREINTRODUCE,d.COURSEWAREINTRODUCE) COURSEWAREINTRODUCE
		,IF(f.COURSEWARETYPE='1',v.VIDEOFILES,d.COURSEWAREFILES) COURSEWAREFILES
		,IF(f.COURSEWARETYPE='1',v.VIDEOCAPTURE,d.COURSEWARECAPTURE) COURSEWARECAPTURE
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_VIDEOCOURSEWARE v on f.COURSEWAREID = v.VIDEOCOURSEWARE_ID AND COURSEWARETYPE='1' AND v.ISDELETE = '0'
		LEFT JOIN BUS_DATACOURSEWARE d on f.COURSEWAREID = d.DATACOURSEWARE_ID AND COURSEWARETYPE='2' AND d.ISDELETE = '0'
		where f.ISDELETE = '0'
		<if test="CURRICULUM_ID != null and CURRICULUM_ID != ''"><!-- 课程ID -->
			AND f.CURRICULUMID = #{CURRICULUM_ID}
		</if>

		ORDER BY f.OPERATTIME DESC
	</select>

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

	<!-- 列表(根据课程ID获取数据) -->
	<select id="findByCurriculumId" parameterType="pd" resultType="pd">
		select
			f.COURSEWAREID,
			f.COURSEWARETYPE
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		AND f.CURRICULUMID = #{CURRICULUM_ID}
	</select>

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

	<!-- 列表(根据课程ID获取习题数据) -->
	<select id="findForStudyByCurriculumId" parameterType="pd" resultType="pd">
		select
		f.KCMIDDLEKJ_ID,
		f.CURRICULUMID,
		f.COURSEWAREID,
		f.COURSEWARETYPE,
		q.QUESTION_ID,
		q.QUESTIONTYPE,
		q.QUESTIONDRY,
		q.OPTIONA,
		q.OPTIONB,
		q.OPTIONC,
		q.OPTIOND,
		q.ANSWER
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_QUESTION q ON q.COURSEWAREID = f.COURSEWAREID AND q.COURSEWARETYPE = f.COURSEWARETYPE
		where f.ISDELETE = '0' AND q.ISDELETE = '0'
		AND f.CURRICULUMID = #{CURRICULUM_ID}
	</select>

	<!-- 列表(根据课件ID获取习题数据) -->
	<select id="findForStudyByCoursewareId" parameterType="pd" resultType="pd">
		select
		f.KCMIDDLEKJ_ID,
		f.CURRICULUMID,
		f.COURSEWAREID,
		f.COURSEWARETYPE,
		q.QUESTION_ID,
		q.QUESTIONTYPE,
		q.QUESTIONDRY,
		q.OPTIONA,
		q.OPTIONB,
		q.OPTIONC,
		q.OPTIOND,
		q.ANSWER
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_QUESTION q ON q.COURSEWAREID = f.COURSEWAREID AND q.COURSEWARETYPE = f.COURSEWARETYPE
		where f.ISDELETE = '0' AND q.ISDELETE = '0'
		AND f.CURRICULUMID = #{CURRICULUMID}
		AND f.COURSEWAREID = #{COURSEWAREID} AND f.COURSEWARETYPE = #{COURSEWARETYPE}
	</select>
	<!-- 列表(根据所选ID获取数据) -->
	<select id="findByCurriculumIds" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
		CURRICULUMID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>

	<select id="findClassHourByCurriculumIds" parameterType="pd" resultType="pd">
		SELECT
		f.COURSEWAREID,
		f.COURSEWARETYPE,
		v.CLASSHOUR as vc,
		d.CLASSHOUR as dc
		FROM
		BUS_KCMIDDLEKJ f
		LEFT JOIN BUS_VIDEOCOURSEWARE v ON f.COURSEWAREID = v.VIDEOCOURSEWARE_ID AND f.COURSEWARETYPE = 1
		LEFT JOIN BUS_DATACOURSEWARE d ON f.COURSEWAREID = d.DATACOURSEWARE_ID AND f.COURSEWARETYPE = 2
		where f.CURRICULUMID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>


	<!-- 【app学习详细】（根据STUDYTASK_ID获取当前的任务下的相关课件） -->
	<select id="listAllByTask" parameterType="pd" resultType="pd">
		select
		f.CURRICULUMID,
		f.COURSEWAREID,
		f.COURSEWARETYPE,
		IF(f.COURSEWARETYPE='1',v.COURSEWARENAME,d.COURSEWARENAME) COURSEWARENAME,
		IF(f.COURSEWARETYPE='1',v.SPEAKER,d.SPEAKER) SPEAKER,
		IF(f.COURSEWARETYPE='1',v.COURSEWAREINTRODUCE,d.COURSEWAREINTRODUCE) COURSEWAREINTRODUCE,
		IF(f.COURSEWARETYPE='1',v.VIDEOCAPTURE,d.COURSEWARECAPTURE) COURSEWARECAPTURE,
		IF(f.COURSEWARETYPE='1',v.VIDEOFILES,d.COURSEWAREFILES) COURSEWAREFILES,
		IF( f.COURSEWARETYPE = '1', IFNULL( v.VIDEOTIME,  0 ), 	IFNULL( d.CLASSHOUR, 0 )) VIDEOTIME,
		IF(f.COURSEWARETYPE='1',IFNULL(a.RESOURCETIME,0),IFNULL(b.RESOURCETIME,0)) RESOURCETIME,
		IF(f.COURSEWARETYPE='1',IFNULL(a.PLAYCOUNT,0),IFNULL(b.PLAYCOUNT,0)) PLAYCOUNT
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_VIDEOCOURSEWARE v on f.COURSEWAREID = v.VIDEOCOURSEWARE_ID AND f.COURSEWARETYPE='1' AND v.ISDELETE = '0'
		LEFT JOIN BUS_DATACOURSEWARE d on f.COURSEWAREID = d.DATACOURSEWARE_ID AND f.COURSEWARETYPE='2' AND d.ISDELETE = '0'
		LEFT JOIN BUS_COURSESTUDYVIDEORECORD a on a.VIDEOCOURSEWARE_ID = v.VIDEOCOURSEWARE_ID AND a.TYPE = '1' and a.ISDELETE = '0' AND a.USER_ID = #{USER_ID} and a.STUDYTASK_ID = #{STUDYTASK_ID}
		LEFT JOIN BUS_COURSESTUDYVIDEORECORD b on b.VIDEOCOURSEWARE_ID = d.DATACOURSEWARE_ID AND b.TYPE = '2' and b.ISDELETE = '0' AND b.USER_ID = #{USER_ID} and  b.STUDYTASK_ID = #{STUDYTASK_ID}
		LEFT JOIN BUS_STUDYTASKCURRICULUM r on r.CURRICULUM_ID = f.CURRICULUMID
		where f.ISDELETE = '0' AND IF(f.COURSEWARETYPE='1',v.VIDEOCOURSEWARE_ID,d.DATACOURSEWARE_ID) IS NOT NULL AND r.STUDYTASK_ID = #{STUDYTASK_ID}
		GROUP BY f.COURSEWAREID, f.COURSEWARETYPE
		ORDER BY f.OPERATTIME DESC
	</select>

	<!-- 删除-->
	<delete id="deleteByCURRICULUMID" parameterType="pd">
		delete from
		<include refid="tableName"></include>
		where
		CURRICULUMID = #{CURRICULUM_ID}
	</delete>

	<select id="countByCourseWareId" parameterType="pd" resultType="String">
		select count(1) from BUS_KCMIDDLEKJ where COURSEWAREID = #{CURRICULUM_ID} and ISDELETE = '0'
	</select>

</mapper>
