<?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.EvaluateManageMapper">
    <select id="getPage" parameterType="com.xjrsoft.module.evaluate.dto.EvaluateManagePageDto" resultType="com.xjrsoft.module.evaluate.vo.EvaluateManagePageVo">
        SELECT t1.id,t2.name AS semester_name,t4.name AS org_name,t3.evaluate_type,t5.name AS evaluate_type_cn,t1.name,t1.start_time,t1.end_time,t1.status FROM evaluate_manage t1
        LEFT JOIN base_semester t2 ON t2.id = t1.base_semester_id
        LEFT JOIN evaluate_template t3 ON t3.id = t1.evaluate_template_id
        LEFT JOIN xjr_department t4 ON t4.id = t1.org_id
        LEFT JOIN xjr_dictionary_detail t5 ON t5.code = t3.evaluate_type AND t5.item_id = 1746831039385366530
        WHERE t1.delete_mark = 0
        <if test="dto.semesterId != null">
            and t1.base_semester_id = #{dto.semesterId}
        </if>
        <if test="dto.orgId != null">
            and t1.org_id = #{dto.orgId}
        </if>
        <if test="dto.evaluateType != null and dto.evaluateType != ''">
            and t3.evaluate_type = #{dto.evaluateType}
        </if>
        <if test="dto.status != null">
            and t1.status = #{dto.status}
        </if>
        <if test="dto.startTime != null">
            and t1.start_time >= #{dto.startTime}
        </if>
        <if test="dto.endTime != null">
            and #{dto.endTime} >= t1.end_time
        </if>
        <if test="dto.name != null and dto.name != ''">
            and t1.name like concat('%', #{dto.name}, '%')
        </if>
        ORDER BY t1.create_date DESC
    </select>

    <select id="getList" parameterType="com.xjrsoft.module.room.dto.RoomPageDto" resultType="com.xjrsoft.module.room.vo.RoomExcelVo">
    </select>

    <select id="getMaxSortCode" resultType="java.lang.Integer">
        SELECT IFNULL(MAX(sort_code),1) FROM evaluate_manage WHERE delete_mark = 0
    </select>

    <select id="getMobileIndexPage" parameterType="com.xjrsoft.module.evaluate.dto.MobileIndexPageDto" resultType="com.xjrsoft.module.evaluate.vo.MobileIndexPageVo">
        select
            distinct
            t.id as evaluateManageId,
            t.name,
            t2.evaluate_type,
            t4.name as evaluateTypeCn,
            (select
                count(tb3.status)
            from evaluate_manage tb1
                left join evaluate_executer tb3 on tb3.evaluate_manage_id = tb1.id
            where tb1.delete_mark = 0
                and tb1.status &lt;&gt; -1
                and tb3.status = 0
                and tb3.user_id = t3.user_id
                and tb1.id = t.id) as ratedCount
        from evaluate_manage t
            left join evaluate_template t2 on t2.id = t.evaluate_template_id
            left join evaluate_executer t3 on t3.evaluate_manage_id = t.id
            left join xjr_dictionary_detail t4 on t4.code = t2.evaluate_type
        where t.delete_mark = 0
            and t.status &lt;&gt; -1
            and t3.user_id = #{dto.loginUserId}
            <if test="dto.keyword != null and dto.keyword != ''">
                and (t.name like concat('%', #{dto.keyword}, '%')
                    or t4.name like concat('%', #{dto.keyword}, '%'))
            </if>
    </select>

    <select id="getMobileResultPage" parameterType="com.xjrsoft.module.evaluate.dto.MobileResultPageDto" resultType="com.xjrsoft.module.evaluate.vo.MobileResultPageVo">
        select
            t.id as evaluateSubmitRecordId,
            t.evaluate_manage_id as evaluateManageId,
            t1.name,
            t.evaluated_object_id as evaluateObjectId,
            t3.object_id as objectId,
            ifnull(t4.name, t5.name) as objectIdCn,
            t2.id as evaluateUserId,
            t2.user_id as userId,
            t6.name as userIdCn,
            t.create_date as startTime,
            t.status as evaluateStatus,
            t7.evaluate_type as evaluateType,
            t8.name as evaluateTypeCn
        from evaluate_submit_record t
            left join evaluate_manage t1 on t1.id = t.evaluate_manage_id
            left join evaluate_executer t2 on t2.id = t.evaluate_executer_id
            left join evaluate_object t3 on t3.id = t.evaluated_object_id
            left join xjr_user t4 on t4.id = t3.object_id
            left join base_class t5 on t5.id = t3.object_id
            left join xjr_user t6 on t6.id = t2.user_id
            left join evaluate_template t7 on t7.id = t1.evaluate_template_id
            left join xjr_dictionary_detail t8 on t8.code = t7.evaluate_type
        where t2.user_id = #{dto.loginUserId}
            and t.evaluate_manage_id = #{dto.evaluateManageId}
            <if test="dto.status != null">
                and t.status = #{dto.status}
            </if>
            <if test="dto.startTime != null and dto.startTime != ''">
                and t1.start_time >= #{dto.startTime}
            </if>
            <if test="dto.endTime != null and dto.startTime != ''">
                and #{dto.endTime} >= t1.end_time
            </if>
        order by t.status;
    </select>

    <select id="getDrillEvaluateClassList" parameterType="com.xjrsoft.module.evaluate.dto.DrillEvaluateClassListDto" resultType="com.xjrsoft.module.evaluate.vo.DrillEvaluateClassListVo">
        select
            t.id as evaluateSubmitRecordId,
            t.evaluate_manage_id as evaluateManageId,
            t1.name,
            t.evaluated_object_id as evaluateObjectId,
            t3.object_id as objectId,
            ifnull(t4.name, t5.name) as objectIdCn,
            t2.id as evaluateUserId,
            t2.user_id as userId,
            t6.name as userIdCn,
            t1.start_time,
            t1.end_time,
            t.status as evaluateStatus,
            t7.evaluate_type as evaluateType,
            t8.name as evaluateTypeCn
        from evaluate_submit_record t
            left join evaluate_manage t1 on t1.id = t.evaluate_manage_id
            left join evaluate_executer t2 on t2.id = t.evaluate_executer_id
            left join evaluate_object t3 on t3.id = t.evaluated_object_id
            left join xjr_user t4 on t4.id = t3.object_id
            left join base_class t5 on t5.id = t3.object_id
            left join xjr_user t6 on t6.id = t2.user_id
            left join evaluate_template t7 on t7.id = t1.evaluate_template_id
            left join xjr_dictionary_detail t8 on t8.code = t7.evaluate_type
            left join base_classroom t9 on t9.id = t5.classroom_id
            left join base_class_major_set t10 on t10.class_id = t5.id
            left join base_major_set t11 on t11.id = t10.major_set_id
        where t.create_date  like concat('%', #{dto.todayDate}, '%')
          and t2.user_id = #{dto.loginUserId}
          and t.evaluate_manage_id = #{dto.evaluateManageId}
        <if test="dto.majorId != null and dto.majorId > 0">
            and t10.major_id = #{dto.majorId}
        </if>
        <if test="dto.officeBuildId != null and dto.officeBuildId > 0">
            and t8.officeBuildId = #{dto.officeBuildId}
        </if>
        order by t.status;
    </select>

    <select id="getWriteInfo" parameterType="com.xjrsoft.module.evaluate.dto.EvaluateWritePageDto" resultType="com.xjrsoft.module.evaluate.vo.EvaluateWriteVo">
        select t1.id, t1.name,t2.total as sumScore,t1.start_time,t1.end_time,
               (SELECT COUNT(id) FROM evaluate_manage_item a WHERE t1.delete_mark = 0 AND a.evaluate_manage_id = t1.id) AS item_count
        from evaluate_manage t1
                 left join evaluate_template t2 on t2.id = t1.evaluate_template_id
        where t1.delete_mark = 0
        and t1.id = #{id}
    </select>
    <select id="getItemList" resultType="com.xjrsoft.module.evaluate.vo.EvaluateManageItemVo">
        SELECT * FROM evaluate_manage_item WHERE delete_mark = 0 AND evaluate_manage_id = #{id} ORDER BY sort_code
    </select>
    <select id="getResultList" parameterType="com.xjrsoft.module.evaluate.dto.ResultListDto" resultType="com.xjrsoft.module.evaluate.vo.EvaluateManageItemVo">
        SELECT t1.id,
               t1.sort_code,
               t1.evaluate_manage_id,
               t1.topic,
               t1.problem,
               t1.score,
               t2.score AS actualScore,
               t2.opinion,
               t1.input_not_null
        FROM evaluate_submit_record t
                 LEFT JOIN evaluate_result t2 ON t2.submit_record_id = t.id
                 left join evaluate_manage_item t1 on t1.id = t2.evaluate_item_id
                 LEFT JOIN evaluate_executer t3 ON t3.id = t.evaluate_executer_id
        WHERE t1.delete_mark = 0
          AND t.id = #{dto.evaluateSubmitRecordId}
        ORDER BY t1.sort_code
    </select>

    <select id="getScoreList" parameterType="com.xjrsoft.module.evaluate.dto.EvaluateManageScoreDto" resultType="com.xjrsoft.module.evaluate.vo.EvaluateManageScoreVo">
        select t.submit_record_id as submitRecordId,
            sum(t.score) as totalScore,
            count(t.score) as itemCount
        from evaluate_result t
            left join evaluate_submit_record t3 on t3.id = t.submit_record_id
        where t.delete_mark = 0
        and t.submit_record_id in
            <foreach item="submitRecordId" index="index" collection="dto.submitRecordIdList" open="(" close=")"
                                           separator=",">
                #{submitRecordId}
            </foreach>
        group by t.submit_record_id;
    </select>

    <insert id="insertEvaluateSubmitRecord" parameterType="java.lang.Long">
        insert into evaluate_submit_record (id, evaluate_manage_id, evaluate_executer_id, create_date, evaluated_object, evaluated_object_id, status)
            (SELECT
                 UUID(),
                 t.evaluate_manage_id,
                 t.id,
                 NOW(),
                 if(t2.evaluate_type='dm_evaluate_class', 'class', 'teacher'),
                 t.evaluate_object_id,
                 0
             FROM evaluate_executer t
                      LEFT JOIN evaluate_manage t1 ON (t1.id = t.evaluate_manage_id)
                      LEFT JOIN evaluate_template t2 ON (t2.id = t1.evaluate_template_id)
             WHERE t.delete_mark = 0
          AND t.evaluate_manage_id = #{evaluateManageId})
    </insert>
</mapper>