<?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.PlaceIncomeMapper">


    <select id="selectPlaceIncome" parameterType="HsGift"
            resultType="com.willalp.canteen.domain.vo.PlaceIncomeVo">
        select t.place_name as placeName,
                sum(t.total) as total,
                date(#{beginTime}) as beginTime,
                date(#{endTime}) as endTime
        from
        (
        SELECT
            a.order_total as total,
            b.place_name,
            a.paytime as sj,
            a.jgid
        FROM
            hs_canteen_order a
        JOIN hs_canteen_dining_place b ON a.place_number = b.place_number and a.zfbz != 3
        union all
        SELECT
            -a.order_total as total,
            b.place_name,
            a.zfsj as sj,
            a.jgid
        FROM
            hs_canteen_order a
        JOIN hs_canteen_dining_place b ON a.place_number = b.place_number and a.zfbz = 1
        union all
        SELECT
            -a.order_total as total,
            b.place_name,
            a.paytime as sj,
            a.jgid
        FROM
            hs_canteen_order a
        JOIN hs_canteen_dining_place b ON a.place_number = b.place_number and a.zfbz = 3
        ) t
        where
            t.jgid = #{jgid}
            <if test="@sqlt.Ognl@isNotEmpty(beginTime)">
                and date(t.sj) >= date(#{beginTime})
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(endTime)">
                and date(t.sj) &lt;= date(#{endTime})
            </if>
        group by t.place_name
    </select>
</mapper>
