<?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.ym.mec.biz.dal.dao.QuestionnaireUserResultDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.QuestionnaireUserResult" id="QuestionnaireUserResult">
		<result column="id_" property="id" />
		<result column="user_id_" property="userId" />
		<result column="username_" property="username" />
		<result column="phone_" property="phone" />
		<result column="active_type_" property="activeType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="active_id_" property="activeId"/>
		<result column="questionnaire_topic_id_" property="questionnaireTopicId" />
		<result column="questionnaire_question_id_" property="questionnaireQuestionId" />
		<result column="questionnaire_question_item_id_list_" property="questionnaireQuestionItemIdList" />
		<result column="additional_value_" property="additionalValue" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
        <result column="tenant_id_" property="tenantId"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="QuestionnaireUserResult" >
		SELECT * FROM questionnaire_user_result WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="QuestionnaireUserResult">
		SELECT qur.* FROM questionnaire_user_result qur
		LEFT JOIN sys_user su ON su.id_ = qur.user_id_
		<where>
			<if test="topicId != null">
				AND qur.questionnaire_topic_id_ = #{topicId}
			</if>
			<if test="userId != null">
				AND qur.user_id_ = #{userId}
			</if>
			<if test="activeId != null">
				AND qur.active_id_ = #{activeId}
			</if>
			<if test="activeType != null">
				AND qur.active_type_ = #{activeType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
		</where>
		ORDER BY su.organ_id_,qur.create_time_ DESC,qur.user_id_,qur.id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.QuestionnaireUserResult" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO questionnaire_user_result (user_id_,questionnaire_topic_id_,questionnaire_question_id_,
		                                       questionnaire_question_item_id_list_,additional_value_,active_type_,active_id_,create_time_,update_time_,tenant_id_)
		VALUES(#{userId},#{questionnaireTopicId},#{questionnaireQuestionId},#{questionnaireQuestionItemIdList},#{additionalValue}
			  ,#{activeType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{activeId},NOW(),NOW(),#{tenantId})
	</insert>

	<insert id="batchInsert1">
		INSERT INTO questionnaire_user_result (user_id_,questionnaire_topic_id_,questionnaire_question_id_,
		questionnaire_question_item_id_list_,additional_value_,active_type_,active_id_,create_time_,update_time_,tenant_id_)
		VALUES
		<foreach collection="questionnaireUserResultList" item="item" separator=",">
			(#{item.userId},#{item.questionnaireTopicId},#{item.questionnaireQuestionId},#{item.questionnaireQuestionItemIdList},#{item.additionalValue}
			,#{item.activeType},#{item.activeId},NOW(),NOW(),#{item.tenantId})
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.QuestionnaireUserResult">
		UPDATE questionnaire_user_result <set>
		<if test="userId != null">
			user_id_ = #{userId},
		</if>
		<if test="activeId != null">
			active_id_ = #{activeId},
		</if>
		<if test="activeType != null">
			active_type_ = #{activeType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		</if>
		<if test="questionnaireQuestionId != null">
			questionnaire_question_id_ = #{questionnaireQuestionId},
		</if>
		<if test="questionnaireTopicId != null">
			questionnaire_topic_id_ = #{questionnaireTopicId},
		</if>
		<if test="questionnaireQuestionItemIdList != null">
			questionnaire_question_item_id_list_ = #{questionnaireQuestionItemIdList},
		</if>
		<if test="additionalValue != null">
			additional_value_ = #{additionalValue},
		</if>
			update_time_ = NOW()
	</set> WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM questionnaire_user_result WHERE id_ = #{id} 
	</delete>
    <delete id="delByActiveIdAndUserId">
		DELETE FROM questionnaire_user_result
		<where>
			<if test="activeType != null">
				AND active_type_ = #{userId,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
			<if test="userId != null">
				AND user_id_ = #{userId}
			</if>
			<if test="activityId != null">
				AND active_id_ = #{activityId}
			</if>
			<if test="topicId != null">
				AND questionnaire_topic_id_ = #{topicId}
			</if>
		</where>
	</delete>

    <!-- 分页查询 -->
	<select id="findResultCount" resultMap="QuestionnaireUserResultDto" parameterType="map">
		SELECT qu1.*,qq.content_,GROUP_CONCAT(qqi.answer_value_ ORDER BY qqi.sort_) answer_value_,qq.type_,qur.username_,qur.phone_
		FROM (SELECT qu.user_id_,su.username_,su.phone_ FROM questionnaire_user_result qu
		LEFT JOIN sys_user su ON su.id_ = qu.user_id_
		WHERE qu.questionnaire_topic_id_ = #{topicId}
		<if test="search != null and search != ''">
			AND (qu.user_id_ = #{search} OR qu.additional_value_ LIKE CONCAT('%',#{search},'%'))
		</if>
		GROUP BY qu.user_id_ <include refid="global.limit"/>) qur
		LEFT JOIN questionnaire_user_result qu1 ON qur.user_id_ = qu1.user_id_
		LEFT JOIN questionnaire_question qq ON qq.id_ = qu1.questionnaire_question_id_
		LEFT JOIN questionnaire_question_item qqi ON FIND_IN_SET(qqi.id_,qu1.questionnaire_question_item_id_list_)
		WHERE qu1.questionnaire_topic_id_ = #{topicId}
		GROUP BY qu1.user_id_,qq.id_
		ORDER BY qu1.user_id_,qq.sort_
	</select>
	<!-- 查询当前表的总记录数 -->
	<select id="countResultCount" resultType="int">
		SELECT COUNT(DISTINCT qu.user_id_) FROM questionnaire_user_result qu WHERE qu.questionnaire_topic_id_ = #{topicId}
	</select>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="QuestionnaireUserResult" parameterType="map">
		SELECT * FROM questionnaire_user_result where tenant_id_ = #{tenantId} ORDER BY id_ <include refid="global.limit"/>
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM questionnaire_user_result where tenant_id_ = #{tenantId}
	</select>

    <select id="countByTopicId" resultType="java.lang.Integer">
		SELECT COUNT(id_) FROM questionnaire_user_result WHERE questionnaire_topic_id_ = #{topicId}
	</select>
	<select id="findDetailCount" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT ria.user_id_) FROM questionnaire_user_result qu
		LEFT JOIN replacement_instrument_activity ria ON ria.replacement_instrument_cooperation_id_ = qu.active_id_ AND ria.user_id_ = qu.user_id_
		AND ria.user_id_ = qu.user_id_
		<include refid="queryDetailPageSql"/>
	</select>
	<resultMap type="com.ym.mec.biz.dal.dto.QuestionnaireUserResultDto" id="QuestionnaireUserResultDto">
		<result column="user_id_" property="userId" />
		<result column="username_" property="username" />
		<result column="phone_" property="phone" />
		<result column="active_type_" property="activeType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="active_id_" property="activeId"/>
		<result column="questionnaire_topic_id_" property="questionnaireTopicId" />
		<collection property="questionnaireResultDtoList" ofType="com.ym.mec.biz.dal.dto.QuestionnaireResultDto">
			<result column="id_" property="userResultId" />
			<result property="content" column="content_"/>
			<result property="answerValue" column="answer_value_"/>
			<result column="questionnaire_question_id_" property="questionnaireQuestionId" />
			<result column="questionnaire_question_item_id_list_" property="questionnaireQuestionItemIdList" />
			<result column="additional_value_" property="additionalValue" />
			<result column="type_" property="type" />
		</collection>
	</resultMap>
	<sql id="queryDetailPageSql">
		<where>
			 qu.tenant_id_ = #{tenantId}
			<if test="search != null and search != ''">
				AND (qu.user_id_ = #{search} OR ria.user_name_ LIKE CONCAT('%',#{search},'%') OR ria.mobile_no_ LIKE CONCAT('%',#{search},'%'))
			</if>
			<if test="activeId != null">
				AND qu.active_id_ = #{activeId}
			</if>
			<if test="activeType != null and activeType != ''">
				AND qu.active_type_ = #{activeType}
			</if>
			<if test="topicId != null">
				AND qu.questionnaire_topic_id_ = #{topicId}
			</if>
		</where>
	</sql>

	<sql id="queryDetailPageSql1">
		<where>
			 qu1.tenant_id_ = #{tenantId}
			<if test="activeId != null">
				AND qu1.active_id_ = #{activeId}
			</if>
			<if test="activeType != null and activeType != ''">
				AND qu1.active_type_ = #{activeType}
			</if>
			<if test="topicId != null">
				AND qu1.questionnaire_topic_id_ = #{topicId}
			</if>
		</where>
	</sql>
	<select id="queryDetailPage" resultMap="QuestionnaireUserResultDto">
		SELECT qu1.*,qur.user_name_ username_,qur.phone_,qq.content_,GROUP_CONCAT(qqi.answer_value_ ORDER BY qqi.sort_) answer_value_
		FROM (SELECT ria.user_id_ id_,ria.user_name_,ria.mobile_no_ phone_ FROM questionnaire_user_result qu
		LEFT JOIN replacement_instrument_activity ria ON ria.replacement_instrument_cooperation_id_ = qu.active_id_ AND ria.user_id_ = qu.user_id_
		AND ria.user_id_ = qu.user_id_
		<include refid="queryDetailPageSql"/>
		GROUP BY ria.user_id_
		<include refid="global.limit"/>) qur
		LEFT JOIN questionnaire_user_result qu1 ON qur.id_ = qu1.user_id_
		LEFT JOIN questionnaire_question qq ON qq.id_ = qu1.questionnaire_question_id_
		LEFT JOIN questionnaire_question_item qqi ON FIND_IN_SET(qqi.id_,qu1.questionnaire_question_item_id_list_)
		<include refid="queryDetailPageSql1"/>
		GROUP BY qu1.user_id_,qq.id_
		ORDER BY qu1.user_id_,qq.sort_
	</select>

	<select id="queryUserResult" resultMap="QuestionnaireUserResultDto">
		SELECT qu1.*, qur.username_, qur.phone_, qq.content_,GROUP_CONCAT( qqi.answer_value_ ORDER BY qqi.sort_ ) answer_value_,qq.type_
		FROM (SELECT qu.user_id_,su.username_,su.phone_
		FROM questionnaire_user_result qu
		LEFT JOIN sys_user su ON su.id_ = qu.user_id_
		WHERE qu.questionnaire_topic_id_ = #{topicId} AND qu.user_id_ = #{userId}
		GROUP BY qu.user_id_ ) qur
		LEFT JOIN questionnaire_user_result qu1 ON qur.user_id_ = qu1.user_id_
		LEFT JOIN questionnaire_question qq ON qq.id_ = qu1.questionnaire_question_id_
		LEFT JOIN questionnaire_question_item qqi ON FIND_IN_SET( qqi.id_, qu1.questionnaire_question_item_id_list_ )
		WHERE qu1.questionnaire_topic_id_ = #{topicId}
		GROUP BY qu1.user_id_,qq.id_
		ORDER BY qu1.user_id_,qq.sort_
	</select>

	<resultMap id="QuestionnaireResultDto" type="com.ym.mec.biz.dal.dto.QuestionnaireResultDto">
		<result column="id_" property="userResultId" />
		<result property="content" column="content_"/>
		<result property="answerValue" column="answer_value_"/>
		<result column="questionnaire_question_id_" property="questionnaireQuestionId" />
		<result column="questionnaire_question_item_id_list_" property="questionnaireQuestionItemIdList" />
		<result column="additional_value_" property="additionalValue" />
	</resultMap>
	<select id="getQuestionResult" resultMap="QuestionnaireResultDto">
		SELECT qur.*,qq.content_,GROUP_CONCAT(qqi.answer_value_ ORDER BY qqi.sort_) answer_value_ FROM questionnaire_user_result qur
		LEFT JOIN questionnaire_question qq ON qq.id_ = qur.questionnaire_question_id_
		LEFT JOIN questionnaire_question_item qqi ON FIND_IN_SET(qqi.id_,qur.questionnaire_question_item_id_list_)
		WHERE qur.active_id_ = #{activeId} AND qur.active_type_ = #{activeType}
		GROUP BY qq.id_
		ORDER BY qq.sort_
	</select>
	<select id="queryUserName" resultType="java.util.Map">
		SELECT qur.user_id_ `key`,su.username_ `value` FROM questionnaire_user_result qur
		LEFT JOIN sys_user su ON su.id_ = qur.user_id_
		WHERE qur.user_id_ IS NOT NULL AND qur.questionnaire_topic_id_ = #{topicId} AND su.username_ IS NOT NULL
		GROUP BY qur.user_id_
	</select>
</mapper>
