<?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.SellOrderDao">
    <resultMap id="SellOrder" type="com.ym.mec.biz.dal.entity.SellOrder">
        <!--@mbg.generated-->
        <!--@Table sell_order-->
        <id column="id_" property="id"/>
        <result column="organ_id_" property="organId"/>
        <result column="edu_teacher_id_" property="eduTeacherId"/>
        <result column="eduTeacher" property="eduTeacher"/>
        <result column="cooperation_organ_id_" property="cooperationOrganId"/>
        <result column="school_name_" property="schoolName"/>
        <result column="trans_no_" property="transNo"/>
        <result column="order_no_" property="orderNo"/>
        <result column="order_id_" property="orderId"/>
        <result column="expect_amount_" property="expectAmount"/>
        <result column="actual_amount_" property="actualAmount"/>
        <result column="balance_amount_" property="balanceAmount"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="parent_goods_id_" property="parentGoodsId"/>
        <result column="goods_id_" property="goodsId"/>
        <result column="goods_name_" property="goodsName"/>
        <result column="sell_cost_" property="sellCost"/>
        <result column="sell_cost2_" property="sellCost2"/>
        <result column="num_" property="num"/>
        <result column="user_id_" property="userId"/>
        <result column="user_name_" property="userName"/>
        <result column="phone_" property="phone"/>
        <result column="organ_name_" property="organName"/>
        <result column="user_id_" property="userId"/>
        <result column="payment_channel_" property="paymentChannel"/>
        <result column="mer_no_" property="merNo"/>
        <result column="batch_no_" property="batchNo"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="refund_time_" property="refundTime"/>
        <result column="sell_time_" property="sellTime"/>
        <result column="create_ime_" property="createIme"/>
        <result column="update_time_" property="updateTime"/>
        <result column="receive_status_" property="receiveStatus"/>
        <result column="stock_type_" property="stockType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="account_type_" property="accountType"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id_, edu_teacher_id_,organ_id_, cooperation_organ_id_, trans_no_,order_id_, order_no_, expect_amount_,
        actual_amount_,balance_amount_, sell_cost_, sell_cost2_, type_,parent_goods_id_, goods_id_,goods_name_, num_,
        user_id_,
        payment_channel_,mer_no_,batch_no_,stock_type_,account_type_,status_, refund_time_,sell_time_, create_ime_,
        update_time_
    </sql>
    <select id="get" parameterType="java.lang.Integer" resultMap="SellOrder">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List"/>
        from sell_order
        where id_ = #{id}
    </select>
    <delete id="delete" parameterType="java.lang.Integer">
        <!--@mbg.generated-->
        delete from sell_order
        where id_ = #{id}
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.SellOrder"
            useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into sell_order (edu_teacher_id_,organ_id_, cooperation_organ_id_, trans_no_,order_id_, order_no_,
        expect_amount_,actual_amount_,balance_amount_, type_,parent_goods_id_, goods_id_,goods_name_, sell_cost_,
        sell_cost2_, num_, user_id_, payment_channel_,
        mer_no_,batch_no_,stock_type_,account_type_, refund_time_,sell_time_, create_ime_, update_time_)
        values (#{eduTeacherId},#{organId}, #{cooperationOrganId}, #{transNo}, #{orderNo},#{orderId}, #{expectAmount},
        #{actualAmount}, #{balanceAmount}, #{type},#{parentGoodsId}, #{goodsId}, #{goodsName}, #{sellCost},
        #{sellCost2}, #{num}, #{userId},
        #{paymentChannel}, #{merNo},#{batchNo},
        #{stockType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{accountType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{refundTime},#{sellTime}, #{createIme}, #{updateTime})
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.SellOrder">
        <!--@mbg.generated-->
        update sell_order
        <set>
            <if test="eduTeacherId != null">
                edu_teacher_id_ = #{eduTeacherId},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="cooperationOrganId != null">
                cooperation_organ_id_ = #{cooperationOrganId},
            </if>
            <if test="transNo != null">
                trans_no_ = #{transNo},
            </if>
            <if test="orderId != null">
                order_id_ = #{orderId},
            </if>
            <if test="orderNo != null">
                order_no_ = #{orderNo},
            </if>
            <if test="expectAmount != null">
                expect_amount_ = #{expectAmount},
            </if>
            <if test="actualAmount != null">
                actual_amount_ = #{actualAmount},
            </if>
            <if test="balanceAmount != null">
                balance_amount_ = #{balanceAmount},
            </if>
            <if test="type != null">
                type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="parentGoodsId != null">
                parent_goods_id_ = #{parentGoodsId},
            </if>
            <if test="goodsId != null">
                goods_id_ = #{goodsId},
            </if>
            <if test="goodsName != null">
                goods_name_ = #{goodsName},
            </if>
            <if test="sellCost != null">
                sell_cost_ = #{sellCost},
            </if>
            <if test="sellCost2 != null">
                sell_cost2_ = #{sellCost2},
            </if>
            <if test="num != null">
                num_ = #{num},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="paymentChannel != null">
                payment_channel_ = #{paymentChannel},
            </if>
            <if test="merNo != null">
                mer_no_ = #{merNo},
            </if>
            <if test="batchNo != null">
                batch_no_ = #{batchNo},
            </if>
            <if test="stockType != null">
                stock_type_ = #{stockType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="accountType != null">
                account_type_ = #{accountType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="status != null">
                status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="refundTime != null">
                refund_time_ = #{refundTime},
            </if>
            <if test="sellTime != null">
                sell_time_ = #{sellTime},
            </if>
            <if test="createIme != null">
                create_ime_ = #{createIme},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
        </set>
        where id_ = #{id}
    </update>

    <update id="batchUpdate" parameterType="com.ym.mec.biz.dal.entity.SellOrder">
        <foreach collection="sellOrders" item="sellOrder" separator=";">
            update sell_order
            <set>
                <if test="sellOrder.eduTeacherId != null">
                    edu_teacher_id_ = #{sellOrder.eduTeacherId},
                </if>
                <if test="sellOrder.organId != null">
                    organ_id_ = #{sellOrder.organId},
                </if>
                <if test="sellOrder.cooperationOrganId != null">
                    cooperation_organ_id_ = #{sellOrder.cooperationOrganId},
                </if>
                <if test="sellOrder.transNo != null">
                    trans_no_ = #{sellOrder.transNo},
                </if>
                <if test="sellOrder.orderId != null">
                    order_id_ = #{sellOrder.orderId},
                </if>
                <if test="sellOrder.orderNo != null">
                    order_no_ = #{sellOrder.orderNo},
                </if>
                <if test="sellOrder.expectAmount != null">
                    expect_amount_ = #{sellOrder.expectAmount},
                </if>
                <if test="sellOrder.actualAmount != null">
                    actual_amount_ = #{sellOrder.actualAmount},
                </if>
                <if test="sellOrder.balanceAmount != null">
                    balance_amount_ = #{sellOrder.balanceAmount},
                </if>
                <if test="sellOrder.type != null">
                    type_ = #{sellOrder.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="sellOrder.parentGoodsId != null">
                    parent_goods_id_ = #{sellOrder.parentGoodsId},
                </if>
                <if test="sellOrder.goodsId != null">
                    goods_id_ = #{sellOrder.goodsId},
                </if>
                <if test="sellOrder.goodsName != null">
                    goods_name_ = #{sellOrder.goodsName},
                </if>
                <if test="sellOrder.sellCost != null">
                    sell_cost_ = #{sellOrder.sellCost},
                </if>
                <if test="sellOrder.sellCost2 != null">
                    sell_cost2_ = #{sellOrder.sellCost2},
                </if>
                <if test="sellOrder.num != null">
                    num_ = #{sellOrder.num},
                </if>
                <if test="sellOrder.userId != null">
                    user_id_ = #{sellOrder.userId},
                </if>
                <if test="sellOrder.paymentChannel != null">
                    payment_channel_ = #{sellOrder.paymentChannel},
                </if>
                <if test="sellOrder.merNo != null">
                    mer_no_ = #{sellOrder.merNo},
                </if>
                <if test="sellOrder.batchNo != null">
                    batch_no_ = #{sellOrder.batchNo},
                </if>
                <if test="sellOrder.stockType != null">
                    stock_type_ = #{sellOrder.stockType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="sellOrder.accountType != null">
                    account_type_ = #{sellOrder.accountType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="sellOrder.status != null">
                    status_ = #{sellOrder.status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="sellOrder.refundTime != null">
                    refund_time_ = #{sellOrder.refundTime},
                </if>
                <if test="sellOrder.sellTime != null">
                    sell_time_ = #{sellOrder.sellTime},
                </if>
                <if test="sellOrder.createIme != null">
                    create_ime_ = #{sellOrder.createIme},
                </if>
                update_time_ = NOW()
            </set>
            where id_ = #{sellOrder.id}
        </foreach>
    </update>

    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id_">
        insert into sell_order (organ_id_, cooperation_organ_id_, trans_no_,order_id_, order_no_, expect_amount_,
        actual_amount_,balance_amount_, type_, parent_goods_id_, goods_id_,goods_name_, sell_cost_, sell_cost2_, num_,
        user_id_, payment_channel_,
        mer_no_,batch_no_,stock_type_,account_type_, sell_time_,edu_teacher_id_, create_ime_, update_time_)
        VALUE
        <foreach collection="sellOrders" separator="," item="sellOrder">
            (#{sellOrder.organId},#{sellOrder.cooperationOrganId},#{sellOrder.transNo},#{sellOrder.orderId},#{sellOrder.orderNo},
            #{sellOrder.expectAmount},#{sellOrder.actualAmount},#{sellOrder.balanceAmount},#{sellOrder.type},#{sellOrder.parentGoodsId},#{sellOrder.goodsId},
            #{sellOrder.goodsName},#{sellOrder.sellCost},#{sellOrder.sellCost2},#{sellOrder.num},#{sellOrder.userId},
            #{sellOrder.paymentChannel},#{sellOrder.merNo},#{sellOrder.batchNo},
            #{sellOrder.stockType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{sellOrder.accountType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            #{sellOrder.sellTime},#{sellOrder.eduTeacherId},NOW(),NOW())
        </foreach>
    </insert>


    <!-- 分页查询 -->
    <select id="queryPage" resultMap="SellOrder" parameterType="map">
        SELECT so.*,su.username_ user_name_,su.phone_,o.name_ organ_name_,co.name_ school_name_,t.real_name_
        eduTeacher,spo.receive_status_
        FROM sell_order so
        LEFT JOIN sys_user su ON so.user_id_ = su.id_
        LEFT JOIN organization o ON o.id_ = so.organ_id_
        LEFT JOIN cooperation_organ co ON co.id_= so.cooperation_organ_id_
        LEFT JOIN sys_user t ON t.id_ = so.edu_teacher_id_
        LEFT JOIN student_payment_order spo ON so.order_id_ = spo.id_
        <include refid="queryPageSql"/>
        ORDER BY so.create_ime_ DESC
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM sell_order so
        LEFT JOIN sys_user su ON so.user_id_ = su.id_
        LEFT JOIN student_payment_order spo ON so.order_id_ = spo.id_
        <include refid="queryPageSql"/>
    </select>

    <sql id="queryPageSql">
        <where>
            <if test="search != null and search != ''">
                AND (so.order_no_ = #{search} OR so.trans_no_ = #{search})
            </if>
            <if test="userName != null and userName != ''">
                AND (so.user_id_ like concat('%',#{userName},'%') OR su.username_ like concat('%',#{userName},'%'))
            </if>
            <if test="organIdList != null and organIdList != ''">
                AND FIND_IN_SET(so.organ_id_,#{organIdList})
            </if>
            <if test="goodsName != null and goodsName != ''">
                AND so.goods_name_ LIKE CONCAT('%',#{goodsName},'%')
            </if>
            <if test="cooperationOrganId != null">
                AND so.cooperation_organ_id_ = #{cooperationOrganId}
            </if>
            <if test="status != null">
                AND so.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="type != null">
                AND so.type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="receiveStatus != null and receiveStatus != ''">
                AND spo.receive_status_ = #{receiveStatus}
            </if>
            <if test="startTime != null">
                AND so.sell_time_ >= #{startTime}
            </if>
            <if test="endTime != null">
                AND so.sell_time_ &lt;= #{endTime}
            </if>
        </where>
    </sql>

    <!-- 获取分部学校的收入支出(乐团订单) -->
    <select id="getMusicGroupMonthReport" resultMap="com.ym.mec.biz.dal.dao.OperatingReportDao.OperatingReport"><![CDATA[
        SELECT spo.organ_id_,
               co.id_                       cooperation_organ_id_,
               SUM(so.actual_amount_)       sell_amount_,
               SUM(so.sell_cost_ * so.num_) sell_cost_
        FROM student_payment_order spo
                 LEFT JOIN sell_order so on so.order_id_ = spo.id_
                 LEFT JOIN music_group mg ON mg.id_ = spo.music_group_id_
                 LEFT JOIN cooperation_organ co ON co.id_ = mg.cooperation_organ_id_
        WHERE spo.status_ = 'SUCCESS'
          AND spo.group_type_ = 'MUSIC'
          AND spo.create_time_ >= #{startTime}
          AND spo.create_time_ <= #{endTime}
        GROUP BY spo.organ_id_, co.id_
        ]]></select>

    <select id="getMusicGroupIncome" resultMap="com.ym.mec.biz.dal.dao.OperatingReportDao.OperatingReport"><![CDATA[
        SELECT spo.organ_id_,
               co.id_                  cooperation_organ_id_,
               SUM(spo.actual_amount_) income_total_
        FROM student_payment_order spo
                 LEFT JOIN music_group mg ON mg.id_ = spo.music_group_id_
                 LEFT JOIN cooperation_organ co ON co.id_ = mg.cooperation_organ_id_
        WHERE spo.status_ = 'SUCCESS'
          AND spo.group_type_ = 'MUSIC'
          AND spo.create_time_ >= #{startTime}
          AND spo.create_time_ <= #{endTime}
        GROUP BY spo.organ_id_, co.id_
        ]]></select>

    <!-- 获取分部学校的收入支出(VIP,网管课) -->
    <select id="getMusicVipPracticeMonthReport" resultMap="com.ym.mec.biz.dal.dao.OperatingReportDao.OperatingReport"><![CDATA[
        SELECT spo.organ_id_,
               co.id_                  cooperation_organ_id_,
               SUM(spo.actual_amount_) income_total_
        FROM student_payment_order spo
                 LEFT JOIN
             (SELECT MIN(music_group_id_) music_group_id_, user_id_
              FROM student_registration
              WHERE music_group_status_ = 'NORMAL'
              GROUP BY user_id_) sr
             ON sr.user_id_ = spo.user_id_
                 LEFT JOIN music_group mg ON mg.id_ = sr.music_group_id_
                 LEFT JOIN cooperation_organ co ON co.id_ = mg.cooperation_organ_id_
        WHERE spo.status_ = 'SUCCESS'
          AND spo.group_type_ IN ('VIP', 'PRACTICE')
          AND spo.create_time_ >= #{startTime}
          AND spo.create_time_ <= #{endTime}
        GROUP BY spo.organ_id_, co.id_
        ]]></select>


    <!-- 获取分部学校的收入支出(外部添加订单) -->
    <select id="getOutOrderMonthReport" resultMap="com.ym.mec.biz.dal.dao.OperatingReportDao.OperatingReport"><![CDATA[
        SELECT spo.organ_id_,
               co.id_                       cooperation_organ_id_,
               SUM(so.actual_amount_)       sell_amount_,
               SUM(so.sell_cost_ * so.num_) sell_cost_
        FROM student_payment_order spo
                 LEFT JOIN sell_order so on so.order_id_ = spo.id_
                 LEFT JOIN cooperation_organ co ON co.id_ = spo.music_group_id_
        WHERE spo.status_ = 'SUCCESS'
          AND spo.group_type_ = 'OUTORDER'
          AND spo.create_time_ >= #{startTime}
          AND spo.create_time_ <= #{endTime}
        GROUP BY spo.organ_id_, co.id_
        ]]></select>

    <!-- 获取分部学校的收入支出(外部添加订单) -->
    <select id="getOutOrderIncome" resultMap="com.ym.mec.biz.dal.dao.OperatingReportDao.OperatingReport"><![CDATA[
        SELECT spo.organ_id_,
               co.id_                  cooperation_organ_id_,
               SUM(spo.actual_amount_) income_total_
        FROM student_payment_order spo
                 LEFT JOIN cooperation_organ co ON co.id_ = spo.music_group_id_
        WHERE spo.status_ = 'SUCCESS'
          AND spo.group_type_ = 'OUTORDER'
          AND spo.create_time_ >= #{startTime}
          AND spo.create_time_ <= #{endTime}
        GROUP BY spo.organ_id_, co.id_
        ]]></select>

    <!-- 获取分部学校的收入(充值) -->
    <select id="getRechargeMonthReport" resultMap="com.ym.mec.biz.dal.dao.OperatingReportDao.OperatingReport"><![CDATA[
        SELECT spo.organ_id_,
               co.id_                  cooperation_organ_id_,
               SUM(spo.actual_amount_) income_total_,
               SUM(spo.actual_amount_) other_income_
        FROM student_payment_order spo
                 LEFT JOIN sporadic_charge_info sci ON sci.id_ = spo.music_group_id_
                 LEFT JOIN music_group mg ON mg.id_ = sci.music_group_id_
                 LEFT JOIN cooperation_organ co ON co.id_ = mg.cooperation_organ_id_
        WHERE spo.status_ = 'SUCCESS'
          AND spo.group_type_ = 'SPORADIC'
          AND sci.charge_type_ = '9'
          AND spo.create_time_ >= #{startTime}
          AND spo.create_time_ <= #{endTime}
        GROUP BY spo.organ_id_, co.id_
        ]]></select>

    <!-- 获取分部学校的收入(零星支付) -->
    <select id="getSporadicMonthReport" resultMap="com.ym.mec.biz.dal.dao.OperatingReportDao.OperatingReport"><![CDATA[
        SELECT spo.organ_id_,
               co.id_                  cooperation_organ_id_,
               SUM(spo.actual_amount_) income_total_
        FROM student_payment_order spo
                 LEFT JOIN sporadic_charge_info sci ON sci.id_ = spo.music_group_id_
                 LEFT JOIN music_group mg ON mg.id_ = sci.music_group_id_
                 LEFT JOIN cooperation_organ co ON co.id_ = mg.cooperation_organ_id_
        WHERE spo.status_ = 'SUCCESS'
          AND spo.group_type_ = 'SPORADIC'
          AND sci.charge_type_ != '9'
          AND spo.create_time_ >= #{startTime}
          AND spo.create_time_ <= #{endTime}
        GROUP BY spo.organ_id_, co.id_
        ]]></select>

    <!-- 获取分部学校的收入支出(维修、商品销售订单) -->
    <select id="getRepairGoodsSellGroupMonthReport"
            resultMap="com.ym.mec.biz.dal.dao.OperatingReportDao.OperatingReport"><![CDATA[
        SELECT spo.organ_id_,
               SUM(so.actual_amount_)       sell_amount_,
               SUM(so.sell_cost_ * so.num_) sell_cost_
        FROM student_payment_order spo
                 LEFT JOIN sell_order so on so.order_id_ = spo.id_
        WHERE spo.status_ = 'SUCCESS'
          AND spo.group_type_ = 'REPAIR'
          AND spo.create_time_ >= #{startTime}
          AND spo.create_time_ <= #{endTime}
        GROUP BY spo.organ_id_
        ]]></select>

    <!-- 获取分部学校的收入支出(维修、商品销售订单) -->
    <select id="getRepairGoodsSellGroupIncome"
            resultMap="com.ym.mec.biz.dal.dao.OperatingReportDao.OperatingReport"><![CDATA[
        SELECT spo.organ_id_,
               SUM(spo.actual_amount_) income_total_
        FROM student_payment_order spo
                 LEFT JOIN sell_order so on so.order_id_ = spo.id_
        WHERE spo.status_ = 'SUCCESS'
          AND spo.group_type_ = 'REPAIR'
          AND spo.create_time_ >= #{startTime}
          AND spo.create_time_ <= #{endTime}
        GROUP BY spo.organ_id_
        ]]></select>

    <!-- 获取分部学校的收入支出(商品销售订单) -->
    <select id="getGoodsSellGroupMonthReport"
            resultMap="com.ym.mec.biz.dal.dao.OperatingReportDao.OperatingReport"><![CDATA[
        SELECT spo.organ_id_,
               so.cooperation_organ_id_,
               SUM(so.actual_amount_)       income_total_,
               SUM(so.actual_amount_)       sell_amount_,
               SUM(so.sell_cost_ * so.num_) sell_cost_
        FROM student_payment_order spo
                 LEFT JOIN sell_order so on so.order_id_ = spo.id_
        WHERE spo.status_ = 'SUCCESS'
          AND spo.group_type_ = 'GOODS_SELL'
          AND spo.create_time_ >= #{startTime}
          AND spo.create_time_ <= #{endTime}
        GROUP BY spo.organ_id_, so.cooperation_organ_id_
        ]]></select>


    <!-- 获取订单的销售列表 -->
    <select id="getOrderSellOrder" resultMap="SellOrder">
        SELECT *
        FROM sell_order
        WHERE order_id_ = #{orderId}
    </select>

    <delete id="deleteByOrderId">
        DELETE
        FROM sell_order
        WHERE order_id_ = #{orderId}
    </delete>

    <select id="getOrderSellCost" resultType="decimal">
        SELECT SUM(sell_cost_ * num_)
        FROM sell_order
        WHERE order_id_ = #{orderId}
    </select>

    <!-- 获取分部学校的收入支出(声部更改) -->
    <select id="getSubjectChangeMonthReport"
            resultMap="com.ym.mec.biz.dal.dao.OperatingReportDao.OperatingReport"><![CDATA[
        SELECT spo.organ_id_,
               spo.music_group_id_          cooperation_organ_id_,
               SUM(so.actual_amount_)       sell_amount_,
               SUM(so.sell_cost_ * so.num_) sell_cost_
        FROM student_payment_order spo
                 LEFT JOIN sell_order so on so.order_id_ = spo.id_
        WHERE spo.status_ = 'SUCCESS'
          AND spo.group_type_ = 'SUBJECT_CHANGE'
          AND spo.create_time_ >= #{startTime}
          AND spo.create_time_ <= #{endTime}
        GROUP BY spo.organ_id_, spo.music_group_id_
        ]]></select>

    <!-- 获取分部学校的收入支出(声部更改) -->
    <select id="getSubjectChangeGroupIncome"
            resultMap="com.ym.mec.biz.dal.dao.OperatingReportDao.OperatingReport"><![CDATA[
        SELECT spo.organ_id_,
               spo.music_group_id_     cooperation_organ_id_,
               SUM(spo.actual_amount_) income_total_
        FROM student_payment_order spo
        WHERE spo.status_ = 'SUCCESS'
          AND spo.group_type_ = 'SUBJECT_CHANGE'
          AND spo.create_time_ >= #{startTime}
          AND spo.create_time_ <= #{endTime}
        GROUP BY spo.organ_id_, spo.music_group_id_
        ]]></select>

    <!-- 获取退货订单的收入和成本 -->
    <select id="getRefundIncome" resultMap="com.ym.mec.biz.dal.dao.OperatingReportDao.OperatingReport"><![CDATA[
        SELECT organ_id_,
               cooperation_organ_id_,
               -SUM(sell_cost_ * num_) sell_cost_
        FROM sell_order
        WHERE status_ = 1
          AND refund_time_ >= #{startTime}
          AND refund_time_ <= #{endTime}
        GROUP BY organ_id_, cooperation_organ_id_
        ]]></select>

    <select id="getSellOrders" resultMap="SellOrder">
        SELECT * FROM sell_order WHERE id_ IN
        <foreach collection="sellOrderIds" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="getSellOrderByParentGoodsId" resultMap="SellOrder">
        SELECT * FROM sell_order WHERE order_id_ = #{orderId} AND status_=0
        <if test="parentGoodsId != null">
            AND parent_goods_id_ = #{parentGoodsId}
        </if>
    </select>

    <select id="getNoneBatchNoSellOrderIds" resultType="int">
        SELECT id_
        FROM sell_order
        WHERE status_ = 0
          AND stock_type_ IS NOT NULL
          AND batch_no_ IS NULL
        ORDER BY sell_time_
    </select>

    <select id="getRefundSellOrder" resultMap="SellOrder">
        SELECT *
        FROM sell_order
        WHERE order_id_ = #{orderId}
          AND status_ = 1
    </select>


    <select id="lockSellOrders" resultMap="SellOrder">
        SELECT * FROM sell_order WHERE id_ IN
        <foreach collection="sellOrderIds" item="sellOrderId" open="(" close=")" separator=",">
            #{sellOrderId}
        </foreach>
    </select>

    <select id="getFeeByType" resultType="com.ym.mec.biz.dal.dto.StudentPaymentOrderExportDto">
        SELECT MAX(spo.expect_amount_)                                expectAmount,
               SUM(IF(g.type_ = 'INSTRUMENT', so.expect_amount_, 0))  musicalFee,
               SUM(IF(g.type_ != 'INSTRUMENT', so.expect_amount_, 0)) teachingFee
        FROM student_payment_order spo
                 LEFT JOIN sell_order so ON spo.id_ = so.order_id_
                 LEFT JOIN goods g ON g.id_ = so.goods_id_
        WHERE spo.id_ = #{orderId}
    </select>
</mapper>
