<?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.willalp.canteen.mapper.HsGiftOrderMapper">

    <resultMap type="HsGiftOrder" id="HsCanteenOrderResult">
        <result property="orderId" column="order_id"/>
        <result property="orderNumber" column="order_number"/>
        <result property="phone" column="phone"/>
        <result property="userNumber" column="user_number"/>
        <result property="userName" column="user_name"/>
        <result property="orderTotal" column="order_total"/>
        <result property="deviceNumber" column="device_number"/>
        <result property="deviceName" column="device_name"/>
        <result property="paytime" column="paytime"/>
        <result property="zfsj" column="zfsj"/>
        <result property="zfbz" column="zfbz"/>
        <result property="jgid" column="jgid"/>
        <result property="isDelete" column="is_delete"/>
        <result property="consumeSign" column="consume_sign"/>
        <result property="userName" column="user_name"/>
        <result property="patternId" column="pattern_id"/>
        <result property="placeNumber" column="place_number"/>
        <result property="phonenumber" column="phonenumber"/>
        <result property="placeName" column="place_name"/>
        <result property="cardCode" column="card_code"/>
        <result property="remark" column="remark"/>
        <result property="statusText" column="status_text"/>
        <result property="isNews" column="is_news"/>
        <result property="isNewsUser" column="is_news_user"/>
    </resultMap>

    <sql id="selectHsCanteenOrderVo">
        select a.order_id,
               a.order_number,
               a.phone,
               a.user_number,
               a.user_name,
               a.order_total,
               he.event_name,
               he.event_time,
               a.device_number,
               a.device_name,
               a.paytime,
               a.zfbz,
               a.jgid,
               a.is_delete,
               a.consume_sign,
               a.place_number,
               a.zfsj,
               a.remark,
               a.status_text,
               a.is_news,
               a.is_news_user,
               b.event_id
        from hs_gift_order a
    </sql>

    <select id="selectHsCanteenOrderList" parameterType="HsGiftOrder" resultMap="HsCanteenOrderResult">
        select
        a.order_number,
        a.order_id,
        a.phone,
        a.user_number,
        a.user_name,
        a.order_total,
        a.device_number,
        a.device_name,
        a.paytime,
        a.zfbz,
        a.jgid,
        a.is_delete,
        a.consume_sign,
        a.place_number,
        a.zfsj,
        a.remark,
        a.status_text,
        a.is_news,
        a.is_news_user,
        b.event_id
        from hs_gift_order a
        LEFT JOIN hs_gift_order_detail b ON a.order_number = b.order_number
        <where>
            a.is_delete = 0 AND b.is_delete = 0
            <if test="orderNumber != null  and orderNumber != ''">and a.order_number = #{orderNumber}</if>
            <if test="userNumber != null  and userNumber != ''">and a.user_number = #{userNumber}</if>
            <if test="userName != null  and userName != ''">and a.user_name = #{userName}</if>
            <if test="deviceNumber != null  and deviceNumber != ''">and a.device_number = #{deviceNumber}</if>
            <if test="deviceName != null  and deviceName != ''">and a.device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="patternId != null ">and c.pattern_id = #{patternId}</if>
            <if test="params.beginPaytime != null and params.beginPaytime != '' and params.endPaytime != null and params.endPaytime != ''">
                and paytime between #{params.beginPaytime} and #{params.endPaytime}
            </if>
            <if test="zfbz > 0 ">and a.zfbz = #{zfbz}</if>
            <if test="eventId != null ">and b.event_id = #{eventId}</if>
            <if test="jgid != null and jgid != ''">and a.jgid = #{jgid}</if>
            <if test="placeNumber != null and placeNumber != ''">and a.place_number = #{placeNumber}</if>
        </where>
        GROUP BY
        a.order_number
        ORDER BY
        paytime DESC
    </select>

    <select id="getSumpay" parameterType="HsGiftOrder" resultType="java.lang.String">
        select sum(order_total) as fee
        from hs_canteen_order
        where zfbz = 0

    </select>
    <select id="selectHsCanteenOrderLists" parameterType="HsGiftOrder" resultMap="HsCanteenOrderResult">
        select substr(a.paytime,1,10) as paytime ,count(a.order_id) as order_id ,sum(a.order_total) as order_total from
        (
        select paytime , order_id, order_total from hs_canteen_order
        <where>
            is_delete = 0 and zfbz=0
            <if test="jgid != null and jgid != ''">and jgid = #{jgid}</if>
        </where>
        ) a
        group by substr(paytime,1,10) order by paytime desc limit 30
    </select>
    <select id="getSumOrderNum" parameterType="HsGiftOrder" resultType="java.lang.String">
        select count(*) as total
        from hs_canteen_order
        where zfbz = 0
    </select>
</mapper>
