<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "mybatis-3-mapper.dtd">
<mapper namespace="com.mortals.xhx.module.feedback.dao.ibatis.FeedbackDaoImpl">

    <!-- 问卷反馈人员列表 -->
    <select id="getFeedbackStaffList" parameterType="com.mortals.xhx.module.feedback.model.FeedbackEntity" resultType="com.mortals.xhx.module.feedback.model.vo.FeedbackStaffInfoVo">
        SELECT * FROM (
            SELECT
                s.feedbackId,
                s.staffId,
                s1.`name`,
                s1.deptId,
                s1.deptName,
                s1.positionName,
                CASE IFNULL(a.id, 0) WHEN 0 THEN 0 ELSE 1 END AS backStatus,
                MAX(a.createTime) AS backTime
            FROM
                mortals_xhx_feedback_staff s
                LEFT JOIN mortals_xhx_feedback_answer a ON s.staffId = a.staffId AND s.feedbackId = a.feedbackId
                LEFT JOIN mortals_xhx_staff s1 ON s.staffId = s1.id
            WHERE
                s.feedbackId = #{feedbackId}
            GROUP BY
                s.staffId
        ) AS t WHERE 1=1
        <if test="backStatus != null">
            AND t.backStatus = #{backStatus}
        </if>
    </select>

    <select id="getFeedbackStaffListCount" parameterType="com.mortals.xhx.module.feedback.model.FeedbackEntity" resultType="int">
        select count(1) from (
            SELECT * FROM (
                SELECT
                    s.feedbackId,
                    s.staffId,
                    s1.`name`,
                    s1.deptId,
                    s1.deptName,
                    s1.positionName,
                    CASE IFNULL(a.id, 0) WHEN 0 THEN 0 ELSE 1 END AS backStatus,
                    MAX(a.createTime) AS backTime
                FROM
                    mortals_xhx_feedback_staff s
                    LEFT JOIN mortals_xhx_feedback_answer a ON s.staffId = a.staffId AND s.feedbackId = a.feedbackId
                    LEFT JOIN mortals_xhx_staff s1 ON s.staffId = s1.id
                WHERE
                    s.feedbackId = #{feedbackId}
                GROUP BY
                    s.staffId
            ) AS t WHERE 1=1
            <if test="backStatus != null">
                AND t.backStatus = #{backStatus}
            </if>
        ) as tt
    </select>

    <!-- 问卷单选多选反馈统计 -->
    <select id="getOptionSummaryList" parameterType="long" resultType="com.mortals.xhx.module.feedback.model.vo.OptionSummaryVo">
        SELECT
            q.id AS questionId,
            q.feedbackId,
            q.content AS question,
            q.questionType,
            o.optionId,
            o.optionContent,
            count(o.id) optionCount
        FROM
            mortals_xhx_feedback_question q,
            mortals_xhx_feedback_answer a,
            mortals_xhx_feedback_answer_option o
        WHERE
            q.id = a.questionId
            AND q.feedbackId = a.feedbackId
            AND a.id = o.answerId
            AND (q.questionType = 1 OR q.questionType = 2)
            AND q.feedbackId = #{feedbackId}
        GROUP BY
            q.id,
            o.optionId;
    </select>

    <!-- 问卷问题反馈详情 -->
    <select id="getQuestionAnswerList" parameterType="long" resultType="com.mortals.xhx.module.feedback.model.vo.QuestionAnswerVo">
        SELECT
            q.id AS questionId,
            q.feedbackId,
            q.content AS question,
            q.questionType,
            a.staffId,
            s.`name`,
            a.content AS answer,
            a.createTime AS backTime
        FROM
            mortals_xhx_feedback_question q,
            mortals_xhx_feedback_answer a,
            mortals_xhx_staff s
        WHERE
            q.id = a.questionId
            AND q.feedbackId = a.feedbackId
            AND a.staffId = s.id
            AND q.questionType != 1
            AND q.questionType != 2
            AND q.id = #{questionId}
    </select>

    <!-- 获取 -->
    <select id="getQuestionAnswerListCount" parameterType="long" resultType="int">
        select count(1)
        from
        (
            SELECT
                q.id AS questionId,
                q.feedbackId,
                q.content AS question,
                q.questionType,
                a.staffId,
                s.`name`,
                a.content AS answer,
                a.createTime AS backTime
            FROM
                mortals_xhx_feedback_question q,
                mortals_xhx_feedback_answer a,
                mortals_xhx_staff s
            WHERE
                q.id = a.questionId
                AND q.feedbackId = a.feedbackId
                AND a.staffId = s.id
                AND q.questionType != 1
                AND q.questionType != 2
                AND q.id = #{questionId}
        ) as a

    </select>
</mapper>