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

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.ITEM_ID,
		f.QUESTION_ID,
		f.OPTIONDES,
		f.CREATE_TIME,
		f.OPERATTIME,
		f.ISDELETE,
		f.ORDERS
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		ITEM_ID,
		QUESTION_ID,
		OPTIONDES,
		CREATE_TIME,
		OPERATTIME,
		ISDELETE,
		ORDERS
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{ITEM_ID},
		#{QUESTION_ID},
		#{OPTIONDES},
		#{CREATE_TIME},
		#{OPERATTIME},
		#{ISDELETE},
		#{ORDERS}
	</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',
		OPERATTIME = #{OPERATTIME}
		where
		QUESTION_ID = #{QUESTION_ID}
	</delete>

	<!-- 删除-->
	<delete id="deleteitem" parameterType="pd">
		delete from
		<include refid="tableName"></include>
		where QUESTION_ID = #{QUESTION_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		ITEM_ID = #{ITEM_ID},
		QUESTION_ID = #{QUESTION_ID},
		OPTIONDES = #{OPTIONDES},
		CREATE_TIME = #{CREATE_TIME},
		OPERATTIME = #{OPERATTIME},
		ORDERS = #{ORDERS}
		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">

		SELECT
		distinct f.ITEM_ID,
		f.QUESTION_ID,
		f.OPTIONDES,
		f.CREATE_TIME,
		f.OPERATTIME,
		f.ISDELETE,
		f.ORDERS
		FROM
		<include refid="tableName"></include> f
		LEFT JOIN bus_survey_question g on g.QUESTION_ID = f.QUESTION_ID AND g.ISDELETE = '0'
		LEFT JOIN bus_survey_answer i on i.QUESTION_ID = f.QUESTION_ID AND i.ISDELETE = '0'
		where f.ISDELETE = '0' and f.ITEM_ID = #{pd.ITEM_ID}
		<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.ITEM_ID ORDER BY f.OPTIONDES ASC
	</select>

	<!-- 获取item选项 -->
	<select id="findByQuestionId" parameterType="pd" resultType="pd">
		SELECT
			f.*
		FROM
			BUS_SURVEY_QUESTION_ITEM f
		WHERE
		f.QUESTION_ID = #{QUESTION_ID} and f.ISDELETE = '0'
		order by f.ORDERS
	</select>




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




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


</mapper>
