<?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.plate.OrderPlateStatisticsMapper">
    <select id="selectPlateCountList" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderGoodsPlateRespVO">
        select g.goods_id,
        count(a.id) as orderCount
        from order_goods g
        left join order_plate a ON g.id = a.goods_id and a.deleted = 0
        join orders o ON o.id = a.order_id and o.deleted = 0
        where g.deleted = 0
        and o.order_date between #{req.createTimeForQry[0]} and #{req.createTimeForQry[1]}
        group by g.goods_id
        order by orderCount desc
        limit 10;
    </select>

    <select id="selectTopTenPlateCountListGroupByOrderDate" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderGoodsPlateRespVO">
        SELECT g.goods_id,
        MIN(g.material) AS material,
        MIN(g.brand) AS brand,
        MIN(g.color) AS color,
        MIN(g.goods_name) AS goods_name,
        MIN(g.height) AS height,
        MIN(g.spec) AS spec,
        MIN(g.texture) AS texture,
        MIN(g.thickness) AS thickness,
        MIN(g.width) AS width,
        COUNT(a.id) AS orderCount
        <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 orderDate
        </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 orderDate
        </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 orderDate
        </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 orderDate
        </if>
        FROM order_goods g
        LEFT JOIN order_plate a ON g.id = a.goods_id and a.deleted = 0
        JOIN orders o ON o.id = a.order_id and o.deleted = 0
        WHERE g.deleted = 0
        AND o.order_date between #{req.createTimeForQry[0]} and #{req.createTimeForQry[1]}
        and g.goods_id in
        <foreach item="goodsId" collection="goodsIds" open="(" separator="," close=")">
            #{goodsId}
        </foreach>
        GROUP BY
        g.goods_id,orderDate;
    </select>

    <select id="selectPlateAreaList" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderGoodsPlateRespVO">
        select g.goods_id,
               SUM(a.area) AS orderArea
        from order_goods g
                 left join order_plate a ON g.id = a.goods_id and a.deleted = 0
                 left join orders o ON o.id = a.order_id and o.deleted = 0
        where g.deleted = 0
        and o.order_date between #{req.createTimeForQry[0]} and #{req.createTimeForQry[1]}
        group by g.goods_id
        order by orderArea desc
        limit 10;
    </select>

    <select id="selectTopTenPlateAreaListGroupByOrderDate"
            resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderGoodsPlateRespVO">
        SELECT g.goods_id,
        MIN(g.material) AS material,
        MIN(g.brand) AS brand,
        MIN(g.color) AS color,
        MIN(g.goods_name) AS goods_name,
        MIN(g.height) AS height,
        MIN(g.spec) AS spec,
        MIN(g.texture) AS texture,
        MIN(g.thickness) AS thickness,
        MIN(g.width) AS width,
        SUM(a.area) AS orderArea
        <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 orderDate
        </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 orderDate
        </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 orderDate
        </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 orderDate
        </if>
        FROM order_goods g
        LEFT JOIN order_plate a ON g.id = a.goods_id and a.deleted = 0
        LEFT JOIN orders o ON o.id = a.order_id and o.deleted = 0
        WHERE g.deleted = 0
        AND o.order_date between #{req.createTimeForQry[0]} and #{req.createTimeForQry[1]}
        and g.goods_id in
        <foreach item="goodsId" collection="goodsIds" open="(" separator="," close=")">
            #{goodsId}
        </foreach>
        GROUP BY
        g.goods_id,orderDate;
    </select>
</mapper>