<?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.EvaluateObjectMapper">
    <select id="getObjectList" parameterType="com.xjrsoft.module.evaluate.dto.EvaluateObjectListDto"
            resultType="com.xjrsoft.module.evaluate.vo.EvaluateObjectListVo">
        SELECT distinct t1.id,t1.base_grade_id as gradeId,t2.name AS grade_name,t1.object_id,ifnull(t3.name, t6.name) AS object_name
        FROM evaluate_object t1
        LEFT JOIN base_grade t2 ON t2.id = t1.base_grade_id
        LEFT JOIN xjr_user t3 ON t3.id = t1.object_id
        LEFT JOIN base_class t6 ON t6.id = t1.object_id
        LEFT JOIN xjr_user_dept_relation t4 ON t4.user_id = t1.object_id
        LEFT JOIN base_class t5 ON t5.grade_id = t2.id
        WHERE t1.delete_mark = 0 and t1.evaluate_manage_id = #{dto.evaluateManageId}
        <if test="dto.gradeId != null and dto.gradeId > 0">
            and t1.base_grade_id = #{dto.gradeId}
        </if>
        <if test="dto.deptId != null and dto.deptId > 0">
            and t4.dept_id = #{dto.deptId}
        </if>
        <if test="dto.classId != null and dto.classId > 0">
            and t5.id = #{dto.classId}
        </if>
        <if test="dto.name != null and dto.name != ''">
            and t3.name like concat("%", #{dto.name}, "%")
        </if>
        ORDER BY t1.id
    </select>

    <select id="getExecuterList" parameterType="com.xjrsoft.module.evaluate.dto.EvaluateObjectListDto"
            resultType="com.xjrsoft.module.evaluate.vo.EvaluateExecuterVo">
        SELECT t1.id, t1.evaluate_object_id,t2.name AS executer_name,t1.user_id as executer_id FROM evaluate_executer t1
        LEFT JOIN xjr_user t2 ON t1.user_id = t2.id
        WHERE t1.delete_mark = 0 and t1.evaluate_manage_id = #{dto.evaluateManageId}
    </select>

    <select id="getNewObjectList" parameterType="com.xjrsoft.module.evaluate.dto.EvaluateObjectListDto" resultType="com.xjrsoft.module.evaluate.vo.EvaluateObjectListVo">
        <if test="dto.evaluateType != null and dto.evaluateType.equals('stu_evaluate_headtea')">
            SELECT t1.id AS class_id,t2.id AS object_id,t2.name AS object_name,t1.grade_id
            FROM base_class t1
            LEFT JOIN xjr_user t2 ON t1.teacher_id = t2.id
            WHERE t1.delete_mark = 0
            <if test="dto.gradeId != null">
                and t1.grade_id = #{dto.gradeId}
            </if>
        </if>
        <if test="dto.evaluateType != null and dto.evaluateType.equals('dm_evaluate_class')">
            SELECT t1.id AS object_id,t1.name AS object_name,t1.grade_id
            FROM base_class t1
            WHERE t1.delete_mark = 0
            <if test="dto.gradeId != null">
                and t1.grade_id = #{dto.gradeId}
            </if>
        </if>
        <if test="dto.evaluateType != null and dto.evaluateType.equals('tea_evaluate_class')">
            SELECT t1.id AS object_id,t1.name AS object_name,t1.grade_id
            FROM base_class t1
            WHERE t1.delete_mark = 0
            <if test="dto.gradeId != null">
                and t1.grade_id = #{dto.gradeId}
            </if>
        </if>
    </select>

    <select id="getNewExecuterList" parameterType="com.xjrsoft.module.evaluate.dto.EvaluateObjectListDto" resultType="com.xjrsoft.module.evaluate.vo.EvaluateExecuterVo">
        <if test="dto.evaluateType != null and dto.evaluateType.equals('stu_evaluate_headtea')">
            SELECT t2.id as executer_id,t2.name as executer_name,t1.class_id,t1.grade_id FROM base_student_school_roll t1
            LEFT JOIN xjr_user t2 ON t1.user_id = t2.id
            WHERE t1.delete_mark = 0 AND t2.delete_mark = 0
            <if test="dto.gradeId != null">
                and t1.grade_id = #{dto.gradeId}
            </if>
        </if>
        <if test="dto.evaluateType != null and dto.evaluateType.equals('dm_evaluate_class')">
            select
            t.id as executer_id,t.name as executer_name
            from xjr_user t
            left join xjr_user_post_relation t1 on t1.user_id = t.id
            where t1.post_id = 1742726034814464002
            and t.delete_mark = 0
        </if>
        <if test="dto.evaluateType != null and dto.evaluateType.equals('tea_evaluate_class')">
            SELECT DISTINCT t2.class_id,t1.id,t1.name FROM xjr_user t1
            INNER JOIN course_table t2 ON t2.teacher_id LIKE CONCAT('%', t1.id,'%')
            WHERE 1 = 1
            <if test="dto.classIds != null and !dto.classIds.isEmpty()">
                and t2.class_id in
                <foreach item="id" index="index" collection="dto.classIds" open="(" close=")"
                         separator=",">
                    #{id}
                </foreach>
            </if>
            AND t2.schedule_date BETWEEN #{dto.startDate} AND #{dto.endDate}
            AND (t2.adjust_type = 'course_exchange' OR t2.adjust_type = 'course_replace' OR t2.adjust_type IS NULL)
            UNION
            SELECT DISTINCT t2.class_id,t1.id,t1.name FROM xjr_user t1
            INNER JOIN course_table_bak t2 ON t2.teacher_id LIKE CONCAT('%', t1.id,'%')
            WHERE 1 = 1
            <if test="dto.classIds != null and !dto.classIds.isEmpty()">
                and t2.class_id in
                <foreach item="id" index="index" collection="dto.classIds" open="(" close=")"
                         separator=",">
                    #{id}
                </foreach>
            </if>
            AND t2.schedule_date BETWEEN #{dto.startDate} AND #{dto.endDate}
            AND (t2.adjust_type = 'course_exchange' OR t2.adjust_type = 'course_replace' OR t2.adjust_type IS NULL)
        </if>
    </select>

    <delete id="objectGradeDeleteExecuter" parameterType="com.xjrsoft.module.evaluate.dto.ObjectGradeDeleteDto">
        delete t from evaluate_executer t
        left join evaluate_object t1 on t1.id = t.evaluate_object_id
        where t1.base_grade_id = #{dto.gradeId} and t.evaluate_manage_id = #{dto.evaluateManageId};
    </delete>

    <delete id="objectGradeDeleteObject" parameterType="com.xjrsoft.module.evaluate.dto.ObjectGradeDeleteDto">
        delete from evaluate_object t
        where t.base_grade_id = #{dto.gradeId} and t.evaluate_manage_id = #{dto.evaluateManageId};
    </delete>
</mapper>