<?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="cn.iocoder.yudao.module.jx.mapper.DriveSchoolReservationCourseMapper">

    <resultMap type="cn.iocoder.yudao.module.jx.domain.DriveSchoolReservationCourse" id="DriveSchoolReservationCourseResult">
        <result property="id"    column="id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="orderTime"    column="order_time"    />
        <result property="timePeriod"    column="time_period"    />
        <result property="coachId"    column="coach_id"    />
        <result property="coachName"    column="coach_name"    />
        <result property="stuSignState"    column="stu_sign_state"    />
        <result property="coachSignState"    column="coach_sign_state"    />
        <result property="stuSigninTime"    column="stu_signin_time"    />
        <result property="stuSignoutTime"    column="stu_signout_time"    />
        <result property="coachSigninTime"    column="coach_signin_time"    />
        <result property="coachSignoutTime"    column="coach_signout_time"    />
        <result property="driveTime"    column="drive_time"    />
        <result property="allDriveTime"    column="allDriveTime"    />
        <result property="type"    column="type"    />
        <result property="auditStatus"    column="audit_status"    />
        <result property="accountType"    column="account_type"    />
        <result property="createTime"    column="create_time"    />
        <result property="creator"    column="creator"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updater"    column="updater"    />
    </resultMap>

    <sql id="selectDriveSchoolReservationCourseVo">
        select id, dept_id, user_id,type, user_name,drive_time,stu_sign_state,coach_sign_state,stu_signin_time, stu_signout_time,coach_signin_time,coach_signout_time,order_time, time_period, coach_id, coach_name, account_type, create_time, creator, update_time, updater from drive_school_reservation_course
    </sql>

    <select id="selectDriveSchoolReservationCourseList" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolReservationCourse" resultMap="DriveSchoolReservationCourseResult">
        <include refid="selectDriveSchoolReservationCourseVo"/>
        <where>
            deleted = 0
            <if test="entity.deptId != null "> and dept_id = #{entity.deptId}</if>
            <if test="entity.userId != null "> and user_id = #{entity.userId}</if>
            <if test="entity.userName != null  and entity.userName != ''"> and user_name like concat('%', #{entity.userName}, '%')</if>
            <if test="entity.orderTime != null "> and order_time = #{entity.orderTime}</if>
            <if test="entity.timePeriod != null  and entity.timePeriod != ''"> and time_period = #{entity.timePeriod}</if>
            <if test="entity.coachId != null "> and coach_id = #{entity.coachId}</if>
            <if test="entity.coachName != null  and entity.coachName != ''"> and coach_name like concat('%', #{entity.coachName}, '%')</if>
            <if test="entity.accountType != null  and entity.accountType != ''"> and account_type = #{entity.accountType}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectDriveSchoolReservationCourseById" parameterType="Long" resultMap="DriveSchoolReservationCourseResult">
        <include refid="selectDriveSchoolReservationCourseVo"/>
        where id = #{id}
    </select>
    <select id="findCourseByCurrentTimeAndJlName"
            resultType="cn.iocoder.yudao.module.jx.domain.DriveSchoolReservationCourse">
        select * from drive_school_reservation_course dsrc
            join drive_school_coach dsc on dsrc.coach_name = dsc.instructor_name where dsrc.deleted = 0 and dsrc.order_time like concat('%', #{currentDate}, '%') and dsc.id = #{jlId}
    </select>
    <select id="findCourseByUserId"
            resultType="cn.iocoder.yudao.module.jx.domain.DriveSchoolReservationCourse">
        select * from drive_school_reservation_course where deleted = 0 and user_id = #{userId}
    </select>

    <insert id="insertDriveSchoolReservationCourse" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolReservationCourse">
        insert into drive_school_reservation_course
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="orderTime != null">order_time,</if>
            <if test="timePeriod != null">time_period,</if>
            <if test="coachId != null">coach_id,</if>
            <if test="coachName != null">coach_name,</if>
            <if test="accountType != null">account_type,</if>
            <if test="type != null">type,</if>
            <if test="createTime != null">create_time,</if>
            <if test="creator != null">creator,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updater != null">updater,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="orderTime != null">#{orderTime},</if>
            <if test="timePeriod != null">#{timePeriod},</if>
            <if test="coachId != null">#{coachId},</if>
            <if test="coachName != null">#{coachName},</if>
            <if test="accountType != null">#{accountType},</if>
            <if test="type != null">#{type},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="creator != null">#{creator},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updater != null">#{updater},</if>
         </trim>
    </insert>
    <insert id="insertBookingInfo">
        insert into drive_school_reservation_course (user_id, order_time, time_period, coach_name, create_time) values (#{userId}, #{currentDate}, #{period}, #{instructorName}, now())
    </insert>

    <update id="updateDriveSchoolReservationCourse" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolReservationCourse">
        update drive_school_reservation_course
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="stuSignState != null">stu_sign_state = #{stuSignState},</if>
            <if test="coachSignState != null">coach_sign_state = #{coachSignState},</if>
            <if test="stuSigninTime != null">stu_signin_time = #{stuSigninTime},</if>
            <if test="stuSignoutTime != null">stu_signout_time = #{stuSignoutTime},</if>
            <if test="coachSigninTime != null">coach_signin_time = #{coachSigninTime},</if>
            <if test="coachSignoutTime != null">coach_signout_time = #{coachSignoutTime},</if>
            <if test="driveTime != null">drive_time = #{driveTime},</if>
            <if test="orderTime != null">order_time = #{orderTime},</if>
            <if test="timePeriod != null">time_period = #{timePeriod},</if>
            <if test="coachId != null">coach_id = #{coachId},</if>
            <if test="coachName != null">coach_name = #{coachName},</if>
            <if test="accountType != null">account_type = #{accountType},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="creator != null">creator = #{creator},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updater != null">updater = #{updater},</if>
            <if test="auditStatus != null">audit_status = #{auditStatus},</if>
            <if test="refuseReason != null">refuse_reason = #{refuseReason},</if>
        </trim>
        where id = #{id}
    </update>
    <update id="updateCourseById">
        update drive_school_reservation_course set coach_name = #{coachName} where id = #{id}
    </update>

    <delete id="deleteDriveSchoolReservationCourseById" parameterType="Long">
        delete from drive_school_reservation_course where id = #{id}
    </delete>

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

    <select id="getReservationNumber"
            resultType="Integer">
        select count(1) number from drive_school_reservation_course where deleted = 0 and dept_id = #{deptId} and account_type =  '2'
    </select>

    <select id="getReservationNumberThree"
            resultType="Integer">
        select count(1) number from drive_school_reservation_course where deleted = 0 and dept_id = #{deptId} and account_type =  '3'
    </select>

    <select id="getIsRecdNumber"
            resultType="Integer">
        select count(1) number from drive_school_pay where deleted = 0 and jxId = #{deptId} and state = '3' or jxId = #{deptId} and state = '0'
    </select>

    <select id="getPaydNumber"
            resultType="Integer">
        select count(1) number from drive_school_pay where deleted = 0 and jxId = #{deptId} and state = '3'
    </select>

    <select id="getSignedNumber"
            resultType="Integer">
        select count(1) number from drive_school_pay where deleted = 0 and jxId = #{deptId} and isSign = '1' and state = '3'
    </select>

    <select id="getReservationCourseList"
            resultType="cn.iocoder.yudao.module.jx.domain.DriveSchoolReservationCourse">
        select * from drive_school_reservation_course
                         where deleted = 0 and dept_id = #{deptId}
    </select>


    <select id="selectDriveSchoolReservationGetListUser" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolReservationCourse" resultMap="DriveSchoolReservationCourseResult">
        <include refid="selectDriveSchoolReservationCourseVo"/>
        <where>
            deleted = 0
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="orderTime != null "> and order_time = #{orderTime}</if>
            <if test="timePeriod != null  and timePeriod != ''"> and time_period = #{timePeriod}</if>
            <if test="coachId != null "> and coach_id = #{coachId}</if>
            <if test="coachName != null  and coachName != ''"> and coach_name like concat('%', #{coachName}, '%')</if>
            <if test="accountType != null  and accountType != ''"> and account_type = #{accountType}</if>
        </where>
        order by create_time desc
    </select>


    <select id="getDriveTime" parameterType="Long" resultMap="DriveSchoolReservationCourseResult">
        SELECT
        SUM(drive_time) allDriveTime
        FROM
        drive_school_reservation_course
        <where>
            deleted = 0
            <if test="userId != null "> and dept_id = #{deptId}</if>
        </where>
    </select>

    <select id="getOftenDriveStudents" parameterType="Long" resultMap="DriveSchoolReservationCourseResult">
        SELECT *
        FROM drive_school_reservation_course
        <where>
            deleted = 0
            create_time >= NOW() - INTERVAL 1 MONTH
            <if test="userId != null "> and coach_id = #{userId}</if>
        </where>
        GROUP BY user_id
        HAVING  COUNT(1) >= 2;
    </select>

    <select id="getReservationCourseListByUserId" parameterType="Long" resultMap="DriveSchoolReservationCourseResult">
        SELECT *
        FROM drive_school_reservation_course
        <where>
            deleted = 0
            <if test="userId != null "> and user_id = #{userId}</if>
        </where>
order by create_time desc
    </select>

    <select id="getAppReservationCourseListByUserId" resultMap="DriveSchoolReservationCourseResult">
        SELECT *
        FROM drive_school_reservation_course
        <where>
            deleted = 0 and
            audit_status = 0
            <if test="userId != null "> and coach_id = #{userId}</if>
        </where>
        order by audit_status,create_time desc
    </select>

    <select id="getAppReservationCourseListByUserIds" resultMap="DriveSchoolReservationCourseResult">
        SELECT *
        FROM drive_school_reservation_course
        <where>
            deleted = 0 and
            audit_status != 0
            <if test="userId != null "> and coach_id = #{userId}</if>
        </where>
        order by audit_status,create_time desc
    </select>

    <select id="getReservationNumberByUserId" resultType="Integer">
        select distinct count(1) number from drive_school_reservation_course where deleted = 0 and coach_id = #{userId} and stu_sign_state !='2' or  coach_id = #{userId} and coach_sign_state !='2'
    </select>
    <select id="queryUserRecord" resultType="cn.iocoder.yudao.module.jx.domain.DriveSchoolReservationCourse">
        SELECT dsrc.*,dsc.type as courseType,
        CASE status
        WHEN 0 THEN '已拒绝'
        WHEN 1 THEN '已审核'
        ELSE '待审核'
        END AS statusStr,
        CASE subject
        WHEN 1 THEN '科目一'
        WHEN 2 THEN '科目二'
        WHEN 3 THEN '科目三'
        WHEN 4 THEN '科目四'
        ELSE '未知'
        END AS subjectStr
        FROM drive_school_reservation_course dsrc
        LEFT JOIN drive_school_course dsc
        ON dsrc.course_id = dsc.id
        <where>
            dsrc.deleted = 0
            <if test="entity.userId != null ">and dsrc.user_id = #{entity.userId}</if>
            <if test="entity.coachId != null ">and dsrc.coach_id = #{entity.coachId}</if>
            <if test="entity.ifCancel != null ">and dsrc.if_cancel = #{entity.ifCancel}</if>
        </where>
        order by dsrc.create_time desc
    </select>


    <update id="updateDriveSchoolReservationCourseByCoach" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolReservationCourse">
        update drive_school_reservation_course
        <trim prefix="SET" suffixOverrides=",">
            <if test="refuseReason != null">refuse_reason = #{refuseReason},</if>
            <if test="auditStatus != null">audit_status = #{auditStatus},</if>
        </trim>
        where id = #{id}
    </update>

</mapper>
