<?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.onlinexxks.StudyTaskMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.STUDY_NAME,
		f.TRAINTYPE,
		f.POSTTYPE,
		f.PEIXUE_START_TIME,
		f.PEIXUE_END_TIME,
		f.KJ_STATE,
		f.GJ_STATE,
		f.STAGEEXAMPAPER_ID,
		f.STUDY_USER_ID,
		f.STUDY_CURRICULUM_ID,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.STATE,
		f.COURSEWARE_COUNT,
		f.SUM_CLASSHOUR,
		f.STUDYTASK_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		STUDY_NAME,
		TRAINTYPE,
		POSTTYPE,
		PEIXUE_START_TIME,
		PEIXUE_END_TIME,
		KJ_STATE,
		GJ_STATE,
		STAGEEXAMPAPER_ID,
		STUDY_USER_ID,
		STUDY_CURRICULUM_ID,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		CORPINFO_ID,
		STATE,
		COURSEWARE_COUNT,
		SUM_CLASSHOUR,
		STUDYTASK_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{STUDY_NAME},
		#{TRAINTYPE},
		#{POSTTYPE},
		#{PEIXUE_START_TIME},
		#{PEIXUE_END_TIME},
		#{KJ_STATE},
		#{GJ_STATE},
		#{STAGEEXAMPAPER_ID},
		#{STUDY_USER_ID},
		#{STUDY_CURRICULUM_ID},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{CORPINFO_ID},
		#{STATE},
		#{COURSEWARE_COUNT},
		#{SUM_CLASSHOUR},
		#{STUDYTASK_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">
		delete from
		<include refid="tableName"></include>
		where
			STUDYTASK_ID = #{STUDYTASK_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			STUDY_NAME = #{STUDY_NAME},
			TRAINTYPE = #{TRAINTYPE},
			POSTTYPE = #{POSTTYPE},
			PEIXUE_START_TIME = #{PEIXUE_START_TIME},
			PEIXUE_END_TIME = #{PEIXUE_END_TIME},
			KJ_STATE = #{KJ_STATE},
			GJ_STATE = #{GJ_STATE},
			STAGEEXAMPAPER_ID = #{STAGEEXAMPAPER_ID},
			STUDY_USER_ID = #{STUDY_USER_ID},
			STUDY_CURRICULUM_ID = #{STUDY_CURRICULUM_ID},
			CREATOR = #{CREATOR},
			CREATTIME = #{CREATTIME},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			ISDELETE = #{ISDELETE},
			CORPINFO_ID = #{CORPINFO_ID},
			STUDYTASK_ID = STUDYTASK_ID
		where
			STUDYTASK_ID = #{STUDYTASK_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		p.PASSSCORE,
		dic1.name as post_type_name,
		dic2.name as train_type_name
		,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
		 left join bus_stageexampaper p on p.STUDYTASK_ID=f.STUDYTASK_ID
		where
			f.STUDYTASK_ID = #{STUDYTASK_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		,dic1.name as post_type_name
		,dic2.name as train_type_name,
		(SELECT count(*) FROM bus_stagestudentrelation s1 WHERE s1.STUDYTASK_ID = f.STUDYTASK_ID ) AS userCount,
		(SELECT count(*) FROM bus_stagestudentrelation s2 WHERE	s2.STUDYTASK_ID = f.STUDYTASK_ID and st.PASSSCORE &lt;= s2.STAGEEXAMSCORE and s2.STAGEEXAMSCORE > '-1') AS hg, -- 合格人数
		(SELECT count(*) FROM bus_stagestudentrelation s3 WHERE s3.STUDYTASK_ID = f.STUDYTASK_ID and s3.STAGEEXAMSCORE > '-1') AS cj -- 参加考试人数
		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
		LEFT JOIN BUS_STAGEEXAMPAPER st on st.STUDYTASK_ID = f.STUDYTASK_ID
		where 1=1
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and ( f.STUDY_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%') )
		</if>
		<if test="pd.STATE != null and pd.STATE != ''"><!-- 关键词检索 -->
			and STATE = #{pd.STATE}
		</if>
		<if test="pd.TRAINTYPE != null and pd.TRAINTYPE != ''"><!-- 关键词检索 -->
			and TRAINTYPE = #{pd.TRAINTYPE}
		</if>
		<if test="pd.POSTTYPE != null and pd.POSTTYPE != ''"><!-- 关键词检索 -->
			and POSTTYPE = #{pd.POSTTYPE}
		</if>
		<if test="pd.STARTTIME != null and pd.STARTTIME != ''">
			and f.PEIXUE_START_TIME &gt;= #{pd.STARTTIME}
		</if>
		<if test="pd.ENDTIME != null and pd.ENDTIME != ''">
			and f.PEIXUE_END_TIME &lt;= #{pd.ENDTIME}
		</if>
		<if test="pd.STATUS != null and pd.STATUS == 1">
			and DATE_FORMAT( f.PEIXUE_START_TIME, '%Y-%m-%d' ) &gt; DATE_FORMAT( NOW(), '%Y-%m-%d' )
		</if>
		<if test="pd.STATUS != null and pd.STATUS == 2">
			and DATE_FORMAT( f.PEIXUE_START_TIME, '%Y-%m-%d' ) &lt;= DATE_FORMAT( NOW(), '%Y-%m-%d' )
			and DATE_FORMAT( f.PEIXUE_END_TIME, '%Y-%m-%d' ) &gt;= DATE_FORMAT( NOW(), '%Y-%m-%d' )
		</if>
		<if test="pd.STATUS != null and pd.STATUS == 3">
			and DATE_FORMAT( f.PEIXUE_END_TIME, '%Y-%m-%d' ) &lt; DATE_FORMAT( NOW(), '%Y-%m-%d' )
		</if>
		<if test="pd.STUDY_USER_ID != null and pd.STUDY_USER_ID != ''"><!-- 关键词检索 -->
			and exists (SELECT 1 FROM bus_stagestudentrelation s WHERE s.USER_ID=#{pd.STUDY_USER_ID} and f.STUDYTASK_ID=s.STUDYTASK_ID)
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		ORDER BY f.OPERATTIME desc ,f.PEIXUE_START_TIME DESC
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		delete from
		<include refid="tableName"></include>
		where
			STUDYTASK_ID in
		<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

	<!-- (全部视频课件+资料课件) -->
	<select id="getAllCourseware" parameterType="pd" resultType="pd">
		select distinct f.* from
		(
			select
				CONCAT_WS('_', v.VIDEOCOURSEWARE_ID, '1') COURSEWARE_ID,
				CONCAT_WS('', '[', '视频课件]', v.COURSEWARENAME) COURSEWARENAME,
				SPEAKER,
				CLASSHOUR,
				v.OPERATTIME
			from
				BUS_VIDEOCOURSEWARE v
			left join bus_kcmiddlekj kk on kk.COURSEWAREID = v.VIDEOCOURSEWARE_ID and kk.COURSEWARETYPE=1
			left join bus_studytaskcurriculum sc on sc.CURRICULUM_ID = kk.CURRICULUMID
			where sc.STUDYTASK_ID = #{STUDYTASK_ID}
			UNION ALL
			select
				CONCAT_WS('_', d.DATACOURSEWARE_ID, '2') COURSEWARE_ID,
				CONCAT_WS('', '[', '资料课件]', d.COURSEWARENAME) COURSEWARENAME,
				SPEAKER,
				CLASSHOUR,
				d.OPERATTIME
			from
				BUS_DATACOURSEWARE d
			left join bus_kcmiddlekj kk on kk.COURSEWAREID = d.DATACOURSEWARE_ID and kk.COURSEWARETYPE=2
			left join bus_studytaskcurriculum sc on sc.CURRICULUM_ID = kk.CURRICULUMID
			where sc.STUDYTASK_ID = #{STUDYTASK_ID}
		) f
		ORDER BY f.OPERATTIME DESC
	</select>
	<!-- CURRICULUM  课程 -->
	<select id="getcurriculumAllBystudyTaskId" parameterType="pd" resultType="pd">
		select * ,COUNT(m.KCMIDDLEKJ_ID) COURSEWARECOUNT
		from BUS_STUDYTASKCURRICULUM f
		LEFT JOIN BUS_CURRICULUM c on c.CURRICULUM_ID = f.CURRICULUM_ID
		LEFT JOIN BUS_KCMIDDLEKJ m ON m.CURRICULUMID = f.CURRICULUM_ID AND m.ISDELETE = '0'
		where 1=1
		<if test="STUDYTASK_ID != null and STUDYTASK_ID != ''"><!-- 关键词检索 -->
			and f.STUDYTASK_ID = #{STUDYTASK_ID}
		</if>
		GROUP BY f.CURRICULUM_ID
	</select>

	<!-- (全部视频课件+资料课件) -->
	<select id="getAllcurriculum" parameterType="pd" resultType="pd">
		select distinct c.*
		from
		<include refid="tableName"></include>f
		left join bus_studytaskcurriculum kk on kk.STUDYTASK_ID = f.STUDYTASK_ID
		left join bus_curriculum c on c.CURRICULUM_ID = kk.CURRICULUM_ID
		where f.STUDYTASK_ID = #{STUDYTASK_ID}
	</select>

	<!-- 更新任务状态 -->
	<update id="updateTaskState" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME},
		STATE = #{STATE}
		where
		STUDYTASK_ID = #{STUDYTASK_ID}
	</update>


	<select id="getAllByuserInfo" parameterType="pd" resultType="pd">
		select IF(f.COURSEWARETYPE='1',v.COURSEWARENAME,d.COURSEWARENAME) COURSEWARENAME
		,IF(f.COURSEWARETYPE='1',v.CLASSHOUR,d.CLASSHOUR) CLASSHOUR ,
		c.PLAYCOUNT,c.RESOURCETIME,f.COURSEWARETYPE
		from BUS_KCMIDDLEKJ 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 c on c.VIDEOCOURSEWARE_ID = f.COURSEWAREID and c.USER_ID =  #{STUDYTASK_USERID}  and c.STUDYTASK_ID = #{STUDYTASK_ID}
		where 1=1 and f.CURRICULUMID in (select CURRICULUM_ID from BUS_STUDYTASKCURRICULUM WHERE STUDYTASK_ID = #{STUDYTASK_ID})
		GROUP BY f.COURSEWAREID
		ORDER BY f.COURSEWARETYPE desc
	</select>

	<!-- 列表 -->
	<select id="userdatalistPage" parameterType="page" resultType="pd">
		SELECT
		s.STUDYTASK_ID,
		s.STUDY_NAME,
		s.SUM_CLASSHOUR,
		f.STAGEEXAMSCORE,
		st.PASSSCORE,
		s.PEIXUE_START_TIME,
		s.PEIXUE_END_TIME,
		TRUNCATE((SUM(IF(co.RESOURCETIME IS NULL,0,(co.RESOURCETIME DIV 60)))+f.COMPLETE_CLASSHOUR),0) yixue,
		dic1. NAME AS post_type_name,
		dic2. NAME AS train_type_name
		FROM
		bus_stagestudentrelation f
		LEFT JOIN BUS_STUDYTASK s ON s.STUDYTASK_ID = f.STUDYTASK_ID
		LEFT JOIN sys_dictionaries dic1 ON dic1.DICTIONARIES_ID = s.POSTTYPE
		LEFT JOIN sys_dictionaries dic2 ON dic2.DICTIONARIES_ID = s.TRAINTYPE
		LEFT JOIN BUS_STAGEEXAMPAPER st ON st.STUDYTASK_ID = f.STUDYTASK_ID
		LEFT JOIN BUS_COURSESTUDYVIDEORECORD co ON co.STUDYTASK_ID = f.STUDYTASK_ID and co.USER_ID = f.USER_ID AND co.PLAYCOUNT = 0
		WHERE
		1 = 1
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and ( s.STUDY_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%') )
		</if>
		<if test="pd.STATE != null and pd.STATE != ''"><!-- 关键词检索 -->
			and STATE = #{pd.STATE}
		</if>
		<if test="pd.TRAINTYPE != null and pd.TRAINTYPE != ''"><!-- 关键词检索 -->
			and TRAINTYPE = #{pd.TRAINTYPE}
		</if>
		<if test="pd.POSTTYPE != null and pd.POSTTYPE != ''"><!-- 关键词检索 -->
			and POSTTYPE = #{pd.POSTTYPE}
		</if>
		<if test="pd.STARTTIME != null and pd.STARTTIME != ''">
			and s.PEIXUE_START_TIME &gt;= #{pd.STARTTIME}
		</if>
		<if test="pd.ENDTIME != null and pd.ENDTIME != ''">
			and s.PEIXUE_END_TIME &lt;= #{pd.ENDTIME}
		</if>
		<if test="pd.STATUS != null and pd.STATUS == 1">
			and DATE_FORMAT( s.PEIXUE_START_TIME, '%Y-%m-%d' ) &gt;= DATE_FORMAT( NOW(), '%Y-%m-%d' )
		</if>
		<if test="pd.STATUS != null and pd.STATUS == 2">
			and DATE_FORMAT( s.PEIXUE_START_TIME, '%Y-%m-%d' ) &lt;= DATE_FORMAT( NOW(), '%Y-%m-%d' )
			and DATE_FORMAT( s.PEIXUE_END_TIME, '%Y-%m-%d' ) &gt;= DATE_FORMAT( NOW(), '%Y-%m-%d' )
		</if>
		<if test="pd.STATUS != null and pd.STATUS == 3">
			and DATE_FORMAT( s.PEIXUE_END_TIME, '%Y-%m-%d' ) &lt;= DATE_FORMAT( NOW(), '%Y-%m-%d' )
		</if>
		<if test="pd.STUDY_USER_ID != null and pd.STUDY_USER_ID != ''"><!-- 关键词检索 -->
			and f.USER_ID = #{pd.STUDY_USER_ID}
		</if>
		GROUP BY f.STAGESTUDENTRELATION_ID
		ORDER BY s.OPERATTIME desc ,s.PEIXUE_START_TIME DESC
	</select>
	<!-- (全部视频课件+资料课件) -->
	<select id="getAllCoursewareByHandout" parameterType="pd" resultType="pd">
		select distinct f.* from
		(
			select
				CONCAT_WS('_', v.VIDEOCOURSEWARE_ID, '1') COURSEWARE_ID,
				v.COURSEWARENAME as COURSEWARENAME,
				'视频课件' as coutType,
				v.SPEAKER,
				v.COURSEWAREINTRODUCE,
				v.CLASSHOUR,
				v.OPERATTIME
			from
				BUS_VIDEOCOURSEWARE v
			left join bus_kcmiddlekj kk on kk.COURSEWAREID = v.VIDEOCOURSEWARE_ID and kk.COURSEWARETYPE=1
			left join bus_studytaskcurriculum sc on sc.CURRICULUM_ID = kk.CURRICULUMID
			where sc.STUDYTASK_ID = #{STUDYTASK_ID}
			UNION ALL
			select
				CONCAT_WS('_', d.DATACOURSEWARE_ID, '2') COURSEWARE_ID,
				d.COURSEWARENAME as COURSEWARENAME,
				'资料课件' as coutType,
				d.SPEAKER,
				d.COURSEWAREINTRODUCE,
				d.CLASSHOUR,
				d.OPERATTIME
			from
				BUS_DATACOURSEWARE d
			left join bus_kcmiddlekj kk on kk.COURSEWAREID = d.DATACOURSEWARE_ID and kk.COURSEWARETYPE=2
			left join bus_studytaskcurriculum sc on sc.CURRICULUM_ID = kk.CURRICULUMID
			where sc.STUDYTASK_ID = #{STUDYTASK_ID}
		) f
		ORDER BY f.OPERATTIME DESC
	</select>
	<!-- 修改 任务的结束时间 -->
	<update id="editEndTime" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		PEIXUE_END_TIME = #{PEIXUE_END_TIME},
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME}
		where
		STUDYTASK_ID = #{STUDYTASK_ID}
	</update>
</mapper>
