<?php

use Illuminate\Support\Carbon;

require_once realpath(dirname(__FILE__) . '/TestCase.php');

// function stat_echo_total($sql)
// {
//     $start = Carbon::parse('2018-01-01');
//     $end = Carbon::parse('2019-10-01');
    
//     $j = $end->diffInMonths($start);
    
//     for ($i=0; $i < $j; $i++) {
//         $month = $start->copy()->addMonths($i)->endOfMonth();
//         $res = DB::select($sql, [$month->format('Y-m-d H:i:s')]);
//         foreach ($res as $key => $value) {
//             echo $month->format('Y-m') . ',' . $value->vehicle . ',' . $value->count . PHP_EOL;
//             // break 2;
//         }
//     }
// }

// // 总用户数
// echo "总用户数,类型,数量" . PHP_EOL;
// $sql = "
// select
// 	count(distinct sim),
// 	p.vehicle
// from
// 	virtual_order_cards_partition c
// join virtual_properties p on
// 	p.company_id = c.company_id and
// 	p.package_id = c.package_id
// where
// 	c.deleted_at is null and
// 	c.refunded_at is null and
// 	c.created_at < ?
// group by
// 	p.vehicle
// ";

// stat_echo_total($sql);

// // 2G用户数
// echo "2G用户数,类型,数量" . PHP_EOL;
// $sql = "
// select
// 	count(distinct sim),
// 	p.vehicle
// from
// 	virtual_order_cards_partition c
// join virtual_properties p on
// 	p.company_id = c.company_id and
// 	p.package_id = c.package_id
// join virtual_packages pack on
//     pack.id = c.package_id and
//     pack.deleted_at is null
// where
// 	c.deleted_at is null and
// 	c.refunded_at is null and
// 	c.service_start_at < ? and
// 	pack.flows <= 100
// group by
// 	p.vehicle
// ";

// stat_echo_total($sql);

// // 4G用户数
// echo "4G用户数,类型,数量" . PHP_EOL;
// $sql = "
// select
// 	count(distinct sim),
// 	p.vehicle
// from
// 	virtual_order_cards_partition c
// join virtual_properties p on
// 	p.company_id = c.company_id and
//     p.package_id = c.package_id
// join virtual_packages pack on
//     pack.id = c.package_id and
//     pack.deleted_at is null
// where
// 	c.deleted_at is null and
// 	c.refunded_at is null and
// 	c.created_at < ? and
// 	pack.flows > 100
// group by
// 	p.vehicle
// ";

// stat_echo_total($sql);

// function stat_echo_service($sql)
// {
//     $start = Carbon::parse('2018-01-01');
//     $end = Carbon::parse('2019-10-01');
    
//     $j = $end->diffInMonths($start);
    
//     for ($i=0; $i < $j; $i++) {
//         $month = $start->copy()->addMonths($i)->endOfMonth();
//         $res = DB::select($sql, [$month->format('Y-m-d H:i:s'), $month->format('Y-m-d H:i:s')]);
//         foreach ($res as $key => $value) {
//             echo $month->format('Y-m') . ',' . $value->vehicle . ',' . $value->count . PHP_EOL;
//             // break 2;
//         }
//     }
// }

// // 活跃用户数
// echo "活跃用户数,类型,数量" . PHP_EOL;
// $sql = "
// select
// 	count(distinct sim),
// 	p.vehicle
// from
// 	virtual_order_cards_partition c
// join virtual_properties p on
// 	p.company_id = c.company_id and
// 	p.package_id = c.package_id
// where
// 	c.deleted_at is null and
// 	c.refunded_at is null and
// 	c.service_start_at < ? and c.service_end_at > ?
// group by
// 	p.vehicle
// ";

// stat_echo_service($sql);

// // 活跃2G用户数
// echo "活跃2G用户数,类型,数量" . PHP_EOL;
// $sql = "
// select
// 	count(distinct sim),
// 	p.vehicle
// from
// 	virtual_order_cards_partition c
// join virtual_properties p on
// 	p.company_id = c.company_id and
// 	p.package_id = c.package_id
// join virtual_packages pack on
//     pack.id = c.package_id and
//     pack.deleted_at is null
// where
// 	c.deleted_at is null and
// 	c.refunded_at is null and
// 	c.service_start_at < ? and c.service_end_at > ? and
// 	pack.flows <= 100
// group by
// 	p.vehicle
// ";

// stat_echo_service($sql);

// // 活跃4G用户数
// echo "活跃4G用户数,类型,数量" . PHP_EOL;
// $sql = "
// select
// 	count(distinct sim),
// 	p.vehicle
// from
// 	virtual_order_cards_partition c
// join virtual_properties p on
// 	p.company_id = c.company_id and
// 	p.package_id = c.package_id
// join virtual_packages pack on
//     pack.id = c.package_id and
//     pack.deleted_at is null
// where
// 	c.deleted_at is null and
// 	c.refunded_at is null and
// 	c.service_start_at < ? and c.service_end_at > ? and
// 	pack.flows > 100
// group by
// 	p.vehicle
// ";

// stat_echo_service($sql);



function stat_echo_price($sql)
{
    $start = Carbon::parse('2018-01-01');
    $end = Carbon::parse('2019-10-01');
    
    $j = $end->diffInMonths($start);
    
    for ($i=0; $i < $j; $i++) {
        $month = $start->copy()->addMonths($i)->endOfMonth();
        $res = DB::select($sql, [$month->format('Y-m-d H:i:s'), $month->format('Y-m-d H:i:s')]);
        foreach ($res as $key => $value) {
            echo $month->format('Y-m') . ',' . $value->vehicle . ',' . sprintf("%.02f", $value->sum/100) . ',' . sprintf("%.02f", $value->avg/100) . PHP_EOL;
            // break 2;
        }
    }
}

// 月度总收入/平均客单价
echo "月度总收入,类型,数量,平均客单价" . PHP_EOL;
$sql = "
select
    sum(c.unit_price*c.counts/pack.service_months),
    avg(c.unit_price/pack.service_months),
	p.vehicle
from
	virtual_order_cards_partition c
join virtual_properties p on
	p.company_id = c.company_id and
    p.package_id = c.package_id
join virtual_packages pack on
    pack.id = c.package_id and
    pack.deleted_at is null
where
	c.deleted_at is null and
	c.refunded_at is null and
	c.service_start_at < ? and c.service_end_at > ? and
	pack.flowed = 0
group by
	p.vehicle
";

stat_echo_price($sql);

// 2G业务收入
echo "2G业务收入,类型,数量,平均客单价" . PHP_EOL;
$sql = "
select
    sum(c.unit_price*c.counts/pack.service_months),
    avg(c.unit_price/pack.service_months),
	p.vehicle
from
	virtual_order_cards_partition c
join virtual_properties p on
	p.company_id = c.company_id and
	p.package_id = c.package_id
join virtual_packages pack on
    pack.id = c.package_id and
    pack.deleted_at is null
where
	c.deleted_at is null and
	c.refunded_at is null and
	c.service_start_at < ? and c.service_end_at > ? and
	pack.flows <= 100 and
	pack.flowed = 0
group by
	p.vehicle
";

stat_echo_price($sql);

// 4G业务收入
echo "4G业务收入,类型,数量,平均客单价" . PHP_EOL;
$sql = "
select
    sum(c.unit_price*c.counts/pack.service_months),
    avg(c.unit_price/pack.service_months),
	p.vehicle
from
	virtual_order_cards_partition c
join virtual_properties p on
	p.company_id = c.company_id and
	p.package_id = c.package_id
join virtual_packages pack on
    pack.id = c.package_id and
    pack.deleted_at is null
where
	c.deleted_at is null and
	c.refunded_at is null and
	c.service_start_at < ? and c.service_end_at > ? and
	pack.flows > 100 and
	pack.flowed = 0
group by
	p.vehicle
";

stat_echo_price($sql);


// function stat_echo_flow($sql)
// {
//     $start = Carbon::parse('2018-01-01');
//     $end = Carbon::parse('2019-10-01');
    
//     $j = $end->diffInMonths($start);
    
//     for ($i=0; $i < $j; $i++) {
//         $month = $start->copy()->addMonths($i)->endOfMonth();
//         $res = DB::select($sql, [$month->format('Ym')]);
//         foreach ($res as $key => $value) {
//             echo $month->format('Y-m') . ',' . $value->vehicle . ',' . sprintf("%.02f", $value->avg) . PHP_EOL;
//             // break 2;
//         }
//     }
// }


// // 用户平均使用流量
// echo "用户平均使用流量,类型,平均流量(Mib)" . PHP_EOL;
// $sql = "
// select
//     avg(f.mebibyte),
// 	p.vehicle
// from
//     virtual_flow_pool_months f
// join virtual_order_cards c on
// 	f.sim = c.sim
// join virtual_properties p on
// 	p.company_id = c.company_id and
//     p.package_id = c.package_id
// join virtual_packages pack on
//     pack.id = c.package_id and
//     pack.deleted_at is null
// where
//     f.month = ?
// group by
// 	p.vehicle
// ";

// stat_echo_flow($sql);

// // 2G用户平均使用流量
// echo "2G用户平均使用流量,类型,平均流量(Mib)" . PHP_EOL;
// $sql = "
// select
//     avg(f.mebibyte),
// 	p.vehicle
// from
//     virtual_flow_pool_months f
// join virtual_order_cards c on
// 	f.sim = c.sim
// join virtual_properties p on
// 	p.company_id = c.company_id and
//     p.package_id = c.package_id
// join virtual_packages pack on
//     pack.id = c.package_id and
//     pack.deleted_at is null
// where
//     f.month = ? and
//     pack.flows <= 100
// group by
// 	p.vehicle
// ";

// stat_echo_flow($sql);

// // 4G用户平均使用流量
// echo "4G用户平均使用流量,类型,平均流量(Mib)" . PHP_EOL;
// $sql = "
// select
//     avg(f.mebibyte),
// 	p.vehicle
// from
//     virtual_flow_pool_months f
// join virtual_order_cards c on
// 	f.sim = c.sim
// join virtual_properties p on
// 	p.company_id = c.company_id and
//     p.package_id = c.package_id
// join virtual_packages pack on
//     pack.id = c.package_id and
//     pack.deleted_at is null
// where
//     f.month = ? and
//     pack.flows > 100
// group by
// 	p.vehicle
// ";

// stat_echo_flow($sql);
