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

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.QUESTIONNUMBER,
		f.QUESTIONTYPE,
		f.QUESTIONDRY,
		f.OPTIONA,
		f.OPTIONB,
		f.OPTIONC,
		f.OPTIOND,
		f.ANSWER,
		f.COURSEWARETYPE,
		f.COURSEWAREID,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.ISPLATFORMPAPER,
		f.QUESTION_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		QUESTIONNUMBER,
		QUESTIONTYPE,
		QUESTIONDRY,
		OPTIONA,
		OPTIONB,
		OPTIONC,
		OPTIOND,
		ANSWER,
		COURSEWARETYPE,
		COURSEWAREID,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		CORPINFO_ID,
		ISPLATFORMPAPER,
		QUESTION_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{QUESTIONNUMBER},
		#{QUESTIONTYPE},
		#{QUESTIONDRY},
		#{OPTIONA},
		#{OPTIONB},
		#{OPTIONC},
		#{OPTIOND},
		#{ANSWER},
		#{COURSEWARETYPE},
		#{COURSEWAREID},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{CORPINFO_ID},
		#{ISPLATFORMPAPER},
		#{QUESTION_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
		QUESTION_ID = #{QUESTION_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		QUESTIONNUMBER = #{QUESTIONNUMBER},
		QUESTIONTYPE = #{QUESTIONTYPE},
		QUESTIONDRY = #{QUESTIONDRY},
		OPTIONA = #{OPTIONA},
		OPTIONB = #{OPTIONB},
		OPTIONC = #{OPTIONC},
		OPTIOND = #{OPTIOND},
		ANSWER = #{ANSWER},
		COURSEWARETYPE = #{COURSEWARETYPE},
		COURSEWAREID = #{COURSEWAREID},
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME},
		QUESTION_ID = QUESTION_ID
		where
		QUESTION_ID = #{QUESTION_ID}
	</update>

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

	<!-- 列表
	 	STUDYCOUNT	联系次数
	 	COURSEEXAMRECORDCOUNT	课考使用次数
	 	STAGEEXAMRECORDCOUNT	阶考使用次数
	 	COURSEEXAMCOUNT	课考关联数
	 	STAGEEXAMCOUNT	阶考关联数	-->
	<select id="datalistPage" parameterType="page" resultType="pd">
		<choose>
			<when test="pd.COURSEWARETYPE != null and pd.COURSEWARETYPE != '' and pd.COURSEWARETYPE == 1">
				select
				v.COURSEWARENAME COURSEWARENAME,
				IFNULL(COUNT(DISTINCT g.COURSESTUDYRECORD_ID),0) STUDYCOUNT,
				IFNULL(COUNT(DISTINCT h.COURSEEXAMRECORD_ID),0) COURSEEXAMRECORDCOUNT,
				IFNULL(COUNT(DISTINCT i.STAGEEXAMRECORD_ID),0) STAGEEXAMRECORDCOUNT,
				IFNULL(COUNT(DISTINCT j.CURRICULUMID),0) COURSEEXAMCOUNT,
				IFNULL(COUNT(DISTINCT k.STAGEEXAMPAPER_ID),0) STAGEEXAMCOUNT,
				<include refid="Field"></include>
				from
				<include refid="tableName"></include> f
				left join BUS_VIDEOCOURSEWARE v on f.COURSEWAREID = v.VIDEOCOURSEWARE_ID and v.ISDELETE = '0'
				LEFT JOIN BUS_COURSESTUDYRECORD g on g.QUESTION_ID = f.QUESTION_ID AND g.ISDELETE = '0'
				LEFT JOIN BUS_COURSEEXAMRECORD h on h.QUESTION_ID = f.QUESTION_ID AND h.ISDELETE = '0'
				LEFT JOIN BUS_STAGEEXAMRECORD i on i.QUESTION_ID = f.QUESTION_ID AND i.ISDELETE = '0'
				LEFT JOIN BUS_COURSEMIDDLE j on j.QUESTIONID = f.QUESTION_ID AND j.ISDELETE = '0'
				LEFT JOIN BUS_STAGEEXAMPAPERQUESTION k on k.QUESTION_ID = f.QUESTION_ID AND k.ISDELETE = '0'
				where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
				and f.COURSEWARETYPE = #{pd.COURSEWARETYPE} and f.COURSEWAREID = #{pd.COURSEWAREID}
			</when>
			<when test="pd.COURSEWARETYPE != null and pd.COURSEWARETYPE != '' and pd.COURSEWARETYPE == 2">
				select
				d.COURSEWARENAME COURSEWARENAME,
				IFNULL(COUNT(DISTINCT g.COURSESTUDYRECORD_ID),0) STUDYCOUNT,
				IFNULL(COUNT(DISTINCT h.COURSEEXAMRECORD_ID),0) COURSEEXAMRECORDCOUNT,
				IFNULL(COUNT(DISTINCT i.STAGEEXAMRECORD_ID),0) STAGEEXAMRECORDCOUNT,
				IFNULL(COUNT(DISTINCT j.CURRICULUMID),0) COURSEEXAMCOUNT,
				IFNULL(COUNT(DISTINCT k.STAGEEXAMPAPER_ID),0) STAGEEXAMCOUNT,
				<include refid="Field"></include>
				from
				<include refid="tableName"></include> f
				left join BUS_DATACOURSEWARE d on f.COURSEWAREID = d.DATACOURSEWARE_ID and d.ISDELETE = '0'
				LEFT JOIN BUS_COURSESTUDYRECORD g on g.QUESTION_ID = f.QUESTION_ID AND g.ISDELETE = '0'
				LEFT JOIN BUS_COURSEEXAMRECORD h on h.QUESTION_ID = f.QUESTION_ID AND h.ISDELETE = '0'
				LEFT JOIN BUS_STAGEEXAMRECORD i on i.QUESTION_ID = f.QUESTION_ID AND i.ISDELETE = '0'
				LEFT JOIN BUS_COURSEMIDDLE j on j.QUESTIONID = f.QUESTION_ID AND j.ISDELETE = '0'
				LEFT JOIN BUS_STAGEEXAMPAPERQUESTION k on k.QUESTION_ID = f.QUESTION_ID AND k.ISDELETE = '0'
				where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
				and f.COURSEWARETYPE = #{pd.COURSEWARETYPE} and f.COURSEWAREID = #{pd.COURSEWAREID}
			</when>
			<otherwise>
				SELECT f.*,
				IFNULL(COUNT(DISTINCT g.COURSESTUDYRECORD_ID),0) STUDYCOUNT,
				IFNULL(COUNT(DISTINCT h.COURSEEXAMRECORD_ID),0) COURSEEXAMRECORDCOUNT,
				IFNULL(COUNT(DISTINCT i.STAGEEXAMRECORD_ID),0) STAGEEXAMRECORDCOUNT,
				IFNULL(COUNT(DISTINCT j.CURRICULUMID),0) COURSEEXAMCOUNT,
				IFNULL(COUNT(DISTINCT k.STAGEEXAMPAPER_ID),0) STAGEEXAMCOUNT FROM (
				select
				v.COURSEWARENAME COURSEWARENAME,
				<include refid="Field"></include>
				from
				<include refid="tableName"></include> f
				left join BUS_VIDEOCOURSEWARE v on f.COURSEWAREID = v.VIDEOCOURSEWARE_ID and v.ISDELETE = '0'
				where f.ISDELETE = '0' and f.COURSEWARETYPE='1' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
				UNION ALL
				select
				d.COURSEWARENAME COURSEWARENAME,
				<include refid="Field"></include>
				from
				<include refid="tableName"></include> f
				left join BUS_DATACOURSEWARE d on f.COURSEWAREID = d.DATACOURSEWARE_ID and d.ISDELETE = '0'
				where f.ISDELETE = '0' and f.COURSEWARETYPE='2' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
				) f
				LEFT JOIN BUS_COURSESTUDYRECORD g on g.QUESTION_ID = f.QUESTION_ID AND g.ISDELETE = '0'
				LEFT JOIN BUS_COURSEEXAMRECORD h on h.QUESTION_ID = f.QUESTION_ID AND h.ISDELETE = '0'
				LEFT JOIN BUS_STAGEEXAMRECORD i on i.QUESTION_ID = f.QUESTION_ID AND i.ISDELETE = '0'
				LEFT JOIN BUS_COURSEMIDDLE j on j.QUESTIONID = f.QUESTION_ID AND j.ISDELETE = '0'
				LEFT JOIN BUS_STAGEEXAMPAPERQUESTION k on k.QUESTION_ID = f.QUESTION_ID AND k.ISDELETE = '0'
				WHERE 1=1
			</otherwise>
		</choose>
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and f.QUESTIONDRY LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			<!--	根据需求自己加检索条件
			(
                f.QUESTIONDRY LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
                 or
                字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
            -->
		</if>
		GROUP BY f.QUESTION_ID ORDER BY f.QUESTIONNUMBER+0 ASC
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		SELECT * FROM (
		select
		v.COURSEWARENAME COURSEWARENAME,
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		left join BUS_VIDEOCOURSEWARE v on f.COURSEWAREID = v.VIDEOCOURSEWARE_ID and v.ISDELETE = '0'
		where f.ISDELETE = '0' and f.COURSEWARETYPE='1' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		UNION ALL
		select
		d.COURSEWARENAME COURSEWARENAME,
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		left join BUS_DATACOURSEWARE d on f.COURSEWAREID = d.DATACOURSEWARE_ID and d.ISDELETE = '0'
		where f.ISDELETE = '0' and f.COURSEWARETYPE='2' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		) f
		ORDER BY f.OPERATTIME DESC
	</select>

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

	<!-- 批量删除(根据所选课件ids删除习题) -->
	<delete id="deleteByCoursewareIds" parameterType="pd">
		update
		<include refid="tableName"></include> f
		set
		f.ISDELETE = '1',
		f.OPERATOR = #{OPERATOR},
		f.OPERATTIME = #{OPERATTIME}
		where f.ISDELETE = '0'
		 and f.COURSEWARETYPE = #{COURSEWARETYPE}
		 and f.COURSEWAREID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
			#{item}
		</foreach>
	</delete>

	<!-- 列表(根据课程所选课件获取习题数据) -->
	<select id="listByCourseware" parameterType="pd" resultType="pd">
		select
			f.QUESTION_ID,
			f.COURSEWAREID,
			f.COURSEWARETYPE
		from
			<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		and CONCAT_WS('_', f.COURSEWAREID, f.COURSEWARETYPE) in (
			SELECT CONCAT_WS('_', m.COURSEWAREID,m.COURSEWARETYPE)
			 FROM BUS_KCMIDDLEKJ m WHERE m.ISDELETE = '0' and m.CURRICULUMID = #{CURRICULUMID})
	</select>

	<!-- 列表(根据阶段考试所选课件获取习题数据) -->
	<select id="findForStageByQuestionNum" parameterType="pd" resultType="pd">
		select
		f.QUESTION_ID,
		f.COURSEWAREID,
		f.COURSEWARETYPE
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		and CONCAT_WS('_', f.COURSEWAREID, f.COURSEWARETYPE) in
		<foreach item="item" index="index" collection="COURSEWARE_IDS" open="(" separator="," close=")">
			#{item,jdbcType=VARCHAR}
		</foreach>
	</select>

	<!-- 列表(根据课程所选课件获取习题数据) -->
	<select id="countByCourseware" parameterType="pd" resultType="integer">
		select COUNT(f.QUESTION_ID)
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		and CONCAT_WS('_', f.COURSEWAREID, f.COURSEWARETYPE) in (
		SELECT CONCAT_WS('_', m.COURSEWAREID,m.COURSEWARETYPE)
		FROM BUS_KCMIDDLEKJ m WHERE m.ISDELETE = '0' and m.CURRICULUMID = #{CURRICULUMID})
	</select>

	<!-- 列表(根据课程所选课件获取习题数据) -->
	<select id="countByStageExamCourseware" parameterType="pd" resultType="integer">
		select COUNT(f.QUESTION_ID)
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		and CONCAT_WS('_', f.COURSEWAREID, f.COURSEWARETYPE) in
		<foreach item="item" index="index" collection="COURSEWARE_IDS" open="(" separator="," close=")">
			#{item,jdbcType=VARCHAR}
		</foreach>
	</select>

	<!-- 获取 类型 对应的习题 -->
	<select id="listAllByType" parameterType="pd" resultType="pd">
		SELECT
			v.TRAINTYPE,
			v.POSTTYPE,
			d.TRAINTYPE,
			d.POSTTYPE,
			f.COURSEWAREID,
			f.COURSEWARETYPE,
			f.QUESTIONTYPE,
			f.QUESTION_ID
		FROM
			BUS_QUESTION f
		LEFT JOIN BUS_VIDEOCOURSEWARE v ON v.VIDEOCOURSEWARE_ID = f.COURSEWAREID
		LEFT JOIN bus_datacourseware d ON d.DATACOURSEWARE_ID = f.COURSEWAREID
		WHERE
			1 = 1
		and f.ISDELETE = '0'
		and f.QUESTIONTYPE  =#{QUESTIONTYPE}
		and f.CORPINFO_ID  =#{CORPINFO_ID}
		and ((v.TRAINTYPE =#{TRAINTYPE} and v.POSTTYPE =#{POSTTYPE}) or  (d.TRAINTYPE =#{TRAINTYPE} and d.POSTTYPE =#{POSTTYPE}))
	</select>

	<select id="hasQuestionNumber" parameterType="pd" resultType="integer" >
		SELECT
			COUNT( 1 )
		FROM
			<include refid="tableName"></include> f
		WHERE
			COURSEWAREID = #{COURSEWAREID}
			and QUESTIONNUMBER = #{QUESTIONNUMBER}
	</select>
<!--	* 获取最大题号，导入时候使用-->
	<select id="maxQuestionNumber" parameterType="pd" resultType="integer" >
		SELECT
		COALESCE(max(QUESTIONNUMBER),0) as position
		FROM
		<include refid="tableName"></include> f
		WHERE
		COURSEWAREID = #{COURSEWAREID}
	</select>

	<select id="getCountByQuestionType" parameterType="pd" resultType="pd">
		SELECT
			f.QUESTIONTYPE ,count(*) as count
		FROM
			BUS_QUESTION f
		LEFT JOIN BUS_VIDEOCOURSEWARE v ON v.VIDEOCOURSEWARE_ID = f.COURSEWAREID
		LEFT JOIN bus_datacourseware d ON d.DATACOURSEWARE_ID = f.COURSEWAREID
		WHERE
			1 = 1
		and f.ISDELETE = '0'
		and f.CORPINFO_ID  =#{CORPINFO_ID}
		and ((v.TRAINTYPE =#{TRAINTYPE} and v.POSTTYPE =#{POSTTYPE}) or  (d.TRAINTYPE =#{TRAINTYPE} and d.POSTTYPE =#{POSTTYPE}))
		GROUP BY QUESTIONTYPE
	</select>

	<select id="getQuestionList" resultType="com.zcloud.entity.PageData">
		SELECT f.*
		FROM BUS_QUESTION f
				 LEFT JOIN BUS_VIDEOCOURSEWARE v ON v.VIDEOCOURSEWARE_ID = f.COURSEWAREID
				 LEFT JOIN bus_datacourseware d ON d.DATACOURSEWARE_ID = f.COURSEWAREID
		WHERE f.ISDELETE = '0'
		  and f.CORPINFO_ID = #{CORPINFO_ID}
		  and ((v.TRAINTYPE = #{TRAINTYPE} and v.POSTTYPE = #{POSTTYPE}) or
			   (d.TRAINTYPE = #{TRAINTYPE} and d.POSTTYPE = #{POSTTYPE}))
		  and f.QUESTIONTYPE = #{QUESTIONTYPE}
	</select>

	<select id="getStage" resultType="com.zcloud.entity.PageData">
		SELECT
			v.TRAINTYPE,
			v.POSTTYPE,
			d.TRAINTYPE,
			d.POSTTYPE,
			f.COURSEWAREID,
			f.COURSEWARETYPE,
			f.QUESTIONTYPE,
			f.QUESTION_ID
		FROM BUS_QUESTION f
				 LEFT JOIN BUS_VIDEOCOURSEWARE v ON v.VIDEOCOURSEWARE_ID = f.COURSEWAREID
				 LEFT JOIN bus_datacourseware d ON d.DATACOURSEWARE_ID = f.COURSEWAREID
		WHERE f.ISDELETE = '0' and f.QUESTION_ID = #{QUESTION_ID}
	</select>
</mapper>
