<?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.xjrsoft.module.evaluate.mapper.EvaluateResultMapper">
    <select id="getIndexPage" parameterType="com.xjrsoft.module.evaluate.dto.EvaluateResultIndexPageDto" resultType="com.xjrsoft.module.evaluate.vo.EvaluateResultIndexPageVo">
        SELECT
        t.id    AS evaluateManageId,
        t1.name AS semesterName,
        t2.name AS orgName,
        t4.name AS evaluateTypeCN,
        t.name,
        t.start_time,
        t.end_time
        FROM evaluate_manage t
        LEFT JOIN base_semester t1 ON (t1.id = t.base_semester_id)
        LEFT JOIN xjr_department t2 ON (t2.id = t.org_id)
        LEFT JOIN evaluate_template t3 ON (t3.id = t.evaluate_template_id)
        LEFT JOIN xjr_dictionary_detail t4 ON (t4.code = t3.evaluate_type)
        WHERE t.delete_mark = 0
          and t.status != -1
            <if test="dto.baseSemesterId != null and dto.baseSemesterId > 0">
              AND t.base_semester_id = #{dto.baseSemesterId}
            </if>
            <if test="dto.orgId != null and dto.orgId > 0">
                AND t.org_id = #{dto.orgId}
            </if>
            <if test="dto.evaluateType != null and dto.evaluateType != ''">
                AND t3.evaluate_type = #{dto.evaluateType}
            </if>
            <if test="dto.name != null and dto.name != ''">
                AND t.name like CONCAT('%',#{dto.name},'%')
            </if>
            <if test="dto.startTime != null and dto.startTime != ''">
                AND t.start_time &gt;= #{dto.startTime}
            </if>
            <if test="dto.endTime != null and dto.endTime != ''">
                AND t.end_time &lt;= #{dto.endTime}
            </if>
        ORDER BY t.id DESC
    </select>

    <select id="getDetailPage" parameterType="com.xjrsoft.module.evaluate.dto.EvaluateResultDetailPageDto" resultType="com.xjrsoft.module.evaluate.vo.EvaluateResultDetailPageVo">
        select
        t.evaluate_manage_id as evaluateManageId,
        t.evaluate_object_id as evaluatedObjectId,
        t.id as evaluatedExecuterId,
        ifnull(t1.name, t2.name) as ObjectIdCN,
        t4.name as userIdCN,
        t.status,
        ifnull(t5.comprehensiveScore, 0) as comprehensiveScore
        from evaluate_executer t
        left join evaluate_object t3 on t3.id = t.evaluate_object_id
        left join xjr_user t1 on t1.id = t3.object_id
        left join base_class t2 on t2.id = t3.object_id
        left join xjr_user t4 on t4.id = t.user_id
        left join (select
        t.evaluated_object_id, t.user_id, sum(t.score) as comprehensiveScore
        from evaluate_result t
        group by t.evaluated_object_id, t.user_id) t5 on t5.evaluated_object_id = t.evaluate_object_id and t5.user_id = t.id
        where t.evaluate_manage_id = #{dto.evaluateManageId}
        <if test="dto.baseGradeId != null and dto.baseGradeId > 0">
            AND t3.base_grade_id = #{dto.baseGradeId}
        </if>
        <if test="dto.status != null">
            AND t.status = #{dto.status}
        </if>
        <if test="dto.classId != null and dto.classId > 0">
            AND t3.object_id = #{dto.classId}
        </if>
        <if test="dto.teacherId != null and dto.teacherId > 0 and (dto.checkObject == null or dto.checkObject == 0)">
            AND (t3.object_id = #{dto.teacherId}
                or t.user_id = #{dto.teacherId})
        </if>
        <if test="dto.teacherId != null and dto.teacherId > 0 and dto.checkObject != null and dto.checkObject == 1">
            AND t3.object_id = #{dto.teacherId}
        </if>
        <if test="dto.teacherId != null and dto.teacherId > 0 and dto.checkObject != null and dto.checkObject == 2">
            AND t.user_id = #{dto.teacherId}
        </if>
        <if test="dto.name != null and dto.name != '' and (dto.checkObject == null or dto.checkObject == 0)">
            AND (t1.name like CONCAT('%',#{dto.name},'%')
                or t2.name like CONCAT('%',#{dto.name},'%')
                or t4.name like CONCAT('%',#{dto.name},'%'))
        </if>
        <if test="dto.name != null and dto.name != '' and dto.checkObject != null and dto.checkObject == 1">
            AND (t1.name like CONCAT('%',#{dto.name},'%')
            or t2.name like CONCAT('%',#{dto.name},'%'))
        </if>
        <if test="dto.name != null and dto.name != '' and dto.checkObject != null and dto.checkObject == 2">
            AND t4.name like CONCAT('%',#{dto.name},'%')
        </if>
    </select>

    <select id="getDetailPageDetail" parameterType="com.xjrsoft.module.evaluate.dto.EvaluateResultDetailPageDetailDto" resultType="com.xjrsoft.module.evaluate.vo.EvaluateResultDetailPageDetailVo">
        select
            ifnull(t1.name, t2.name) as ObjectIdCN,
            t4.name as userIdCN,
            ifnull(t5.comprehensiveScore, 0) as actulTotal,
            t6.name,
            t7.total
        from evaluate_object t
                 left join xjr_user t1 on t1.id = t.object_id
                 left join base_class t2 on t2.id = t.object_id
                 left join evaluate_executer t3 on t3.evaluate_object_id = t.id
                 left join xjr_user t4 on t4.id = t3.user_id
                 left join (select
                                t.evaluated_object_id, t.user_id, sum(t.score) as comprehensiveScore
                            from evaluate_result t
                            group by t.evaluated_object_id, t.user_id) t5 on t5.evaluated_object_id = t.id and t5.user_id = t3.id
                 left join evaluate_manage t6 on t6.id = t.evaluate_manage_id
                 left join evaluate_template t7 on t7.id = t6.evaluate_template_id
        where t.delete_mark = 0
          and t.evaluate_manage_id = #{dto.evaluateManageId}
          and t.id = #{dto.evaluatedObjectId}
          and t3.id = #{dto.evaluatedExecuterId}
    </select>

    <select id="getDetailPageDetailList" parameterType="com.xjrsoft.module.evaluate.dto.EvaluateResultDetailPageDetailDto" resultType="com.xjrsoft.module.evaluate.vo.EvaluateResultDetailPageDetailList">
        select
            t.score,
            t.problem,
            t.topic,
            t1.score as actualScore,
            t1.opinion
        from evaluate_manage_item t
                 left join evaluate_result t1 on t1.evaluate_item_id = t.id
        where t.delete_mark = 0
          and t.evaluate_manage_id = #{dto.evaluateManageId}
          and t1.evaluated_object_id = #{dto.evaluatedObjectId}
          and t1.user_id = #{dto.evaluatedExecuterId}
    </select>

    <select id="listByObjectIdAndEvaluateItemId" resultType="com.xjrsoft.module.evaluate.vo.ResultViewingPageVo">
        select
            sum(t.score) as sumScore,
            t.evaluate_item_id as evaluateManageItemId,
            t1.object_id as objectId
        from evaluate_result t
                 left join evaluate_object t1 on t1.id = t.evaluated_object_id
        where t.evaluate_item_id in
        <foreach item="evaluateManageItemId" index="index" collection="evaluateManageItemIdList" open="(" close=")"
                 separator=",">
            #{evaluateManageItemId}
        </foreach>
          and t1.object_id in
        <foreach item="evaluateObjectId" index="index" collection="evaluateObjectIdList" open="(" close=")"
                 separator=",">
            #{evaluateObjectId}
        </foreach>
        group by t1.object_id, t.evaluate_item_id;
    </select>

    <select id="listTopicScoreByObjectIdAndTopic" resultType="com.xjrsoft.module.evaluate.vo.ResultViewingPageVo">
        select sum(t.score)       as topicScore,
        t1.object_id       as objectId,
        t2.topic
        from evaluate_result t
        left join evaluate_object t1 on t1.id = t.evaluated_object_id
        left join evaluate_manage_item t2 on t2.id = t.evaluate_item_id
        where t.evaluate_item_id in
        <foreach item="evaluateManageItemId" index="index" collection="evaluateManageItemIdList" open="(" close=")"
                 separator=",">
            #{evaluateManageItemId}
        </foreach>
        and t1.object_id in
        <foreach item="evaluateObjectId" index="index" collection="evaluateObjectIdList" open="(" close=")"
                 separator=",">
            #{evaluateObjectId}
        </foreach>
        group by t1.object_id, t2.topic;
    </select>

    <select id="listSynthesisScore" resultType="com.xjrsoft.module.evaluate.vo.ResultViewingPageVo">
        select sum(t.score)       as synthesisScore,
        t1.object_id       as objectId
        from evaluate_result t
        left join evaluate_object t1 on t1.id = t.evaluated_object_id
        where t.evaluate_item_id in
        <foreach item="evaluateManageItemId" index="index" collection="evaluateManageItemIdList" open="(" close=")"
                 separator=",">
            #{evaluateManageItemId}
        </foreach>
        and t1.object_id in
        <foreach item="evaluateObjectId" index="index" collection="evaluateObjectIdList" open="(" close=")"
                 separator=",">
            #{evaluateObjectId}
        </foreach>
        group by t1.object_id;
    </select>

    <select id="getTeaEvaluateClassList" parameterType="com.xjrsoft.module.evaluate.dto.TeaEvaluateClassDto" resultType="com.xjrsoft.module.evaluate.vo.TeaEvaluateClassListVo">
        SELECT t2.evaluate_item_id as id, t2.problem,t1.score,t1.user_id,t5.object_id as evaluated_object_id FROM evaluate_result t1
        INNER JOIN evaluate_manage_item t2 ON t1.evaluate_item_id = t2.id
        INNER JOIN evaluate_manage t3 ON t3.id = t2.evaluate_manage_id
        INNER JOIN evaluate_template t4 ON t3.evaluate_template_id = t4.id
        inner join evaluate_object t5 on t1.evaluated_object_id = t5.id
        WHERE t1.delete_mark = 0 AND t2.delete_mark = 0 AND t3.delete_mark = 0
        AND t4.delete_mark = 0 AND t5.delete_mark = 0
        AND t3.year = #{dto.year}
        AND t3.month = #{dto.month}
        AND t4.evaluate_type = #{dto.evaluateType}
    </select>
</mapper>