<?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.cf.imes.module.executor.dal.mysql.order.OrderSupStatisticsMapper">

    <sql id="dateFormat">
        <if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@QUARTER.getValue()">
            ,CONCAT(YEAR(o.order_date), '-', QUARTER(o.order_date)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@MONTH.getValue()">
            ,CONCAT(YEAR(o.order_date), '-', MONTH(o.order_date)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@WEEK.getValue()">
            ,CONCAT(YEAR(o.order_date), '-', MONTH(o.order_date), '-', FLOOR((DayOfMonth(o.order_date)-1)/7)+1) AS date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@DAY.getValue()">
            ,CONCAT(YEAR(o.order_date), '-', MONTH(o.order_date), '-', DAY(o.order_date)) as date
        </if>
    </sql>

    <select id="selectOrderSquareProduceToday" resultType="java.lang.Double">
        SELECT SUM(op.area) as area
        FROM orders o
                 LEFT JOIN order_plate op on op.order_id = o.id
        WHERE o.deleted = 0 AND o.create_time BETWEEN #{startTime} AND #{endTime}
    </select>

    <select id="selectOrderCountLapseByOrderDate"
            resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatisticsIsLapseRespVO">
        select count(o.id) as orderCount
        <include refid="dateFormat"/>
        from orders o
        where o.order_date between #{req.createTime[0]} and #{req.createTime[1]}
        and (o.deleted = 1 or o.status = 0)
        group by date;
    </select>

    <select id="selectOrderCountNotLapseByOrderDate"
            resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatisticsIsLapseRespVO">
        select count(o.id) as orderCount
        <include refid="dateFormat"/>
        from orders o
        where o.order_date between #{req.createTime[0]} and #{req.createTime[1]}
        and o.deleted = 0 and o.status != 0
        group by date;
    </select>

    <select id="selectPlateAreaLapseByOrderDate"
            resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatisticsAreaRespVO">
        select sum(op.area) as areaSum
        <include refid="dateFormat"/>
        from orders o
        left join order_plate op on op.order_id = o.id and op.deleted = 0
        where o.order_date between #{req.createTime[0]} and #{req.createTime[1]}
        and (o.deleted = 1 or o.status = 0)
        group by date;
    </select>

    <select id="selectPlateAreaNotLapseByOrderDate"
            resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatisticsAreaRespVO">
        select sum(op.area) as areaSum
        <include refid="dateFormat"/>
        from orders o
        left join order_plate op on op.order_id = o.id and op.deleted = 0
        where o.order_date between #{req.createTime[0]} and #{req.createTime[1]}
        and o.deleted = 0 and o.status != 0
        group by date;
    </select>

</mapper>