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

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.COURSEWARENAME,
		f.SPEAKER,
		f.COURSEWAREINTRODUCE,
		f.COURSEWAREFILES,
		f.COURSEWARECAPTURE,
		f.COURSEWAREUSERS,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.TRAINTYPE,
		f.POSTTYPE,
		f.ONTLINETYPE,
		f.CLASSHOUR,
		f.STATE,
		f.DATACOURSEWARE_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		COURSEWARENAME,
		SPEAKER,
		COURSEWAREINTRODUCE,
		COURSEWAREFILES,
		COURSEWARECAPTURE,
		COURSEWAREUSERS,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		CORPINFO_ID,
		TRAINTYPE,
		POSTTYPE,
		ONTLINETYPE,
		CLASSHOUR,
		STATE,
		DATACOURSEWARE_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{COURSEWARENAME},
		#{SPEAKER},
		#{COURSEWAREINTRODUCE},
		#{COURSEWAREFILES},
		#{COURSEWARECAPTURE},
		#{COURSEWAREUSERS},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{CORPINFO_ID},
		#{TRAINTYPE},
		#{POSTTYPE},
		#{ONTLINETYPE},
		#{CLASSHOUR},
		#{STATE},
		#{DATACOURSEWARE_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
		DATACOURSEWARE_ID = #{DATACOURSEWARE_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		COURSEWARENAME = #{COURSEWARENAME},
		SPEAKER = #{SPEAKER},
		COURSEWAREINTRODUCE = #{COURSEWAREINTRODUCE},
		COURSEWAREFILES = #{COURSEWAREFILES},
		COURSEWARECAPTURE = #{COURSEWARECAPTURE},
		COURSEWAREUSERS = #{COURSEWAREUSERS},
		TRAINTYPE = #{TRAINTYPE},
		POSTTYPE = #{POSTTYPE},
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME},
		ONTLINETYPE = #{ONTLINETYPE},
		CLASSHOUR = #{CLASSHOUR},
		DATACOURSEWARE_ID = DATACOURSEWARE_ID
		where
		DATACOURSEWARE_ID = #{DATACOURSEWARE_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		,(select count(1) from BUS_KCMIDDLEKJ where COURSEWAREID = #{DATACOURSEWARE_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.DATACOURSEWARE_ID = #{DATACOURSEWARE_ID}
	</select>

	<!-- 列表 -->
	<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,
		ts.name as TEACHERNAME,
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_KCMIDDLEKJ g on g.CURRICULUMID = f.DATACOURSEWARE_ID AND g.COURSEWARETYPE = '2' AND g.ISDELETE = '0'
		LEFT JOIN BUS_STAGEEXAMPAPERCOURSEWARE h on h.COURSEWARE_ID = f.DATACOURSEWARE_ID AND h.COURSEWARETYPE = '2' 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 BUS_TEACHER ts on f.SPEAKER = ts.TEACHER_ID
		where f.ISDELETE = '0'
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''">
			and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
				f.COURSEWARENAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		<if test="pd.tarinTypeID != null and pd.tarinTypeID != ''"><!-- 培训行业类型 -->
			and f.TRAINTYPE = #{pd.tarinTypeID}
		</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.teacherKey != null and pd.teacherKey != ''"><!-- 教师姓名 -->
			and  ts.NAME LIKE CONCAT(CONCAT('%', #{pd.teacherKey}),'%')
		</if>
		<if test="pd.trainingSectionKey != null and pd.trainingSectionKey != ''"><!-- 培训板块 -->
			and f.DATACOURSEWARE_ID in (select RECORD_ID from bus_coursewarerelation crt where  crt.DICTIONARIES_IDS LIKE CONCAT(CONCAT('%', #{pd.trainingSectionKey}),'%'))
		</if>
		GROUP BY f.DATACOURSEWARE_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}
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		ISDELETE = '1',
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME}
		where
		DATACOURSEWARE_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
		DATACOURSEWARE_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.DATACOURSEWARE_ID, '2') COURSEWAREID
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{CORPINFO_ID}
		<if test="COURSEWAREUSERS != null and COURSEWAREUSERS != ''"><!-- 关键词检索 -->
			and f.COURSEWAREUSERS LIKE CONCAT(CONCAT('%,', #{COURSEWAREUSERS}),',%')
		</if>
		ORDER BY f.CREATTIME DESC
	</select>

	<!-- 下拉选项列表(全部视频课件+资料课件) -->
	<select id="getAllCourseware" parameterType="pd" resultType="pd">
		select * from (
			select
			CONCAT_WS('_', f.VIDEOCOURSEWARE_ID, '1') COURSEWARE_ID,
			CONCAT_WS('', '[', '视频课件]', f.COURSEWARENAME) COURSEWARENAME,
			f.OPERATTIME
			from
				BUS_VIDEOCOURSEWARE f
			where f.ISDELETE = '0' and f.CORPINFO_ID = #{CORPINFO_ID}
			UNION ALL
			select
			CONCAT_WS('_', f.DATACOURSEWARE_ID, '2') COURSEWARE_ID,
			CONCAT_WS('', '[', '资料课件]', f.COURSEWARENAME) COURSEWARENAME,
			f.OPERATTIME
			from
				BUS_DATACOURSEWARE f
			where f.ISDELETE = '0' and f.CORPINFO_ID = #{CORPINFO_ID}
			) f
			ORDER BY f.OPERATTIME DESC
	</select>

	<!-- 修改 -->
	<update id="editByState" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		STATE = #{STATE},
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME}
		where
		DATACOURSEWARE_ID = #{DATACOURSEWARE_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,
		ts.NAME SPEAKERNAME
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_KCMIDDLEKJ k ON k.COURSEWAREID = f.DATACOURSEWARE_ID
		LEFT JOIN sys_dictionaries dic1 on dic1.DICTIONARIES_ID = f.POSTTYPE
		LEFT JOIN sys_dictionaries dic2 on dic2.DICTIONARIES_ID = f.TRAINTYPE
		LEFT JOIN BUS_TEACHER ts on f.SPEAKER = ts.TEACHER_ID
		where f.ISDELETE = '0'
		and k.CURRICULUMID =  #{CURRICULUMID}
		<if test="CHAPTER_ID != null and CHAPTER_ID != ''"><!-- 关键词检索 -->
			and k.CHAPTER_ID = #{CHAPTER_ID}
		</if>
	</select>
</mapper>
