<?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.course.mapper.SchoolCourseOrderMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="queryPage" resultType="cn.iocoder.yudao.module.course.vo.SchoolCourseOrderVO">
        SELECT
        main.*,
        CASE
        WHEN EXISTS (SELECT 1
        FROM drive_school_course_order dsc
        WHERE dsc.old_order_id = main.id and dsc.deleted = 0 and main.deleted = 0)
        THEN TRUE
        ELSE FALSE
        END AS isCreated
        FROM
        drive_school_course_order main
        <where>
        main.deleted = 0
            <if test="entity.orderNo!= null and entity.orderNo != ''"> and main.order_no = #{entity.orderNo}</if>
            <if test="entity.userName != null and entity.userName != ''"> and main.user_name like concat('%', #{entity.userName}, '%')</if>
            <if test="entity.userId != null and entity.userId != ''"> and main.user_id = #{entity.userId}</if>
            <if test="entity.paymentStatus != null and entity.paymentStatus != ''"> and main.payment_status = #{entity.paymentStatus}</if>
            <if test="entity.ifEnd != null and entity.ifEnd != ''"> and main.if_end = #{entity.ifEnd}</if>
            <if test="entity.coachUserName != null and entity.coachUserName != ''"> and main.coach_user_name = #{entity.coachUserName}</if>
            <if test="entity.isSign != null "> and main.is_sign = #{entity.isSign}</if>
            <if test="entity.startTimeStr!=null and entity.startTimeStr!=''">
                AND main.create_time &gt;= #{entity.startTimeStr}
            </if>
            <if test="entity.endTimeStr!=null and entity.endTimeStr!=''">
                AND main.create_time &lt;= #{entity.endTimeStr}
            </if>
            <if test="entity.cashierConfirm != ''">
                <choose>
                    <when test="entity.cashierConfirm == 1">
                        AND main.cashier_confirm = 1
                    </when>
                    <when test="entity.cashierConfirm == 0">
                        AND main.cashier_confirm = 0
                    </when>
                    <when test="entity.cashierConfirm == null">
                        AND main.cashier_confirm IS NULL
                    </when>
                </choose>
            </if>

            <if test="entity.discountIndex != null and entity.discountIndex != ''">
                <choose>
                    <when test="entity.discountIndex == 1">
                        AND main.discount &gt; 0
                    </when>
                    <when test="entity.discountIndex == 0">
                        AND (main.discount = 0 or main.discount IS NULL)
                    </when>
                </choose>
            </if>

        </where>
        order by main.create_time desc
    </select>

    <select id="queryPageNew" resultType="cn.iocoder.yudao.module.course.vo.SchoolCourseOrderVO">
        SELECT
        main.*,
        ds.photo,
        ds.type,
        CASE
        WHEN EXISTS (SELECT 1
        FROM drive_school_course_order dsc
        WHERE dsc.old_order_id = main.id and dsc.deleted = 0 and main.deleted = 0)
        THEN TRUE
        ELSE FALSE
        END AS isCreated
        FROM
        drive_school_course_order main
        LEFT JOIN drive_school_course ds on main.course_id = ds.id and ds.deleted = 0
        <where>
            main.deleted = 0
            <if test="entity.orderNo!= null and entity.orderNo != ''"> and main.order_no = #{entity.orderNo}</if>
            <if test="entity.userName != null and entity.userName != ''"> and main.user_name like concat('%', #{entity.userName}, '%')</if>
            <if test="entity.userId != null and entity.userId != ''"> and main.user_id = #{entity.userId}</if>
            <if test="entity.paymentStatus != null and entity.paymentStatus != ''"> and main.payment_status = #{entity.paymentStatus}</if>
            <if test="entity.ifEnd != null and entity.ifEnd != ''"> and main.if_end = #{entity.ifEnd}</if>
        </where>
        order by main.create_time desc
    </select>

    <select id="selectByCoachUserId" resultType="java.lang.Double">
        SELECT
        SUM(dsco.reserve_money+dsco.rest_money )
        FROM
        drive_school_course_order dsco
        <if test="coachId != null and coachId != ''">
            LEFT JOIN drive_school_student dss ON dsco.user_id = dss.user_id
            AND dss.deleted = 0
        </if>
        WHERE
        dsco.deleted = 0
        AND dsco.payment_status IN ( '2', '3', '4', '5' )
        <if test="coachId != null and coachId != ''">
            AND dss.source_user_id = #{coachId}
        </if>
        <if test="startTime!=null and startTime!=''">
            AND dsco.create_time &gt;= #{startTime}
        </if>
        <if test="endTime!=null and endTime!=''">
            AND dsco.create_time &lt;= #{endTime}
        </if>
    </select>

    <select id="selectByCoachUserIdIsConfirmed" resultType="java.lang.Double">
        SELECT
        SUM(dsco.reserve_money+dsco.rest_money )
        FROM
        drive_school_course_order dsco
        <if test="coachId != null and coachId != ''">
            LEFT JOIN drive_school_student dss ON dsco.user_id = dss.user_id
            AND dss.deleted = 0
        </if>
        WHERE
        dsco.deleted = 0
        AND dsco.payment_status IN ( '2', '3', '4', '5' )
          AND dsco.cashier_confirm = 1
        <if test="coachId != null and coachId != ''">
            AND dss.source_user_id = #{coachId}
        </if>
        <if test="startTime!=null and startTime!=''">
            AND dsco.create_time &gt;= #{startTime}
        </if>
        <if test="endTime!=null and endTime!=''">
            AND dsco.create_time &lt;= #{endTime}
        </if>
    </select>


    <select id="getOrderInfo" resultType="cn.iocoder.yudao.module.course.vo.SchoolCourseOrderVO">
        SELECT *
        FROM drive_school_course_order
        WHERE user_id = #{userId}
          AND payment_status IN (2, 3, 4, 5)
          AND if_end = 0

          AND deleted = 0
        ORDER BY create_time DESC
        LIMIT 1
    </select>
    <!-- AND if_assignment_coach = 1 -->
    <insert id="createStudentUsers" parameterType="cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO system_users (username,
                                  password,
                                  status,
                                  nickname,
                                  user_age,
                                  sex,
                                  mobile,
                                  id_card,
                                  id_card_image,
                                  create_time,
                                  deleted)
        VALUES (#{username},
                #{password},
                #{status},
                #{nickname},
                #{userAge},
                #{sex},
                #{mobile},
                #{idCard},
                #{idCardImage},
                #{createTime},
                #{deleted})

    </insert>

    <select id="getSchemeByStudentId" resultType="string">
        select dsco.scheme_id FROM drive_school_process dsc
        left join drive_school_course_order dsco on dsco.user_id = dsc.user_id
        where
            dsc.user_id=#{studentId}
          AND dsc.subject=#{subject}
          AND dsco.course_id = #{courseId}
          AND dsco.payment_status=2
          AND dsco.if_end=0
          AND dsco.deleted=0
          AND dsc.deleted=0
          AND dsco.if_assignment_coach=1
    </select>

    <select id="getOrderCountBySchemeId" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM drive_school_course_order
        WHERE scheme_id=#{schemeId}
        AND deleted = 0
    </select>

    <!--<select id="getOrderByCoachId" resultType="cn.iocoder.yudao.module.course.vo.SchoolCourseOrderVO">
        SELECT dsco.* FROM drive_school_student dss
        LEFT JOIN drive_school_course_order dsco ON dsco.user_id = dss.user_id AND dsco.deleted = 0
        WHERE dss.deleted = 0
        <if test="entity.coachUserId!= null and entity.coachUserId!= ''">
            AND dss.source_user_id = #{entity.coachUserId}
        </if>
        <if test="entity.source!= null and entity.source!= ''">
            AND dss.source = #{entity.source}
        </if>
        <if test="entity.userName != null and entity.userName != '' ">
            AND dsco.user_name like concat('%', #{entity.userName}, '%')
        </if>
        <if test="entity.startTimeStr!=null and entity.startTimeStr!='' ">
            AND dsco.create_time &gt;= #{entity.startTimeStr}
        </if>
        <if test="entity.endTimeStr!=null and entity.endTimeStr!='' ">
            AND dsco.create_time &lt;= #{entity.endTimeStr}
        </if>
        <choose>
            <when test="sort=='asc'">
                ORDER BY
                dsco.create_time ASC
            </when>
            <otherwise>
                ORDER BY
                dsco.create_time DESC
            </otherwise>
        </choose>
    </select>-->

    <select id="getOrderByCoachId" resultType="cn.iocoder.yudao.module.course.vo.SchoolCourseOrderVO">
        SELECT dsco.* FROM drive_school_course_order dsco
        LEFT JOIN drive_school_student dss ON dsco.user_id = dss.user_id AND dss.deleted = 0
        WHERE dsco.deleted = 0
        <if test="entity.coachUserId!= null and entity.coachUserId!= ''">
            AND dss.source_user_id = #{entity.coachUserId}
        </if>
        <if test="entity.source!= null and entity.source!= ''">
            AND dss.source = #{entity.source}
        </if>
        <if test="entity.userName != null and entity.userName != '' ">
            AND dsco.user_name like concat('%', #{entity.userName}, '%')
        </if>
        <if test="entity.startTimeStr!=null and entity.startTimeStr!='' ">
            AND dsco.create_time &gt;= #{entity.startTimeStr}
        </if>
        <if test="entity.endTimeStr!=null and entity.endTimeStr!='' ">
            AND dsco.create_time &lt;= #{entity.endTimeStr}
        </if>
        <choose>
            <when test="entity.sort=='asc'">
                ORDER BY
                dsco.create_time ASC
            </when>
            <otherwise>
                ORDER BY
                dsco.create_time DESC
            </otherwise>
        </choose>
    </select>

    <select id="getCoachMoney" resultType="cn.iocoder.yudao.module.course.vo.SchoolCourseOrderVO">
        SELECT dsc.user_id AS coachUserId,dsc.name AS coachUserName,
        SUM(COALESCE(dsco.reserve_money, 0) + COALESCE(dsco.rest_money, 0)) AS totalPrice
        FROM drive_school_coach dsc
        LEFT JOIN drive_school_student dss ON dss.source_user_id = dsc.user_id AND dss.deleted = 0
        LEFT JOIN drive_school_course_order dsco ON dss.user_id = dsco.user_id AND dsco.deleted = 0
        WHERE dsc.deleted = 0
        <if test="entity.cashierConfirm != null and entity.cashierConfirm != '' ">
            <choose>
                <when test="entity.cashierConfirm == 0">
                    AND (dsco.cashier_confirm IS NULL OR dsco.cashier_confirm = #{entity.cashierConfirm})
                </when>
                <otherwise>
                    AND dsco.cashier_confirm = #{entity.cashierConfirm}
                </otherwise>
            </choose>
        </if>
        <if test="entity.coachUserId!= null and entity.coachUserId!= ''">
            AND dss.source_user_id = #{entity.coachUserId}
        </if>
        <if test="entity.source!= null and entity.source!= ''">
            AND dss.source = #{entity.source}
        </if>
        <if test="entity.startTimeStr!=null and entity.startTimeStr!='' ">
            AND dsco.create_time &gt;= #{entity.startTimeStr}
        </if>
        <if test="entity.endTimeStr!=null and entity.endTimeStr!='' ">
            AND dsco.create_time &lt;= #{entity.endTimeStr}
        </if>
        GROUP BY dsc.user_id
        ORDER BY totalPrice DESC
    </select>

    <select id="getOrderMoneyByCoachId" resultType="cn.iocoder.yudao.module.course.vo.SchoolCourseOrderVO">
        SELECT dsco.* FROM drive_school_course_order dsco
        LEFT JOIN drive_school_student dss ON dsco.user_id = dss.user_id AND dss.deleted = 0 AND dss.source_user_id IS NOT NULL
        where dsco.deleted = 0 AND dsco.payment_status IN ( '2', '3', '4', '5' ) AND dsco.is_sign = 1
        <if test="entity.coachUserId!=null and entity.coachUserId!='' ">
            AND dss.source_user_id = #{entity.coachUserId}
        </if>
        <if test="entity.cashierConfirm != null and entity.cashierConfirm != '' ">
            <choose>
                <when test="entity.cashierConfirm == 0">
                    AND (dsco.cashier_confirm IS NULL OR dsco.cashier_confirm = #{entity.cashierConfirm})
                </when>
                <otherwise>
                    AND dsco.cashier_confirm = #{entity.cashierConfirm}
                </otherwise>
            </choose>
        </if>
        <if test="entity.startTimeStr!=null and entity.startTimeStr!='' ">
            AND dsco.create_time &gt;= #{entity.startTimeStr}
        </if>
        <if test="entity.endTimeStr!=null and entity.endTimeStr!='' ">
            AND dsco.create_time &lt;= #{entity.endTimeStr}
        </if>
        GROUP BY dss.user_id
        ORDER BY dss.create_time DESC
    </select>

    <select id="getOrderExportData" resultType="cn.iocoder.yudao.module.course.vo.CourseOrderExportVO">
        SELECT
            dsco.user_id AS studentId,
            dsco.user_name AS studentName,
            dsco.user_no AS studentIdCard,
            dsco.user_phone AS studentPhone,
            dsco.create_time AS signUpTime,
            dsco.is_sign,
            dsco.pay_fees_time,
            dsco.reserve_money,
            dsco.course_name,
            dsco.course_type,
            dsco.order_remark,
            dsco.cashier_confirm,
            dsco.cashier_confirm_remark,
            dsco.cashier_confirm_time,
            dss.source,
            dsco.payment_account,
            dsco.sign_time,
            dsco.discount,
            dsco.actual_payment,
        MAX(CASE WHEN dsp.subject = 2 THEN COALESCE(dsp.coach_name, '') END) AS subject2CoachName,
        MAX(CASE WHEN dscd.course_subject = 2 THEN COALESCE(dscd.deduct, 0) END) AS subject2Deduct,
        MAX(CASE WHEN dsp.subject = 3 THEN COALESCE(dsp.coach_name, '') END) AS subject3CoachName,
        MAX(CASE WHEN dscd.course_subject = 3 THEN COALESCE(dscd.deduct, 0) END) AS subject3Deduct
        FROM
            drive_school_course_order dsco
                LEFT JOIN drive_school_process dsp ON dsp.course_id = dsco.course_id AND dsp.user_id = dsco.user_id AND dsp.subject IN (2, 3) AND dsp.deleted = 0
                LEFT JOIN drive_school_course_deduct dscd ON dsco.scheme_id = dscd.scheme_id AND dsco.course_id = dscd.course_id AND dscd.course_subject IN (2, 3) AND dscd.deleted = 0
                LEFT JOIN drive_school_student dss ON dsco.user_id = dss.user_id AND dss.deleted = 0
        WHERE dsco.deleted = 0
        AND dsco.if_end = 0
        <if test="entity.courseId != null and entity.courseId != '' ">
            AND dsco.course_id = #{entity.courseId}
        </if>
        <if test="entity.studentPhone != null and entity.studentPhone != '' ">
            AND dsco.user_phone like concat('%', #{entity.studentPhone}, '%')
        </if>
        <if test="entity.studentName != null and entity.studentName != '' ">
            AND dsco.user_name like concat('%', #{entity.studentName}, '%')
        </if>
        <if test="entity.coachId != null and entity.coachId != '' ">
            AND dsp.coach_id = #{entity.coachId}
        </if>
        <if test="entity.courseType != null and entity.courseType != '' ">
            AND dsco.course_type = #{entity.courseType}
        </if>

        <if test="entity.signUpStartTimeStr!=null and entity.signUpStartTimeStr!='' ">
            AND dsco.create_time &gt;= #{entity.signUpStartTimeStr}
        </if>
        <if test="entity.signUpEndTimeStr!=null and entity.signUpEndTimeStr!='' ">
            AND dsco.create_time &lt;= #{entity.signUpEndTimeStr}
        </if>

        <if test="entity.payFeesStartTimeStr!=null and entity.payFeesStartTimeStr!='' ">
            AND dsco.pay_fees_time &gt;= #{entity.payFeesStartTimeStr}
        </if>
        <if test="entity.payFeesEndTimeStr!=null and entity.payFeesEndTimeStr!='' ">
            AND dsco.pay_fees_time &lt;= #{entity.payFeesEndTimeStr}
        </if>

        <if test="entity.cashierConfirmStartTimeStr!=null and entity.cashierConfirmStartTimeStr!='' ">
            AND dsco.cashier_confirm_time &gt;= #{entity.cashierConfirmStartTimeStr}
        </if>
        <if test="entity.cashierConfirmEndTimeStr!=null and entity.cashierConfirmEndTimeStr!='' ">
            AND dsco.cashier_confirm_time &lt;= #{entity.cashierConfirmEndTimeStr}
        </if>
        <if test="entity.paymentStatus != null and entity.paymentStatus != '' ">
            AND dsco.payment_status = #{entity.paymentStatus}
        </if>
        <!--<if test="entity.cashierConfirm != null and entity.cashierConfirm != '' ">
            AND dsco.cashier_Confirm = #{entity.cashierConfirm}
        </if>-->
        <if test="entity.isSign != null">
            AND dsco.is_sign = #{entity.isSign}
        </if>
        <if test="entity.cashierConfirm != ''">
            <choose>
                <when test="entity.cashierConfirm == 1">
                    AND dsco.cashier_confirm = 1
                </when>
                <when test="entity.cashierConfirm == 0">
                    AND dsco.cashier_confirm = 0
                </when>
                <when test="entity.cashierConfirm == null">
                    AND dsco.cashier_confirm IS NULL
                </when>
            </choose>
        </if>
        <if test="entity.discountIndex != null and entity.discountIndex != ''">
            <choose>
                <when test="entity.discountIndex == 1">
                    AND dsco.discount &gt; 0
                </when>
                <when test="entity.discountIndex == 0">
                    AND (dsco.discount = 0 or dsco.discount IS NULL)
                </when>
            </choose>
        </if>
        <if test="entity.source != null and entity.source != '' ">
            AND dss.source = #{entity.source}
        </if>
        <if test="entity.studentIdCard != null and entity.studentIdCard != '' ">
            <choose>
                <!-- 精确匹配：当输入18位时 -->
                <when test="entity.studentIdCard.length() == 18">
                    AND dsco.id_card = #{entity.studentIdCard}
                </when>
                <!-- 后4位匹配：当输入正好4位时 -->
                <when test="entity.studentIdCard.length() == 4">
                    AND RIGHT(dsco.id_card, 4) = #{entity.studentIdCard}
                </when>
                <!-- 模糊搜索：当输入大于4位但不足18位时 -->
                <otherwise>
                    AND dsco.id_card LIKE concat('%', #{entity.studentIdCard}, '%')
                </otherwise>
            </choose>
        </if>
        GROUP BY
        dsco.user_id,
        dsco.user_name,
        dsco.user_no,
        dsco.user_phone,
        dsco.create_time,
        dsco.is_sign,
        dsco.pay_fees_time,
        dsco.reserve_money,
        dsco.course_name,
        dsco.course_type,
        dsco.order_remark,
        dsco.cashier_confirm,
        dsco.cashier_confirm_remark,
        dsco.cashier_confirm_time,
        dss.source,
        dsco.payment_account,
        dsco.sign_time
        ORDER BY dsco.create_time DESC, dsco.user_id
    </select>




    <select id="getOrderStatistics" resultType="cn.iocoder.yudao.module.course.vo.CourseOrderStatisticsVO">
        SELECT
        COUNT(DISTINCT dss.user_id) AS studentCount,
        SUM(dsco.reserve_money) AS totalAmount,
        SUM(dsco.discount) AS totalDiscount,
        SUM(dsco.actual_payment) AS totalActualPayment,
        SUM(IFNULL(sub.subject2Deduct, 0)) AS subject2DeductTotal,
        SUM(IFNULL(sub.subject3Deduct, 0)) AS subject3DeductTotal
        FROM drive_school_student dss
        LEFT JOIN drive_school_course_order dsco
        ON dss.user_id = dsco.user_id
        AND dsco.deleted = 0
        AND dsco.if_end = 0
        LEFT JOIN (
        SELECT
        dscd.course_id,
        dscd.scheme_id,
        SUM(CASE WHEN dscd.course_subject = '2' THEN COALESCE(dscd.deduct, 0) ELSE 0 END) AS subject2Deduct,
        SUM(CASE WHEN dscd.course_subject = '3' THEN COALESCE(dscd.deduct, 0) ELSE 0 END) AS subject3Deduct
        FROM drive_school_course_deduct dscd
        WHERE dscd.deleted = 0
        GROUP BY dscd.course_id, dscd.scheme_id
        ) sub ON (
        (dsco.scheme_id IS NOT NULL AND dsco.scheme_id = sub.scheme_id AND dsco.course_id = sub.course_id)
        OR
        (
        dsco.scheme_id IS NULL AND EXISTS (
        SELECT 1 FROM drive_school_course_scheme s
        WHERE s.course_id = dsco.course_id AND s.is_default = 1 AND s.id = sub.scheme_id AND s.deleted = 0
        )
        )
        )
        WHERE dss.deleted = 0
        AND dsco.user_id IS NOT NULL
        AND dsco.is_sign = 1

        <!-- 筛选条件部分 -->
        <if test="entity.studentName != null and entity.studentName != '' ">
            AND dss.name LIKE CONCAT('%', #{entity.studentName}, '%')
        </if>
        <if test="entity.studentPhone != null and entity.studentPhone != '' ">
            AND dss.phone LIKE CONCAT('%', #{entity.studentPhone}, '%')
        </if>
        <if test="entity.source != null and entity.source != '' ">
            AND dss.source = #{entity.source}
        </if>
        <if test="entity.studentIdCard != null and entity.studentIdCard != '' ">
            <choose>
                <when test="entity.studentIdCard.length() == 18">
                    AND dss.id_card = #{entity.studentIdCard}
                </when>
                <when test="entity.studentIdCard.length() == 4">
                    AND RIGHT(dss.id_card, 4) = #{entity.studentIdCard}
                </when>
                <otherwise>
                    AND dss.id_card LIKE CONCAT('%', #{entity.studentIdCard}, '%')
                </otherwise>
            </choose>
        </if>
        <if test="entity.courseId != null and entity.courseId != '' ">
            AND dsco.course_id = #{entity.courseId}
        </if>
        <if test="entity.coachId != null and entity.coachId != '' ">
            AND dsco.coach_user_id = #{entity.coachId}
        </if>
        <if test="entity.courseType != null and entity.courseType != '' ">
            AND dsco.course_type = #{entity.courseType}
        </if>
        <if test="entity.signUpStartTimeStr != null and entity.signUpStartTimeStr != '' ">
            AND dsco.create_time &gt;= #{entity.signUpStartTimeStr}
        </if>
        <if test="entity.signUpEndTimeStr != null and entity.signUpEndTimeStr != '' ">
            AND dsco.create_time &lt;= #{entity.signUpEndTimeStr}
        </if>
        <if test="entity.payFeesStartTimeStr != null and entity.payFeesStartTimeStr != '' ">
            AND dsco.pay_fees_time &gt;= #{entity.payFeesStartTimeStr}
        </if>
        <if test="entity.payFeesEndTimeStr != null and entity.payFeesEndTimeStr != '' ">
            AND dsco.pay_fees_time &lt;= #{entity.payFeesEndTimeStr}
        </if>
        <if test="entity.cashierConfirmStartTimeStr != null and entity.cashierConfirmStartTimeStr != '' ">
            AND dsco.cashier_confirm_time &gt;= #{entity.cashierConfirmStartTimeStr}
        </if>
        <if test="entity.cashierConfirmEndTimeStr != null and entity.cashierConfirmEndTimeStr != '' ">
            AND dsco.cashier_confirm_time &lt;= #{entity.cashierConfirmEndTimeStr}
        </if>
        <if test="entity.paymentStatus != null and entity.paymentStatus != '' ">
            AND dsco.payment_status = #{entity.paymentStatus}
        </if>
        <if test="entity.isSign != null">
            AND dsco.is_sign = #{entity.isSign}
        </if>
        <if test="entity.cashierConfirm != ''">
            <choose>
                <when test="entity.cashierConfirm == 1">
                    AND dsco.cashier_confirm = 1
                </when>
                <when test="entity.cashierConfirm == 0">
                    AND dsco.cashier_confirm = 0
                </when>
                <when test="entity.cashierConfirm == null">
                    AND dsco.cashier_confirm IS NULL
                </when>
            </choose>
        </if>
        <if test="entity.discountIndex != null and entity.discountIndex != ''">
            <choose>
                <when test="entity.discountIndex == 1">
                    AND dsco.discount &gt; 0
                </when>
                <when test="entity.discountIndex == 0">
                    AND (dsco.discount = 0 or dsco.discount IS NULL)
                </when>
            </choose>
        </if>
    </select>


    <select id="getMoneyInfoOnFinance" resultType="cn.iocoder.yudao.module.course.vo.FinanceInfoOnCoachAndStudent">
        SELECT
            dsc.user_id AS coachId,
            dsc.name AS coachName,
            COUNT(DISTINCT dss.id) AS totalStudents,
            SUM(CASE WHEN dsco.course_type = 'C1' THEN 1 ELSE 0 END) AS c1Count,
            SUM(CASE WHEN dsco.course_type = 'C2' THEN 1 ELSE 0 END) AS c2Count,
            SUM(dsco.reserve_money + dsco.rest_money) AS totalMoney
        FROM drive_school_coach dsc
        LEFT JOIN drive_school_student dss ON dss.source_user_id = dsc.user_id
                AND dss.deleted = 0
                AND dss.source IN ('02', '03', '04')
        LEFT JOIN  drive_school_course_order dsco ON dsco.user_id = dss.user_id
                AND dsco.deleted = 0
                AND dsco.if_end = 0
                AND dsco.is_sign = 1
        WHERE
            dsc.deleted = 0
        <if test="entity.coachId != null and entity.coachId != '' ">
            AND dsc.user_id = #{entity.coachId}
        </if>
        <if test="entity.startTime != null and entity.startTime != '' ">
            AND dsco.create_time &gt;= #{entity.startTime}
        </if>
        <if test="entity.endTime != null and entity.endTime != '' ">
            AND dsco.create_time &lt;= #{entity.endTime}
        </if>
        GROUP BY
            dsc.user_id, dsc.name
        ORDER BY
        totalStudents DESC;
    </select>


    <select id="getFinanceStudentByCoachId" resultType="cn.iocoder.yudao.module.course.vo.FinanceInfoOnCoachAndStudent">
        SELECT
            dss.user_id AS studentId,
            dss.avatar,
            dss.name AS studentName,
            dss.phone,
            dsco.sign_time
        FROM drive_school_student dss
        LEFT JOIN drive_school_coach dsc ON dss.source_user_id = dsc.user_id AND dsc.deleted = 0
        LEFT JOIN drive_school_course_order dsco ON dsco.user_id = dss.user_id
                AND dsco.deleted = 0
                AND dsco.if_end = 0
                AND dsco.is_sign = 1
        where dss.deleted = 0
            AND dss.source IN ('02', '03', '04')
            AND dsc.user_id = #{entity.coachId}
        <if test="entity.startTime != null and entity.startTime != '' ">
            AND dsco.create_time &gt;= #{entity.startTime}
        </if>
        <if test="entity.endTime != null and entity.endTime != '' ">
            AND dsco.create_time &lt;= #{entity.endTime}
        </if>
        <if test="entity.courseType != null and entity.courseType != '' ">
            AND dsco.course_type = #{entity.courseType}
        </if>
    </select>
</mapper>
