<?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.miaxis.wx.mapper.WxOrderMapper">

    <resultMap type="WxOrder" id="WxOrderResult">
        <result property="id"    column="id"    />
        <result property="outTradeNo"    column="out_trade_no"    />
        <result property="transactionId"    column="transaction_id"    />
        <result property="tradeType"    column="trade_type"    />
        <result property="tradeState"    column="trade_state"    />
        <result property="tradeStateDesc"    column="trade_state_desc"    />
        <result property="bankType"    column="bank_type"    />
        <result property="attach"    column="attach"    />
        <result property="successTime"    column="success_time"    />
        <result property="openid"    column="openid"    />
        <result property="goodsName"    column="goods_name"    />
        <result property="total"    column="total"    />
        <result property="payerTotal"    column="payer_total"    />
        <result property="currency"    column="currency"    />
        <result property="payerCurrency"    column="payer_currency"    />
        <result property="deviceId"    column="device_id"    />
        <result property="seat"    column="seat"    />
        <result property="reservedPhone"    column="reserved_phone"    />
        <result property="acceptChangeSeat"    column="accept_change_seat"    />
        <result property="seatId"    column="seat_id"    />
        <result property="seatNo"    column="seat_no"    />
        <result property="orderState"    column="order_state"    />
        <result property="orderPrice"    column="order_price"    />
        <result property="closeCause"    column="close_cause"    />
        <result property="realSeat"    column="real_seat"    />
        <result property="ticketCode"    column="ticket_code"    />
        <result property="ticketImage"    column="ticket_image"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectWxOrderVo">
        select * from wx_order
    </sql>

    <select id="getByOutTradeNo" parameterType="string" resultType="com.miaxis.wx.domain.WxOrder">
         select * from wx_order where out_trade_no =#{outTradeNo}
    </select>


    <select id="selectWxOrderList" parameterType="WxOrder" resultMap="WxOrderResult">
        <include refid="selectWxOrderVo"/>
        <where>
            <if test="outTradeNo != null "> and out_trade_no = #{outTradeNo}</if>
            <if test="transactionId != null  and transactionId != ''"> and transaction_id = #{transactionId}</if>
            <if test="tradeType != null  and tradeType != ''"> and trade_type = #{tradeType}</if>
            <if test="tradeState != null  and tradeState != ''"> and trade_state = #{tradeState}</if>
            <if test="tradeStateDesc != null  and tradeStateDesc != ''"> and trade_state_desc = #{tradeStateDesc}</if>
            <if test="bankType != null  and bankType != ''"> and bank_type = #{bankType}</if>
            <if test="attach != null  and attach != ''"> and attach = #{attach}</if>
            <if test="successTime != null "> and success_time = #{successTime}</if>
            <if test="openid != null  and openid != ''"> and openid = #{openid}</if>
            <if test="goodsName != null  and goodsName != ''"> and goods_name like concat('%', #{goodsName}, '%')</if>
            <if test="total != null "> and total = #{total}</if>
            <if test="payerTotal != null "> and payer_total = #{payerTotal}</if>
            <if test="currency != null  and currency != ''"> and currency = #{currency}</if>
            <if test="payerCurrency != null  and payerCurrency != ''"> and payer_currency = #{payerCurrency}</if>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="seat != null  and seat != ''"> and seat = #{seat}</if>
            <if test="reservedPhone != null  and reservedPhone != ''"> and reserved_phone = #{reservedPhone}</if>
            <if test="acceptChangeSeat != null "> and accept_change_seat = #{acceptChangeSeat}</if>
            <if test="seatId != null  and seatId != ''"> and seat_id = #{seatId}</if>
            <if test="seatno != null  and seatno != ''"> and seat_no = #{seatNo}</if>
            <if test="orderState != null  and orderState != ''"> and order_state = #{orderState}</if>
            <if test="orderPrice != null "> and order_price = #{orderPrice}</if>
            <if test="closeCause != null  and closeCause != ''"> and close_cause = #{closeCause}</if>
            <if test="realSeat != null  and realSeat != ''"> and real_seat = #{realSeat}</if>
            <if test="ticketCode != null  and ticketCode != ''"> and ticket_code = #{ticketCode}</if>
            <if test="ticketImage != null  and ticketImage != ''"> and ticket_image = #{ticketImage}</if>
        </where>
    </select>


    <select id="selectOrderList" resultType="com.miaxis.wx.domain.WxOrder">
        select * from wx_order
         where openid=#{openid} and trade_state ='SUCCESS'
        <if test="days != null  and days != ''">
            and  DATE_SUB(CURDATE(), INTERVAL #{days} DAY) &lt;= date(create_time)
        </if>
        <if test="orderStatus != null  and orderStatus != ''">
            and  order_status = #{orderStatus}
        </if>
         order by create_time desc
    </select>


    <select id="selectPcOrderList" resultType="com.miaxis.wx.domain.WxOrder">
        select * from wx_order
        where trade_state ='SUCCESS'
        <if test="outTradeNo != null  and outTradeNo != ''">
            and  out_trade_no = #{outTradeNo}
        </if>
        <if test="orderStatus != null  and orderStatus != ''">
            and  order_status = #{orderStatus}
        </if>
        <if test="goodsType != null  and goodsType != ''">
            and  goods_type = #{goodsType}
        </if>
        <if test="startTime != null">
            and DATE_FORMAT(create_time,'%Y%m%d')<![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null">
            and DATE_FORMAT(create_time,'%Y%m%d') <![CDATA[ < ]]> #{endTime}
        </if>
        order by create_time desc
    </select>

    <select id="QuerySchoolOrderList" resultType="com.miaxis.wx.vo.WxSchoolOrder">
        select t2.nick_name,t1.id,t1.out_trade_no,t1.success_time,t1.total,t2.head_image from wx_order t1 left join user_info t2 on (t1.openid =t2.openid)
        where trade_state ='SUCCESS'
        <if test="schoolName != null  and schoolName != ''">
            and  t1.openid in (select openid from user_info where school_name = #{schoolName})
        </if>
        <if test="startTime != null">
            and DATE_FORMAT(t1.create_time,'%Y%m%d')<![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null">
            and DATE_FORMAT(t1.create_time,'%Y%m%d') <![CDATA[ < ]]> #{endTime}
        </if>
        order by t1.create_time desc
    </select>
    <select id="getTotalPay" resultType="int">
        select sum(total) as totalPay from wx_order t1
        where trade_state ='SUCCESS'
        <if test="schoolName != null  and schoolName != ''">
            and  t1.openid in (select openid from user_info where school_name = #{schoolName})
        </if>
        <if test="startTime != null">
            and DATE_FORMAT(t1.create_time,'%Y%m%d')<![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null">
            and DATE_FORMAT(t1.create_time,'%Y%m%d') <![CDATA[ < ]]> #{endTime}
        </if>
        order by t1.create_time desc
    </select>

</mapper>
