<?php
namespace App\Domains\Stats\Services;

use App\Dicts;
use App\Core\Service;
use Illuminate\Support\Facades\DB;
use App\Domains\Virtual\Services\CompanyService;
use App\Domains\Virtual\Services\PackageService;
use App\Domains\Virtual\Repositories\OrderCardPartitionRepository;

class CompanyReportService extends Service
{
    protected $orderCardPartitionRepository;

    protected static $typeNames = ['首年', '非首年', '续费包', '加油包'];

    /**
     * 构造函数
     *
     * @return void
     */
    public function __construct(OrderCardPartitionRepository $orderCardPartitionRepository)
    {
        $this->orderCardPartitionRepository = $orderCardPartitionRepository;
    }

    /**
     * 企业月报表
     *
     * @return void
     */
    public function index(array $conditions = [])
    {
        begin_time_consuming();

        $select = [
            'company_id',
            'package_id',
            'type',
            'unit_price',
            DB::raw('count(distinct sim) as members'),
            DB::raw('CASE "type" WHEN 3 THEN SUM(counts) ELSE count(*)  END AS counts'),
        ];

        $query = $this->orderCardPartitionRepository->select($select)
            ->withConditions($conditions)
            ->groupBy(['company_id', 'package_id', 'type', 'unit_price'])
            ->orderBy('company_id')->orderBy('package_id');

        $res = $query->paginate($conditions['limit']);

        $res->map(function ($item) {
            $company = CompanyService::load($item->company_id);
            $package = PackageService::load($item->package_id);
            $item->company_name = $company['name'];
            $item->package_name = $package['name'];
            $item->service_months = $package['service_months'] ?? 1;
            $item->unit_price = sprintf('%.02f', $item->unit_price/100);
            $item->month_price = sprintf('%.02f', $item->unit_price/$item->service_months);
            $item->total_price = sprintf('%.02f', $item->unit_price/$item->service_months*$item->counts);
            $item->type_name = self::$typeNames[$item->type];
            unset($item->order);
        });

        return $res;
    }

    /**
     * 企业月报表明细
     *
     * @return void
     */
    public function detail(array $conditions = [])
    {
        $select = [
            'sim',
            'company_id',
            'package_id',
            'type',
            DB::raw('CASE "type" WHEN 3 THEN SUM(counts) ELSE count(*)  END AS counts'),
            'unit_price'
        ];

        $query = $this->orderCardPartitionRepository->select($select)
            ->withConditions($conditions)
            ->groupBy('company_id', 'package_id', 'type', 'unit_price', 'sim')
            ->orderBy('sim');

        $res = $query->paginate($conditions['limit']);

        $carrierOperators = app(Dicts::class)->get('carrier_operator');

        $res->map(function ($item) use ($carrierOperators) {
            $company = CompanyService::load($item->company_id);
            $package = PackageService::load($item->package_id);
            $item->company_name = $company['name'];
            $item->package_name = $package['name'];
            $item->carrier_operator_name = $carrierOperators[$package['carrier_operator']];
            $month_price = $item->unit_price / $package['service_months'];
            $item->month_price = sprintf('%.02f', $month_price/100);
            unset($item->order);
        });

        return $res;
    }
}
