companyRepository = $companyRepository; $this->orderRepository = $orderRepository; $this->orderCardRepository = $orderCardRepository; $this->orderRenewalCardRepository = $orderRenewalCardRepository; $this->orderRenewalPackageCardRepository = $orderRenewalPackageCardRepository; $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->orderCardRepository->select($select)->groupBy($groupBy)->get()->pluck('counts', 'company_id')->toArray(); $conditions = array_only($conditions, ['starttime', 'endtime']); $counts = $this->orderCardRepository->select($select)->withConditions($conditions)->groupBy($groupBy) ->get()->pluck('counts', 'company_id')->toArray(); $orderRenewalCard = $this->orderRenewalCardRepository->select($select)->withConditions($conditions)->groupBy($groupBy) ->get()->pluck('counts', 'company_id')->toArray(); // $orderRenewalPackage = $this->orderRenewalPackageCardRepository->select($select)->withConditions($conditions)->groupBy($groupBy) // ->get()->pluck('counts', 'company_id')->toArray(); // $renewed_counts = array_merge_sum($orderRenewalCard, $orderRenewalPackage); $renewed_counts = $orderRenewalCard; if ($conditions['starttime'] && $conditions['endtime']) { $valid_counts = $this->orderCardPartitionRepository->select($select)->where('service_start_at', '>=', $conditions['starttime'])->where('service_end_at', '<=', $conditions['endtime']) ->groupBy($groupBy)->get()->pluck('counts', 'company_id')->toArray(); } else { $valid_counts = $this->orderCardPartitionRepository->select($select)->whereNotNull('service_start_at')->groupBy($groupBy)->get()->pluck('counts', 'company_id')->toArray(); } $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; $item->valid_counts = $valid_counts[$item['id']] ?? 0; }); return $companies; } }