<?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.api.app.dao.butler.AppQuestionnaireDao">
    <select id="list" parameterType="java.lang.Integer" resultType="com.api.vo.app.AppQuestionnaireVo">
        select * from sys_questionnaire where is_delete = 1 and is_release = 1 and answer_type = 1 or answer_type = #{type}
    </select>

    <select id="findPersonnelByIds" parameterType="com.api.model.app.AppQuestionnairePersonnel" resultType="com.api.model.app.AppQuestionnairePersonnel">
        select * from sys_questionnaire_personnel where questionnaire_id = #{questionnaireId} and answer_id = #{answerId}
    </select>

    <select id="findAnswerIdById" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select answer_id from sys_questionnaire_personnel where questionnaire_id = #{questionnaireId}
    </select>

    <select id="findQuestionnaireById" parameterType="java.lang.Integer" resultType="com.api.vo.app.AppQuestionnaireDetailVo">
        select * from sys_questionnaire where id = #{id} and is_delete = 1 and is_release = 1
    </select>

    <select id="findTopicById" parameterType="java.lang.Integer" resultType="com.api.vo.app.AppQuestionnaireTopicVo">
        select * from sys_questionnaire_topic where questionnaire_id = #{questionnaireId}
    </select>

    <select id="findChoiceByTopicId" parameterType="java.lang.Integer" resultType="com.api.vo.app.AppQuestionnaireChoiceVo">
        select * from sys_questionnaire_multiple_choice where questionnaire_topic_id = #{topicId}
    </select>

    <update id="accumulationAnswerNum" parameterType="java.lang.Integer">
        update sys_questionnaire set answer_num = answer_num + 1 where id =#{id}
    </update>

    <select id="findTopicByTopicId" parameterType="java.lang.Integer" resultType="com.api.vo.app.AppQuestionnaireTopicVo">
        select * from sys_questionnaire_topic where id =#{topicId}
    </select>

    <insert id="insertChoiceAnswer" parameterType="com.api.model.app.AppQuestionnaireChoiceAnswer">
        insert into sys_questionnaire_single_multiple_choice_answer(
            questionnaire_id,
            questionnaire_topic_id,
            questionnaire_multiple_choice_id,
            create_id,
            create_date
        ) values (
            #{questionnaireId},
            #{questionnaireTopicId},
            #{questionnaireMultipleChoiceId},
            #{createId},
            #{createDate}
        )
    </insert>

    <insert id="insertJudgmentAnswer" parameterType="com.api.model.app.AppQuestionJudgmentAnswer">
        insert into sys_questionnaire_judgment_answer(
            questionnaire_id,
            questionnaire_topic_id,
            answer,
            create_id,
            create_date
        ) values (
            #{questionnaireId},
            #{questionnaireTopicId},
            #{answer},
            #{createId},
            #{createDate}
        )
    </insert>

    <insert id="insertShortAnswer" parameterType="com.api.model.app.AppQuestionnaireShortAnswer">
        insert into sys_questionnaire_short_answer(
            questionnaire_id,
            questionnaire_topic_id,
            answer,
            create_id,
            create_date
        ) values (
            #{questionnaireId},
            #{questionnaireTopicId},
            #{answer},
            #{createId},
            #{createDate}
        )
    </insert>

    <insert id="insertPersonnel" parameterType="com.api.model.app.AppQuestionnairePersonnel">
        insert into sys_questionnaire_personnel(
            questionnaire_id,
            answer_id,
            answer_date
        )values (
            #{questionnaireId},
            #{answerId},
            #{answerDate}
        )
    </insert>


</mapper>