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

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.ANSWER_ID,
		f.SURVEY_ID,
		f.CORPINFO_ID,
		f.QUESTION_ID,
		f.QUESTIONTYPE,
		f.ISDELETE,
		f.ANSWER,
		f.OPERATTIME,
		f.CREATE_TIME
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		ANSWER_ID,
		SURVEY_ID,
		CORPINFO_ID,
		QUESTION_ID,
		QUESTIONTYPE,
		ISDELETE,
		ANSWER,
		OPERATTIME,
		CREATE_TIME
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{ANSWER_ID},
		#{SURVEY_ID},
		#{CORPINFO_ID},
		#{QUESTION_ID},
		#{QUESTIONTYPE},
		#{ISDELETE},
		#{ANSWER},
		#{OPERATTIME},
		#{CREATE_TIME}
	</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 ANSWER_ID = #{ANSWER_ID}
	</insert>

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

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		ANSWER_ID = #{ANSWER_ID},
		SURVEY_ID = #{SURVEY_ID},
		CORPINFO_ID = #{CORPINFO_ID},
		QUESTION_ID = #{QUESTION_ID},
		QUESTIONTYPE = #{QUESTIONTYPE},
		ISDELETE = #{ISDELETE},
		ANSWER = #{ANSWER},
		OPERATTIME = #{OPERATTIME},
		CREATE_TIME = #{CREATE_TIME}
		where
		ANSWER_ID = #{ANSWER_ID}
	</update>


	<!-- 通过ID获取数据 -->
	<select id="getAnswers" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
		f.SURVEY_ID = #{SURVEY_ID} and f.QUESTION_ID = #{QUESTION_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
		FROM
		<include refid="tableName"></include> f
		left join bus_survey d on f.SURVEY_ID = d.SURVEY_ID and d.ISDELETE = '0'
		LEFT JOIN bus_survey_question_item g on g.QUESTION_ID = f.QUESTION_ID AND g.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.QUESTION_ID = f.QUESTION_ID AND i.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''">&lt;!&ndash; 关键词检索 &ndash;&gt;
			and f.QUESTIONDRY LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			&lt;!&ndash;	根据需求自己加检索条件
			(
                f.QUESTIONDRY LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
                 or
                字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
            &ndash;&gt;
		</if>
		&lt;!&ndash;GROUP BY f.QUESTION_ID ORDER BY f.QUESTIONNUMBER+0 ASC&ndash;&gt;
	</select>
-->

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

		SELECT
		Distinct f.ANSWER_ID,
		f.SURVEY_ID,
		f.CORPINFO_ID,
		f.QUESTION_ID,
		f.QUESTIONTYPE,
		f.ISDELETE,
		f.ANSWER,
		f.OPERATTIME,
		f.CREATE_TIME
		 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_question_item h on h.QUESTION_ID = f.QUESTION_ID AND h.ISDELETE = '0'
		LEFT JOIN bus_survey 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>

	</select>

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


	<!-- 列表(全部) -->
	<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'
		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'
		) f
		ORDER BY f.OPERATTIME DESC
	</select>

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

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

	<!-- 列表(根据所选ID获取数据) -->
	<select id="findByIds" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
		ANSWER_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.ANSWER_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
			#{item}
		</foreach>
	</delete>

	<!-- 列表(根据课程所选课件获取习题数据) -->
	<select id="listByCourseware" parameterType="pd" resultType="pd">
		select
			f.SURVEY_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.SURVEY_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.SURVEY_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.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
		<include refid="tableName"></include>
		set
		IS_PUBLISH = #{IS_PUBLISH},
		OPERATTIME = #{OPERATTIME}
		where
		SURVEY_ID = #{SURVEY_ID}
	</update>
</mapper>
