<?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.SrsSportTestMapper">
    
    <resultMap type="SrsSportTest" id="SrsSportTestResult">
        <result property="id"    column="id"    />
        <result property="stuId"    column="stu_id"    />
        <result property="testScore"    column="test_score"    />
        <result property="stuYearId"    column="stu_year_id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="stuNo" column="stu_no"/>
        <result property="name" column="name"/>
        <result property="stuYearName" column="stu_year_name"/>
        <result property="className" column="class_name"/>
        <association property="student" javaType="SrsStudent" resultMap="studentJoin" />
    </resultMap>
    <resultMap type="SrsStudent" id="studentJoin">
        <result property="stuId"    column="stu_id"    />
        <result property="name"    column="name"    />
        <result property="stuNo"    column="stu_no"    />
        <result property="gender"    column="gender"    />
        <result property="birthday"    column="birthday"    />
        <result property="idCard"    column="id_card"    />
        <result property="deptId"    column="dept_id"    />
        <result property="majorId"    column="major_id"    />
        <result property="phone"    column="phone"    />
        <result property="address"    column="address"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>
<!--    <resultMap type="SrsSportTestStudentVo" id="SrsSportTestStudentVoS">-->
<!--        <result property="id"    column="id"    />-->
<!--        <result property="stuNo"    column="stu_no"    />-->
<!--        <result property="stuYearId"    column="stu_year_id"    />-->
<!--    </resultMap>-->

    <sql id="selectSrsSportTestVo">
        select id,
            stu_id,
            test_score,
            stu_year_id,
            del_flag,
            create_by,
            create_time,
            update_by,
            update_time
        from srs_sport_test
    </sql>

<!--    <select id="selectSportTestStuNo" resultMap="SrsSportTestStudentVoS">-->
<!--        select sst.stu_id,ss.stu_no,sst.stu_year_id from srs_sport_test as sst-->
<!--        left join srs_student ss on sst.stu_id = ss.stu_id-->
<!--        where ss.stu_id in-->
<!--        <foreach collection="" item="item" index="index" open="(" close=")" separator=",">-->
<!--            #{item}-->
<!--        </foreach>-->
<!--    </select>-->

    <select id="selectSrsSportTestList" parameterType="SrsSportTest" resultMap="SrsSportTestResult">
        <!--<include refid="selectSrsSportTestVo"/>-->
        select a.id,
            a.stu_id,
            a.test_score,
            a.stu_year_id,
            a.del_flag,
            a.create_by,
            a.create_time,
            a.update_by,
            a.update_time
--             b.name,
--             c.stu_year_name,
--             d.class_name
        from srs_sport_test a
        left join srs_student as b on a.stu_id = b.stu_id
        left join srs_stu_year as c on a.stu_year_id = c.id
        left join srs_class as d on b.class_id = d.class_id
        <where>  
            <if test="stuId != null "> and a.stu_id = #{stuId}</if>
            <if test="testScore != null "> and a.test_score = #{testScore}</if>
            <if test="stuYearId != null "> and a.stu_year_id = #{stuYearId}</if>
            <if test="testScoreFrom != null "> and a.test_score &gt;= #{testScoreFrom}</if>
            <if test="testScoreTo != null "> and a.test_score &lt;= #{testScoreTo}</if>
        </where>
        order by a.stu_year_id, d.class_name
    </select>

    <select id="getStuIdByStuNosToInsert" resultType="com.srs.comprehensive.domain.Vo.StuIdNo">
        select stu_id,stu_no from srs_student
        where stu_no in
        <foreach collection="stuNos" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <select id="getStuIdByStuNosToUpdate"  resultType="com.srs.comprehensive.domain.Vo.StuIdNo">
        select a.id as id , b.stu_id as stu_id , b.stu_no as stu_no from srs_sport_test as a
        left join srs_student as b on a.stu_id = b.stu_id
        where b.stu_no in
        <foreach collection="stuNos" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and stu_year_id = #{stuYearId}
    </select>

    <select id="listAllSportInfo" resultType="com.srs.comprehensive.domain.Vo.CphScoreVo">
        select b.stu_no,a.test_score as score from srs_sport_test as a
        left join srs_student as b on a.stu_id = b.stu_id
        where a.stu_year_id = #{stuYearId}
    </select>
    
    <select id="selectSrsSportTestById" parameterType="Long" resultMap="SrsSportTestResult">
        <include refid="selectSrsSportTestVo"/>
        where id = #{id}
    </select>
 <!--根据学生id查询 -->
    <select id="selectSrsSportTestByStuId" parameterType="Long" resultMap="SrsSportTestResult">
        <include refid="selectSrsSportTestVo"/>
        where stu_id = #{stuId}
    </select>

    <insert id="insertSrsSportTest" parameterType="SrsSportTest" useGeneratedKeys="true" keyProperty="id">
        insert into srs_sport_test
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stuId != null">stu_id,</if>
            <if test="stuNo != null">stu_no,</if>
            <if test="testScore != null">test_score,</if>
            <if test="stuYearId != null">stu_year_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stuId != null">#{stuId},</if>
            <if test="stuNo != null">#{stuNo},</if>
            <if test="testScore != null">#{testScore},</if>
            <if test="stuYearId != null">#{stuYearId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateSrsSportTest" parameterType="SrsSportTest">
        update srs_sport_test
        <trim prefix="SET" suffixOverrides=",">
            <if test="stuId != null">stu_id = #{stuId},</if>
            <if test="stuNo != null">stu_no = #{stuNo},</if>
            <if test="testScore != null">test_score = #{testScore},</if>
            <if test="stuYearId != null">stu_year_id = #{stuYearId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateSrsSportTestAllS" parameterType="java.util.List">
        <foreach collection="updateSrsSportTestStudent" item="item" index="index" open="" close="" separator=";">
            update srs_sport_test
            set test_score=${item.testScore}
            where id = ${item.id}
        </foreach>
    </update>
    <update id="updateSrsSportTestAll" parameterType="java.util.List">
        update srs_sport_test
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="test_score=case" suffix="end,">
                <foreach collection="updateSrsSportTestStudent" item="i" index="index">
                    <if test="i.testScore != null ">
                        when id=#{i.id} then #{i.testScore}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="updateSrsSportTestStudent" separator="or" item="i" index="index" >
            id=#{i.id}
        </foreach>
    </update>

    <delete id="deleteSrsSportTestById" parameterType="Long">
        delete from srs_sport_test where id = #{id}
    </delete>

    <delete id="deleteSrsSportTestByIds" parameterType="String">
        delete from srs_sport_test where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
<!--    批量添加体能测试-->
    <insert id="forAddSportTest" parameterType="java.util.List">
        insert into srs_sport_test(stu_id, test_score, stu_year_id)
        values
        <foreach collection="list" item="item" separator=",">
            (
            <if test="item.stuNo != null">
             (select stu_id from srs_student where stu_no= #{item.stuNo}),</if>
            <if test="item.testScore != null">#{item.testScore},</if>
            <if test="item.stuYearId != null">#{item.stuYearId},</if>

            )
        </foreach>
    </insert>
</mapper>