<?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.iotechn.unimall.data.mapper.OrderMapper">


    <select id="selectOrderPage" resultType="com.iotechn.unimall.data.dto.order.OrderDTO">
        SELECT
            id,
            order_no AS orderNo,
            `status`,
            actual_price AS actualPrice,
            gmt_create AS gmtCreate
        FROM
            unimall_order
        WHERE
            user_id = #{userId}
            <if test="status != null and status != 0">
                AND `status` = #{status}
            </if>
        ORDER BY id DESC
            LIMIT #{offset}, #{limit}

    </select>

    <select id="countOrder" resultType="java.lang.Long">
        SELECT
            count(1)
        FROM
            unimall_order
        WHERE
            user_id = #{userId}
        <if test="status != null and status != 0">
            AND `status` = #{status}
        </if>
    </select>

    <select id="selectAreaStatistics" resultType="com.iotechn.unimall.data.model.KVModel">
        SELECT
            `province` AS `key`,
            count( 1 ) AS `value`
        FROM
            unimall_order
        WHERE
            `province` IS NOT NULL
        GROUP BY
            `province`
    </select>

    <select id="selectChannelStatistics" resultType="com.iotechn.unimall.data.model.KVModel">
        SELECT
            `channel` AS `key`,
            count( 1 ) AS `value`
        FROM
            unimall_order
        GROUP BY
            `channel`
    </select>

    <select id="selectOrderCountStatistics" resultType="com.iotechn.unimall.data.model.KVModel">
        SELECT
            DATE_FORMAT( gmt_create, '%Y-%m-%d' ) AS `key`,
            count( 1 ) AS `value`
        FROM
            unimall_order AS u
        WHERE
            gmt_create &gt; #{gmtStart}
        GROUP BY
            `key`;
    </select>

    <select id="selectOrderSumStatistics" resultType="com.iotechn.unimall.data.model.KVModel">
        SELECT
            DATE_FORMAT( gmt_create, '%Y-%m-%d' ) AS `key`,
            sum( pay_price ) AS `value`
        FROM
            unimall_order AS u
        WHERE
            pay_price IS NOT NULL
            AND gmt_create &gt; #{gmtStart}
        GROUP BY
            `key`;
    </select>

    <select id="selectExpireOrderNos" resultType="java.lang.String">
        SELECT
           order_no
        FROM
            unimall_order
        WHERE
            `status` = #{status}
            AND gmt_update &lt; #{time}
    </select>


</mapper>