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

    <resultMap type="com.ym.mec.biz.dal.entity.StudentPaymentOrderDetail" id="StudentPaymentOrderDetail">
        <result column="id_" property="id"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="goods_id_list_" property="goodsIdList"/>
        <result column="price_" property="price"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="tenant_id_" property="tenantId"/>
        <result column="payment_order_id_" property="paymentOrderId"/>
        <result column="kit_group_purchase_type_" property="kitGroupPurchaseType"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="student_instrument_id_" property="studentInstrumentId"/>
        <result column="is_renew_" property="isRenew"/>
        <result column="income_item_" property="incomeItem"/>
        <result column="income_" property="income"/>
        <result column="balance_income_" property="balanceIncome"/>
        <result column="minuend_stock_goods_id_list_" property="minuendStockGoodsIdList"/>
        <result column="user_id_" property="userId"/>
        <collection property="goodsList" ofType="com.ym.mec.biz.dal.entity.Goods">
            <result column="goods_id_" property="id"/>
            <result column="goods_name_" property="name"/>
            <result column="group_purchase_price_" property="groupPurchasePrice"/>
            <result column="discount_price_" property="discountPrice"/>
            <result column="agree_cost_price_" property="agreeCostPrice"/>
            <result column="goods_type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
            <result column="complement_goods_id_list_" property="complementGoodsIdList"/>
        </collection>
    </resultMap>
    
    <resultMap type="com.ym.mec.biz.dal.dto.Mapper" id="Mapper">
        <result column="key_" property="key"/>
        <result column="value_" property="value"/>
    </resultMap>
    
    <resultMap type="com.ym.mec.biz.dal.dto.StudentApplyInstrumentDto" id="StudentApplyInstrumentDto">
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="student_instrument_id_" property="studentInstrumentId"/>
        <result column="user_id_" property="userId"/>
        <result column="student_payment_order_id_" property="studentPyamentOrderId"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="StudentPaymentOrderDetail">
        SELECT *
        FROM student_payment_order_detail
        WHERE id_ = #{id}
    </select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="StudentPaymentOrderDetail">
        SELECT *
        FROM student_payment_order_detail where tenant_id_ = #{tenantId}
        ORDER BY id_
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentPaymentOrderDetail" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO student_payment_order_detail
        (type_,goods_id_list_,price_,create_time_,update_time_,payment_order_id_,kit_group_purchase_type_,
         student_instrument_id_,is_renew_,income_item_,income_,balance_income_,minuend_stock_goods_id_list_,tenant_id_,remit_fee_)
        VALUES(#{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{goodsIdList},#{price},now(),now(),
        #{paymentOrderId},#{kitGroupPurchaseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
               #{studentInstrumentId},#{isRenew},#{incomeItem},#{income},#{balanceIncome},#{minuendStockGoodsIdList},#{tenantId},#{remitFee})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentPaymentOrderDetail">
        UPDATE student_payment_order_detail
        <set>
            <if test="price != null">
                price_ = #{price},
            </if>
            <if test="goodsIdList != null">
                goods_id_list_ = #{goodsIdList},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="paymentOrderId != null">
                payment_order_id_ = #{paymentOrderId},
            </if>
            <if test="kitGroupPurchaseType != null">
                kit_group_purchase_type_ =
                #{kitGroupPurchaseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="type != null">
                type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="studentInstrumentId != null">
                student_instrument_id_ = #{studentInstrumentId},
            </if>
            <if test="incomeItem != null">
                income_item_ = #{incomeItem},
            </if>
            <if test="income != null">
                income_ = #{income},
            </if>
            <if test="balanceIncome != null">
                balance_income_ = #{balanceIncome},
            </if>
            <if test="minuendStockGoodsIdList != null">
                minuend_stock_goods_id_list_ = #{minuendStockGoodsIdList},
            </if>
        </set>
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>
    
    <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
	        UPDATE student_payment_order_detail
	        <set>
	            <if test="item.price != null">
	                price_ = #{item.price},
	            </if>
	            <if test="item.goodsIdList != null">
	                goods_id_list_ = #{item.goodsIdList},
	            </if>
	            <if test="item.updateTime != null">
	                update_time_ = NOW(),
	            </if>
	            <if test="item.paymentOrderId != null">
	                payment_order_id_ = #{item.paymentOrderId},
	            </if>
	            <if test="item.kitGroupPurchaseType != null">
	                kit_group_purchase_type_ =
	                #{item.kitGroupPurchaseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
	            </if>
	            <if test="item.type != null">
	                type_ = #{item.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
	            </if>
	            <if test="item.studentInstrumentId != null">
	                student_instrument_id_ = #{item.studentInstrumentId},
	            </if>
	            <if test="item.incomeItem != null">
	                income_item_ = #{item.incomeItem},
	            </if>
	            <if test="item.income != null">
	                income_ = #{item.income},
	            </if>
	            <if test="item.balanceIncome != null">
	                balance_income_ = #{item.balanceIncome},
	            </if>
	            <if test="item.minuendStockGoodsIdList != null">
	                minuend_stock_goods_id_list_ = #{item.minuendStockGoodsIdList},
	            </if>
	        </set>
	        WHERE id_ = #{item.id} and tenant_id_ = #{item.tenantId}
        </foreach>
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
        DELETE
        FROM student_payment_order_detail
        WHERE id_ = #{id}
    </delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="StudentPaymentOrderDetail" parameterType="map">
        SELECT * FROM student_payment_order_detail where tenant_id_ = #{tenantId} ORDER BY id_
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*)
        FROM student_payment_order_detail where tenant_id_ = #{tenantId}
    </select>

    <insert id="batchAdd" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id_">
        INSERT INTO student_payment_order_detail
        (type_,goods_id_list_,price_,remit_fee_,create_time_,update_time_,payment_order_id_,
         kit_group_purchase_type_,student_instrument_id_,is_renew_,income_item_,income_,balance_income_,minuend_stock_goods_id_list_,tenant_id_)
        VALUE
        <foreach collection="studentPaymentOrderDetailList" item="orderDetail" separator=",">
            (#{orderDetail.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            #{orderDetail.goodsIdList},#{orderDetail.price},#{orderDetail.remitFee},now(),now(),#{orderDetail.paymentOrderId},
            #{orderDetail.kitGroupPurchaseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            #{orderDetail.studentInstrumentId},#{orderDetail.isRenew},#{orderDetail.incomeItem},#{orderDetail.income},#{orderDetail.balanceIncome},#{orderDetail.minuendStockGoodsIdList},#{orderDetail.tenantId})
        </foreach>
    </insert>

    <!-- 查询注册订单详情 -->
    <select id="findApplyOrderSuccess" resultMap="StudentPaymentOrderDetail">
        SELECT spod.*
        FROM student_payment_order spo
                 LEFT JOIN student_payment_order_detail spod ON spo.id_ = spod.payment_order_id_
                 LEFT JOIN student_registration sr ON spo.user_id_ = sr.user_id_
        WHERE spo.music_group_id_ = #{musicGroupId}
          AND spo.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
          AND spo.type_ = 'APPLY'
          AND sr.music_group_id_ = #{musicGroupId}
          AND sr.music_group_status_ != 'QUIT' AND spod.id_ IS NOT NULL
    </select>

    <!-- 查询订单详情 -->
    <select id="findApplyOrderGoods" resultMap="StudentPaymentOrderDetail">
        SELECT *
        FROM student_payment_order_detail
        WHERE payment_order_id_ = #{orderId}
    </select>

    <!-- 查询用户注册订单详情 -->
    <select id="findUserApplyOrder" resultMap="StudentPaymentOrderDetail">
        SELECT spod.*
        FROM student_payment_order_detail spod
                 LEFT JOIN student_payment_order spo ON spo.id_ = spod.payment_order_id_
        WHERE spo.user_id_ = #{userId}
          AND spo.music_group_id_ = #{musicGroupId}
          AND spo.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
          AND spo.type_ = 'APPLY'
    </select>
    <resultMap id="musicalListDetailDtoMap" type="com.ym.mec.biz.dal.dto.MusicalListDetailDto">
        <result property="paymentOrderId" column="payment_order_id_"/>
        <result property="username" column="username_"/>
        <result property="userId" column="user_id_"/>
        <result property="musicGroupName" column="muaic_group_name_"/>
        <result property="accessoriesAmount" column="accessories_amount_"/>
        <result property="courseAmount" column="course_amount_"/>
        <result property="kitGroupPurchaseTypeEnum" column="kit_group_purchase_type_"/>
        <result property="musicalAmount" column="musical_amount_"/>
        <result property="orderAmount" column="order_amount_"/>
        <result property="organName" column="organ_name_"/>
        <result property="goodsNames" column="goods_names_"/>
    </resultMap>
    <select id="getMusicalListDetail" resultMap="musicalListDetailDtoMap">
        SELECT spo.id_ payment_order_id_, spo.user_id_
        FROM student_payment_order spo
                 LEFT JOIN student_registration sr ON spo.user_id_ = sr.user_id_
        WHERE spo.music_group_id_ = #{musicGroupId}
        <if test="userId != null">
            AND spo.user_id_ = #{userId}
        </if>
          AND spo.status_ = 'SUCCESS'
          AND sr.music_group_id_ = #{musicGroupId}
          AND sr.music_group_status_ != 'QUIT'
    </select>
    <select id="queryGoodsNames" resultType="java.util.Map">
        SELECT spod.payment_order_id_ 'key',GROUP_CONCAT(g.name_) 'value'
        FROM student_payment_order_detail spod
        LEFT JOIN goods g ON FIND_IN_SET(g.id_,spod.goods_id_list_)
        WHERE spod.payment_order_id_ IN
        <foreach collection="paymentOrderNo" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        GROUP BY spod.payment_order_id_
    </select>
    <select id="queryGoodsPrice" resultType="java.util.Map">
        SELECT spod.payment_order_id_ 'key' ,SUM(price_) 'value' FROM student_payment_order_detail spod
        WHERE FIND_IN_SET(spod.type_,#{types})
        AND spod.payment_order_id_ IN
        <foreach collection="paymentOrderNo" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        GROUP BY spod.payment_order_id_
    </select>

    <select id="findApplyOrderMusical" resultMap="StudentPaymentOrderDetail">
        SELECT *
        FROM student_payment_order_detail
        WHERE payment_order_id_ = #{orderId}
          AND type_ = 'MUSICAL'
        LIMIT 1
    </select>

    <select id="getOrderDetail" resultMap="StudentPaymentOrderDetail">
        SELECT spod.*,
               g.id_   goods_id_,
               g.name_ goods_name_,
               g.group_purchase_price_,
               g.discount_price_,
               g.agree_cost_price_,
               g.type_ goods_type_,
               g.complement_goods_id_list_
        FROM student_payment_order_detail spod
                 LEFT JOIN goods g ON FIND_IN_SET(g.id_, spod.goods_id_list_)
        WHERE spod.payment_order_id_ = #{orderId}
          AND spod.goods_id_list_ IS NOT NULL
    </select>

    <select id="getOrderDetailType" resultType="java.lang.String">
        SELECT DISTINCT type_
        FROM student_payment_order_detail
        WHERE payment_order_id_ = #{orderId}
    </select>

    <select id="getStudentApplyDetail" resultMap="StudentPaymentOrderDetail">
        SELECT spod.*,
               g.id_   goods_id_,
               g.name_ goods_name_,
               g.group_purchase_price_,
               g.discount_price_,
               g.agree_cost_price_,
               g.type_ goods_type_
        FROM student_payment_order_detail spod
                 LEFT JOIN goods g ON FIND_IN_SET(g.id_, spod.goods_id_list_)
                 LEFT JOIN student_payment_order spo on spod.payment_order_id_ = spo.id_
        WHERE spo.user_id_ = #{studentId}
          AND spo.music_group_id_ = #{musicGroupId}
          AND spo.status_ = 'SUCCESS'
          AND spo.type_ = 'APPLY'
          AND spod.type_ IN ('MUSICAL', 'ACCESSORIES', 'TEACHING', 'OTHER', 'COURSE')
    </select>

    <select id="queryPurchaseTypeMap" resultType="java.util.Map">
        SELECT payment_order_id_ 'key',kit_group_purchase_type_ 'value'
        FROM student_payment_order_detail
        WHERE payment_order_id_ IN
        <foreach collection="paymentOrderNo" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        AND type_='MUSICAL'
    </select>

    <select id="getOrderGoodies" resultMap="StudentPaymentOrderDetail">
        SELECT spod.*,
        g.id_ goods_id_,
        g.name_ goods_name_,
        g.group_purchase_price_,
        g.discount_price_,
        g.agree_cost_price_,
        g.type_ goods_type_,
        g.complement_goods_id_list_
        FROM student_payment_order_detail spod
        LEFT JOIN goods g ON FIND_IN_SET(g.id_, spod.goods_id_list_)
        WHERE spod.payment_order_id_ IN
        <foreach collection="orderIds" open="(" close=")" separator="," item="orderId">
            #{orderId}
        </foreach>
        AND spod.goods_id_list_ IS NOT NULL
    </select>

    <select id="getWithIds" resultMap="StudentPaymentOrderDetail">
        SELECT * FROM student_payment_order_detail WHERE payment_order_id_ IN
        <foreach collection="paymentOrderIds" item="paymentOrderId" open="(" close=")" separator=",">
            #{paymentOrderId}
        </foreach>
    </select>

    <select id="getOrderDetailByType" resultMap="StudentPaymentOrderDetail">
        SELECT * FROM student_payment_order_detail WHERE payment_order_id_ = #{orderId} AND type_ IN
        <foreach collection="orderDetailTypes" item="orderDetailType" open="(" close=")" separator=",">
            #{orderDetailType}
        </foreach>
    </select>

    <select id="getWithUserAndOrderType" resultMap="StudentPaymentOrderDetail">
        SELECT spod.* FROM student_payment_order_detail spod
        LEFT JOIN student_payment_order spo ON spo.id_=spod.payment_order_id_
        WHERE spo.user_id_= #{userId}
        AND spod.type_ = #{orderType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        AND spo.status_='SUCCESS'
    </select>

    <select id="getOrderDetailByOrderId" resultMap="StudentPaymentOrderDetail">
        SELECT * FROM student_payment_order_detail WHERE payment_order_id_ IN
        <foreach collection="orderIdList" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
    </select>

    <select id="getInstrumentNumInMusicApply" resultMap="Mapper">
        SELECT count(distinct spo.user_id_) value_,spo.music_group_id_ key_ 
        FROM student_payment_order_detail spod left join student_payment_order spo on spod.payment_order_id_ = spo.id_ 
        WHERE spo.music_group_id_ IN
        <foreach collection="musicGroupIds" item="musicGroupId" open="(" close=")" separator=",">
            #{musicGroupId}
        </foreach>
        AND spo.type_ = 'APPLY' and spo.status_ = 'SUCCESS' and spod.type_ = 'MUSICAL'
        GROUP BY spo.music_group_id_
    </select>

    <select id="queryByType" resultMap="StudentApplyInstrumentDto" parameterType="map">
    	select spo.user_id_,spo.music_group_id_,spod.student_instrument_id_ from student_payment_order_detail spod left join student_payment_order spo on spod.payment_order_id_ = spo.id_ 
        WHERE spo.music_group_id_ = #{musicGroupId}
        AND spo.type_ = 'APPLY' and spo.status_ = 'SUCCESS' and spod.type_ = #{orderDetailType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
    </select>
    <select id="getGoodsSellPrice" resultMap="com.ym.mec.biz.dal.dao.GoodsDao.Goods">
        SELECT g.id_,g.goods_category_id_,g.sn_,g.name_,g.brand_,g.specification_,g.image_,g.type_,spod.price_ group_purchase_price_
        FROM student_payment_order_detail spod
        LEFT JOIN goods g ON spod.goods_id_list_ = g.id_
        WHERE spod.payment_order_id_ IN
        <foreach collection="orderIds" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
        AND spod.type_ = #{type}
        GROUP BY spod.id_
    </select>
    <select id="findByOrderId" resultMap="StudentPaymentOrderDetail">
        SELECT * FROM student_payment_order_detail WHERE payment_order_id_ = #{paymentOrderId}
    </select>
    <select id="findByCalenderId" resultMap="StudentPaymentOrderDetail">
        SELECT spod.*,spo.user_id_ FROM student_payment_order spo
        LEFT JOIN student_payment_order_detail spod  ON spo.id_ = spod.payment_order_id_
        WHERE spo.id_ IN (
        SELECT c.id_ FROM (SELECT payment_order_id_ id_ FROM music_group_payment_calender_detail
        WHERE music_group_payment_calender_id_ = #{calenderId} AND payment_order_id_ IS NOT NULL
        UNION
        SELECT id_ FROM student_payment_order WHERE calender_id_ = #{calenderId} AND status_ = 'SUCCESS')c) AND spod.id_ IS NOT NULL
    </select>
</mapper>
