<?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.SchoolCommissionMapper">

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

    <select id="queryPage" resultType="cn.iocoder.yudao.module.course.vo.SchoolCommissionVO">
        SELECT
        main.*,
        dsc.name AS courseName,
        dsc.type AS courseType
        FROM
        drive_school_commission main
        LEFT JOIN drive_school_course dsc ON main.course_id = dsc.id AND dsc.deleted = 0
        <where>
            main.deleted = 0
            <if test="entity.coachName != null and entity.coachName != '' ">and main.coach_name like concat('%',
                #{entity.coachName}, '%')
            </if>
            <if test="entity.studentName != null and entity.studentName != '' ">and main.student_name like concat('%',
                #{entity.studentName}, '%')
            </if>
            <if test="entity.checkName != null and entity.checkName != '' ">and main.check_name like concat('%',
                #{entity.checkName}, '%')
            </if>
            <if test="entity.courseName != null and entity.courseName != '' ">and dsc.name like concat('%',
                #{entity.courseName}, '%')
            </if>
        </where>
        order by main.create_time desc
    </select>
    <select id="selectByCoachId" resultType="cn.iocoder.yudao.module.course.entity.SchoolCommission">
        SELECT
        dsc.*
        FROM
        drive_school_commission dsc
        WHERE
        dsc.deleted = 0
        <if test="coachId != null and coachId != ''">
            AND dsc.coach_user_id = #{coachId}
        </if>
        <if test="startTime!=null and startTime!=''">
            AND dsc.create_time &gt;= #{startTime}
        </if>
        <if test="endTime!=null and endTime!=''">
            AND dsc.create_time &lt;= #{endTime}
        </if>
        ORDER BY dsc.create_time DESC
    </select>

    <select id="getSchoolCommissionById" resultType="cn.iocoder.yudao.module.course.vo.SchoolCommissionVO">
        SELECT
        main.*,
        dsc.name AS courseName,
        dsc.type AS courseType
        FROM
        drive_school_commission main
        LEFT JOIN drive_school_course dsc ON main.course_id = dsc.id AND dsc.deleted = 0
        WHERE main.deleted = 0 AND main.id = #{id}
        order by main.create_time desc
    </select>


    <select id="getCoachTotalCommission" resultType="cn.iocoder.yudao.module.course.vo.SchoolCommissionVO">
        SELECT coach_user_id,coach_name, SUM(commission_amount) AS commissionAmount FROM drive_school_commission
        WHERE deleted = 0
        <if test="entity.coachUserId!=null and entity.coachUserId!=''">
          AND coach_user_id = #{entity.coachUserId}
        </if>
        <if test="entity.ifPay!=null and entity.ifPay!=''">
            AND if_pay = #{entity.ifPay}
        </if>
        <if test="entity.startTimeStr!=null and entity.startTimeStr!='' ">
            AND create_time &gt;= #{entity.startTimeStr}
        </if>
        <if test="entity.endTimeStr!=null and entity.endTimeStr!='' ">
            AND create_time &lt;= #{entity.endTimeStr}
        </if>
        GROUP BY coach_user_id
    </select>

    <select id="getCommissionListByCoachId" resultType="cn.iocoder.yudao.module.course.vo.SchoolCommissionVO">
        SELECT course.name AS courseName, course.type AS courseType, dsc.* FROM drive_school_commission dsc
                 LEFT JOIN drive_school_course course ON dsc.course_id = course.id AND course.deleted = 0
        WHERE dsc.deleted = 0
        <if test="entity.coachUserId!=null and entity.coachUserId!=''">
            AND dsc.coach_user_id = #{entity.coachUserId}
        </if>
        <if test="entity.ifPay!=null and entity.ifPay!=''">
            AND dsc.if_pay = #{entity.ifPay}
        </if>
        <if test="entity.startTimeStr!=null and entity.startTimeStr!='' ">
            AND dsc.create_time &gt;= #{entity.startTimeStr}
        </if>
        <if test="entity.endTimeStr!=null and entity.endTimeStr!='' ">
            AND dsc.create_time &lt;= #{entity.endTimeStr}
        </if>
    </select>

    <select id="getCommissionListExport" resultType="cn.iocoder.yudao.module.course.vo.CommissionExportVO">
        SELECT
        coach.id_number AS coachIdCard,
        dsc.coach_name,
        dsc.student_name,
        student.id_card AS studentIdCard,
        dsc.commission_amount,
        course.name AS courseName,
        course.type AS courseType,
        dsc.subject,
        dsc.check_name,
        dsc.check_remark,
        dsc.create_time,
        dsc.exam_time,
        dsc.student_pay,
        student.source
        FROM drive_school_commission dsc
        LEFT JOIN drive_school_coach coach ON coach.user_id = dsc.coach_user_id AND coach.deleted = 0
        LEFT JOIN drive_school_course course ON course.id = dsc.course_id AND course.deleted = 0
        LEFT JOIN drive_school_student student ON student.user_id = dsc.student_id AND student.deleted = 0
        where dsc.deleted = 0 AND dsc.subject IN (2,3)
        <if test="entity.coachId != null ">
            AND dsc.coach_user_id = #{entity.coachId}
        </if>
        <if test="entity.studentName != null and entity.studentName != '' ">
            AND dsc.student_name LIKE CONCAT('%', #{entity.studentName}, '%')
        </if>
        <if test="entity.courseId != null and entity.courseId != '' ">
            AND dsc.course_id = #{entity.courseId}
        </if>
        <if test="entity.courseType != null and entity.courseType != '' ">
            AND course.type = #{entity.courseType}
        </if>
        <if test="entity.subject != null and entity.subject != '' ">
            AND dsc.subject = #{entity.subject}
        </if>
        <if test="entity.source != null and entity.source != '' ">
            AND student.source = #{entity.source}
        </if>
        <if test="entity.checkName != null and entity.checkName != '' ">
            AND dsc.check_name LIKE CONCAT('%', #{entity.checkName}, '%')
        </if>
        <if test="entity.startTimeStr!=null and entity.startTimeStr!='' ">
            AND dsc.create_time &gt;= #{entity.startTimeStr}
        </if>
        <if test="entity.endTimeStr!=null and entity.endTimeStr!='' ">
            AND dsc.create_time &lt;= #{entity.endTimeStr}
        </if>
        <if test="entity.studentIdCard != null and entity.studentIdCard != '' ">
            <choose>
                <!-- 精确匹配：当输入18位时 -->
                <when test="entity.studentIdCard.length() == 18">
                    AND student.id_card = #{entity.studentIdCard}
                </when>
                <!-- 后4位匹配：当输入正好4位时 -->
                <when test="entity.studentIdCard.length() == 4">
                    AND RIGHT(student.id_card, 4) = #{entity.studentIdCard}
                </when>
                <!-- 模糊搜索：当输入大于4位但不足18位时 -->
                <otherwise>
                    AND student.id_card LIKE concat('%', #{entity.studentIdCard}, '%')
                </otherwise>
            </choose>
        </if>
        ORDER BY dsc.create_time DESC
    </select>

    <select id="getCommissionStatistics" resultType="cn.iocoder.yudao.module.course.vo.CommissionStatisticsVO">
        SELECT
        SUM(CASE WHEN dsc.subject = 2 THEN 1 ELSE 0 END) AS subject2PassCount,
        SUM(CASE WHEN dsc.subject = 2 THEN dsc.commission_amount ELSE 0 END) AS subject2Commission,
        SUM(CASE WHEN dsc.subject = 3 THEN 1 ELSE 0 END) AS subject3PassCount,
        SUM(CASE WHEN dsc.subject = 3 THEN dsc.commission_amount ELSE 0 END) AS subject3Commission,
        COUNT(*) AS totalPassCount,
        SUM(dsc.commission_amount) AS totalCommission
        FROM drive_school_commission dsc
        LEFT JOIN drive_school_coach coach ON coach.user_id = dsc.coach_user_id AND coach.deleted = 0
        LEFT JOIN drive_school_course course ON course.id = dsc.course_id AND course.deleted = 0
        LEFT JOIN drive_school_student student ON student.user_id = dsc.student_id AND student.deleted = 0
        WHERE dsc.deleted = 0 AND dsc.subject IN (2,3)
        <!-- 这里保持与getCommissionListNoPage相同的条件 -->
        <if test="entity.coachId != null ">
            AND dsc.coach_user_id = #{entity.coachId}
        </if>
        <if test="entity.studentName != null and entity.studentName != '' ">
            AND dsc.student_name LIKE CONCAT('%', #{entity.studentName}, '%')
        </if>
        <if test="entity.courseId != null and entity.courseId != '' ">
            AND dsc.course_id = #{entity.courseId}
        </if>
        <if test="entity.courseType != null and entity.courseType != '' ">
            AND course.type = #{entity.courseType}
        </if>
        <if test="entity.subject != null and entity.subject != '' ">
            AND dsc.subject = #{entity.subject}
        </if>
        <if test="entity.source != null and entity.source != '' ">
            AND student.source = #{entity.source}
        </if>
        <if test="entity.checkName != null and entity.checkName != '' ">
            AND dsc.check_name LIKE CONCAT('%', #{entity.checkName}, '%')
        </if>
        <if test="entity.startTimeStr!=null and entity.startTimeStr!='' ">
            AND dsc.create_time &gt;= #{entity.startTimeStr}
        </if>
        <if test="entity.endTimeStr!=null and entity.endTimeStr!='' ">
            AND dsc.create_time &lt;= #{entity.endTimeStr}
        </if>
        <if test="entity.studentIdCard != null and entity.studentIdCard != '' ">
            <choose>
                <when test="entity.studentIdCard.length() == 18">
                    AND student.id_card = #{entity.studentIdCard}
                </when>
                <when test="entity.studentIdCard.length() == 4">
                    AND RIGHT(student.id_card, 4) = #{entity.studentIdCard}
                </when>
                <otherwise>
                    AND student.id_card LIKE concat('%', #{entity.studentIdCard}, '%')
                </otherwise>
            </choose>
        </if>
    </select>

    <select id="getFinanceDeductList" resultType="cn.iocoder.yudao.module.course.vo.FinanceDeductInfoVO">
        SELECT
        coach.user_id AS coachId,
        coach.name AS coachName,
        coach.image,
        SUM(dsc.commission_amount) AS commissionSum,
        SUM(CASE WHEN dsc.subject = 2 THEN dsc.commission_amount ELSE 0 END) AS towCommissionSum,
        SUM(CASE WHEN dsc.subject = 3 THEN dsc.commission_amount ELSE 0 END) AS threeCommissionSum
        FROM drive_school_coach coach
        LEFT JOIN drive_school_commission dsc
        ON dsc.coach_user_id = coach.user_id
        AND dsc.deleted = 0
        <if test="entity.startTime != null and entity.startTime != '' ">
            AND dsc.create_time &gt;= #{entity.startTime}
        </if>
        <if test="entity.endTime!= null and entity.endTime!= '' ">
            AND dsc.create_time &lt;= #{entity.endTime}
        </if>
        WHERE coach.deleted = 0 AND coach.type = 'jl'
        <if test="entity.coachId != null ">
            AND coach.user_id = #{entity.coachId}
        </if>
        GROUP BY coach.user_id, coach.name, coach.image
        ORDER BY commissionSum DESC
    </select>

    <select id="getFinanceDeductDetailsListByCoach" resultType="cn.iocoder.yudao.module.course.vo.FinanceDeductInfoVO">
        SELECT
            dsc.student_id,
            dsc.student_name,
            dsc.commission_amount,
            dsc.subject,
            dsc.create_time,
            course.type AS courseType,
            course.name AS courseName
        FROM drive_school_commission dsc
        LEFT JOIN drive_school_course course ON dsc.course_id = course.id AND course.deleted = 0
        where dsc.deleted = 0
        <if test="entity.coachId != null and entity.coachId != '' ">
            AND dsc.coach_user_id = #{entity.coachId}
        </if>
        <if test="entity.startTime != null and entity.startTime != '' ">
            AND dsc.create_time &gt;= #{entity.startTime}
        </if>
        <if test="entity.endTime != null and entity.endTime != '' ">
            AND dsc.create_time &lt;= #{entity.endTime}
        </if>
        <if test="entity.subject != null ">
            AND dsc.subject = #{entity.subject}
        </if>
        <if test="entity.studentName != null and entity.studentName != '' ">
            AND dsc.student_name like concat('%', #{entity.studentName}, '%')
        </if>
        <if test="entity.courseType != null and entity.courseType != '' ">
            AND course.type = #{entity.courseType}
        </if>
        ORDER BY dsc.create_time DESC
    </select>
</mapper>
