芝麻web文件管理V1.00
编辑当前文件:/home/forge/stage.herta-bht.smartcon-survey.com/app/Services/StatementTimelineService.php
publishedWaves = DataUpload::getPublishedWaves($this->countryId); $this->aggregatedInterviews = $this->getAggregatedInterviews(); $this->quarterInfo = Interview::getQuarterInfo($this->countryId, $this->publishedWaves); } /** * Get the base query for interviews. * * @return Builder|DataAggregation The base query builder instance */ private function baseQuery(): Builder|DataAggregation { return DataAggregation::where('country_id', $this->countryId) ->whereIn('wave', $this->publishedWaves); } /** * Get the total count of unique users for a given brand and wave. * * @param Brand $brand The brand * @return int The total count of unique users */ private function getTotal(Brand $brand, int $quarter, int $year): int { return $this->baseQuery()->where([ 'question_name' => 'Q2', 'quarter' => $quarter, 'year' => $year, 'model_id' => $brand->sid, 'value' => '1', ]) ->sum('aggregate'); } /** * Get the collection of interviews. * * @return EloquentCollection The collection of interviews */ private function getAggregatedInterviews(): EloquentCollection { return $this->baseQuery()->whereIn('question_name', ['Q9', 'Q19'])->get(); } /** * Calculate the value for a given brand and wave. * * @param Brand $brand The brand * @return int The calculated value */ private function calculateValue(Brand $brand, int $quarter, int $year): int { return $this->aggregatedInterviews->where('model_id', $brand->sid) ->where('statement_id', $this->statementId) ->where('quarter', $quarter) ->where('year', $year) ->where('value', '1') ->sum('aggregate'); } /** * Calculate the data for a given brand. * * @param Brand $brand The brand * @return array The calculated data */ private function calculateData(Brand $brand): array { $data = []; foreach ($this->quarterInfo as $wave) { $n = $this->calculateValue($brand, $wave['quarter'], $wave['year']); $total = $this->getTotal($brand, $wave['quarter'], $wave['year']); $data[] = [ 'x' => "Q{$wave['quarter']} {$wave['year']}", 'y' => $total === 0 ? 0 : round(($n / $total) * 100), 'n' => $n, 'base' => $total, ]; } return $data; } /** * Get the values for a given brand. * * @param Brand $brand The brand * @return array The values for the brand */ private function getValues(Brand $brand): array { return [ 'id' => $brand->sid, 'label' => $brand->name, 'borderColor' => $brand->color, 'data' => $this->calculateData($brand), ]; } /** * Get the data for a given statement. * * @param Statement $statement The statement * @return array The data for the statement */ public function getData(Statement $statement): array { $this->statementId = $statement->id; return [ 'title' => $statement->text, 'totalN' => $this->quarterInfo->sum('aggregate'), 'data' => [ 'labels' => $this->quarterInfo->map(fn ($quarter) => "Q{$quarter['quarter']} {$quarter['year']}")->values()->toArray(), 'datasets' => $this->brands->map(fn ($brand) => $this->getValues($brand))->toArray(), ], ]; } }