71 lines
2.3 KiB
PHP
71 lines
2.3 KiB
PHP
<?php
|
|
namespace App\Domains\Stats\Services;
|
|
|
|
use App\Core\Service;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Domains\Virtual\Repositories\OrderRepository;
|
|
use App\Domains\Virtual\Repositories\CompanyRepository;
|
|
use App\Domains\Virtual\Repositories\OrderCardPartitionRepository;
|
|
|
|
class CompanyCountService extends Service
|
|
{
|
|
protected $companyRepository;
|
|
protected $orderRepository;
|
|
protected $orderCardPartitionRepository;
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(
|
|
CompanyRepository $companyRepository,
|
|
OrderRepository $orderRepository,
|
|
OrderCardPartitionRepository $orderCardPartitionRepository
|
|
) {
|
|
$this->companyRepository = $companyRepository;
|
|
$this->orderRepository = $orderRepository;
|
|
$this->orderCardPartitionRepository = $orderCardPartitionRepository;
|
|
}
|
|
|
|
|
|
/**
|
|
* 企业统计
|
|
*
|
|
* @return void
|
|
*/
|
|
public function index(array $conditions = [])
|
|
{
|
|
$companies = $this->companyRepository->withConditions(array_only($conditions, ['name']))
|
|
->select(['id', 'name'])->applyConditions()->withTrashed()->paginate($conditions['limit']);
|
|
|
|
if (empty($companies)) {
|
|
return $companies;
|
|
}
|
|
|
|
$groupBy = 'company_id';
|
|
|
|
$select = [$groupBy, DB::raw('count(distinct sim) as counts')];
|
|
|
|
$total = $this->orderCardPartitionRepository->select($select)->where('type', 0)->groupBy($groupBy)->get()->pluck('counts', 'company_id')->toArray();
|
|
|
|
$conditions = array_only($conditions, ['starttime', 'endtime']);
|
|
|
|
$counts = $this->orderCardPartitionRepository->select($select)->where('type', 0)->withConditions($conditions)->groupBy($groupBy)
|
|
->get()->pluck('counts', 'company_id')->toArray();
|
|
|
|
$orderRenewalCard = $this->orderCardPartitionRepository->select($select)->where('type', 1)->withConditions($conditions)->groupBy($groupBy)
|
|
->get()->pluck('counts', 'company_id')->toArray();
|
|
|
|
$renewed_counts = $orderRenewalCard;
|
|
|
|
$companies->map(function ($item) use ($total, $counts, $renewed_counts, $valid_counts) {
|
|
$item->total = $total[$item['id']] ?? 0;
|
|
$item->counts = $counts[$item['id']] ?? 0;
|
|
$item->renewed_counts = $renewed_counts[$item['id']] ?? 0;
|
|
});
|
|
|
|
return $companies;
|
|
}
|
|
}
|