<?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.TeacherEvaluationStatusMapper">

    <resultMap type="TeacherEvaluationStatus" id="TeacherEvaluationStatusResult">
        <result property="id"                column="id"                />
        <result property="teacherId"         column="teacher_id"        />
        <result property="teacherName"       column="teacher_name"      />
        <result property="employeeId"        column="employee_id"       />
        <result property="deptId"            column="dept_id"           />
        <result property="deptName"          column="dept_name"         />
        <result property="stuYearId"         column="stu_year_id"       />
        <result property="stuYearName"       column="stu_year_name"     />
        <result property="isCompleted"       column="is_completed"      />
        <result property="todoCount"         column="todo_count"        />
        <result property="scoreImported"     column="score_imported"    />
        <result property="remarks"           column="remarks"           />
        <result property="lastUpdateTime"    column="last_update_time"  />
        <result property="createTime"        column="create_time"       />
        <result property="updateTime"        column="update_time"       />
    </resultMap>

    <sql id="selectTeacherEvaluationStatusVo">
        select t.teacher_id, t.name as teacher_name, t.employee_id,
               t.dept_id, d.dept_name,
               y.id as stu_year_id, y.stu_year_name,
               CASE
                   WHEN (COALESCE(todo_count.cnt, 0) = 0) AND (COALESCE(score_count.cnt, 0) > 0)
                       THEN 1
                   ELSE 0
                   END as is_completed,
               COALESCE(todo_count.cnt, 0) as todo_count,
               CASE WHEN COALESCE(score_count.cnt, 0) > 0 THEN 1 ELSE 0 END as score_imported
        from cph_teacher t
                 left join sys_dept d on t.dept_id = d.dept_id
                 left join srs_stu_year y on 1=1
                 left join (
            select c.teacher_id, COUNT(*) as cnt, ci.stu_year_id as year_id
            from cph_audit_details cad
                     inner join cph_iam ci on cad.project_id = ci.id
                     inner join srs_student s on cad.submitter_id = s.stu_id
                     inner join srs_class c on s.class_id = c.class_id
            where cad.status_code in (1, 2)
            group by c.teacher_id, ci.stu_year_id
        ) todo_count on t.teacher_id = todo_count.teacher_id and y.id = todo_count.year_id
                 left join (
            select c.teacher_id, sc.stu_year_id, COUNT(*) as cnt
            from srs_class c
                     inner join srs_student s on c.class_id = s.class_id
                     inner join srs_ce_score sc on s.stu_id = sc.stu_id
            group by c.teacher_id, sc.stu_year_id
        ) score_count on t.teacher_id = score_count.teacher_id and y.id = score_count.stu_year_id
    </sql>

    <select id="selectTeacherEvaluationStatusList" parameterType="TeacherEvaluationStatus" resultMap="TeacherEvaluationStatusResult">
        <include refid="selectTeacherEvaluationStatusVo"/>
        <where>
            <if test="deptId != null "> and t.dept_id = #{deptId}</if>
            <if test="teacherName != null  and teacherName != ''"> and t.name like concat('%', #{teacherName}, '%')</if>
            <if test="employeeId != null  and employeeId != ''"> and t.employee_id like concat('%', #{employeeId}, '%')</if>
            <if test="stuYearId != null "> and y.id = #{stuYearId}</if>
        </where>
            order by d.dept_name, t.name, y.stu_year_name desc
    </select>

    <select id="selectTeacherEvaluationStatusByCondition" resultMap="TeacherEvaluationStatusResult">
        <include refid="selectTeacherEvaluationStatusVo"/>
        <where>
            <if test="deptId != null "> and t.dept_id = #{deptId}</if>
            <if test="teacherName != null  and teacherName != ''"> and t.name like concat('%', #{teacherName}, '%')</if>
            <if test="employeeId != null  and employeeId != ''"> and t.employee_id like concat('%', #{employeeId}, '%')</if>
            <if test="stuYearId != null "> and y.id = #{stuYearId}</if>
        </where>
        order by d.dept_name, t.name, y.stu_year_name desc
    </select>



    <!-- 查询学院名称列表，用于下拉框 - 只查询学院级别的部门 -->
    <select id="selectDeptNameList" resultType="java.util.Map">
        select distinct d.dept_id as value, d.dept_name as label,d.order_num
        from sys_dept d
            inner join cph_teacher t on d.dept_id = t.dept_id
        where d.del_flag = '0' and d.status = '0'
        order by d.order_num
    </select>

</mapper>
