<?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.EduBillOrderItemMapper">
	
	<resultMap id="orderItemMap" type="com.yida.data.mall.vo.h5.H5BillItemVO">
		<id column="item_id" property="itemId"/>
		<result column="item_price" property="itemPrice"/>
		<result column="item_name" property="itemName"/>
		<result column="reduction_price" property="reductionPrice"/>
		<collection property="subItemList" ofType="com.yida.data.mall.vo.h5.H5BillItemSubVO"
				select="getSubItemPayInfo"
				column="{itemId=item_id,orderId=order_id,studentId=student_id}">
		</collection>
	</resultMap>
	
	<!-- 查询学生对应账单项目的缴费信息 -->
	<select id="getItemPayInfo"
			resultMap="orderItemMap">
		SELECT DISTINCT
			i.item_id,
			o.student_id,
		    o.order_id
		FROM
			edu_bill_order_item i
		LEFT JOIN edu_bill_order o ON i.order_id = o.order_id
		WHERE
			o.pay_status = '1'
		AND o.bill_id = #{billId}
		AND o.student_id = #{studentId}
		AND i.item_id = #{itemId}
	</select>
	
	<!-- 查询学生对应账单项目选项的缴费信息 -->
	<select id="getSubItemPayInfo"
			resultType="com.yida.data.mall.vo.h5.H5BillItemSubVO">
		SELECT DISTINCT
			*
		FROM
			edu_bill_order_item_sub
		WHERE
			item_id = #{itemId}
		AND order_id = #{orderId}
		AND student_id = #{studentId}
	</select>
	
	<!-- 查询学生账单缴费信息 -->
	<select id="getPayInfo"
			resultMap="orderItemMap">
		SELECT DISTINCT
			i.item_id,
		    i.item_name,
		    i.item_price,
		    i.reduction_price,
			o.student_id,
			o.order_id
		FROM
			edu_bill_order_item i
		LEFT JOIN edu_bill_order o ON i.order_id = o.order_id
		WHERE
			 o.pay_status = '1'
		  AND o.bill_id = #{billId}
		  AND o.student_id = #{studentId}
	</select>
	
	<resultMap id="itemMap" type="com.yida.data.mall.vo.BillPayVO">
		<result column="item_id" property="itemId"/>
		<result column="sub_item_id" property="subItemId"/>
		
		<collection property="billPayPriceVO" ofType="com.yida.data.mall.vo.BillPayPriceVO"
				select="getPayPrice"
				column="{itemId=item_id,subItemId=sub_item_id,studentId=student_id,billId=bill_id}">
		</collection>
	</resultMap>
	
	<!-- 查询学生具体缴费金额 -->
	<select id="listItemPayInfo" resultMap="itemMap">
		SELECT
			i.id AS item_id,
			s.id AS sub_item_id,
		    i.bill_id,
		    ${studentId} AS student_id
		FROM
			edu_bill_item i
		LEFT JOIN ( SELECT id, item_id FROM edu_bill_item_sub WHERE del_flag = 0 AND bill_id = #{billId} ) s ON i.id = s.item_id
		WHERE
			i.del_flag = 0
		AND i.bill_id = #{billId}
	</select>
	
	<!-- 查询项目或选项缴费金额 -->
	<select id="getPayPrice" resultType="com.yida.data.mall.vo.BillPayPriceVO">
		<choose>
			<when test="subItemId == null">
				SELECT
					SUM(i.item_price) as item_price,
					SUM(i.reduction_price) as reduction_price
				FROM
					edu_bill_order_item i
				LEFT JOIN edu_bill_order o ON i.order_id = o.order_id
				WHERE
					o.del_flag = 0
				AND i.del_flag = 0
				AND i.item_id = #{itemId}
				AND o.bill_id = #{billId}
				AND o.student_id = #{studentId}
				AND o.pay_status = '1'
			</when>
			<otherwise>
				SELECT
					SUM(s.sub_item_price) AS item_price,
					SUM(i.reduction_price) as reduction_price
				FROM
					edu_bill_order_item_sub s
				LEFT JOIN edu_bill_order_item i ON s.order_item_id = i.id
				LEFT JOIN edu_bill_order o ON i.order_id = o.order_id
				WHERE
					s.del_flag = 0
				AND o.del_flag = 0
				AND i.del_flag = 0
				AND s.sub_item_id = #{subItemId}
				AND s.item_id = #{itemId}
				AND o.bill_id = #{billId}
				AND o.student_id = #{studentId}
				AND o.pay_status = '1'
			</otherwise>
		</choose>
	</select>

	<!-- 查询学生是否有对应账单项目的缴费信息	-->
	<select id="countItemPayInfo" resultType="int">
		SELECT
		    count(DISTINCT o.order_id)
		FROM
			edu_bill_order_item i
		LEFT JOIN edu_bill_order o ON i.order_id = o.order_id
		WHERE
			o.pay_status = '1'
		  AND o.bill_id = #{billId}
		  AND o.student_id = #{studentId}
		  AND i.item_id = #{itemId}
	</select>

</mapper>
