<?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="org.springblade.modules.finance.mapper.OrderMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="orderResultMap" type="org.springblade.modules.finance.entity.Order">
        <result column="id" property="id"/>
        <result column="is_delete" property="isDelete"/>
        <result column="create_user_id" property="createUserId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user_id" property="updateUserId"/>
        <result column="update_time" property="updateTime"/>
        <result column="order_no" property="orderNo"/>
        <result column="order_type" property="orderType"/>
        <result column="quantity" property="quantity"/>
        <result column="order_state" property="orderState"/>
        <result column="total_amount" property="totalAmount"/>
        <result column="payment_amount" property="paymentAmount"/>
        <result column="payment_mode" property="paymentMode"/>
        <result column="payment_time" property="paymentTime"/>
        <result column="payment_state" property="paymentState"/>
        <result column="discount_amount" property="discountAmount"/>
        <result column="postage" property="postage"/>
        <result column="invoice_state" property="invoiceState"/>
        <result column="is_aftersales" property="isAftersales"/>
        <result column="send_time" property="sendTime"/>
        <result column="record_time" property="recordTime"/>
        <result column="distributeMode" property="distributemode"/>
        <result column="logistics_state" property="logisticsState"/>
        <result column="consignee" property="consignee"/>
        <result column="consignee_phone_number" property="consigneePhoneNumber"/>
        <result column="is_cart" property="isCart"/>
        <result column="transaction_no" property="transactionNo"/>
    </resultMap>


    <select id="selectOrderPage" resultMap="orderResultMap">
        select * from biz_order where is_deleted = 0
    </select>
    <select id="selectTimeoutOrder" resultType="org.springblade.modules.finance.entity.Order">
        SELECT * FROM biz_order WHERE order_state = 1 and TIMESTAMPDIFF(SECOND,create_time,NOW()) > #{seconds}
    </select>

    <select id="selectStaticByDay"  resultType="java.util.Map">
       SELECT
        date_table.dates dateValue,
        IFNULL( temp.totalAmount, 0.00 ) totalAmount,
        IFNULL( temp.totalCount, 0 ) totalCount
        FROM
            (
                SELECT
                @s := @s + 1 AS indexs,
                DATE_FORMAT( DATE( DATE_SUB( CURRENT_DATE, INTERVAL @s DAY ) ), '%Y-%m-%d' ) AS dates
                FROM
                mysql.help_topic,
                ( SELECT @s := - 1 ) temp
                WHERE
                @s <![CDATA[<]]> #{dayNum}
                ORDER BY
                dates
            ) date_table
        LEFT JOIN (
            SELECT
                IFNULL( SUM( total_amount ), 0.00 ) totalAmount,count( 1 ) totalCount, DATE( create_time ) dateValue
                FROM biz_order
            WHERE
                order_state NOT IN ( 1, 5 )
                AND DATE_SUB( CURDATE(), INTERVAL #{dayNum} DAY ) <![CDATA[<=]]> DATE( create_time )
            GROUP BY DATE( create_time )
            ) temp ON date_table.dates = temp.dateValue
        ORDER BY
        date_table.dates
    </select>
    <select id="selectCurrYear" resultType="java.util.Map">
        SELECT
            CONCAT( m_table.m, '月' ) dateValue,
            IFNULL( temp.totalAmount, 0.00 ) totalAmount,
            IFNULL( temp.totalCount, 0 ) totalCount
        FROM
        ( SELECT @s := @s + 1 AS m FROM mysql.help_topic, ( SELECT @s := 0 ) temp WHERE @s <![CDATA[<]]> 12 ) m_table
        LEFT JOIN (
        SELECT
        IFNULL( SUM( total_amount ), 0.00 ) totalAmount,
        count( 1 ) totalCount,
        MONTH ( create_time ) m
        FROM
            biz_order
        WHERE
            order_state NOT IN ( 1, 5 )
        AND YEAR ( create_time ) = YEAR (
            NOW())
        GROUP BY
        MONTH ( create_time )) temp ON temp.m = m_table.m
    </select>
</mapper>
