<?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.miaxis.newgzpt.mapper.GzptTrainRecordMapper">
    <select id="saveGzptTrainRecord" parameterType="com.miaxis.newgzpt.domain.GzptTrainRecord">
        insert into GZPT_TRAIN_RECORD VALUES (
        seq_gzpt_train_record.nextval,
        #{studentId},
        #{beginTime},
        #{endTime},
        #{trainType},
        #{trainTime},
        #{isSubmit},
        #{pxkm},
        #{source},
        #{reason},
        #{classid})
    </select>



    <select id="getTrainFinishNew" resultType="com.miaxis.newgzpt.vo.TrainFinishVO" parameterType="com.miaxis.newgzpt.dto.TrainFinishDTO" >
        select cri_id id,cri_starttime startTime, cri_endtime endTime,cri_vaild_time duration,cri_vaild_time vaildTime, cri_train_subject trainSubject, substr(cri_subjcode,0,1) as pxkmType ,c.tci_name coachName,c.tci_photo_path photoPath,c.tci_id coachId,r.cri_classid classId from tms_train_class_record@tms${dqbh} r,tms_coach_info@tms${dqbh} c
        where r.cri_coach_id = c.tci_id(+) and r.cri_audit_result = '0'
        and r.cri_is_pay in (1,2)
        <if test="studentId!=null">
            and r.cri_student_id = #{studentId}
        </if>
        <if test="trainSubject!=null">
            and r.cri_train_subject = #{trainSubject}
        </if>
    </select>


    <select id="getTrainSubjectCredit" resultType="com.miaxis.newgzpt.vo.TrainCreditVO" parameterType="com.miaxis.newgzpt.dto.TrainCreditDTO" >
        select tsc_train_subject         subject,
               tsc_APPLY_EXAM_SUBJECT    applySubject,
               tsc_train_subject_name    subjectName,
               tsc_permit_drive_car_type carType,
               tsc_credit_ration         ration
        from TMS_TRAIN_SUBJECT_CREDIT@tms${dqbh} c
        <where>
            <if test="cx!=null">
                and c.TSC_PERMIT_DRIVE_CAR_TYPE = #{cx}
            </if>
        </where>
    </select>

    <select id="getTrainFinishKm" resultType="com.miaxis.newgzpt.vo.TrainFinishKmVO" parameterType="com.miaxis.newgzpt.dto.TrainFinishDTO" >
        select
        (select sum(cri_vaild_time)  from tms_train_class_record@tms${dqbh} r where r.cri_audit_result = '0' and r.cri_is_pay in (1, 2) and r.cri_train_subject = 1
        <choose>
            <when test="studentId!=null">
                and r.cri_student_id = #{studentId}) km1,
            </when>
            <otherwise>
                ) km1,
            </otherwise>
        </choose>
        (select sum(cri_vaild_time) from tms_train_class_record@tms${dqbh} r where r.cri_audit_result = '0' and r.cri_is_pay in (1, 2) and r.cri_train_subject = 2
        <choose>
            <when test="studentId!=null">
                and r.cri_student_id = #{studentId}) km2,
            </when>
            <otherwise>
                ) km2,
            </otherwise>
        </choose>
        (select sum(cri_vaild_time)  from tms_train_class_record@tms${dqbh} r where r.cri_audit_result = '0' and r.cri_is_pay in (1, 2) and r.cri_train_subject = 3
        <choose>
            <when test="studentId!=null">
                and r.cri_student_id = #{studentId}) km3,
            </when>
            <otherwise>
                ) km3,
            </otherwise>
        </choose>
        (select sum(cri_vaild_time)  from tms_train_class_record@tms${dqbh} r where r.cri_audit_result = '0' and r.cri_is_pay in (1, 2) and r.cri_train_subject = 4
        <choose>
            <when test="studentId!=null">
                and r.cri_student_id = #{studentId}) km4
            </when>
            <otherwise>
                ) km4
            </otherwise>
        </choose>
        from dual
    </select>


</mapper>
