<?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.courseware.VideoCoursewareMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.COURSEWARENAME,
		f.SPEAKER,
		f.COURSEWAREINTRODUCE,
		f.VIDEOFILES,
		f.VIDEOCAPTURE,
		f.VIDEOTIME,
		f.VIDEOUSERS,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.TRAINTYPE,
		f.POSTTYPE,
		f.ONTLINETYPE,
		f.CLASSHOUR,
		f.STATE,
		f.ISPLATFORM,
		f.PLATFORMID,
		f.VIDEOCOURSEWARE_ID_REMOTE,
		f.CURRICULUM_ID_REMOTE,
		f.CURRENCY,
		f.VIDEOCOURSEWARE_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		COURSEWARENAME,
		SPEAKER,
		COURSEWAREINTRODUCE,
		VIDEOFILES,
		VIDEOCAPTURE,
		VIDEOTIME,
		VIDEOUSERS,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		CORPINFO_ID,
		TRAINTYPE,
		POSTTYPE,
		ONTLINETYPE,
		CLASSHOUR,
		STATE,
		ISPLATFORM,
		PLATFORMID,
		VIDEOCOURSEWARE_ID_REMOTE,
		CURRICULUM_ID_REMOTE,
		CURRENCY,
		VIDEOCOURSEWARE_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{COURSEWARENAME},
		#{SPEAKER},
		#{COURSEWAREINTRODUCE},
		#{VIDEOFILES},
		#{VIDEOCAPTURE},
		#{VIDEOTIME},
		#{VIDEOUSERS},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{CORPINFO_ID},
		#{TRAINTYPE},
		#{POSTTYPE},
		#{ONTLINETYPE},
		#{CLASSHOUR},
		#{STATE},
		#{ISPLATFORM},
		#{PLATFORMID},
		#{VIDEOCOURSEWARE_ID_REMOTE},
		#{CURRICULUM_ID_REMOTE},
		#{CURRENCY},
		#{VIDEOCOURSEWARE_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
		VIDEOCOURSEWARE_ID = #{VIDEOCOURSEWARE_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		COURSEWARENAME = #{COURSEWARENAME},
		SPEAKER = #{SPEAKER},
		COURSEWAREINTRODUCE = #{COURSEWAREINTRODUCE},
		VIDEOFILES = #{VIDEOFILES},
		VIDEOCAPTURE = #{VIDEOCAPTURE},
		VIDEOTIME = #{VIDEOTIME},
		VIDEOUSERS = #{VIDEOUSERS},
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME},
		TRAINTYPE = #{TRAINTYPE},
		POSTTYPE = #{POSTTYPE},
		ONTLINETYPE = #{ONTLINETYPE},
		CLASSHOUR = #{CLASSHOUR},
		VIDEOCOURSEWARE_ID = VIDEOCOURSEWARE_ID
		where
		VIDEOCOURSEWARE_ID = #{VIDEOCOURSEWARE_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		,(select count(1) from BUS_KCMIDDLEKJ where COURSEWAREID = #{VIDEOCOURSEWARE_ID}) as kjCount
		,dic1.name as post_type_name
		,dic2.name as train_type_name
		from
		<include refid="tableName"></include> f
		LEFT JOIN sys_dictionaries dic1 on dic1.DICTIONARIES_ID = f.POSTTYPE
		LEFT JOIN sys_dictionaries dic2 on dic2.DICTIONARIES_ID = f.TRAINTYPE
		where
		f.VIDEOCOURSEWARE_ID = #{VIDEOCOURSEWARE_ID}
	</select>

	<!-- 列表
	CURRICULUMCOUNT 关联课程数
	STAGECOUNT 关联阶考试卷数-->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		IFNULL(COUNT(DISTINCT g.CURRICULUMID),0) CURRICULUMCOUNT,
		IFNULL(COUNT(DISTINCT h.STAGEEXAMPAPER_ID),0) STAGECOUNT,
		dic1.name as post_type_name , dic2.name as train_type_name,
		(select COUNT(*) from BUS_QUESTION c where c.COURSEWAREID = f.VIDEOCOURSEWARE_ID and c.ISDELETE = 0) as NUMEXAMPLE,
		<include refid="Field"></include>,
		(select GROUP_CONCAT(v.NAME) from vi_coursewarerelation v where v.RECORD_ID = f.VIDEOCOURSEWARE_ID and v.RECORD_TYPE_ID = 'relatedTerm') as relatedTermName,
		GROUP_CONCAT(v.NAME)  as trainingSectionName
		from
		<include refid="tableName"></include> f
		 LEFT JOIN BUS_KCMIDDLEKJ g on g.CURRICULUMID = f.VIDEOCOURSEWARE_ID AND g.COURSEWARETYPE = '1' AND g.ISDELETE = '0'
		 LEFT JOIN BUS_STAGEEXAMPAPERCOURSEWARE h on h.COURSEWARE_ID = f.VIDEOCOURSEWARE_ID AND h.COURSEWARETYPE = '1' AND h.ISDELETE = '0'
		LEFT JOIN sys_dictionaries dic1 on dic1.DICTIONARIES_ID = f.POSTTYPE
		LEFT JOIN sys_dictionaries dic2 on dic2.DICTIONARIES_ID = f.TRAINTYPE
		LEFT JOIN vi_coursewarerelation v ON v.RECORD_ID = f.VIDEOCOURSEWARE_ID AND v.RECORD_TYPE_ID = 'trainingSection'
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID} and f.ISPLATFORM != 3
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
                f.COURSEWARENAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
                 or
				f.SPEAKER LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		<if test="pd.COURSEWARENAME != null and pd.COURSEWARENAME != ''"><!-- 关键词检索 -->
			and
			f.COURSEWARENAME LIKE CONCAT(CONCAT('%', #{pd.COURSEWARENAME}),'%')

		</if>
		<if test="pd.trainingSectionKey != null and pd.trainingSectionKey != ''"><!-- 培训行业类型 -->
			and v.DICTIONARIES_IDS LIKE CONCAT(CONCAT('%', #{pd.trainingSectionKey}),'%')
		</if>
		<if test="pd.TRAINTYPE != null and pd.TRAINTYPE != ''"><!-- 培训行业类型 -->
			and f.TRAINTYPE = #{pd.TRAINTYPE}
		</if>
		<if test="pd.POSTTYPE != null and pd.POSTTYPE != ''"><!-- 培训行业类型 -->
			and f.POSTTYPE = #{pd.POSTTYPE}
		</if>
		<if test="pd.STATE != null and pd.STATE != ''"><!-- 状态 -->
			and f.STATE = #{pd.STATE}
		</if>
		<if test="pd.SPEAKER != null and pd.SPEAKER != ''"><!-- 关键词检索 -->
			and f.SPEAKER LIKE CONCAT(CONCAT('%', #{pd.SPEAKER}),'%')
		</if>
		GROUP BY f.VIDEOCOURSEWARE_ID ORDER BY f.CREATTIME 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}
		<if test="VIDEOCOURSEWARE_ID != null and VIDEOCOURSEWARE_ID != ''">
			and VIDEOCOURSEWARE_ID in
			<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
				#{item}
			</foreach>
		</if>

	</select>

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

	<!-- 下拉选项列表(全部) -->
	<select id="getSelect" parameterType="pd" resultType="pd">
		select
		f.COURSEWARENAME,
		CONCAT_WS('_', f.VIDEOCOURSEWARE_ID, '1') COURSEWAREID
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{CORPINFO_ID}
		<if test="VIDEOUSERS != null and VIDEOUSERS != ''"><!-- 关键词检索 -->
			and f.VIDEOUSERS LIKE CONCAT(CONCAT('%,', #{VIDEOUSERS}),',%')
		</if>
		ORDER BY f.CREATTIME DESC
	</select>

	<!-- 修改 -->
	<update id="editByState" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		STATE = #{STATE},
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME}
		where
		VIDEOCOURSEWARE_ID = #{VIDEOCOURSEWARE_ID}
	</update>

	<!-- 列表(全部) -->
	<select id="listAllByCurriculumId" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		,dic1.name as post_type_name
		,dic2.name as train_type_name
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_KCMIDDLEKJ k ON k.COURSEWAREID = f.VIDEOCOURSEWARE_ID
		LEFT JOIN sys_dictionaries dic1 on dic1.DICTIONARIES_ID = f.POSTTYPE
		LEFT JOIN sys_dictionaries dic2 on dic2.DICTIONARIES_ID = f.TRAINTYPE
		where f.ISDELETE = '0'
			and f.CORPINFO_ID = #{CORPINFO_ID}
			and k.CURRICULUMID = #{CURRICULUMID}
		<if test="CHAPTER_ID != null and CHAPTER_ID != ''"><!-- 关键词检索 -->
			and k.CHAPTER_ID = #{CHAPTER_ID}
		</if>
	</select>
    <select id="getCountIndex" resultType="com.zcloud.entity.PageData">
		SELECT
			f.VIDEOCOURSEWARE_ID_REMOTE,
			f.CURRICULUM_ID_REMOTE,
			f.CLASSHOUR,
			f.VIDEOFILES,
			'' as COURSEWAREFILES,
			f.COURSEWARENAME,
			dic1.NAME AS POST_NAME,
			f.VIDEOCAPTURE CAPTURE,
			'1' AS TYPE
		FROM
			BUS_VIDEOCOURSEWARE f
				LEFT JOIN sys_dictionaries dic1 ON dic1.DICTIONARIES_ID = f.POSTTYPE
		WHERE
			f.ISDELETE = '0'
		  AND f.CORPINFO_ID = #{CORPINFO_ID}
		  AND f.ISPLATFORM != 3 UNION ALL
		SELECT
			'' as VIDEOCOURSEWARE_ID_REMOTE,
			'' as CURRICULUM_ID_REMOTE,
			'' as CLASSHOUR,
			'' as VIDEOFILES,
			f.COURSEWAREFILES,
			f.COURSEWARENAME,
			dic1.NAME AS POST_NAME,
			CONCAT( '', f.COURSEWARECAPTURE ) CAPTURE,
			'2' AS TYPE
		FROM
			BUS_DATACOURSEWARE f
				LEFT JOIN sys_dictionaries dic1 ON dic1.DICTIONARIES_ID = f.POSTTYPE
		WHERE
			f.ISDELETE = '0'
		  AND f.CORPINFO_ID = #{CORPINFO_ID}
	</select>
</mapper>
