<?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.bonus.canteen.core.dataScreening.mapper.DataScreeningMapper">

    <select id="selectTurnover" resultType="com.bonus.canteen.core.dataScreening.vo.SingleModelVO">
        select
            IFNULL(sum( real_amount ),0) AS num
        from order_info
        where order_date = #{date} and pay_state = '3' and order_state in (1,2)
        <if test="type != null">
            <choose>
                <when test="type == 11">
                    and order_type in (2,11)
                </when>
                <otherwise>
                    and order_type = #{type}
                </otherwise>
            </choose>
        </if>
    </select>
    <select id="selectOrder" resultType="com.bonus.canteen.core.dataScreening.vo.SingleModelVO">
        select
            IFNULL(count( 1 ),0) AS num
        from order_info
        where order_date = #{date} and pay_state = '3' and order_state in (1,2)
        <if test="type != null">
            <choose>
                <when test="type == 11">
                    and order_type in (2,11)
                </when>
                <otherwise>
                    and order_type = #{type}
                </otherwise>
            </choose>
        </if>
    </select>
    <select id="selectPerson" resultType="com.bonus.canteen.core.dataScreening.vo.SingleModelVO">
        select
        count( DISTINCT user_id ) AS num
        from order_info
        where order_date = #{date} and pay_state = '3' and order_state in (1,2)
        <if test="type != null">
            <choose>
                <when test="type == 11">
                    and order_type in (2,11)
                </when>
                <otherwise>
                    and order_type = #{type}
                </otherwise>
            </choose>
        </if>
    </select>
    <select id="selectDish" resultType="com.bonus.canteen.core.dataScreening.vo.SingleModelVO">
        select
            count( DISTINCT mrd2.dishes_id ) AS num
        from menu_recipe_detail mrd
        left join menu_recipe_dishes mrd2 on mrd.detail_id = mrd2.detail_id
        where mrd.apply_date = #{date} and mrd.detail_type = '2' and mrd.del_flag = '2'
    </select>
    <select id="getCanteenOrdersAndSalesTrends" resultType="com.bonus.canteen.core.dataScreening.vo.EchartsVO">
        SELECT
            IFNULL( count( 1 ), 0 ) AS orderValue,
            IFNULL(sum( real_amount ),0) AS salesValue,
            order_date as orderDate
        FROM
            order_info
        WHERE
            order_date BETWEEN #{startDate} and #{endDate} and pay_state = '3' and order_state in (1,2)  and order_type in (2,11)
        GROUP BY order_date
        order by order_date
    </select>
    <select id="getSupermarketOrderAndSalesTrend"
            resultType="com.bonus.canteen.core.dataScreening.vo.EchartsVO">
        SELECT
            IFNULL( count( 1 ), 0 ) AS orderValue,
            IFNULL(sum( real_amount ),0) AS salesValue,
            order_date as orderDate
        FROM
            order_info
        WHERE
            order_date BETWEEN #{startDate} and #{endDate} and pay_state = '3' and order_state in (1,2)  and order_type = '4'
        GROUP BY order_date
        order by order_date
    </select>
    <select id="getThisMonthDishSalesRanking" resultType="com.bonus.canteen.core.dataScreening.vo.SingleModelVO">
        SELECT
            sum(od.quantity) AS num,
            md.dishes_name as name
        FROM
            order_detail od
                left join order_info oi on oi.order_id = od.order_id
                left join menu_dishes md on md.dishes_id = od.goods_id
        WHERE
            oi.order_date between #{startDate} and #{endDate} and md.dishes_name is not null
        group by od.goods_id
        order by num desc
    </select>
    <select id="selectOrderByDate" resultType="com.bonus.canteen.core.dataScreening.vo.SingleModelVO">
        select
        IFNULL(count( 1 ),0) AS num
        from order_info
        where order_date between #{startDate} and #{endDate}  and pay_state = '3' and order_state in (1,2)
        <if test="type != null and type != ''">
            and order_type = #{type}
        </if>
    </select>
</mapper>