<?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.device.consume.mapper.EduConsumeOrderMapper">
	
	<!-- 前端查询消费订单列表数据 -->
	<select id="listH5ConsumeOrderPage"
			resultType="com.yida.data.device.vo.consume.H5ConsumeOrderPageVO">
		SELECT o.*,
			   s.student_name
		FROM edu_consume_order o
		LEFT JOIN edu_consume_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>
	
	<!-- 前端查询消费订单详情 -->
	<select id="getH5ConsumeOrderInfo"
			resultType="com.yida.data.device.vo.consume.H5ConsumeOrderPageVO">
		SELECT o.*,
		s.student_name
		FROM edu_consume_order o
		LEFT JOIN edu_consume_order_student s ON o.order_id = s.order_id
		WHERE
		o.del_flag = 0
		<if test="orderId != null">
			AND o.order_id = #{orderId}
		</if>
		<if test="orderCode != null and orderCode != ''">
			AND o.order_code = #{orderCode}
		</if>
	</select>
	
	<!-- 查询所有已经超时订单 -->
	<select id="selectTimeOutConsumeOrder"
			resultType="com.yida.data.common.core.entity.consume.EduConsumeOrder">
		SELECT
			*
		FROM
			edu_consume_order
		WHERE
			order_status = '0'
		  AND TIMESTAMPDIFF( SECOND, create_date, now( ) ) > 300
	</select>
	
	<!-- 查询退款中状态的账单订单 -->
	<select id="selectRefundOrder"
			resultType="com.yida.data.device.vo.consume.ConsumeRefundOrderVO">
		SELECT
			b.order_id,
			b.order_code,
			r.refund_code,
			r.id refund_id,
			b.pay_way,
			b.dept_id
		FROM
			edu_consume_order b
		LEFT JOIN edu_consume_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>
	
	<resultMap id="consumeOrderMap" type="com.yida.data.device.vo.consume.ConsumeOrderPageVO">
		<id column="order_id" property="orderId"/>
		<result column="order_code" property="orderCode" />
		<result column="transaction_number" property="transactionNumber" />
		<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="is_refund" property="isRefund" />
		
		<association property="student" javaType="com.yida.data.common.core.entity.consume.EduConsumeOrderStudent">
			<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="listConsumeOrderPage" resultMap="consumeOrderMap">
		SELECT
			o.*,
			s.*,
			s.id as order_student_id
		FROM
			edu_consume_order o
		LEFT JOIN edu_consume_order_student s ON o.order_id = s.order_id
		WHERE
			o.del_flag = 0
		AND	o.dept_id IN
		<foreach collection="schoolIds" open="(" separator="," close=")" item="schoolId">
			#{schoolId}
		</foreach>
		<if test='dto.orderCode != null and dto.orderCode != ""'>
			AND o.order_code LIKE CONCAT( '%', #{dto.orderCode}, '%' )
		</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.stu_card_number LIKE CONCAT( '%', #{dto.searchData}, '%' )
			)
		</if>
		order by
			o.create_date desc
	</select>
	
</mapper>
