<?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.school.transaction.mapper.EduApplyProductOrderMapper">
  <resultMap id="baseMap" type="com.yida.data.common.core.entity.apply.EduApplyProductOrder">
    <id column="order_id" property="orderId"/>
    <result column="create_date" property="createDate"/>
    <result column="order_code" property="orderCode"/>
    <result column="transaction_number" property="transactionNumber"/>
    <result column="product_id" property="productId"/>
    <result column="product_name" property="productName"/>
    <result column="product_total_price" property="productTotalPrice"/>
    <result column="product_num" property="productNum"/>
    <result column="pracctical_money" property="praccticalMoney"/>
    <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"/>
    <result column="school_id" property="schoolId"/>
    <collection property="orderStudentList"
      ofType="com.yida.data.common.core.entity.apply.EduApplyProductOrderStudent">
      <result property="studentId" column="student_id"/>
      <result property="studentName" column="student_name"/>
    </collection>
  </resultMap>
  <select id="listPageEduProductOrder"
    parameterType="com.yida.data.common.core.entity.apply.EduApplyProductOrder"
    resultType="com.yida.data.common.core.entity.apply.EduApplyProductOrder">
    SELECT
    distinct
    a.order_id,
    a.order_code,
    c.school_name,
    a.product_id,
    a.product_name,
    a.product_price,
    a.product_total_price,
    a.pracctical_money,
    a.phone,
    a.order_status,
    a.create_date
    FROM
    edu_apply_product_order a
    left join edu_product d on a.product_id=d.product_id
    left JOIN edu_apply_product_refund b ON a.order_code = b.order_code
    LEFT JOIN edu_apply_product_order_student c ON a.order_id = c.order_id
    WHERE a.del_flag = 0
    and c.school_id in
    <foreach collection="schoolIds" item="schoolId" open="(" close=")" separator=",">
      #{schoolId}
    </foreach>
    <if test="dto.studentName != null and dto.studentName != ''">
      and c.student_name like concat('%',#{dto.studentName},'%')
    </if>
    <if test="dto.studentNumber != null and dto.studentNumber != ''">
      and c.student_number =#{dto.studentNumber}
    </if>
    <if test="dto.provinceId != null">
      and c.province_id=#{dto.provinceId}
    </if>
    <if test="dto.cityId != null">
      and c.city_id=#{dto.cityId}
    </if>
    <if test="dto.countyId != null">
      and c.county_id=#{dto.countyId}
    </if>
    <if test="dto.townId != null">
      and c.town_id=#{dto.townId}
    </if>

    <if test="dto.campusId != null">
      and c.campus_id=#{dto.campusId}
    </if>
    <if test="dto.sectionId != null">
      and c.section_id=#{dto.sectionId}
    </if>
    <if test="dto.gradeId != null">
      and c.grade_id=#{dto.gradeId}
    </if>
    <if test="dto.classId != null">
      and c.class_id=#{dto.classId}
    </if>
    <if test="dto.orderStatus != null">
      and order_status=#{dto.orderStatus}
    </if>
    <if test="dto.orderCode != null and dto.orderCode != ''">
      and a.order_code like concat('%',#{dto.orderCode},'%')
    </if>
    <if test="dto.phone != null and dto.phone != ''">
      and phone=#{dto.phone}
    </if>
    <if test="dto.productName != null and dto.productName != ''">
      and a.product_name like concat('%',#{dto.productName},'%')
    </if>
    <if test="dto.startTime != null and dto.endTime != null">
      and a.create_date between #{dto.startTime} and #{dto.endTime}
    </if>
    order by a.create_date desc
  </select>

  <select id="listEduProductOrder" parameterType="com.yida.data.school.vo.transcation.EduProductOrderVO"
    resultType="com.yida.data.common.core.entity.apply.EduApplyProductOrder">
    SELECT
    distinct
    a.order_id,
    a.order_code,
    a.product_name,
    a.order_status,
    a.create_date,
    a.product_total_price,
    a.pracctical_money,
    GROUP_CONCAT(b.student_name) as studentList
    FROM
    edu_apply_product_order a
    LEFT JOIN edu_apply_product_order_student b ON a.order_id = b.order_id
    left join edu_product c on a.product_id=c.product_id
    WHERE c.school_id=#{schoolId} and a.Phone = #{phone}
    and a.del_flag = 0
    <if test="schoolId != null">
      AND b.school_id = #{schoolId}
    </if>
    <if test="studentId != null">
      AND b.student_id = #{studentId}
    </if>
    GROUP BY a.order_id desc
    ORDER BY a.create_date DESC
  </select>

  <select id="selectExpireOrder" resultMap="baseMap">
    select a.*,b.student_id,b.student_name,b.school_id
    from edu_apply_product_order a
    left join edu_apply_product_order_student b on a.order_id=b.order_id
    where order_status=0
    and TIMESTAMPDIFF(SECOND, create_date, #{now}) > 900
  </select>

  <select id="countStudentOrder" resultType="Integer">
    select count(1)
    from edu_apply_product_order a
    inner join edu_apply_product_order_student b on a.order_id = b.order_id
    where a.del_flag=0
    and a.order_status in ('0','1')
    and a.pay_status='0'
    and a.product_id =#{productId}
    and b.student_id=#{studentId}
  </select>

  <select id="selectNotPayOrderStudent"
    resultType="com.yida.data.common.core.entity.apply.EduApplyProductOrderStudent">
    select b.*
    from edu_apply_product_order a
    left join edu_apply_product_order_student b on a.order_id=b.order_id
    where b.student_id=#{studentId}
    and a.product_id=#{productId}
    and a.del_flag=0
    and a.order_status in ('0','1')
    and a.pay_status='0'
    order by a.create_date desc
    limit 1
  </select>

  <select id="selectRefundOrder"
    resultType="com.yida.data.school.vo.transcation.RefundOrderVO">
    select a.order_id, a.order_code, b.refund_code, b.id refund_id, a.pay_way, c.school_id
    from edu_apply_product_order a
           left join edu_apply_product_refund b on a.order_code = b.order_code
           left join edu_product c on a.product_id = c.product_id
    where a.del_flag = 0
      and a.order_status = 3
      and pay_status = 1
  </select>

  <select id="getOrderByCode" resultMap="baseMap">
    select a.*,b.student_id,b.student_name,b.school_id
    from edu_apply_product_order a
    left join edu_apply_product_order_student b on a.order_id = b.order_id
    where a.del_flag = 0 and a.order_code=#{orderCode}
  </select>
</mapper>