<?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.qmth.exam.reserve.dao.StudentDao">
    <update id="updateStudentApplyNumber" parameterType="java.lang.Long">
        UPDATE t_student s SET s.apply_number =( SELECT count( 1 ) FROM t_student_course WHERE student_id = #{studentId} )
        WHERE
            s.id = #{studentId}
    </update>
    <select id="listNoFinishStudent" resultType="com.qmth.exam.reserve.entity.StudentEntity">
        SELECT s.id, s.apply_number, s.category_id
        FROM t_student s
                 LEFT JOIN (SELECT student_id,
                                   count(1) nums
                            FROM t_student_apply sa,
                                 t_student s
                            WHERE sa.student_id = s.id
                              AND sa.cancel = #{cancel}
                              AND apply_task_id = #{taskId}
                            GROUP BY student_id) ts ON ts.student_id = s.id
        WHERE apply_task_id = #{taskId}
          AND s.apply_number - ifnull(ts.nums, 0) > 0
    </select>

    <select id="findInfoById" resultType="com.qmth.exam.reserve.bean.student.StudentInfo">
        select stu.id,
               stu.name,
               stu.student_code,
               stu.identity_number,
               stu.photo_path,
               stu.gender,
               stu.apply_number,
               stu.open_id,
               stu.uid,
               stu.org_id,
               stu.apply_task_id,
               stu.category_id,
               c.name category_name,
               c.self_apply_enable
        from t_student stu
                 left join t_category c on c.id = stu.category_id
        where stu.id = #{studentId}
    </select>

    <select id="listNoFinishCategory" resultType="com.qmth.exam.reserve.bean.stdapply.CategoryVO">
        SELECT DISTINCT s.category_id AS id
        FROM t_category g,
             t_student s
                 LEFT JOIN (SELECT student_id,
                                   count(1) nums
                            FROM t_student_apply sa,
                                 t_student s
                            WHERE sa.student_id = s.id
                              AND sa.cancel = #{cancel}
                              AND apply_task_id = #{taskId}
                            GROUP BY student_id) ts ON ts.student_id = s.id
        WHERE apply_task_id = #{taskId}
          AND g.id = s.category_id
          AND g.ENABLE = 1
          AND s.apply_number - ifnull(ts.nums, 0) > 0
    </select>

    <select id="pageStudent" resultType="com.qmth.exam.reserve.bean.student.StudentVO">
        SELECT
        sc.id,
        s.NAME,
        s.identity_number identityNumber,
        s.student_code studentCode,
        c.NAME teachingName,
        s.apply_number applyNumber,
        sc.course_name courseName,
        sc.course_code courseCode,
        s.photo_path photoPath
        FROM
        t_student s,
        t_category c,
        t_student_course sc
        WHERE
        s.category_id = c.id
        and sc.student_id=s.id
        <if test="req.taskId != null">
            and s.apply_task_id=#{req.taskId}
        </if>
        <if test="req.teachingId != null ">
            and s.category_id=#{req.teachingId}
        </if>
        <if test="req.name != null and req.name !=''">
            and s.name like concat('%', #{req.name}, '%')
        </if>
        <if test="req.identityNumber != null and req.identityNumber !=''">
            and s.identity_number like concat('%', #{req.identityNumber}, '%')
        </if>
        <if test="req.studentCode != null and req.studentCode !=''">
            and s.student_code like concat('%', #{req.studentCode}, '%')
        </if>
        order by s.update_time desc
    </select>

    <select id="listStudent" resultType="com.qmth.exam.reserve.bean.student.StudentExportVO"  parameterType="com.qmth.exam.reserve.bean.student.StudentReq">
        SELECT
        s.NAME,
        s.identity_number identityNumber,
        s.student_code studentCode,
        c.NAME teachingName,
        sc.course_name courseName,
        sc.course_code courseCode
        FROM
        t_student s,
        t_category c,
        t_student_course sc
        WHERE
        s.category_id = c.id
        and sc.student_id=s.id
        <if test="req.taskId != null">
            and s.apply_task_id=#{req.taskId}
        </if>
        <if test="req.teachingId != null ">
            and s.category_id=#{req.teachingId}
        </if>
        <if test="req.name != null and req.name !=''">
            and s.name like concat('%', #{req.name}, '%')
        </if>
        <if test="req.identityNumber != null and req.identityNumber !=''">
            and s.identity_number like concat('%', #{req.identityNumber}, '%')
        </if>
        <if test="req.studentCode != null and req.studentCode !=''">
            and s.student_code like concat('%', #{req.studentCode}, '%')
        </if>
        order by s.update_time desc
    </select>

</mapper>
