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

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.CURRICULUMNAME,
		f.CURRICULUMINTRODUCE,
		f.CURRICULUMAIM,
		f.STARTTIME,
		f.ENDTIME,
		f.CURRICULUMUSERS,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.TRAINTYPE,
		f.POSTTYPE,
		f.CLASSHOUR,
		f.CAPTURE,
		f.ISPLATFORM,
		f.CURRICULUM_ID_REMOTE,
		f.CURRENCY,
		f.CURRICULUM_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CURRICULUMNAME,
		CURRICULUMINTRODUCE,
		CURRICULUMAIM,
		STARTTIME,
		ENDTIME,
		CURRICULUMUSERS,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		CORPINFO_ID,
		TRAINTYPE,
		POSTTYPE,
		CLASSHOUR,
		CAPTURE,
		ISPLATFORM,
		CURRICULUM_ID_REMOTE,
		CURRENCY,
		CURRICULUM_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CURRICULUMNAME},
		#{CURRICULUMINTRODUCE},
		#{CURRICULUMAIM},
		#{STARTTIME},
		#{ENDTIME},
		#{CURRICULUMUSERS},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{CORPINFO_ID},
		#{TRAINTYPE},
		#{POSTTYPE},
		#{CLASSHOUR},
		#{CAPTURE},
		#{ISPLATFORM},
		#{CURRICULUM_ID_REMOTE},
		#{CURRENCY},
		#{CURRICULUM_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
		CURRICULUM_ID = #{CURRICULUM_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		CURRICULUMNAME = #{CURRICULUMNAME},
		CURRICULUMINTRODUCE = #{CURRICULUMINTRODUCE},
		CURRICULUMAIM = #{CURRICULUMAIM},
		STARTTIME = #{STARTTIME},
		ENDTIME = #{ENDTIME},
		CURRICULUMUSERS = #{CURRICULUMUSERS},
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME},
		CAPTURE = #{CAPTURE},
		CLASSHOUR = #{CLASSHOUR},
		TRAINTYPE = #{TRAINTYPE},
		POSTTYPE = #{POSTTYPE},
		CURRICULUM_ID = CURRICULUM_ID
		where
		CURRICULUM_ID = #{CURRICULUM_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" 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 sys_dictionaries dic1 on dic1.DICTIONARIES_ID = f.POSTTYPE
		LEFT JOIN sys_dictionaries dic2 on dic2.DICTIONARIES_ID = f.TRAINTYPE
		where
		f.CURRICULUM_ID = #{CURRICULUM_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		,COUNT(m.KCMIDDLEKJ_ID) COURSEWARECOUNT
		,count(if( m.COURSEWARETYPE = '1' ,1,null)) as videoCount
		,count(if( m.COURSEWARETYPE = '2' ,1,null)) as dataCount
		,p.COURSEPAPERS_ID
		,p.EXAMNAME
		,dic1.name as post_type_name
		,dic2.name as train_type_name
		from
		<include refid="tableName"></include> f
		 LEFT JOIN BUS_KCMIDDLEKJ m ON m.CURRICULUMID = f.CURRICULUM_ID AND m.ISDELETE = '0'
		LEFT JOIN BUS_COURSEPAPERS p ON p.CURRICULUMID = f.CURRICULUM_ID AND p.ISDELETE = '0'
		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 = #{pd.CORPINFO_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			<!--	根据需求自己加检索条件 -->
                f.CURRICULUMNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
                 or
				f.CURRICULUMINTRODUCE LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				 or
				f.CURRICULUMAIM LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		<if test="pd.CURRICULUMNAME != null and pd.CURRICULUMNAME != ''"><!-- 关键词检索 -->
			and
			<!--	根据需求自己加检索条件 -->
			f.CURRICULUMNAME LIKE CONCAT(CONCAT('%', #{pd.CURRICULUMNAME}),'%')
		</if>
		<if test="pd.LEARNERCATEGORY != null and pd.LEARNERCATEGORY != ''"><!-- 关键词检索 -->
			and f.CURRICULUMUSERS = #{pd.LEARNERCATEGORY}
		</if>
		<if test="pd.STARTTIMEBEGIN != null and pd.STARTTIMEBEGIN != ''"><!-- 关键词检索 -->
			and f.STARTTIME &gt;= #{pd.STARTTIMEBEGIN}
		</if>
		<if test="pd.STARTTIMEEND != null and pd.STARTTIMEEND != ''">
			and f.STARTTIME &lt;= #{pd.STARTTIMEEND}
		</if>
		<if test="pd.ENDTIMEBEGIN != null and pd.ENDTIMEBEGIN != ''"><!-- 关键词检索 -->
			and f.ENDTIME &gt;= #{pd.ENDTIMEBEGIN}
		</if>

		<if test="pd.POSTTYPE != null and pd.POSTTYPE != ''">
			and f.POSTTYPE = #{pd.POSTTYPE}
		</if>
		<if test="pd.TRAINTYPE != null and pd.TRAINTYPE != ''"><!-- 培训行业类型 -->
			and f.TRAINTYPE = #{pd.TRAINTYPE}
		</if>
		GROUP BY f.CURRICULUM_ID 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 = #{CORPINFO_ID}
		<if test="STUDYTASK_ID != null and STUDYTASK_ID != ''"><!-- 关键词检索 -->
			and exists (SELECT 1 FROM BUS_STUDYTASKCURRICULUM s WHERE s.STUDYTASK_ID=#{STUDYTASK_ID} and f.CURRICULUM_ID=s.CURRICULUM_ID)
		</if>
	</select>

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

	<!-- 我的课程（获取当前用户的所属课程） -->
	<select id="pageDataByUserlistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		,COUNT(m.KCMIDDLEKJ_ID) COURSEWARECOUNT
		,p.COURSEPAPERS_ID
		,p.EXAMNAME
		,r.COURSESTUDENTRELATION_ID
		,r.STUDYCOUNT
		,r.COURSEEXAMCOUNT
		,r.STAGEEXAMCOUNT
		,r.STUDYSTATE
		,r.COURSEEXAMSTATE
		,r.STAGEEXAMSTATE
		,r.COURSEEXAMSCORE
		,r.STAGEEXAMSCORE
		from
		<include refid="tableName"></include> f
		 LEFT JOIN BUS_COURSESTUDENTRELATION r ON f.CURRICULUM_ID = r.CURRICULUM_ID AND r.ISDELETE='0'
		LEFT JOIN BUS_KCMIDDLEKJ m ON m.CURRICULUMID = f.CURRICULUM_ID AND m.ISDELETE = '0'
		LEFT JOIN BUS_COURSEPAPERS p ON p.CURRICULUMID = f.CURRICULUM_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
			(
				f.CURRICULUMNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			<!--	根据需求自己加检索条件
                字段1 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
                 or
                字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            -->
			)
		</if>
		<if test="pd.STUDYSTATE != null and pd.STUDYSTATE != ''">
			<choose>
				<when test="pd.STUDYSTATE == 1">	<!-- 未开始 -->
					and r.STUDYSTATE = #{pd.STUDYSTATE}
				</when>
				<when test="pd.STUDYSTATE == 2">	<!-- 进行中 -->
					and r.STUDYSTATE = #{pd.STUDYSTATE}
				</when>
				<when test="pd.STUDYSTATE == 3">	<!-- 已通过 -->
					and r.COURSEEXAMSTATE = #{pd.STUDYSTATE}
				</when>
				<when test="pd.STUDYSTATE == 4">	<!-- 已结束 -->
					and r.STUDYSTATE = #{pd.STUDYSTATE}
				</when>
			</choose>
		</if>
		AND r.USER_ID = #{pd.USER_ID}
		GROUP BY f.CURRICULUM_ID ORDER BY f.OPERATTIME DESC
	</select>

	<!-- 学员课程列表（查询课程信息及可想关联的学员统计） -->
	<select id="listSummarylistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		,COUNT(DISTINCT m.KCMIDDLEKJ_ID) COURSEWARECOUNT
		,COUNT(DISTINCT (CASE WHEN (r.COURSEEXAMSTATE = '3') THEN r.USER_ID END) ) COMPLETENUM
		,COUNT(DISTINCT r.USER_ID) PARTICIPANTNUM
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_KCMIDDLEKJ m ON m.CURRICULUMID = f.CURRICULUM_ID AND m.ISDELETE = '0'
		LEFT JOIN BUS_COURSESTUDENTRELATION r ON f.CURRICULUM_ID = r.CURRICULUM_ID AND r.ISDELETE='0'
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			<!--	根据需求自己加检索条件 -->
			f.CURRICULUMNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			f.CURRICULUMINTRODUCE LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			f.CURRICULUMAIM LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		<if test="pd.LEARNERCATEGORY != null and pd.LEARNERCATEGORY != ''"><!-- 关键词检索 -->
			and f.CURRICULUMUSERS = #{pd.LEARNERCATEGORY}
		</if>
		<if test="pd.STARTTIMEBEGIN != null and pd.STARTTIMEBEGIN != ''"><!-- 关键词检索 -->
			and f.STARTTIME &gt;= #{pd.STARTTIMEBEGIN}
		</if>
		<if test="pd.STARTTIMEEND != null and pd.STARTTIMEEND != ''">
			and f.STARTTIME &lt;= #{pd.STARTTIMEEND}
		</if>
		<if test="pd.ENDTIMEBEGIN != null and pd.ENDTIMEBEGIN != ''"><!-- 关键词检索 -->
			and f.ENDTIME &gt;= #{pd.ENDTIMEBEGIN}
		</if>
		<if test="pd.ENDTIMEEND != null and pd.ENDTIMEEND != ''">
			and f.ENDTIME &lt;= #{pd.ENDTIMEEND}
		</if>
		GROUP BY f.CURRICULUM_ID ORDER BY f.OPERATTIME DESC
	</select>

</mapper>
