<?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="cn.iocoder.byzs.module.bjdx.dal.mysql.questionnaire.QuestionnaireMapper">

    <select id="selectConfigQuestionList"
            parameterType="cn.iocoder.byzs.module.bjdx.controller.admin.questionnaire.vo.ConfigQuestionRespVO"
            resultType="cn.iocoder.byzs.module.bjdx.controller.admin.questionnaire.vo.ConfigQuestionRespVO">
        SELECT
            cq.id,qc.tenant_id as tenantId,
            q.id as questionnaireId,
            cq.cq_quest_type as cqQuestType,
            cq.cq_question as cqQuestion,
            cq.cq_quest_analysis as cqQuestAnalysis,
            cq.cq_quest_answer_id as cqQuestAnswerId,
            qc.bq_order as bqOrder,
            cq.cq_quest_score as cqQuestScore
        FROM bjdx_questionnaire_config qc
        LEFT JOIN bjdx_questionnaire q ON q.id = qc.bqc_questionnaire_id
        LEFT JOIN bjdx_course_question cq ON qc.bqc_question_id = cq.id
        WHERE
            qc.deleted = 0 AND q.deleted = 0 AND cq.deleted = 0
            <if test="questionnaireId != null">
                q.id = #{questionnaireId}
            </if>
            <if test="cqQuestType != null">
                AND cq.cq_quest_type = #{cqQuestType}
            </if>
            <if test="cqQuestion != null">
                AND cq.cq_question LIKE CONCAT('%', #{cqQuestion}, '%')
            </if>
        order by qc.bq_order
        <if test="pageSize != null and pageSize != -1">
            limit #{pageNo}, #{pageSize}
        </if>
    </select>

    <select id="selectConfigQuestionCount" resultType="java.lang.Integer">
        SELECT count(*)
        FROM bjdx_questionnaire_config qc
        LEFT JOIN bjdx_questionnaire q ON q.id = qc.bqc_questionnaire_id
        LEFT JOIN bjdx_course_question cq ON qc.bqc_question_id = cq.id
        WHERE
        <if test="questionnaireId != null">
            q.id = #{questionnaireId}
        </if>
        <if test="cqQuestType != null">
            AND cq.cq_quest_type = #{cqQuestType}
        </if>
        <if test="cqQuestion != null">
            AND cq.cq_question LIKE CONCAT('%', #{cqQuestion}, '%')
        </if>
        order by qc.bq_order
    </select>

    <select id="selectConfigQuestionNoList"
            parameterType="cn.iocoder.byzs.module.bjdx.controller.admin.questionnaire.vo.ConfigQuestionRespVO"
            resultType="cn.iocoder.byzs.module.bjdx.controller.admin.questionnaire.vo.ConfigQuestionRespVO">
        SELECT
        cq.id,
        cq.cq_quest_type as cqQuestType,
        cq.cq_question as cqQuestion,
        cq.cq_quest_analysis as cqQuestAnalysis,
        cq.cq_quest_answer_id as cqQuestAnswerId
        FROM bjdx_course_question cq
        <!-- 左连接子查询结果，用于判断是否存在关联 -->
        LEFT JOIN (
            SELECT qc.bqc_question_id id
            FROM bjdx_questionnaire q
            LEFT JOIN bjdx_questionnaire_config qc ON qc.bqc_questionnaire_id = q.id
            WHERE
                <if test="questionnaireId != null">
                    q.id = #{questionnaireId}
                </if>
        ) t ON cq.id = t.id
        WHERE
            <!-- 当子查询无匹配时，t.id为NULL -->
            t.id IS NULL
            <if test="cqQuestType != null">
                AND cq.cq_quest_type = #{cqQuestType}
            </if>
            <if test="cqQuestion != null">
                AND cq.cq_question LIKE CONCAT('%', #{cqQuestion}, '%')
            </if>
    </select>

    <insert id="addConfigQuest" parameterType="cn.iocoder.byzs.module.bjdx.controller.admin.questionnaire.vo.ConfigQuestionRespVO">
        INSERT INTO bjdx_questionnaire_config (bqc_questionnaire_id, bqc_question_id, bq_order, tenant_id)
        SELECT
            #{questionnaireId},
            q.id,
            @max_order := @max_order + 1,
            #{tenantId}
        FROM (
            <!-- 使用foreach直接生成所有ID行，无需单独处理第一个元素 -->
            <foreach collection="questionIds" item="id" index="index" open="" separator="UNION ALL " close="">
                SELECT #{questionnaireId} AS qn_id, #{id} AS id
            </foreach>
        ) AS q
        CROSS JOIN (SELECT @max_order := COALESCE((
            SELECT MAX(bq_order)
            FROM bjdx_questionnaire_config
            WHERE bqc_questionnaire_id = #{questionnaireId}
        ), 0)) AS init;
    </insert>

    <delete id="delConfigQuest" parameterType="cn.iocoder.byzs.module.bjdx.controller.admin.questionnaire.vo.ConfigQuestionRespVO">
        DELETE FROM bjdx_questionnaire_config WHERE bqc_questionnaire_id = #{questionnaireId} AND bqc_question_id IN
        <foreach collection="questionIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>