<?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.yonge.cooleshow.biz.dal.dao.CourseScheduleStudentPaymentDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.CourseScheduleStudentPayment">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="user_id_" jdbcType="INTEGER" property="userId"/>
        <result column="course_group_id_" jdbcType="INTEGER" property="courseGroupId"/>
        <result column="course_id_" jdbcType="INTEGER" property="courseId"/>
        <result column="order_no_" jdbcType="VARCHAR" property="orderNo"/>
        <result column="original_price_" jdbcType="VARCHAR" property="originalPrice"/>
        <result column="expect_price_" jdbcType="VARCHAR" property="expectPrice"/>
        <result column="actual_price_" jdbcType="VARCHAR" property="actualPrice"/>
        <result column="created_time_" jdbcType="TIMESTAMP" property="createdTime"/>
        <result column="updated_time_" jdbcType="TIMESTAMP" property="updatedTime"/>
        <result column="course_type_" jdbcType="VARCHAR" property="courseType"/>
        <result column="group_join_" jdbcType="TINYINT" property="groupJoin"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_
        , user_id_, course_group_id_, course_id_, order_no_, original_price_, expect_price_,
        actual_price_, created_time_, updated_time_, course_type_, group_join_
    </sql>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.yonge.cooleshow.biz.dal.entity.CourseScheduleStudentPayment">
        insert into course_schedule_student_payment(user_id_, course_group_id_, course_id_, order_no_, original_price_,
        expect_price_, actual_price_, created_time_, updated_time_, course_type_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.userId}, #{entity.courseGroupId}, #{entity.courseId}, #{entity.orderNo}, #{entity.originalPrice},
            #{entity.expectPrice}, #{entity.actualPrice}, #{entity.createdTime}, #{entity.updatedTime},
            #{entity.courseType})
        </foreach>
    </insert>

    <update id="adjustPlayMidi">
        UPDATE course_schedule_student_payment cssp
        <set>
            <if test="content == null or content == ''">
                cssp.play_midi_ = NULL,cssp.updated_time_ = NOW()
            </if>
            <if test="content != null and content != ''">
                cssp.play_midi_ = #{content},cssp.updated_time_ = NOW()
            </if>
        </set>
        <where>
            cssp.course_id_ = #{courseScheduleId}
            <if test="userId != null and userId != ''">
                AND FIND_IN_SET(cssp.user_id_,#{userId})
            </if>
        </where>
    </update>
    <update id="adjustExamSong">
        UPDATE course_schedule_student_payment SET music_sheet_download_json_ = #{musicSheetJson}
        WHERE course_id_ = #{courseScheduleId}
        <if test="userId != null">
            AND user_id_ = #{userId}
        </if>
    </update>
    <update id="adjustPlayMidiAndMusicSheet">
        UPDATE course_schedule_student_payment cssp
        <set>
            <if test="content == null or content == ''">
                cssp.play_midi_ = NULL,cssp.updated_time_ = NOW(),
            </if>
            <if test="content != null and content != ''">
                cssp.play_midi_ = #{content},cssp.updated_time_ = NOW(),
            </if>
            <if test="musicSheetJson != null">
                cssp.music_sheet_download_json_ = #{musicSheetJson}
            </if>
        </set>
        <where>
            cssp.course_id_ = #{scheduleId}
            <if test="userId != null and userId != ''">
                AND FIND_IN_SET(cssp.user_id_,#{userId})
            </if>
        </where>
    </update>
    <!--更新用户课程组入群状态-->
    <update id="updateGroupJoinStatus">
        UPDATE course_schedule_student_payment cssp
        <set>
            <if test="groupJoin != null">
                cssp.group_join_ = #{groupJoin}
            </if>
        </set>
        <where>
            <if test="courseGroupId != null">
                AND cssp.course_group_id_ = #{courseGroupId}
            </if>
            <if test="courseGroupType != null and courseGroupType != ''">
                AND cssp.course_type_ = #{courseGroupType}
            </if>
            <if test="studentIds != null and studentIds.size > 0">
                AND cssp.user_id_ IN <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">#{studentId}</foreach>
            </if>
        </where>
    </update>
    <select id="queryStudentIds" resultType="java.lang.Long">
        SELECT DISTINCT user_id_ FROM course_schedule_student_payment
        <where>
            <if test="courseGroupId != null">
                AND course_group_id_ = #{courseGroupId}
            </if>
            <if test="courseGroupType != null and courseGroupType != ''">
                AND course_type_ = #{courseGroupType}
            </if>
        </where>
        ORDER BY created_time_ ASC
    </select>
    <select id="queryByCourseId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/> FROM course_schedule_student_payment
        WHERE course_id_ = #{courseScheduleId}
    </select>

    <select id="queryStudentInfoByGroupId" parameterType="map" resultType="com.yonge.cooleshow.biz.dal.vo.LiveCourseInfoVo$CourseBuyStudentVo">
        select distinct o.user_id_      as studentId,
               b.real_name_    as studentName,
               b.username_    as userName,
               b.avatar_ as avatar,
               a.create_time_ as createTime
        from user_order_detail as a
        left join user_order as o on a.order_no_ = o.order_no_
                 left join sys_user as b on o.user_id_ = b.id_
        where a.biz_id_ = #{param.groupId} and a.good_type_ = 'LIVE'
        <if test="param.orderState != null and param.orderState != ''">
            AND o.status_ = #{param.orderState}
        </if>
        <if test="param.userId != null and param.userId != ''">
            AND o.user_id_ = #{param.userId}
        </if>
    </select>
    <select id="selectAll" resultType="java.lang.Long">
        SELECT user_id_ FROM course_schedule_student_payment WHERE course_type_ = 'PRACTICE'
    </select>
    <select id="selectPaymentList" resultType="com.yonge.cooleshow.biz.dal.vo.CourseScheduleStudentPaymentVo"
            parameterType="java.lang.String">
        SELECT
            p.user_id_ AS userId,
            p.course_group_id_ AS courseGroupId,
            p.course_id_ AS courseId,
            p.course_type_ AS courseType,
            p.expect_price_ AS expectPrice,
            p.actual_price_ AS actualPrice,
            g.teacher_id_ AS teacherId,
            s.class_num_ AS classNum
        FROM course_schedule_student_payment p
        LEFT JOIN course_group g ON p.course_group_id_ = g.id_
        LEFT JOIN course_schedule s ON p.course_id_ = s.id_
        WHERE p.order_no_=#{orderNo}
    </select>

    <select id="selectOrderNoByGroupId" resultType="java.lang.String">
        select
        distinct order_no_
        from course_schedule_student_payment cssp
        where cssp.course_group_id_ = #{courseGroupId}
    </select>
    <select id="selectUser" resultType="com.yonge.cooleshow.biz.dal.vo.CourseScheduleStudentVo">
        SELECT
            c.id_ AS courseId,
            c.course_group_id_ AS courseGroupId,
            c.teacher_id_ AS teacherId,
            p.user_id_ AS studentId,
            c.end_time_ AS endTime,
            c.single_course_time_ AS singleCourseTime,
            c.type_ AS type,
            p.order_no_ AS orderNo,
            s.actual_salary_ AS actualSalary,
            g.name_ AS groupName
        FROM course_schedule c
        LEFT JOIN course_schedule_student_payment p ON c.id_ = p.course_id_
        LEFT JOIN course_schedule_teacher_salary s ON c.id_ = s.course_schedule_id_
        LEFT JOIN course_group g ON c.course_group_id_ = g.id_
        WHERE c.lock_ = 0
        AND c.status_ IN ('ING','NOT_START')
        AND NOW() &gt;= c.end_time_
        ORDER BY c.end_time_
    </select>
    <select id="queryNoJoinStu" resultType="com.yonge.cooleshow.biz.dal.dto.BasicUserInfoDto">
        SELECT CASE WHEN su.username_ IS NULL THEN su.real_name_ ELSE su.username_ END userName,
        su.avatar_ headUrl,cssp.user_id_ userId
        FROM course_schedule_student_payment cssp
        LEFT JOIN sys_user su ON su.id_ = cssp.user_id_
        WHERE cssp.course_id_ = #{scheduleId}
        <if test="studentIds != null and studentIds.size > 0">
            AND cssp.user_id_ NOT IN
            <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
                #{studentId}
            </foreach>
        </if>
    </select>
</mapper>
