<?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.xxfc.platform.order.mapper.DailyMembersOrderStatisticsMapper">
    <!--    获取会员订单成交金-->
    <select id="getTravelGmv" resultType="HashMap">
        select
        date(DATE_SUB(now(),interval #{day} day)) as oneDay,
        IFNULL(SUM(real_amount),0) as gmv
        from
        base_order
        where
        type=3
        AND
        date(FROM_UNIXTIME(pay_time/1000))=date(DATE_SUB(now(),interval #{day} day))
    </select>

    <select id="monthOrderTotal" resultType="com.xxfc.platform.order.entity.OrderStatistics">
        SELECT
        IFNULL( sum( gmv ), 0 ) AS totalGmv
        FROM
        daily_members_order_statistics
        WHERE
        1=1
        <if test="companyId != null">
            and branch_company_id = #{companyId}
        </if>

    </select>
    <!--    <select id="updateByExampleSelective">-->
    <!--                SELECT-->
    <!--        IFNULL(sum(gmv),0) as totalGmv-->
    <!--        FROM-->
    <!--        daily_members_order_statistics-->
    <!--        WHERE-->
    <!--        branch_company_id =#{companyId}-->
    <!--        and-->
    <!--        DATE_FORMAT(one_day,'%Y-%c')=DATE_FORMAT(DATE_SUB(NOW(),interval 1 day),'%Y-%c')-->

    <!--    </select>-->

    <select id="getMemberOrderStatistics" parameterType="com.xxfc.platform.order.pojo.OrderQuery"
            resultMap="order">
        SELECT
        <if test="type==2">
            DATE_FORMAT(date( FROM_UNIXTIME( a.account_time / 1000 ) ),'%x-%v') AS period,
        </if>
        <if test="type==3">
            DATE_FORMAT(date( FROM_UNIXTIME( a.account_time / 1000 ) ),'%Y-%m') AS period,
        </if>
        <if test="type==1">
            date( FROM_UNIXTIME( a.account_time / 1000 ) ) AS period,
        </if>
        count( a.id ) AS number,
        SUM( a.account_amount ) AS amount,
        d.member_level AS level
        FROM
        order_account a
        LEFT JOIN base_order b ON a.order_id = b.id
        LEFT JOIN order_member_detail d ON d.order_id = b.id
        WHERE
        a.account_status = 1
        AND a.account_type = 101
        AND b.type = 3
        <if test="startTime != null">
            AND a.crt_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND a.crt_time &lt;= #{endTime}
        </if>
        GROUP BY
        <if test="type==2">
            DATE_FORMAT(date( FROM_UNIXTIME( a.account_time / 1000 ) ),'%x-%v'),
        </if>
        <if test="type==3">
            DATE_FORMAT(date( FROM_UNIXTIME( a.account_time / 1000 ) ),'%Y-%m'),
        </if>

        <if test="type==1">
            date( FROM_UNIXTIME( a.account_time / 1000 ) ),
        </if>
        d.member_level
    </select>
    <resultMap id="order" type="com.xxfc.platform.order.entity.MembersOrder">
        <result column="period" property="period"></result>
        <collection property="members" ofType="com.xxfc.platform.order.pojo.Member">
            <result column="number" property="number"></result>
            <result column="amount" property="amount"></result>
            <result column="level" property="level"></result>
        </collection>
    </resultMap>
</mapper>