<?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.telephone.mapper.EduTelephoneOrderMapper">
    <sql id="pageRes">
        select
        a.id,b.student_name,b.card_no,a.money,a.pay_phone,a.pay_relation,a.after_balance,a.order_status
        ,a.create_date
        from edu_telephone_order a
        left join edu_telephone_card b on a.card_id = b.id
        where b.del_flag = 0
        <if test="dto.schoolId != null">
            and b.school_id=#{dto.schoolId}
        </if>
        <if test="dto.campusId != null">
            and b.campus_id=#{dto.campusId}
        </if>
        <if test="dto.sectionId != null">
            and b.section_id=#{dto.sectionId}
        </if>
        <if test="dto.gradeId != null">
            and b.grade_id=#{dto.gradeId}
        </if>
        <if test="dto.classId != null">
            and b.class_id=#{dto.classId}
        </if>
        <if test="dto.cardNo != null and dto.cardNo != ''">
            and b.card_no like concat('%',#{dto.cardNo},'%')
        </if>
        <if test="dto.studentName != null and dto.studentName != ''">
            and b.student_name like concat('%',#{dto.studentName},'%')
        </if>
        <if test="dto.payPhone != null and dto.payPhone != ''">
            and a.pay_phone like concat('%',#{dto.payPhone},'%')
        </if>
        <if test="dto.startTime != null">
            and a.create_date BETWEEN #{dto.startTime} and #{dto.endTime}
        </if>
        order by a.create_date desc
    </sql>
    <select id="listOrderPage" resultType="com.yida.data.device.vo.telephone.OrderPageVO">
        <include refid="pageRes">
        </include>
    </select>

    <select id="listOrder" resultType="com.yida.data.device.vo.telephone.OrderPageVO">
        <include refid="pageRes">
        </include>
    </select>

    <select id="getOrder" resultType="com.yida.data.device.vo.telephone.OrderInfoVO">
        select
        a.id,b.student_name,b.card_no,b.school_name,concat(b.grade_name,b.class_name) gradeClass
        ,a.money,a.pay_phone,a.pay_relation,a.after_balance,a.order_status
        ,a.create_date,a.pay_date
        from edu_telephone_order a
        left join edu_telephone_card b on a.card_id = b.id
        where b.del_flag = 0 and a.id=#{id}
        order by a.create_date desc
    </select>
    <select id="getTotalMoney" resultType="java.math.BigDecimal">
        SELECT
        sum(money)
        FROM
        edu_telephone_order
        WHERE 1=1
        and school_id = #{schoolId}
        and order_status = 2
        <if test="type == 1">
            and TO_DAYS(NOW()) - TO_DAYS(create_date) &lt;= 1
        </if>
        <if test="type == 2">
            and  DATE_FORMAT(create_date, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
        </if>
    </select>
</mapper>
