<?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.SubjectChangeDao">
    <resultMap id="SubjectChange" type="com.ym.mec.biz.dal.entity.SubjectChange">
        <!--@mbg.generated-->
        <!--@Table subject_change-->
        <id column="id_" property="id"/>
        <result column="edu_teacher_id_" property="eduTeacherId"/>
        <result column="eduTeacher" property="eduTeacher"/>
        <result column="student_id_" property="studentId"/>
        <result column="studentName" property="studentName"/>
        <result column="organ_id_" property="organId"/>
        <result column="organName" property="organName"/>
        <result column="cooperation_organ_id_" property="cooperationOrganId"/>
        <result column="cooperationOrganName" property="cooperationOrganName"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="musicGroupName" property="musicGroupName"/>
        <result column="original_order_id_" property="originalOrderId"/>
        <result column="original_subject_id_" property="originalSubjectId"/>
        <result column="original_course_fee_" property="originalCourseFee"/>
        <result column="original_musical_" property="originalMusical"/>
        <result column="original_musical_price_" property="originalMusicalPrice"/>
        <result column="original_accessories_" property="originalAccessories"/>
        <result column="original_accessories_price_" property="originalAccessoriesPrice"/>
        <result column="original_cost_" property="originalCost"/>
        <result column="original_kit_group_purchase_type_" property="originalKitGroupPurchaseType"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="kit_group_purchase_type_" property="kitGroupPurchaseType"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="change_subject_id_" property="changeSubjectId"/>
        <result column="change_course_fee_" property="changeCourseFee"/>
        <result column="change_musical_" property="changeMusical"/>
        <result column="change_musical_price_" property="changeMusicalPrice"/>
        <result column="change_accessories_" property="changeAccessories"/>
        <result column="change_accessories_price_" property="changeAccessoriesPrice"/>
        <result column="change_cost_" property="changeCost"/>
        <result column="goods_margin_" property="goodsMargin"/>
        <result column="course_margin_" property="courseMargin"/>
        <result column="cost_margin_" property="costMargin"/>
        <result column="sell_amount_" property="sellAmount"/>
        <result column="order_id_" property="orderId"/>
        <result column="order_no_" property="orderNo"/>
        <result column="trans_no_" property="transNo"/>
        <result column="payment_channel_" property="paymentChannel"/>
        <result column="mer_no_" property="merNo"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="sell_time_" property="sellTime"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="version_" property="version"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id_,edu_teacher_id_,student_id_,organ_id_,cooperation_organ_id_,
        music_group_id_,original_order_id_,original_subject_id_,original_course_fee_, original_musical_,
        original_musical_price_,
        original_accessories_,original_accessories_price_,original_kit_group_purchase_type_,kit_group_purchase_type_,change_subject_id_,
        change_course_fee_, change_musical_,
        original_cost_,change_musical_price_,change_accessories_, change_accessories_price_,change_cost_, goods_margin_,
        course_margin_,cost_margin_,sell_amount_,order_id_,order_no_,trans_no_,
        status_,payment_channel_,mer_no_,sell_time_,create_time_,
        update_time,version_
    </sql>
    <select id="get" parameterType="java.lang.Integer" resultMap="SubjectChange">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List"/>
        from subject_change
        where id_ = #{id}
    </select>
    <delete id="delete" parameterType="java.lang.Integer">
        <!--@mbg.generated-->
        delete from subject_change
        where id_ = #{id}
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.SubjectChange"
            useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into subject_change
        (student_id_,edu_teacher_id_,organ_id_,cooperation_organ_id_,music_group_id_,original_order_id_,original_subject_id_,original_course_fee_,
        original_musical_,
        original_musical_price_,original_accessories_,original_accessories_price_,original_cost_,original_kit_group_purchase_type_,kit_group_purchase_type_,
        change_subject_id_,change_course_fee_, change_musical_, change_musical_price_,
        change_accessories_, change_accessories_price_, change_cost_,goods_margin_,
        course_margin_,cost_margin_,sell_amount_,order_id_,
        order_no_,trans_no_,status_,sell_time_, create_time_, update_time,version_)
        values
        (#{studentId},#{eduTeacherId},#{organId},#{cooperationOrganId},#{musicGroupId},#{originalOrderId},#{originalSubjectId},#{originalCourseFee},
        #{originalMusical},
        #{originalMusicalPrice},
        #{originalAccessories},
        #{originalAccessoriesPrice},#{originalCost},#{originalKitGroupPurchaseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{kitGroupPurchaseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{changeSubjectId},#{changeCourseFee}, #{changeMusical},
        #{changeMusicalPrice}, #{changeAccessories}, #{changeAccessoriesPrice},#{changeCost}, #{goodsMargin},
        #{courseMargin},#{costMargin},
        #{sellAmount},#{orderId},#{orderNo},#{transNo},#{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{sellTime},
        #{createTime}, #{updateTime},#{version})
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.SubjectChange">
        <!--@mbg.generated-->
        update subject_change
        <set>
            <if test="eduTeacherId !=null ">
                edu_teacher_id_ = #{eduTeacherId},
            </if>
            <if test="studentId != null">
                student_id_ = #{studentId},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="originalCourseFee != null">
                original_course_fee_ = #{originalCourseFee},
            </if>
            <if test="originalMusical != null">
                original_musical_ = #{originalMusical},
            </if>
            <if test="originalMusicalPrice != null">
                original_musical_price_ = #{originalMusicalPrice},
            </if>
            <if test="originalAccessories != null">
                original_accessories_ = #{originalAccessories},
            </if>
            <if test="originalAccessoriesPrice != null">
                original_accessories_price_ = #{originalAccessoriesPrice},
            </if>
            <if test="originalCost != null">
                original_cost_ = #{originalCost},
            </if>
            <if test="changeCourseFee != null">
                change_course_fee_ = #{changeCourseFee},
            </if>
            <if test="changeMusical != null">
                change_musical_ = #{changeMusical},
            </if>
            <if test="changeMusicalPrice != null">
                change_musical_price_ = #{changeMusicalPrice},
            </if>
            <if test="changeAccessories != null">
                change_accessories_ = #{changeAccessories},
            </if>
            <if test="changeAccessoriesPrice != null">
                change_accessories_price_ = #{changeAccessoriesPrice},
            </if>
            <if test="changeCost != null">
                change_cost_ = #{changeCost},
            </if>
            <if test="goodsMargin != null">
                goods_margin_ = #{goodsMargin},
            </if>
            <if test="courseMargin != null">
                course_margin_ = #{courseMargin},
            </if>
            <if test="costMargin != null">
                cost_margin_ = #{costMargin},
            </if>
            <if test="sellAmount != null">
                sell_amount_ = #{sellAmount},
            </if>
            <if test="orderId != null">
                order_id_ = #{orderId},
            </if>
            <if test="orderNo != null">
                order_no_ = #{orderNo},
            </if>
            <if test="transNo != null">
                trans_no_ = #{transNo},
            </if>
            <if test="status != null">
                status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="paymentChannel != null">
                payment_channel_=#{paymentChannel},
            </if>
            <if test="merNo != null">
                mer_no_ = #{merNo},
            </if>
            <if test="sellTime != null">
                sell_time_ = #{sellTime},
            </if>
            <if test="createTime != null">
                create_time_ = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            version_ = version_+1
        </set>
        where id_ = #{id} AND version_ = #{version}
    </update>

    <select id="getStudentLastChange" resultMap="SubjectChange">
        SELECT *
        FROM subject_change
        WHERE student_id_ = #{studentId}
          AND music_group_id_ = #{musicGroupId}
          AND status_ = 2
        ORDER BY create_time_ DESC
        LIMIT 1
    </select>

    <select id="getChangeInfo" resultMap="SubjectChange">
        SELECT sc.*, su.username_ studentName
        FROM subject_change sc
                 LEFT jOIN sys_user su ON su.id_ = sc.student_id_
        WHERE sc.id_ = #{id}
    </select>

    <select id="getMusicGroupWaitPay" resultMap="SubjectChange">
        SELECT *
        FROM subject_change
        WHERE music_group_id_ = #{musicGroupId}
          AND status_ IN (0, 1)
    </select>

    <select id="queryPage" resultMap="SubjectChange">
        SELECT * FROM subject_change sc
        <include refid="queryPageSql"/>
        ORDER BY sc.id_ DESC
        <include refid="global.limit"/>
    </select>

    <select id="getPageList" resultMap="SubjectChange">
        SELECT sc.*,s.username_ studentName,o.name_ organName,co.name_ cooperationOrganName,edu.real_name_ eduTeacher,
        mg.name_ musicGroupName FROM subject_change sc
        LEFT JOIN music_group mg ON mg.id_ = sc.music_group_id_
        LEFT JOIN sys_user s ON sc.student_id_ = s.id_
        LEFT JOIN sys_user edu ON edu.id_ = sc.edu_teacher_id_
        LEFT JOIN organization o ON sc.organ_id_ = o.id_
        LEFT JOIN cooperation_organ co ON sc.cooperation_organ_id_ = co.id_
        <include refid="queryPageSql"/>
        ORDER BY sc.sell_time_ DESC
        <include refid="global.limit"/>
    </select>
    <sql id="queryPageSql">
        <where>
            <if test="search != null and search != ''">
                AND (sc.order_no_ = #{search} OR sc.trans_no_ = #{search} OR s.username_ LIKE CONCAT('%',#{search},'%'))
            </if>
            <if test="organIdList != null and organIdList != ''">
                AND FIND_IN_SET(sc.organ_id_,#{organIdList})
            </if>
            <if test="status != null">
                AND sc.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="cooperationOrganId != null">
                AND sc.cooperation_organ_id_ = #{cooperationOrganId}
            </if>
            <if test="startTime != null">
                AND sc.sell_time_ >= #{startTime}
            </if>
            <if test="endTime != null">
                AND sc.sell_time_ &lt;= #{endTime}
            </if>
        </where>
    </sql>
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM subject_change sc
        LEFT JOIN sys_user s ON sc.student_id_ = s.id_
        <include refid="queryPageSql"/>
    </select>

    <select id="getChangeGoods" resultMap="SubjectChange">
        SELECT GROUP_CONCAT(sc.original_musical_)     musicGroupName,
               GROUP_CONCAT(sc.original_accessories_) original_accessories_,
               GROUP_CONCAT(sc.change_musical_)       trans_no_,
               GROUP_CONCAT(sc.change_accessories_)   change_accessories_
        FROM subject_change sc
                 LEFT JOIN student_registration sr ON sc.student_id_ = sr.user_id_
        WHERE sc.music_group_id_ = #{musicGroupId}
          AND sc.status_ = 2
          AND sr.music_group_status_ != 'QUIT'
        GROUP BY sc.music_group_id_
    </select>
    <resultMap id="SubjectChangeDto" type="com.ym.mec.biz.dal.entity.SubjectChange" extends="SubjectChange">
        <collection property="changeMusicalGoods" ofType="com.ym.mec.biz.dal.entity.Goods">
            <result property="id" column="musical_id_"/>
            <result property="name" column="musical_name_"/>
        </collection>
        <collection property="changeAccessoriesGoods" ofType="com.ym.mec.biz.dal.entity.Goods">
            <result property="id" column="accessories_id_"/>
            <result property="name" column="accessories_name_"/>
        </collection>
    </resultMap>

    <select id="getStudentGoods" resultMap="SubjectChangeDto">
        SELECT sc.*, m.id_ musical_id_, m.name_ musical_name_, a.id_ accessories_id_, a.name_ accessories_name_
        FROM subject_change sc
                 LEFT JOIN goods m ON m.id_ = sc.change_musical_
                 LEFT JOIN goods a ON FIND_IN_SET(a.id_, sc.change_accessories_)
        WHERE sc.id_ IN (
            SELECT MAX(id_)
            FROM subject_change
            WHERE music_group_id_ = #{musicGroupId}
              AND status_ = 2
            GROUP BY student_id_
        )
    </select>
    <select id="getStudentWaitPay" resultMap="SubjectChange">
        SELECT *
        FROM subject_change
        WHERE music_group_id_ = #{musicGroupId}
          AND student_id_ = #{studentId}
          AND status_ IN (0, 1)
        LIMIT 1
    </select>
    <select id="getMargin" resultMap="SubjectChange">
        SELECT student_id_,SUM(goods_margin_+course_margin_) goods_margin_
        FROM subject_change
        WHERE music_group_id_ = #{musicGroupId}
          AND status_ = 2
        GROUP BY student_id_
    </select>
</mapper>