<?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.yida.data.mall.mapper.EduBillOrderMapper">
	
	<!--查询已支付金额-->
	<select id="sumPayOrderPrice" resultType="java.math.BigDecimal">
		SELECT SUM(practical_money) AS pay_money
		FROM edu_bill_order
		WHERE bill_id = #{billId}
		<if test="studentId != null">
			AND student_id = #{studentId}
		</if>
		AND pay_status = '1'
	</select>
	
	<!-- 查询账单缴费人数 -->
	<select id="sumPayStudentNumber" resultType="java.lang.Long">
		SELECT
			COUNT( DISTINCT o.student_id )
		FROM
			edu_bill_order o
		LEFT JOIN edu_bill_student s ON ( o.student_id = s.student_id AND o.bill_id = s.bill_id )
		WHERE
			o.del_flag = 0
		  AND s.del_flag = 0
		  AND o.pay_status = '1'
		  AND o.bill_id = #{billId}
		  AND s.student_id IN
		 <foreach collection="studentIds" open="(" close=")" item="item" separator=",">
			 #{item}
		 </foreach>
	</select>
	
	<!-- 前端查询账单订单列表数据 -->
	<select id="listH5BillOrderPage"
			resultType="com.yida.data.mall.vo.h5.H5BillOrderPageVO">
		SELECT o.*,
			   s.student_name
		FROM edu_bill_order o
				 LEFT JOIN edu_bill_order_student s ON o.order_id = s.order_id
		WHERE o.dept_id = #{dto.deptId}
		  AND o.student_id = #{dto.studentId}
		  AND o.del_flag = 0
		ORDER BY o.create_date DESC
	</select>
	
	<resultMap id="h5BillOrderInfoMap" type="com.yida.data.mall.vo.h5.H5BillOrderInfoVO">
		<id column="order_id" property="orderId"/>
		<result column="bill_id" property="billId"/>
		<result column="bill_title" property="billTitle"/>
		<result column="order_status" property="orderStatus"/>
		<result column="pay_status" property="payStatus"/>
		<result column="total_money" property="totalMoney"/>
		<result column="practical_money" property="practicalMoney"/>
		<result column="student_name" property="studentName"/>
		<result column="pay_date" property="payDate"/>
		<result column="dept_id" property="deptId"/>
		
		<collection property="itemList" ofType="com.yida.data.mall.vo.h5.H5BillItemVO"
				select="listH5BillOrderItem"
				column="order_id">
		</collection>
	</resultMap>
	
	<resultMap id="h5BillOrderItemMap" type="com.yida.data.mall.vo.h5.H5BillItemVO">
		<id column="item_id" property="itemId"/>
		<result column="item_name" property="itemName"/>
		
		<collection property="subItemList" ofType="com.yida.data.mall.vo.h5.H5BillItemSubVO"
				select="listH5BillOrderItemSub"
				column="{itemId=item_id,orderId=order_id}">
		</collection>
	</resultMap>
	
	<!-- 前端查询账单订单详情 -->
	<select id="getH5BillOrderInfo"
			resultMap="h5BillOrderInfoMap">
		SELECT o.*,
			   s.student_name
		FROM edu_bill_order o
		LEFT JOIN edu_bill_order_student s ON o.order_id = s.order_id
		WHERE
			o.del_flag = 0
		<if test="billOrderId != null">
			AND o.order_id = #{billOrderId}
		</if>
		<if test="orderCode != null and orderCode != ''">
			AND o.order_code = #{orderCode}
		</if>
	</select>
	
	<!-- 前端查询账单订单项目详情 -->
	<select id="listH5BillOrderItem" resultMap="h5BillOrderItemMap">
		select *
		from edu_bill_order_item
		where order_id = #{orderId}
	</select>
	
	<!-- 前端查询账单订单项目选项详情 -->
	<select id="listH5BillOrderItemSub"
			resultType="com.yida.data.mall.vo.h5.H5BillItemSubVO">
		select *
		from edu_bill_order_item_sub
		where order_id = #{orderId}
		  and item_id = #{itemId}
	</select>
	
	<!-- 查询所有已经超时订单 -->
	<select id="selectTimeOutBillOrder"
			resultType="com.yida.data.common.core.entity.mall.EduBillOrder">
		SELECT
			*
		FROM
			edu_bill_order
		WHERE
			order_status = '0'
	    AND TIMESTAMPDIFF( SECOND, create_date, now( ) ) > 900
	</select>
	
	<!-- 查询退款中状态的账单订单 -->
	<select id="selectRefundOrder"
			resultType="com.yida.data.mall.vo.BillRefundOrderVO">
		SELECT
			b.order_id,
			b.order_code,
			r.refund_code,
			r.id refund_id,
			b.pay_way,
			b.dept_id
		FROM
			edu_bill_order b
		LEFT JOIN edu_bill_order_refund r ON b.order_code = r.order_code
		WHERE
			b.del_flag = 0
		  AND b.order_status = '3'
		  AND b.pay_status = '1'
	</select>
	
	<!--查询部门已支付金额-->
	<select id="sumDeptPayOrderPrice" resultType="java.math.BigDecimal">
		SELECT
			SUM( o.practical_money ) AS pay_money
		FROM
			edu_bill_order o
		LEFT JOIN edu_bill_order_student s on o.order_id = s.order_id
		WHERE
			o.bill_id = #{billId}
		  AND o.pay_status = '1'
		  <if test="type == 1">
			  AND s.class_id = #{deptId}
		  </if>
		<if test="type == 2">
			AND s.grade_id = #{deptId}
		</if>
	</select>
	
</mapper>
