<?php

namespace App\Domains\Stats\Services;

use App\Core\Service;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use App\Domains\Virtual\Services\CompanyService;

class QuantityService extends Service
{
    /**
     * 构造函数
     *
     * @return void
     */
    public function __construct() { }

    /**
     * 统计
     *
     * @return void
     */
    public function index(array $conditions = [])
    {
        $conditions['start_time'];

        $conditions['end_time'];


        // $sql = "SELECT
        // SUM(CASE WHEN created_at >= '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS all_month,
        // SUM(CASE WHEN created_at >= '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS charge_month,
        // SUM(CASE WHEN created_at >= '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS new_month,
        // SUM(CASE WHEN created_at >= '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS expire_month,
        // SUM(CASE WHEN created_at >= '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS renew_month,
        // SUM(CASE WHEN created_at >= '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS activate_month,
        // SUM(CASE WHEN created_at >= '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS stop_month,
        // FROM virtual_order_cards_partition"

        // $diff = Carbon::parse($conditions['end_time'])->diffInMonths(Carbon::parse($conditions['start_time']));

        // $start = Carbon::parse($conditions['start_time']);
        // for ($i=0; $i < $diff; $i++) {
        //     $month = $start->copy()->addMonths($i);


        //     # code...
        // }

        $sql = 'SELECT c.company_id, count(*) AS activates, SUM(CASE WHEN p.id IS NOT NULL THEN 1 ELSE 0 END) AS renewals ' .
        'FROM virtual_order_cards AS c ' .
        'LEFT JOIN  virtual_order_cards_partition AS p ON p.type IN (1, 2) AND p.sim = c.sim ';
        $where = 'WHERE c.service_start_at IS NOT NULL ';
        $group = 'GROUP BY c.company_id ';

        if($conditions['company_id']){
            $where .= 'AND c.company_id = ' . $conditions['company_id'] . ' ';
        }

        if($conditions['activate_start_time']){
            $where .= "AND c.service_start_at >= '" . $conditions['activate_start_time'] . "' ";
        }

        if($conditions['activate_end_time']){
            $where .= "AND c.service_end_at <= '" . $conditions['activate_end_time'] . "' ";
        }

        if($conditions['renewal_start_time']){
            $where .= "AND p.service_start_at >= '" . $conditions['renewal_start_time'] . "' ";
        }

        if($conditions['renewal_start_time']){
            $where .= "AND p.service_start_at >= '" . $conditions['renewal_start_time'] . "' ";
        }

        $sql = $sql . $where . $group;

        $data = DB::select($sql);

        if(!$data){
            return [];
        }

        foreach ($data as &$item) {
            $item = (array)$item;
            $item['company_name'] = CompanyService::load($item['company_id'])['name'];
            $item['ratio'] = $item['activates'] == 0 ? 0 : $item['renewals']/$item['activates'];
            $item['ratio'] = sprintf('%.02f', $item['ratio']);
        }

        return $data;
    }
}
