<?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.EduBillStatisticsMapper">
	
	<resultMap id="itemMap" type="com.yida.data.mall.vo.BillStatisticsItemVO">
		<id column="item_id" property="itemId"/>
		<result column="item_name" property="itemName"/>
		<result column="item_price" property="itemPrice"/>
		<result column="reduction_price" property="reductionPrice"/>
		<collection property="subItemList" ofType="com.yida.data.mall.vo.h5.H5BillItemSubVO"
				select="getSubItemPayInfo"
				column="{itemId=item_id,billId=bill_id}">
		</collection>
	</resultMap>
	
	<!-- 查询账单总缴费信息 -->
	<select id="getPayInfo" resultMap="itemMap">
		SELECT
			i.item_id,
			i.item_name,
			i.bill_id,
			SUM( i.item_price ) AS item_price,
			SUM( i.reduction_price ) AS reduction_price,
			COUNT( s.student_id ) AS pay_num
		FROM
			edu_bill_order_item i
		LEFT JOIN edu_bill_order o ON i.order_id = o.order_id
		LEFT JOIN edu_bill_order_student s ON o.order_id = s.order_id
		WHERE
			o.pay_status = '1'
		  AND o.bill_id = #{billId}
		GROUP BY
			i.item_id,
			i.item_name,
			i.bill_id
	</select>
	
	<!-- 查询对应账单项目选项的缴费信息 -->
	<select id="getSubItemPayInfo"
			resultType="com.yida.data.mall.vo.h5.H5BillItemSubVO">
		SELECT
			s.sub_item_id,
			s.sub_item_name,
			SUM( s.sub_item_price ) AS sub_item_price,
			COUNT( s.student_id ) AS pay_num
		FROM
			edu_bill_order_item_sub s
		LEFT JOIN edu_bill_order o ON s.order_id = o.order_id
		LEFT JOIN edu_bill_order_student bs ON o.order_id = bs.order_id
		WHERE
			o.pay_status = '1'
		  AND o.bill_id = #{billId}
		  AND s.item_id = #{itemId}
		GROUP BY
			s.sub_item_id,
			s.sub_item_name
	</select>
	
	<resultMap id="deptItemMap" type="com.yida.data.mall.vo.BillStatisticsItemVO">
		<id column="item_id" property="itemId"/>
		<result column="item_name" property="itemName"/>
		<result column="item_price" property="itemPrice"/>
		<result column="reduction_price" property="reductionPrice"/>
		<result column="dept_name" property="deptName"/>
		<result column="pay_num" property="payNum"/>
		<collection property="subItemList" ofType="com.yida.data.mall.vo.h5.H5BillItemSubVO"
				select="getDeptSubItemPayInfo"
				column="{itemId=item_id,billId=bill_id,deptId=dept_id,deptType=dept_type}">
		</collection>
	</resultMap>
	
	<!-- 账单部门缴费信息统计 -->
	<select id="getDeptPayInfo" resultMap="deptItemMap">
		SELECT
			i.item_id,
			i.item_name,
			i.bill_id,
			<if test="type == 1">
				s.class_name AS dept_name,
			</if>
			<if test="type == 2">
				s.grade_name AS dept_name,
			</if>
			SUM( i.item_price ) AS item_price,
			SUM( i.reduction_price ) AS reduction_price,
			COUNT( s.student_id ) AS pay_num,
			${deptId} AS dept_id,
			${type} AS dept_type
		FROM
			edu_bill_order_item i
		LEFT JOIN edu_bill_order o ON i.order_id = o.order_id
		LEFT JOIN edu_bill_order_student s ON o.order_id = s.order_id
		WHERE
			o.pay_status = '1'
		AND o.bill_id = #{billId}
		<if test="type == 1">
			AND s.class_id = #{deptId}
		</if>
		<if test="type == 2">
			AND s.grade_id = #{deptId}
		</if>
		GROUP BY
			i.item_id,
			i.item_name,
			i.bill_id,
			dept_name
	</select>
	
	<!-- 账单部门缴费子选项信息统计 -->
	<select id="getDeptSubItemPayInfo"
			resultType="com.yida.data.mall.vo.h5.H5BillItemSubVO">
		SELECT
			s.sub_item_id,
			s.sub_item_name,
			SUM( s.sub_item_price ) AS sub_item_price,
			COUNT( s.student_id ) AS pay_num
		FROM
			edu_bill_order_item_sub s
				LEFT JOIN edu_bill_order o ON s.order_id = o.order_id
				LEFT JOIN edu_bill_order_student bs ON o.order_id = bs.order_id
		WHERE
			o.pay_status = '1'
		  AND o.bill_id = #{billId}
		  AND s.item_id = #{itemId}
		<if test="deptType == 1">
			AND bs.class_id = #{deptId}
		</if>
		<if test="deptType == 2">
			AND bs.grade_id = #{deptId}
		</if>
		GROUP BY
			s.sub_item_id,
			s.sub_item_name
	</select>
	
	<resultMap id="exportItemMap" type="com.yida.data.mall.vo.BillPayVO">
		<result column="item_id" property="itemId"/>
		<result column="sub_item_id" property="subItemId"/>
		
		<association property="payNum" javaType="long"
				select="getPayNum"
				column="{itemId=item_id,subItemId=sub_item_id,billId=bill_id}">
		</association>
		
		<collection property="billPayPriceVO" ofType="com.yida.data.mall.vo.BillPayPriceVO"
				select="getPayPrice"
				column="{itemId=item_id,subItemId=sub_item_id,billId=bill_id}">
		</collection>
	</resultMap>
	
	<!-- 查询账单总缴费信息 -->
	<select id="listTotalItemPayInfo" resultMap="exportItemMap">
		SELECT
			i.id AS item_id,
			s.id AS sub_item_id,
			i.bill_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}
		ORDER BY i.id
	</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.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.pay_status = '1'
			</otherwise>
		</choose>
	</select>
	
	<!-- 查询项目或选项缴费人数 -->
	<select id="getPayNum" resultType="long">
		<choose>
			<when test="subItemId == null">
				SELECT
					COUNT(DISTINCT s.student_id) AS num
				FROM
					edu_bill_order_item i
				LEFT JOIN edu_bill_order o ON i.order_id = o.order_id
				LEFT JOIN edu_bill_order_student s ON o.order_id = s.order_id
				WHERE
					o.del_flag = 0
				AND i.del_flag = 0
				AND i.item_id = #{itemId}
				AND o.bill_id = #{billId}
				AND o.pay_status = '1'
			</when>
			<otherwise>
				SELECT
					COUNT(DISTINCT os.student_id) AS num
				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
				LEFT JOIN edu_bill_order_student os ON o.order_id = os.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.pay_status = '1'
			</otherwise>
		</choose>
	</select>
	
	<resultMap id="exportDeptItemMap" type="com.yida.data.mall.vo.BillPayVO">
		<result column="item_id" property="itemId"/>
		<result column="sub_item_id" property="subItemId"/>
		
		<association property="payNum" javaType="long"
				select="getDeptPayNum"
				column="{itemId=item_id,subItemId=sub_item_id,billId=bill_id,deptId=dept_id,type=type}">
		</association>
		
		<collection property="billPayPriceVO" ofType="com.yida.data.mall.vo.BillPayPriceVO"
				select="getDeptPayPrice"
				column="{itemId=item_id,subItemId=sub_item_id,billId=bill_id,deptId=dept_id,type=type}">
		</collection>
	</resultMap>

	<!-- 账单部门缴费信息统计 -->
	<select id="listDeptItemPayInfo" resultMap="exportDeptItemMap">
		SELECT
			i.id AS item_id,
			s.id AS sub_item_id,
			i.bill_id,
		    ${deptId} AS dept_id,
		    ${type} AS type
		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}
		ORDER BY i.id
	</select>
	
	<!-- 查询部门项目或选项缴费金额 -->
	<select id="getDeptPayPrice" 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,
				    <if test="type == 1">
						s.class_name AS dept_name
					</if>
					<if test="type == 2">
						s.grade_name AS dept_name
					</if>
				FROM
					edu_bill_order_item i
				LEFT JOIN edu_bill_order o ON i.order_id = o.order_id
				LEFT JOIN edu_bill_order_student s ON o.order_id = s.order_id
				WHERE
					o.del_flag = 0
				AND i.del_flag = 0
				AND i.item_id = #{itemId}
				AND o.bill_id = #{billId}
				<if test="type == 1">
					AND s.class_id = #{deptId}
				</if>
				<if test="type == 2">
					AND s.grade_id = #{deptId}
				</if>
				AND o.pay_status = '1'
				GROUP BY
					dept_name
			</when>
			<otherwise>
				SELECT
					SUM( s.sub_item_price ) AS item_price,
					SUM( i.reduction_price ) AS reduction_price,
					<if test="type == 1">
						os.class_name AS dept_name
					</if>
					<if test="type == 2">
						os.grade_name AS dept_name
					</if>
				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
				LEFT JOIN edu_bill_order_student os ON o.order_id = os.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}
				<if test="type == 1">
					AND os.class_id = #{deptId}
				</if>
				<if test="type == 2">
					AND os.grade_id = #{deptId}
				</if>
				AND o.pay_status = '1'
				GROUP BY
					dept_name
			</otherwise>
		</choose>
	</select>
	
	<!-- 查询项目或选项缴费人数 -->
	<select id="getDeptPayNum" resultType="long">
		<choose>
			<when test="subItemId == null">
				SELECT
					COUNT(DISTINCT s.student_id) AS num
				FROM
					edu_bill_order_item i
				LEFT JOIN edu_bill_order o ON i.order_id = o.order_id
				LEFT JOIN edu_bill_order_student s ON o.order_id = s.order_id
				WHERE
					o.del_flag = 0
				AND i.del_flag = 0
				AND i.item_id = #{itemId}
				AND o.bill_id = #{billId}
				<if test="type == 1">
					AND s.class_id = #{deptId}
				</if>
				<if test="type == 2">
					AND s.grade_id = #{deptId}
				</if>
				AND o.pay_status = '1'
			</when>
			<otherwise>
				SELECT
					COUNT(DISTINCT os.student_id) AS num
				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
				LEFT JOIN edu_bill_order_student os ON o.order_id = os.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}
				<if test="type == 1">
					AND os.class_id = #{deptId}
				</if>
				<if test="type == 2">
					AND os.grade_id = #{deptId}
				</if>
				AND o.pay_status = '1'
			</otherwise>
		</choose>
	</select>
	
</mapper>
