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

    <resultMap type="CphTeacher" id="CphTeacherResult">
        <result property="teacherId" column="teacher_id"/>
        <result property="name" column="name"/>
        <result property="gender" column="gender"/>
        <result property="age" column="age"/>
        <result property="employeeId" column="employee_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="phoneNumber" column="phone_number"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <association property="dept" column="dept_id" javaType="SysDept" resultMap="deptResult"/>
    </resultMap>
    <resultMap id="deptResult" type="SysDept">
        <id property="deptId" column="dept_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="ancestors" column="ancestors"/>
        <result property="orderNum" column="order_num"/>
        <result property="leader" column="leader"/>
        <result property="status" column="dept_status"/>
    </resultMap>


    <sql id="selectCphTeacherJoin">
        select t.teacher_id,
               t.name,
               t.gender,
               t.age,
               t.employee_id,
               t.dept_id,
               t.phone_number,
               t.status,
               t.create_time,
               t.update_time,
               d.dept_name
        from cph_teacher t
                 left join sys_dept d on t.dept_id = d.dept_id
    </sql>
    <sql id="selectCphTeacherVo">
        select teacher_id,
               name,
               gender,
               age,
               employee_id,
               dept_id,
               phone_number,
               status,
               create_time,
               update_time
        from cph_teacher

    </sql>

    <select id="selectTeacherList" resultMap="CphTeacherResult">
        select *
        from cph_teacher
    </select>

    <select id="listOwnStu" resultType="com.srs.comprehensive.domain.Vo.CphTeacherOwnStu">
        select *
        from (select a.stu_year_id,
                     b.stu_year_name,
                     a.cph_score,
                     a.stu_no,
                     e.`name`                                                                                  as stu_name,
                     e.`status`,
                     c.class_name,
                     RANK() OVER (PARTITION BY a.class_id,a.stu_year_id ORDER BY a.cph_score DESC )            as class_rank,
                     RANK() OVER (PARTITION BY a.major_id,c.grade_id,a.stu_year_id ORDER BY a.cph_score DESC ) as major_rank,
                     a.iam_score,
                     a.classtwo_score,
                     a.stu_score,
                     a.sport_score,
                     a.ce_score,
                     carder_id,
                     cc.carder_name,
                     d.employee_id,
                     a.class_id
              from cph_total_score as a
                       left join srs_stu_year as b on a.stu_year_id = b.id
                       left join srs_class as c on a.class_code = c.class_code
                       left join cph_teacher as d on c.teacher_id = d.teacher_id
                       left join srs_student as e on a.stu_no = e.stu_no
                       left join cph_carders_student ccs on a.stu_no = ccs.stu_no
                       left join cph_carders cc on ccs.carder_id = cc.id) as t1
        where t1.employee_id = #{tNo}
        ORDER BY t1.class_id, t1.stu_year_name desc, t1.stu_no
    </select>

    <select id="listOwnMajor" resultType="SrsMajors">
        select distinct c.major_name, c.major_id
        from srs_student as a
                 left join srs_class as b on a.class_id = b.class_id
                 left join srs_majors as c on b.major_id = c.major_id
                 left join cph_teacher as d on b.teacher_id = d.teacher_id
        where d.employee_id = #{tNo}
    </select>

    <select id="listOwnGrade" resultType="SrsGrade">
        select distinct c.grade_name, c.grade_id
        from srs_student as a
                 left join srs_class as b on a.class_id = b.class_id
                 left join srs_grade as c on b.grade_id = c.grade_id
                 left join cph_teacher as d on b.teacher_id = d.teacher_id
        where d.employee_id = #{tNo}
    </select>

    <select id="getOwnUnTodoCount" resultType="hashmap">
        select count(*) as count_todo
        from cph_iam as a
                 left join srs_student as b on a.to_add = b.stu_id
                 left join srs_class as c on b.class_id = c.class_id
                 left join cph_teacher as d on c.teacher_id = d.teacher_id
        where a.audit_status = 1
          and d.employee_id = #{tno}
    </select>

    <select id="getIamRecordByStuNo" resultType="com.srs.comprehensive.domain.Vo.CphRecord">
        select a.iam_score as record_score, b.stu_no, c.rule_name, d.stu_year_name, a.create_time, a.description
        from cph_iam as a
                 left join srs_student as b on a.to_add = b.stu_id
                 left join cph_rules as c on a.rule_id = c.rule_id
                 left join srs_stu_year as d on a.stu_year_id = d.id
        where a.audit_status = 6
          and b.stu_no = #{stuNo}
    </select>

    <select id="listCourse" resultType="com.srs.comprehensive.domain.Vo.CphTeacherLookScore">
        select a.stu_year_id,
               b.stu_year_name,
               a.stu_score                                                                               as score,
               a.stu_no,
               e.`name`                                                                                  as stu_name,
               e.`status`,
               c.class_name,
               RANK() OVER (PARTITION BY a.class_id,a.stu_year_id,c.grade_id ORDER BY a.stu_score DESC ) as class_rank,
               RANK() OVER (PARTITION BY a.major_id,a.stu_year_id,c.grade_id ORDER BY a.stu_score DESC ) as major_rank
        from cph_total_score as a
                 left join srs_stu_year as b on a.stu_year_id = b.id
                 left join srs_class as c on a.class_code = c.class_code
                 left join cph_teacher as d on c.teacher_id = d.teacher_id
                 left join srs_student as e on a.stu_no = e.stu_no
        where d.employee_id = #{tno}
        ORDER BY a.class_id asc, b.stu_year_name desc, a.stu_no asc
    </select>

    <select id="listIam" resultType="com.srs.comprehensive.domain.Vo.CphTeacherLookScore">
        select *
        from (select a.stu_year_id,
                     b.stu_year_name,
                     a.iam_score as score,
                     a.stu_no,
                     e.`name`    as stu_name,
                     e.`status`,
                     c.class_name
              from cph_total_score as a
                       left join srs_stu_year as b on a.stu_year_id = b.id
                       left join srs_class as c on a.class_code = c.class_code
                       left join cph_teacher as d on c.teacher_id = d.teacher_id
                       left join srs_student as e on a.stu_no = e.stu_no
              where d.employee_id = #{tno}
              ORDER BY a.class_id asc, b.stu_year_name desc, a.stu_no asc) as t1
    </select>

    <select id="listCe" resultType="com.srs.comprehensive.domain.Vo.CphTeacherLookScore">
        select a.stu_year_id,
               b.stu_year_name,
               a.ce_score as score,
               a.stu_no,
               c.stu_name,
               c.`status`,
               c.class_name
        from srs_ce_score as a
                 left join srs_stu_year as b on a.stu_year_id = b.id
                 left join view_stu_info as c on a.stu_no = c.stu_no
        where c.t_no = #{tno}
        ORDER BY c.class_id asc, b.stu_year_name desc, a.stu_no asc
    </select>

    <select id="listSport" resultType="com.srs.comprehensive.domain.Vo.CphTeacherLookScore">
        select a.stu_year_id,
               b.stu_year_name,
               a.sport_score as score,
               a.stu_no,
               e.`name`      as stu_name,
               e.`status`,
               c.class_name
        from cph_total_score as a
                 left join srs_stu_year as b on a.stu_year_id = b.id
                 left join srs_class as c on a.class_code = c.class_code
                 left join cph_teacher as d on c.teacher_id = d.teacher_id
                 left join srs_student as e on a.stu_no = e.stu_no
        where d.employee_id = #{tno}
        ORDER BY a.class_id asc, b.stu_year_name desc, a.stu_no asc
    </select>

    <select id="listClassTwo" resultType="com.srs.comprehensive.domain.Vo.CphTeacherLookScore">
        select *
        from (select a.stu_year_id,
                     b.stu_year_name,
                     a.classtwo_score as score,
                     a.stu_no,
                     e.`name`         as stu_name,
                     e.`status`,
                     c.class_name
              from cph_total_score as a
                       left join srs_stu_year as b on a.stu_year_id = b.id
                       left join srs_class as c on a.class_code = c.class_code
                       left join cph_teacher as d on c.teacher_id = d.teacher_id
                       left join srs_student as e on a.stu_no = e.stu_no
              where d.employee_id = #{tno}
              ORDER BY a.class_id asc, b.stu_year_name desc, a.stu_no asc) as t1
    </select>

    <select id="searchTeacherByTno" parameterType="String" resultMap="CphTeacherResult">
        <include refid="selectCphTeacherVo"/>
        where employee_id = #{tno}
    </select>

    <select id="selectCphTeacherNameList" resultMap="CphTeacherResult">
        select teacher_id, name, employee_id
        from cph_teacher
    </select>
    <select id="selectCphTeacherList" parameterType="CphTeacher" resultMap="CphTeacherResult">
        <include refid="selectCphTeacherJoin"/>
        <where>
            <if test="name != null  and name != ''">and name like concat('%',#{name},'%')</if>
            <if test="deptName != null  and deptName != ''">and d.dept_name like concat('%',#{deptName},'%')</if>
            <if test="gender != null  and gender != ''">and gender = #{gender}</if>
            <if test="age != null ">and age = #{age}</if>
            <if test="employeeId != null  and employeeId != ''">and employee_id like concat('%',#{employeeId},'%')
            </if>
            <if test="deptId != null  and deptId != ''">and t.dept_id = #{deptId}</if>
            <if test="phoneNumber != null  and phoneNumber != ''">and phone_number = #{phoneNumber}</if>
            <if test="status != null ">and status = #{status}</if>
        </where>
    </select>

    <select id="selectCphTeacherByTeacherId" parameterType="Long" resultMap="CphTeacherResult">
        <include refid="selectCphTeacherJoin"/>
        where teacher_id = #{teacherId}
    </select>

    <insert id="insertCphTeacher" parameterType="CphTeacher" useGeneratedKeys="true" keyProperty="teacherId">
        insert into cph_teacher
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="gender != null">gender,</if>
            <if test="age != null">age,</if>
            <if test="employeeId != null">employee_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="phoneNumber != null">phone_number,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="gender != null">#{gender},</if>
            <if test="age != null">#{age},</if>
            <if test="employeeId != null">#{employeeId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="phoneNumber != null">#{phoneNumber},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateCphTeacher" parameterType="CphTeacher">
        update cph_teacher
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="age != null">age = #{age},</if>
            <if test="employeeId != null">employee_id = #{employeeId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="phoneNumber != null">phone_number = #{phoneNumber},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where teacher_id = #{teacherId}
    </update>
    <update id="updateSrsClassList" parameterType="java.util.List">
        update cph_teacher
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="name=case" suffix="end,">
                <foreach collection="studentList" item="i" index="index">
                    <if test="i.name != null ">
                        when employee_id=#{i.employeeId} then #{i.name}
                    </if>
                </foreach>
            </trim>
            <trim prefix="gender=case" suffix="end,">
                <foreach collection="studentList" item="i" index="index">
                    <if test="i.gender != null ">
                        when employee_id=#{i.employeeId} then #{i.gender}
                    </if>
                </foreach>
            </trim>
            <trim prefix="dept_id=case" suffix="end,">
                <foreach collection="studentList" item="i" index="index">
                    <if test="i.deptId != null ">
                        when employee_id=#{i.employeeId} then #{i.deptId}
                    </if>
                </foreach>
            </trim>
            <trim prefix="update_time=case" suffix="end,">
                <foreach collection="studentList" item="i" index="index">
                    <if test="i.updateTime != null ">
                        when employee_id=#{i.employeeId} then #{i.updateTime}
                    </if>
                </foreach>
            </trim>
        </trim>
        where employee_id in
        <foreach collection="studentList" separator="," item="i" index="index" open="(" close=")">
            #{i.employeeId}
        </foreach>
    </update>

    <delete id="deleteCphTeacherByTeacherId" parameterType="Long">
        delete
        from cph_teacher
        where teacher_id = #{teacherId}
    </delete>

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

    <select id="selectCphTeacher" resultMap="CphTeacherResult">
        select *
        from cph_teacher
        where employee_id = #{employeeId}
    </select>

    <select id="selectDeptCode" resultType="Long">
        select a.dept_id
        from sys_dept as a
        where dept_code = #{ksh}
    </select>
    <select id="selectCphTeacherEmployeeId" resultType="java.lang.String">
        select employee_id
        from cph_teacher
        where teacher_id = (select teacher_id
                            from srs_class
                            where class_id = #{classId})
    </select>
    <select id="selectAllByDeptId" resultMap="CphTeacherResult">
        select
        <include refid="selectCphTeacherJoin"/>
        from sys_dept
        where
        dept_id = #{deptId,jdbcType=NUMERIC}
    </select>


    <select id="countOwnStu" parameterType="CphSearch" resultType="CountStu">
        select count(a.stu_no) as sum,
        sum(
        CASE
        WHEN `a`.`gender` = '男' THEN
        1 ELSE 0
        END
        ) AS `boy`,
        sum(
        CASE
        WHEN `a`.`gender` = '女' THEN
        1 ELSE 0
        END
        ) AS `girl`
        from view_stu_info as a
        <where>
            <if test="tNo != null ">and a.t_no = #{tNo}</if>
            <if test="status != null ">and a.`status` = #{status}</if>
        </where>
    </select>

    <select id="countLiveStu" parameterType="CphSearch" resultType="CountStu">
        select count(b.stu_no) as sum,
        sum(
        CASE
        WHEN `b`.`gender` = '男' THEN
        1 ELSE 0
        END
        ) AS `boy`,
        sum(
        CASE
        WHEN `b`.`gender` = '女' THEN
        1 ELSE 0
        END
        ) AS `girl`
        from srs_dormitory_student as a
        left join view_stu_info as b on a.stu_no = b.stu_no
        <where>
            <if test="tNo != null ">and b.t_no = #{tNo}</if>
        </where>
    </select>

    <select id="countOwnHisClass" parameterType="CphSearch" resultType="CphClassRankScore">

        SELECT 'all' as class_name, count(a.class_id) as class_count
        from srs_class as a
                 left join cph_teacher as b on a.teacher_id = b.teacher_id
        <where>
            <if test="tNo != null ">and b.employee_id = #{tNo}</if>
        </where>
        union
        select c.grade_name as class_name, count(a.class_id) as class_count
        from srs_class as a
                 left join cph_teacher as b on a.teacher_id = b.teacher_id
                 left join srs_grade as c on a.grade_id = c.grade_id
        <where>
            <if test="tNo != null ">and b.employee_id = #{tNo}</if>
        </where>
        GROUP BY c.grade_id
    </select>

    <select id="countOwnHisAsClass" parameterType="CphSearch" resultType="java.util.HashMap">
        select b.class_name, count(*) total,
                 SUM(case when a.gender='男' then '1' else 0 end) as sexM,
                 sum(case when a.gender='女' then '1' else 0 end) as sexW
          from  srs_student   a
              left join  srs_class b  on a.class_id=b.class_id
              left join  cph_teacher e  on  b.teacher_id=e.teacher_id
        <where>
            <if test="tNo != null ">and e.employee_id = #{tNo}</if>
        </where>
        group by b.class_name,b.class_id
    </select>
    <select id="selectSXZZ" resultType="hashmap" parameterType="com.srs.comprehensive.domain.Dto.CphExport">
        select t1.1学年,t1.2学院,t1.3专业,t1.4班级,t1.5学号,t1.6姓名,t1.7思想品德,t1.8班级排名,t1.9专业排名,t1.10学生状态,t1.11导出时间
        from (
        select b.stu_year_name as '1学年',
        f.dept_name as '2学院',
        d.major_name as '3专业',
        c.class_name as '4班级',
        a.stu_no as '5学号'
        , e.`name` as '6姓名',
        a.iam_score as '7思想品德',
        RANK() OVER(PARTITION BY a.class_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as '8班级排名' ,
        RANK() OVER(PARTITION BY a.major_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as '9专业排名',
        a.stu_year_id,a.class_id,a.major_id,a.cph_score,c.grade_id,
        case
        when e.`status` = '01' then '在校'
        when e.`status` = '02' then '变更离校'
        when e.`status` = '03' then '毕业'
        end as '10学生状态',
        now() as '11导出时间'
        from cph_total_score as a
        left join srs_stu_year as b on a.stu_year_id = b.id
        left join srs_class as c on a.class_code = c.class_code
        left join srs_student as e on a.stu_no = e.stu_no
        left join srs_majors as d on c.major_id = d.major_id
        left join sys_dept as f on d.college_id = f.dept_id
        ) as t1
        <where>
            <if test="stuYearId != null">and t1.stu_year_id = #{stuYearId}</if>
            <if test="classId != null">and t1.class_id = #{classId}</if>
            <if test="majorId != null">and t1.major_id = #{majorId}</if>
            <if test="gradeId != null">and t1.grade_id = #{gradeId}</if>
            <if test="deptId != null">and t1.college_id = #{deptId}</if>
        </where>
        ORDER BY t1.stu_year_id,t1.class_id,t1.cph_score desc
    </select>
    <select id="selectZHPJ" resultType="hashmap" parameterType="com.srs.comprehensive.domain.Dto.CphExport">
        select t1.1学年,t1.2学院,t1.3专业,t1.4班级,t1.5学号,t1.6姓名,t1.7综合评价,
        t1.8班级排名,t1.9专业排名,t1.10学生状态,t1.11导出时间
        from (
        select b.stu_year_name as '1学年',
        f.dept_name as '2学院',
        d.major_name as '3专业',
        c.class_name as '4班级',
        a.stu_no as '5学号'
        , e.`name` as '6姓名',
        a.ce_score as '7综合评价',
        RANK() OVER(PARTITION BY a.class_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as '8班级排名' ,
        RANK() OVER(PARTITION BY a.major_id,a.stu_year_id,c.grade_id ORDER BY a.cph_score DESC ) as '9专业排名',
        a.stu_year_id,a.class_id,a.major_id,a.cph_score,c.grade_id,
        case
        when e.`status` = '01' then '在校'
        when e.`status` = '02' then '变更离校'
        when e.`status` = '03' then '毕业'
        end as '10学生状态',
        now() as '11导出时间'
        from cph_total_score as a
        left join srs_stu_year as b on a.stu_year_id = b.id
        left join srs_class as c on a.class_code = c.class_code
        left join srs_student as e on a.stu_no = e.stu_no
        left join srs_majors as d on c.major_id = d.major_id
        left join sys_dept as f on d.college_id = f.dept_id
        ) as t1
        <where>
            <if test="stuYearId != null">and t1.stu_year_id = #{stuYearId}</if>
            <if test="classId != null">and t1.class_id = #{classId}</if>
            <if test="majorId != null">and t1.major_id = #{majorId}</if>
            <if test="gradeId != null">and t1.grade_id = #{gradeId}</if>
            <if test="deptId != null">and t1.college_id = #{deptId}</if>
        </where>
        ORDER BY t1.stu_year_id,t1.class_id,t1.cph_score desc
    </select>

</mapper>
