<?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.OrderStatisticsMapper">
    <select id="selectOrderSquareProduce" resultType="java.lang.Double">
        SELECT SUM(op.area) as area FROM orders o
        JOIN order_plate op on op.order_id = o.id
        WHERE o.deleted = 0 and o.status = 3;
    </select>

    <sql id="dateFormat">
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@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.framework.common.enums.StatisticsUnit@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.framework.common.enums.StatisticsUnit@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.framework.common.enums.StatisticsUnit@DAY.getValue()">
            ,CONCAT(YEAR(o.order_date), '-', MONTH(o.order_date), '-', DAY(o.order_date)) as date
        </if>
    </sql>

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

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

    <select id="selectOrderAreaGroupByOrderDate"
            resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatisticsAreaRespVO">
        select sum(o.area) as areaSum
        <include refid="dateFormat"/>
        from orders o
        where o.order_date between #{req.createTimeForQry[0]} and #{req.createTimeForQry[1]}
        and o.deleted = 0
        group by date;
    </select>
</mapper>