<?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.ym.mec.biz.dal.dao.CloudTeacherOrderDao">
    <resultMap id="CloudTeacherOrder" type="com.ym.mec.biz.dal.entity.CloudTeacherOrder">
        <!--@mbg.generated-->
        <!--@Table cloud_teacher_order-->
        <id column="id_" property="id"/>
        <result column="organ_id_" property="organId"/>
        <result column="student_id_" property="studentId"/>
        <result column="type_" property="type"/>
        <result column="level_" property="level"/>
        <result column="time_" property="time"/>
        <result column="amount_" property="amount"/>
        <result column="refund_amount_" property="refundAmount"/>
        <result column="status_" property="status"/>
        <result column="start_time_" property="startTime"/>
        <result column="end_time_" property="endTime"/>
        <result column="order_id_" property="orderId"/>
        <result column="remark_" property="remark"/>
        <result column="version_" property="version"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="music_group_id_" property="musicGroupId"/>
    </resultMap>
    
    <resultMap type="com.ym.mec.biz.dal.dto.CloudTeacherOrderDto" id="CloudTeacherOrderDto">
    	<id column="id_" property="cloudTeacherOrder.id"/>
        <result column="organ_id_" property="cloudTeacherOrder.organId"/>
        <result column="student_id_" property="cloudTeacherOrder.studentId"/>
        <result column="type_" property="cloudTeacherOrder.type"/>
        <result column="level_" property="cloudTeacherOrder.level"/>
        <result column="time_" property="cloudTeacherOrder.time"/>
        <result column="amount_" property="cloudTeacherOrder.amount"/>
        <result column="refund_amount_" property="cloudTeacherOrder.refundAmount"/>
        <result column="status_" property="cloudTeacherOrder.status"/>
        <result column="start_time_" property="cloudTeacherOrder.startTime"/>
        <result column="end_time_" property="cloudTeacherOrder.endTime"/>
        <result column="order_id_" property="cloudTeacherOrder.orderId"/>
        <result column="trans_status_" property="studentPaymentOrder.status"/>
        <result column="expect_amount_" property="studentPaymentOrder.expectAmount"/>
        <result column="name_" property="memberRankSetting.name"/>
        <result column="icon_" property="memberRankSetting.icon"/>
    </resultMap>

    <select id="get" resultMap="CloudTeacherOrder">
        <!--@mbg.generated-->
        select * from cloud_teacher_order where id_ = #{id}
    </select>
    <delete id="delete">
        <!--@mbg.generated-->
        delete from cloud_teacher_order
        where id_ = #{id}
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.CloudTeacherOrder"
            useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into cloud_teacher_order (organ_id_,student_id_, type_, level_, time_, amount_, refund_amount_, status_,
        order_id_,start_time_,end_time_,
        remark_, create_time_, update_time_)
        values (#{orderId},#{studentId}, #{type},#{level}, #{time}, #{amount}, #{refundAmount},
        #{status},#{orderId},#{startTime},#{endTime}, #{remark}, NOW(), NOW())
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.CloudTeacherOrder">
        <!--@mbg.generated-->
        update cloud_teacher_order
        <set>
            <if test="orderId != null">
                organ_id_ = #{orderId},
            </if>
            <if test="studentId != null">
                student_id_ = #{studentId},
            </if>
            <if test="type != null">
                type_ = #{type},
            </if>
            <if test="level != null">
                level_ = #{level},
            </if>
            <if test="time != null">
                time_ = #{time},
            </if>
            <if test="amount != null">
                amount_ = #{amount},
            </if>
            <if test="refundAmount != null">
                refund_amount_ = #{refundAmount},
            </if>
            <if test="status != null">
                status_ = #{status},
            </if>
            <if test="startTime != null">
                start_time_ = #{startTime},
            </if>
            <if test="endTime != null">
                end_time_ = #{endTime},
            </if>
            <if test="orderId != null">
                order_id_ = #{orderId},
            </if>
            <if test="remark != null">
                remark_ = #{remark},
            </if>
            <if test="createTime != null">
                create_time_ = #{createTime},
            </if>
            version_ = version_+1,
            update_time_ = NOW(),
        </set>
        where id_ = #{id} AND version_ = #{version}
    </update>

    <!-- 全查询 -->
    <select id="findAll" resultMap="CloudTeacherOrder">
        SELECT *
        FROM cloud_teacher_order
    </select>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="CloudTeacherOrderDto" parameterType="map">
        SELECT cto.*,spo.status_ trans_status_,spo.expect_amount_,mrs.name_,mrs.icon_ FROM cloud_teacher_order cto 
        left join student_payment_order spo on cto.organ_id_ = spo.id_
        left join member_rank_setting mrs on mrs.id_ = cto.level_
        where spo.type_ = 'MEMBER'
        <if test="status != null">
        	and cto.status_ = #{status}
        </if>
        <if test="studentId != null">
        	and cto.student_id_ = #{studentId}
        </if>
        order by spo.pay_time_ desc,spo.id_ desc
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(cto.id_)
        FROM cloud_teacher_order cto left join student_payment_order spo on cto.organ_id_ = spo.id_
        where spo.type_ = 'MEMBER'
        <if test="status != null">
        	and cto.status_ = #{status}
        </if>
        <if test="studentId != null">
        	and cto.student_id_ = #{studentId}
        </if>
        order by cto.create_time_ desc
    </select>

    <select id="getNoStartCloudTeacherOrder" resultMap="CloudTeacherOrder">
        SELECT cto.*, spo.music_group_id_
        FROM cloud_teacher_order cto
                 LEFT JOIN student_payment_order spo ON cto.order_id_ = spo.id_
                 LEFT JOIN music_group mg on spo.music_group_id_ = mg.id_
        WHERE cto.status_ = 1
        ORDER BY spo.pay_time_ ASC
    </select>

    <select id="getStudentCloudTeacherOrders" resultMap="CloudTeacherOrder">
        SELECT *
        FROM cloud_teacher_order
        WHERE order_id_ IN (
        SELECT id_
        FROM student_payment_order
        WHERE status_ = 'SUCCESS'
        AND user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
        GROUP BY user_id_
        )
        AND student_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
        AND status_ IN (1,2)
    </select>

    <select id="getStudentCloudTeacherOrder" resultMap="CloudTeacherOrder">
        SELECT *
        FROM cloud_teacher_order
        WHERE order_id_ IN (
            SELECT id_
            FROM student_payment_order
            WHERE status_ = 'SUCCESS'
              AND user_id_ = #{studentId}
        )
          AND student_id_ = #{studentId}
          AND status_ IN (1, 2)
    </select>

    <select id="queryByOrderId" resultMap="CloudTeacherOrder">
        SELECT cto.* FROM cloud_teacher_order cto
        WHERE order_id_ = #{orderId}
    </select>
    
    <select id="queryOrderInfoByOrderId" resultMap="CloudTeacherOrderDto">
        SELECT cto.*,spo.status_ trans_status_,spo.expect_amount_,mrs.name_,mrs.icon_ FROM cloud_teacher_order cto 
        left join student_payment_order spo on cto.organ_id_ = spo.id_
        left join member_rank_setting mrs on mrs.id_ = cto.level_
        where spo.type_ = 'MEMBER' and order_id_ = #{orderId}
    </select>
</mapper>