<?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.czg.service.order.mapper.ShopOrderStatisticMapper">
    <select id="getTradeData" resultType="com.czg.order.entity.ShopOrderStatistic">
        select
        sum(sale_amount) as sale_amount,
        sum(sale_count) as sale_count,
        sum(discount_amount) as discount_amount,
        sum(discount_count) as discount_count,
        sum(refund_amount) as refund_amount,
        sum(refund_count) as refund_count,
        sum(wechat_pay_count) as wechat_pay_count,
        sum(wechat_pay_amount) as wechat_pay_amount,
        sum(ali_pay_count) as ali_pay_count,
        sum(ali_pay_amount) as ali_pay_amount,
        sum(credit_pay_count) as credit_pay_count,
        sum(credit_pay_amount) as credit_pay_amount,
        sum(member_pay_count) as member_pay_count,
        sum(member_pay_amount) as member_pay_amount,
        sum(scan_pay_count) as scan_pay_count,
        sum(scan_pay_amount) as scan_pay_amount,
        sum(cash_pay_count) as cash_pay_count,
        sum(cash_pay_amount) as cash_pay_amount,
        sum(recharge_amount) as recharge_amount,
        avg(customer_unit_price) as customer_unit_price,
        avg(table_turnover_rate) as table_turnover_rate,
        max(update_time) as update_time
        from tb_shop_order_statistic
        where shop_id = #{shopId}
        <if test="beginDate != null and beginDate != ''">
            and create_day >= str_to_date(#{beginDate}, '%Y-%m-%d')
        </if>
        <if test="endDate != null and endDate != ''">
            <![CDATA[
            and create_day <= str_to_date(#{endDate}, '%Y-%m-%d')
            ]]>
        </if>
        group by shop_id
    </select>
    <select id="getNewMemberCount" resultType="java.lang.Long">
        select count(1) from tb_shop_user where shop_id = #{shopId}
        <if test="beginDate != null and beginDate != ''">
            and create_time >= str_to_date(#{beginDate}, '%Y-%m-%d %H:%i:%s')
        </if>
        <if test="endDate != null and endDate != ''">
            <![CDATA[
            and create_time <= str_to_date(#{endDate}, '%Y-%m-%d %H:%i:%s')
            ]]>
        </if>
    </select>
    <select id="getPayTypeAmountCount" resultType="java.util.Map">
        SELECT
            t1.pay_type as payType,
            sum(t1.pay_amount) as amount,
            sum(t1.refund_amount) as refund,
            count(case when t1.refund_amount>0 then 1 end) as refundCount,
            sum(t1.discount_amount) as discount,
            count(case when t1.discount_amount>0 then 1 end) as discountCount,
            count(*) as count
        FROM
        tb_order_info t1
        WHERE  t1.shop_id = #{shopId}
        and t1.paid_time is not null
        <if test="beginDate != null and beginDate != ''">
            and t1.create_time >= str_to_date(#{beginDate}, '%Y-%m-%d %H:%i:%s')
        </if>
        <if test="endDate != null and endDate != ''">
            <![CDATA[
            and t1.create_time <= str_to_date(#{endDate}, '%Y-%m-%d %H:%i:%s')
            ]]>
        </if>
        group by t1.pay_type
    </select>
    <select id="getVipRechargeAmountCount" resultType="java.util.Map">
        select
            t1.biz_code as bizCode,sum(t1.amount) as amount,count(*) as count
        from tb_shop_user_flow t1
        where t1.shop_id = #{shopId}
        <if test="beginDate != null and beginDate != ''">
            and t1.create_time >= str_to_date(#{beginDate}, '%Y-%m-%d %H:%i:%s')
        </if>
        <if test="endDate != null and endDate != ''">
            <![CDATA[
            and t1.create_time <= str_to_date(#{endDate}, '%Y-%m-%d %H:%i:%s')
            ]]>
        </if>
        group by t1.biz_code
    </select>
    <select id="getCustomerUnitPrice" resultType="java.math.BigDecimal">
        SELECT
        ifnull((sum(t1.pay_amount)-sum(t1.refund_amount))/count(ifnull(case t1.seat_num when 0 then 1 end,1)),0.00) as customerUnitPrice
        FROM
        tb_order_info t1
        WHERE t1.shop_id = #{shopId}
        and t1.paid_time is not null
        <if test="beginDate != null and beginDate != ''">
            and t1.create_time >= str_to_date(#{beginDate}, '%Y-%m-%d %H:%i:%s')
        </if>
        <if test="endDate != null and endDate != ''">
            <![CDATA[
            and t1.create_time <= str_to_date(#{endDate}, '%Y-%m-%d %H:%i:%s')
            ]]>
        </if>
    </select>
    <select id="getTableTurnoverRate" resultType="java.math.BigDecimal">
        SELECT
            ifnull((count(*)-count(DISTINCT t1.table_code))/count(DISTINCT t1.table_code)*100,0.00)
        FROM
        tb_order_info t1
        WHERE t1.shop_id = #{shopId}
        and t1.paid_time is not null
        <if test="beginDate != null and beginDate != ''">
            and t1.create_time >= str_to_date(#{beginDate}, '%Y-%m-%d %H:%i:%s')
        </if>
        <if test="endDate != null and endDate != ''">
            <![CDATA[
            and t1.create_time <= str_to_date(#{endDate}, '%Y-%m-%d %H:%i:%s')
            ]]>
        </if>
    </select>
    <select id="getShopIdList" resultType="java.lang.Long">
        select id from tb_shop_info order by id
    </select>
</mapper>
