<?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.questionnaire.SurveyMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.TITLE,
		f.BEGIN_TIME,
		f.END_TIME,
		f.CREATE_TIME,
		f.CREATOR,
		f.ISDELETE,
		f.OPERATTIME,
		f.SURVEY_ID,
		f.TYPE
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		TITLE,
		BEGIN_TIME,
		END_TIME,
		CREATE_TIME,
		CREATOR,
		ISDELETE,
		OPERATTIME,
		SURVEY_ID,
		TYPE
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{TITLE},
		#{BEGIN_TIME},
		#{END_TIME},
		#{CREATE_TIME},
		#{CREATOR},
		#{ISDELETE},
		#{OPERATTIME},
		#{SURVEY_ID},
		#{TYPE}
	</sql>

	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into
		<include refid="tableName"></include>
		(
		<include refid="Field2"></include>
		) values (
		<include refid="FieldValue"></include>
		)
		ON DUPLICATE KEY UPDATE SURVEY_ID = #{SURVEY_ID}
	</insert>

	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		ISDELETE = '1',
		OPERATTIME = #{OPERATTIME}
		where
		SURVEY_ID = #{SURVEY_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		SURVEY_ID = #{SURVEY_ID},
		TITLE = #{TITLE},
		BEGIN_TIME = #{BEGIN_TIME},
		END_TIME = #{END_TIME},
		CREATE_TIME = #{CREATE_TIME},
		CREATOR = #{CREATOR},
		OPERATTIME = #{OPERATTIME},
		SURVEY_ID = #{SURVEY_ID},
		TYPE = #{TYPE}
		where
		SURVEY_ID = #{SURVEY_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		(select count(DISTINCT sa.USER_ID) from bus_survey_answer sa where sa.isdelete= '0' and sa.SURVEY_ID =f.SURVEY_ID) PARTICIPANTCOUNT
		from
		<include refid="tableName"></include> f
		where
		f.SURVEY_ID = #{SURVEY_ID}
	</select>



	<!-- 列表
	 	STUDYCOUNT	联系次数
	 	COURSEEXAMRECORDCOUNT	课考使用次数
	 	STAGEEXAMRECORDCOUNT	阶考使用次数
	 	COURSEEXAMCOUNT	课考关联数
	 	STAGEEXAMCOUNT	阶考关联数	-->


	<select id="datalistPage" parameterType="page" resultType="pd">

		SELECT
		f.SURVEY_ID,
		 f.TITLE,
		f.BEGIN_TIME,
		f.END_TIME,
		f.CREATE_TIME,
		f.IS_PUBLISH,
		f.CREATOR,
		f.ISDELETE,
		d.QUESTION_ID,
		f.TYPE
		 FROM
		<include refid="tableName"></include> f
		left join bus_survey_question d on f.SURVEY_ID = d.SURVEY_ID and d.ISDELETE = '0'
		LEFT JOIN bus_survey_corpinfo h on h.SURVEY_ID = f.SURVEY_ID AND h.ISDELETE = '0'
		LEFT JOIN bus_survey_answer i on i.SURVEY_ID = f.SURVEY_ID AND i.ISDELETE = '0'
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and f.TITLE LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			<!--	根据需求自己加检索条件
			(
                f.TITLE LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
                 or
                字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
            -->
		</if>
		group by f.SURVEY_ID order by f.CREATE_TIME desc
	</select>

	<!-- 新增问卷和企业关系信息 -->
	<insert id="saveNotAndCorp" parameterType="pd">
		insert into  bus_survey_corpinfo
		( SURVEY_ID ,CORPINFO_ID,ISDELETE,SURVEY_CORP_ID,OPERATTIME,CORPINFO_NAME
		) values
		<foreach item="item" index="index"	 collection="list" separator=",">
			(  #{item.surveyId},    #{item.corpId}	,'0',#{item.SURVEY_CORP_ID},#{OPERATTIME},#{item.CORPINFO_NAME})
		</foreach>
	</insert>



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




	<!-- 列表(根据课程所选课件获取习题数据) -->
	<select id="countByStageExamCourseware" parameterType="pd" resultType="integer">
		select COUNT(f.SURVEY_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.SURVEY_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 ((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
		SURVEY_ID = #{SURVEY_ID}
			and QUESTION_ID = #{QUESTION_ID}
	</select>
<!--	* 获取最大题号，导入时候使用-->
	<select id="maxQuestionNumber" parameterType="pd" resultType="integer" >
		SELECT
		COALESCE(max(QUESTION_ID),0) as position
		FROM
		<include refid="tableName"></include> f
		WHERE
		SURVEY_ID = #{SURVEY_ID}
	</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 ((v.TRAINTYPE =#{TRAINTYPE} and v.POSTTYPE =#{POSTTYPE}) or  (d.TRAINTYPE =#{TRAINTYPE} and d.POSTTYPE =#{POSTTYPE}))
		GROUP BY QUESTIONTYPE
	</select>
	<!-- 修改 -->
	<update id="editByState" parameterType="pd">
		update
		bus_survey
		set
		IS_PUBLISH = #{IS_PUBLISH},
		OPERATTIME = #{OPERATTIME}
		where
		SURVEY_ID = #{SURVEY_ID}
	</update>
</mapper>
