<?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.srs.comprehensive.mapper.QuestionnaireAnswerMapper">

    <resultMap type="QuestionnaireAnswer" id="AnswerResult">
        <result property="id"    column="id"    />
        <result property="title"    column="title"    />
        <result property="questions"    column="questions"    />
        <result property="userId"    column="user_id"    />
        <result property="status"    column="status"    />
        <result property="questionnaireId"    column="questionnaire_id"    />
        <result property="targetId"    column="target_id"    />
        <result property="targetName"    column="target_name"    />
        <result property="score"    column="score"    />
    </resultMap>
    <resultMap type="QuestionnaireResult" id="QuestionnaireResultMap">
        <result property="title"    column="title"    />
        <result property="questionnaireId"    column="questionnaire_id"    />
        <result property="targetId"    column="target_id"    />
        <result property="targetName"    column="target_name"    />
        <result property="deptName"    column="dept_name"    />
        <result property="employeeId"    column="employee_id"    />
        <result property="jwcAvgScore"    column="jwc_avg_score"    />
        <result property="ejxyldAvgScore"    column="ejxyld_avg_score"    />
        <result property="fdyAvgScore"    column="fdy_avg_score"    />
        <result property="xsAvgScore"    column="xs_avg_score"    />
        <result property="studentNum"    column="student_num"    />
        <result property="studentFillNum"    column="student_fill_num"    />
    </resultMap>

    <sql id="selectAnswerVo">
        select id, title, questions, user_id, status, questionnaire_id, target_id,target_name,score from t_answer
    </sql>

    <select id="selectAnswerList" parameterType="QuestionnaireAnswer" resultMap="AnswerResult">
        <include refid="selectAnswerVo"/>

        <where>
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="questions != null  and questions != ''"> and questions = #{questions}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="questionnaireId != null "> and questionnaire_id = #{questionnaireId}</if>
            <if test="targetId != null "> and target_id = #{targetId}</if>
            <if test="targetName != null "> and target_name =like concat('%',#{targetName, '%') }</if>
        </where>
    </select>

    <select id="selectAnswerById" parameterType="Long" resultMap="AnswerResult">
        <include refid="selectAnswerVo"/>
        where id = #{id}
    </select>

    <insert id="insertAnswer" parameterType="QuestionnaireAnswer" useGeneratedKeys="true" keyProperty="id">
        insert into t_answer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null">title,</if>
            <if test="questions != null">questions,</if>
            <if test="userId != null">user_id,</if>
            <if test="status != null">status,</if>
            <if test="questionnaireId != null">questionnaire_id,</if>
            <if test="targetId != null">target_id,</if>
            <if test="targetName != null">target_name,</if>
            <if test="score != null">score,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null">#{title},</if>
            <if test="questions != null">#{questions},</if>
            <if test="userId != null">#{userId},</if>
            <if test="status != null">#{status},</if>
            <if test="questionnaireId != null">#{questionnaireId},</if>
            <if test="targetId != null">#{targetId},</if>
            <if test="targetName != null">#{targetName},</if>
            <if test="score != null">#{score},</if>
         </trim>
    </insert>

    <update id="updateAnswer" parameterType="QuestionnaireAnswer">
        update t_answer
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null">title = #{title},</if>
            <if test="questions != null">questions = #{questions},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="targetName != null">target_name = #{targetName},</if>
            <if test="targetId != null">target_id = #{targetId},</if>
            <if test="questionnaireId != null">questionnaire_id = #{questionnaireId},</if>
            <if test="score != null">score = #{score},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAnswerById" parameterType="Long">
        delete from t_answer where id = #{id}
    </delete>


    <delete id="deleteAnswerByIds" parameterType="String">
        delete from t_answer where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

<!--    <select id="ratioScore" parameterType="QuestionnaireResult" resultMap="QuestionnaireResultMap">-->
<!--        SELECT questionnaire_id, target_id, GROUP_CONCAT(DISTINCT target_name) as target_name, GROUP_CONCAT(DISTINCT-->
<!--        title) as title,-->
<!--        GROUP_CONCAT(DISTINCT dept_name) dept_name, GROUP_CONCAT(DISTINCT employee_id) employee_id,-->
<!--        ROUND(sum(if(LOCATE("学工处", role_name), avg_score, 0)), 2) as jwc_avg_score,-->
<!--        ROUND(sum(if(LOCATE("Junior_college_leader", role_key), avg_score, 0)), 2) as ejxyld_avg_score,-->
<!--        ROUND(sum(if(LOCATE("testteacher", role_key) > 0, avg_score, 0)), 2) as fdy_avg_score,-->
<!--        ROUND(sum(if(LOCATE("学生", role_name), avg_score, 0)), 2) as xs_avg_score-->
<!--        FROM (-->
<!--        select-->
<!--        questionnaire_id,-->
<!--        target_id,-->
<!--        GROUP_CONCAT(DISTINCT employee_id) as employee_id,-->
<!--        GROUP_CONCAT(DISTINCT target_name) as target_name,-->
<!--        GROUP_CONCAT(DISTINCT dept_name) dept_name,-->
<!--        GROUP_CONCAT(DISTINCT role_key) as role_key,-->
<!--        GROUP_CONCAT(DISTINCT role_name) as role_name,-->
<!--        avg(avg_score) as avg_score,-->
<!--        avg(total_score) as total_score,-->
<!--        avg(totalAnswerPaperNum) as totalAnswerPaperNum,-->
<!--        GROUP_CONCAT(DISTINCT title) as title-->
<!--        FROM-->
<!--        (-->
<!--        select-->
<!--        questionnaire_id,-->
<!--        target_id,-->
<!--        GROUP_CONCAT(DISTINCT target_name) as target_name,-->
<!--        GROUP_CONCAT(DISTINCT dept_name) dept_name,-->
<!--        role_key,-->
<!--        GROUP_CONCAT(DISTINCT role_name) as role_name,-->
<!--        avg(score) as avg_score,-->
<!--        SUM(score) as total_score,-->
<!--        COUNT(user_id) as totalAnswerPaperNum,-->
<!--        GROUP_CONCAT(DISTINCT employee_id) as employee_id,-->
<!--        GROUP_CONCAT(DISTINCT title) as title-->
<!--        FROM-->
<!--        (-->
<!--        SELECT-->
<!--        r.role_key,-->
<!--        r.role_id,-->
<!--        r.role_name,-->
<!--        a.target_name,-->
<!--        a.target_id,-->
<!--        score,-->
<!--        a.user_id,-->
<!--        questionnaire_id,-->
<!--        q.title,-->
<!--        d.dept_name,-->
<!--        th.employee_id-->
<!--        FROM `t_answer` a-->
<!--        LEFT join sys_user u on a.user_id = u.user_name-->
<!--        LEFT JOIN sys_user_role ur on ur.user_id = u.user_id-->
<!--        LEFT JOIN sys_role r on ur.role_id = r.role_id-->
<!--        LEFT JOIN t_questionnaire q on q.id = a.questionnaire_id-->
<!--        LEFT JOIN cph_teacher th on th.teacher_id = a.target_id-->
<!--        LEFT JOIN sys_dept d on d.dept_id = u.dept_id-->
<!--        where a.`status` = 1-->
<!--        <if test="title != null and title != ''">and a.title like concat('%', #{title}, '%')</if>-->
<!--        <if test="targetName != null and targetName != ''">and th.name like concat('%', #{targetName}, '%')</if>-->
<!--        <if test="deptId != null">and th.dept_id = #{deptId}</if>-->
<!--        <if test="employeeId != null">and th.employee_id = #{employeeId}</if>-->
<!--        <if test="deptName != null">and d.dept_name like concat('%', #{deptName}, '%')</if>-->
<!--        <if test="stuNo != null">and EXISTS(-->
<!--            SELECT * from srs_student s LEFT JOIN srs_class c on c.class_id = s.class_id where th.teacher_id =-->
<!--            c.teacher_id and s.stu_no = #{stuNo}-->
<!--            )-->
<!--        </if>-->
<!--        order by d.dept_name-->
<!--        ) temp GROUP BY questionnaire_id, target_id, role_key, dept_name, employee_id-->
<!--        ) as temp1 GROUP BY questionnaire_id, target_id, dept_name, employee_id-->
<!--        ) as temp2 GROUP BY questionnaire_id, target_id, dept_name, employee_id-->
<!--        ORDER BY dept_name-->
<!--    </select>-->


    <select id="ratioScore" parameterType="QuestionnaireResult" resultMap="QuestionnaireResultMap">
        select a.title,a.target_id,a.questionnaire_id, COUNT(id) as student_fill_num , avg(a.score) as xs_avg_score,
        (
        SELECT COUNT(*)
        FROM srs_student s
        left JOIN srs_class c on s.class_id = c.class_id
        where s.status = '01' and c.teacher_id = a.target_id
        ) as student_num,t.employee_id,a.target_name,d.dept_name
        FROM t_answer a
        LEFT JOIN cph_teacher t ON a.target_id = t.teacher_id
        LEFT JOIN sys_user u ON t.employee_id = u.user_name
        LEFT JOIN sys_dept d ON u.dept_id = d.dept_id
        <where>
            a.`status` = 1
            <if test="title != null and title != ''">and a.title like concat('%', #{title}, '%')</if>
            <if test="targetName != null and targetName != ''">and a.target_name like concat('%', #{targetName}, '%')</if>
            <if test="deptId != null">and d.dept_id = #{deptId}</if>
            <if test="employeeId != null">and t.employee_id = #{employeeId}</if>
            <if test="deptName != null">and d.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="stuNo != null">and EXISTS(
                SELECT * from srs_student s LEFT JOIN srs_class c on c.class_id = s.class_id where th.teacher_id =
                c.teacher_id and s.stu_no = #{stuNo}
                )
            </if>
        </where>
        GROUP BY a.target_id ,a.questionnaire_id,t.name,a.target_name,d.dept_name,a.title
        ORDER BY d.dept_name
    </select>

</mapper>
