<?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.EduBillMapper">
	
	<resultMap id="h5BillInfoMap" type="com.yida.data.mall.vo.h5.H5BillSelectVO">
		<id column="id" property="billId"/>
		<result column="bill_title" property="billTitle"/>
		<result column="bill_start_date" property="billStartDate"/>
		<result column="bill_end_date" property="billEndDate"/>
		<result column="bill_instruction" property="billInstruction"/>
		<result column="last_enable_date" property="lastEnableDate"/>
		
		<collection property="itemList" ofType="com.yida.data.mall.vo.h5.H5BillItemVO"
				select="listH5BillItem"
				column="{billId=id,studentId=student_id}">
		</collection>
	</resultMap>
	
	<resultMap id="h5BillItemMap" type="com.yida.data.mall.vo.h5.H5BillItemVO">
		<id column="id" property="itemId"/>
		<result column="item_name" property="itemName"/>
		<result column="item_price" property="itemPrice"/>
		<result column="must_pay" property="mustPay"/>
		<result column="reduction_status" property="reductionStatus"/>
		<result column="family_type" property="familyType"/>
		<result column="reduction_price" property="reductionPrice"/>
		<result column="item_option" property="itemOption"/>
		<result column="indefinite_status" property="indefiniteStatus"/>
		
		<association property="itemPayStatus" select="getItemPayStatus"
				javaType="java.lang.Integer" jdbcType="INTEGER"
				column="{itemId=id,billId=bill_id,studentId=student_id}" />
		<collection property="subItemList" ofType="com.yida.data.mall.vo.h5.H5BillItemSubVO"
				select="listH5BillItemSub"
				column="{itemId=id,billId=bill_id,studentId=student_id}">
		</collection>
	</resultMap>
	
	<resultMap id="h5BillItemSubMap" type="com.yida.data.mall.vo.h5.H5BillItemSubVO">
		<id column="id" property="subItemId"/>
		<result column="sub_item_name" property="subItemName"/>
		<result column="sub_item_price" property="subItemPrice"/>
		
		<association property="subItemPayStatus" select="getSubItemPayStatus"
				javaType="java.lang.Integer" jdbcType="INTEGER"
				column="{subItemId=id,billId=bill_id,studentId=student_id,itemId=item_id}" />
	</resultMap>
	
	<!-- 查询缴费账单列表数据 -->
	<select id="listBillPage"
			resultType="com.yida.data.mall.vo.BillSelectPageVO">
		SELECT
			*
		FROM
			edu_bill
		WHERE
			del_flag = 0
		AND dept_id = #{dto.deptId}
	  	<if test='dto.billTitle != null and dto.billTitle != ""'>
			AND bill_title LIKE CONCAT( '%', #{dto.billTitle}, '%' )
		</if>
		<if test='dto.enableStatus != null'>
			AND enable_status = #{dto.enableStatus}
		</if>
		ORDER BY
			create_date DESC
	</select>
	
	<!-- 查询前台账单列表 -->
	<select id="listH5Bill"
			resultMap="h5BillInfoMap">
		SELECT DISTINCT
			*,
		    ${dto.studentId} as student_id
		FROM
			edu_bill b
		LEFT JOIN edu_bill_student s ON b.id = s.bill_id
		WHERE
			b.del_flag = 0
		AND s.del_flag = 0
		AND b.dept_id = #{dto.deptId}
		AND b.bill_start_date &lt; NOW( )
		AND b.bill_end_date &gt; NOW()
		AND s.student_id = #{dto.studentId}
		AND b.enable_status = 1
		ORDER BY
			b.last_enable_date DESC
	</select>
	
	<!-- 查询前台账单详情 -->
	<select id="getH5BillInfo"
			resultMap="h5BillInfoMap">
		SELECT DISTINCT
			*
		FROM
			edu_bill b
		LEFT JOIN edu_bill_student s ON b.id = s.bill_id
		WHERE
			b.del_flag = 0
		AND s.del_flag = 0
		AND b.dept_id = #{dto.deptId}
		AND b.bill_start_date &lt; NOW( )
		AND s.student_id = #{dto.studentId}
		AND b.enable_status = 1
		AND b.id = #{dto.billId}
		ORDER BY
			b.last_enable_date DESC
	</select>
	
	<!-- 查询前台账单项目 -->
	<select id="listH5BillItem"
			resultMap="h5BillItemMap">
		SELECT
			*,
		    ${studentId} as student_id
		FROM
			edu_bill_item
		WHERE
			del_flag = 0
		and	bill_id = #{billId}
	</select>
	
	<!-- 查询前台账单项目选项 -->
	<select id="listH5BillItemSub"
			resultMap="h5BillItemSubMap">
		SELECT
			*,
			${studentId} as student_id
		FROM
			edu_bill_item_sub
		WHERE
			del_flag = 0
		and item_id = #{itemId}
	</select>
	
	<!-- 查询学生缴费项目支付状态 -->
	<select id="getItemPayStatus" resultType="java.lang.Integer">
		SELECT
			IF(SUM(i.item_price) IS NULL, 0, 1) AS item_pay_status
		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'
	</select>
	
	<!-- 查询学生缴费项目子选项支付状态 -->
	<select id="getSubItemPayStatus" resultType="java.lang.Integer">
		SELECT
			IF(SUM(s.sub_item_price) IS NULL, 0, 1) AS sub_item_pay_status
		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'
	</select>
	
	<resultMap id="billOrderMap" type="com.yida.data.mall.vo.BillOrderSelectPageVO">
		<id column="order_id" property="orderId"/>
		<result column="order_code" property="orderCode" />
		<result column="transaction_number" property="transactionNumber" />
		<result column="bill_id" property="billId" />
		<result column="bill_title" property="billTitle" />
		<result column="total_money" property="totalMoney" />
		<result column="practical_money" property="practicalMoney" />
		<result column="phone" property="phone" />
		<result column="pay_date" property="payDate" />
		<result column="order_status" property="orderStatus" />
		<result column="pay_status" property="payStatus" />
		<result column="pay_way" property="payWay" />
		<result column="pay_type" property="payType" />
		<result column="is_refund" property="isRefund" />
		<result column="refund_date" property="refundDate" />
		
		<association property="student" javaType="com.yida.data.common.core.entity.mall.EduBillOrderStudent">
			<id column="order_student_id" property="id" />
			<id column="student_id" property="studentId" />
			<result column="student_name" property="studentName" />
			<result column="student_number" property="studentNumber" />
			<result column="student_family_type" property="studentFamilyType" />
			<result column="student_parent_type" property="studentParentType" />
			<result column="school_id" property="schoolId" />
			<result column="school_name" property="schoolName" />
			<result column="campus_id" property="campusId" />
			<result column="campus_name" property="campusName" />
			<result column="section_id" property="sectionId" />
			<result column="section_name" property="sectionName" />
			<result column="grade_id" property="gradeId" />
			<result column="grade_name" property="gradeName" />
			<result column="class_id" property="classId" />
			<result column="class_name" property="className" />
		</association>
	</resultMap>
	
	<!-- 查询账单订单列表数据 -->
	<select id="listBillOrderPage"
			resultMap="billOrderMap">
		SELECT
			o.*,
		    s.*,
		    s.id as order_student_id
		FROM
			edu_bill_order o
		LEFT JOIN edu_bill_order_student s ON o.order_id = s.order_id
		WHERE
		    o.del_flag = 0
		AND	o.dept_id = #{dto.schoolId}
		<if test='dto.billTitle != null and dto.billTitle != ""'>
			AND o.bill_title LIKE CONCAT( '%', #{dto.billTitle}, '%' )
		</if>
		<if test='dto.orderStatus != null and dto.orderStatus != ""'>
			AND o.order_status = #{dto.orderStatus}
		</if>
		<if test='dto.searchData != null and dto.searchData != ""'>
			AND (
			    s.student_name LIKE CONCAT( '%', #{dto.searchData}, '%' )
			    or s.student_number LIKE CONCAT( '%', #{dto.searchData}, '%' )
			)
		</if>
		<if test="studentList != null and studentList.size() > 0">
			AND s.student_id in
			<foreach collection="studentList" open="(" close=")" separator="," item="student">
				#{student.id}
			</foreach>
		</if>
		order by
			create_date desc
	</select>
	
	<!-- 查询已开始未结束的账单 -->
	<select id="selectNeedStatisticsBill"
		resultType="com.yida.data.common.core.entity.mall.EduBill">
		SELECT
			*
		FROM
			edu_bill
		WHERE
			del_flag = 0
		AND NOW( ) BETWEEN bill_start_date
		AND date_add( bill_end_date, INTERVAL 1 HOUR )
	</select>
	
	<!-- 查询已开始的账单 -->
	<select id="selectStartBill"
			resultType="com.yida.data.common.core.entity.mall.EduBill">
		SELECT
			*
		FROM
			edu_bill
		WHERE
			del_flag = 0
		  AND NOW( ) >= bill_start_date
	</select>
	
</mapper>
