<?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.CphClasstwoMapper">
    
    <resultMap type="CphClasstwo" id="CphClasstwoResult">
        <result property="id"    column="id"    />
        <result property="submitter"    column="submitter"    />
        <result property="stuYearId"    column="stu_year_id"    />
        <result property="toAdd"    column="to_add"    />
        <result property="material"    column="material"    />
        <result property="classtwoScore"    column="classtwo_score"    />
        <result property="ruleId"    column="rule_id"    />
        <result property="description"    column="description"    />
        <result property="auditStatus"    column="audit_status"    />
        <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="finalScore"  column="final_score"/>

        <association property="rules"    column="to_add" javaType="CphRules" resultMap="CphRulesResult" />
        <association property="stuYearS"    column="stu_year_id" javaType="srsStuYear" resultMap="SrsStuYearResult" />
        <association property="user"    column="submitter" javaType="sysUser" resultMap="SysUserResult" />
        <association property="student" javaType="SrsStudent" resultMap="studentJoin" />
    </resultMap>
    <resultMap type="SrsStuYear" id="SrsStuYearResult">
        <result property="id"    column="id"    />
        <result property="stuYearName"    column="stu_year_name"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
    </resultMap>
    <resultMap type="CphRules" id="CphRulesResult">
        <result property="ruleId"    column="rule_id"    />
        <result property="ruleName"    column="rule_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="maxScore"    column="max_score"    />
        <result property="minScore"    column="min_score"    />
    </resultMap>
    <resultMap type="SysUser" id="SysUserResult">
    <id     property="userId"       column="user_id"      />
    <result property="deptId"       column="dept_id"      />
    <result property="userName"     column="user_name"    />
    <result property="nickName"     column="nick_name"    />
    </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"    />
        <association property="srsClass" javaType="SrsClass" resultMap="SrsClassResult" />
    </resultMap>
    <resultMap type="SrsClass" id="SrsClassResult">
        <result property="classId"    column="class_id"    />
        <result property="classCode"    column="class_code"    />
        <result property="majorId"    column="major_id"    />
        <result property="className"    column="class_name"    />
        <result property="gradeId"    column="grade_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="teacherId" column="teacher_id"/>
        <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"    />
    </resultMap>

    <sql id="selectCphClasstwoVo">
        select id, submitter, stu_year_id, to_add, material,classtwo_score,rule_id, description, audit_status, del_flag, create_by, create_time, update_by, update_time from cph_classtwo
    </sql>

    <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 cph_classtwo as a
        left join srs_student as b on a.to_add = 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="selectCphClasstwoList" parameterType="CphClasstwo" resultMap="CphClasstwoResult">
        <include refid="selectCphClasstwoVo"/>
        <where>  
            <if test="submitter != null "> and submitter = #{submitter}</if>
            <if test="stuYearId != null "> and stu_year_id = #{stuYearId}</if>
            <if test="toAdd != null "> and to_add = #{toAdd}</if>
            <if test="material != null  and material != ''"> and material = #{material}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="auditStatus != null  and auditStatus != ''"> and audit_status = #{auditStatus}</if>
        </where>
    </select>
    <sql id="selectCphClasstwoJoin">
        select ct.id, ct.submitter, ct.stu_year_id, ct.to_add, ct.material,
               ct.classtwo_score,ct.rule_id, ct.description, ct.audit_status,
               ct.del_flag, ct.create_by,
               ct.create_time, ct.update_by, ct.update_time,r.*,sy.*,u.*,stu.*,sc.*,cts.classtwo_score as final_score

        from cph_classtwo ct
             left join cph_rules r on r.rule_id = ct.rule_id
              left join srs_stu_year sy on sy.id = ct.stu_year_id
            left join sys_user u on  u.user_id = ct.submitter
             left join srs_student stu on stu.stu_id = ct.to_add
            left join srs_class sc on stu.class_id=sc.class_id
            left join cph_total_score as cts on stu.stu_no =  cts.stu_no and ct.stu_year_id = cts.stu_year_id
    </sql>
<!--    第二课堂连表查询-->
    <select id="selectCphClasstwoListJoin" parameterType="CphClasstwo" resultMap="CphClasstwoResult">
        <include refid="selectCphClasstwoJoin"/>
        <where>
            <if test="submitter != null "> and ct.submitter = #{submitter}</if>
            <if test="stuYearId != null "> and ct.stu_year_id = #{stuYearId}</if>
            <if test="toAdd != null "> and ct.to_add = #{toAdd}</if>
            <if test="material != null  and material != ''"> and ct.material = #{material}</if>
            <if test="description != null  and description != ''"> and ct.description = #{description}</if>
            <if test="auditStatus != null  and auditStatus != ''"> and ct.audit_status = #{auditStatus}</if>
            <if test="student.classId != null "> and stu.class_id = #{student.classId}</if>
            <if test="student.stuNo != null and student.stuNo!=''"> and stu.stu_no = #{student.stuNo}</if>
            <if test="student.name != null "> and stu.name like #{student.name}</if>
        </where>
    </select>

    <select id="listAllSportInfo" resultType="com.srs.comprehensive.domain.Vo.CphScoreVo">
        select b.stu_no,a.classtwo_score as score from cph_classtwo as a
        left join srs_student as b on a.to_add = b.stu_id
        where a.stu_year_id = #{stuYearId}
    </select>
    
    <select id="selectCphClasstwoById" parameterType="Long" resultMap="CphClasstwoResult">
        <include refid="selectCphClasstwoVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertCphClasstwo" parameterType="CphClasstwo" useGeneratedKeys="true" keyProperty="id">
        insert into cph_classtwo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="submitter != null">submitter,</if>
            <if test="stuYearId != null">stu_year_id,</if>
            <if test="toAdd != null">to_add,</if>
            <if test="classtwoScore != null">classtwo_score,</if>
            <if test="ruleId != null">rule_id,</if>
            <if test="material != null">material,</if>
            <if test="description != null">description,</if>
            <if test="auditStatus != null and auditStatus != ''">audit_status,</if>
            <if test="delFlag != null and delFlag != ''">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="id != null">#{id},</if>
            <if test="submitter != null">#{submitter},</if>
            <if test="stuYearId != null">#{stuYearId},</if>
            <if test="toAdd != null">#{toAdd},</if>
            <if test="classtwoScore != null">#{classtwoScore},</if>
            <if test="ruleId != null">#{ruleId},</if>
            <if test="material != null">#{material},</if>
            <if test="description != null">#{description},</if>
            <if test="auditStatus != null and auditStatus != ''">#{auditStatus},</if>
            <if test="delFlag != null and delFlag != ''">#{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="updateCphClasstwo" parameterType="CphClasstwo">
        update cph_classtwo
        <trim prefix="SET" suffixOverrides=",">
            <if test="submitter != null">submitter = #{submitter},</if>
            <if test="stuYearId != null">stu_year_id = #{stuYearId},</if>
            <if test="toAdd != null">to_add = #{toAdd},</if>
            <if test="material != null">material = #{material},</if>
            <if test="classtwoScore != null">classtwo_score = #{classtwoScore},</if>
            <if test="ruleId != null">rule_id = #{ruleId},</if>
            <if test="description != null">description = #{description},</if>
            <if test="auditStatus != null and auditStatus != ''">audit_status = #{auditStatus},</if>
            <if test="delFlag != null and delFlag != ''">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="updateCphClasstwoList" parameterType="java.util.List">
        <foreach collection="needUpdateQuery" item="item" index="index" open="" close="" separator=";">
            update cph_classtwo
            <set>
                classtwo_score=${item.classtwoScore}
            </set>
            where id = ${item.id}
        </foreach>
    </update>

    <delete id="deleteCphClasstwoById" parameterType="Long">
        delete from cph_classtwo where id = #{id}
    </delete>

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


</mapper>