public function export_comments(Request $request)
{
//控制器中调用导出业务类的方法
return (new ExportComment)->export_options($request)->download('活动风采评价.xlsx', \Maatwebsite\Excel\Excel::XLSX);
}
namespace Ano\Comment\Repositories\Backend;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Modules\Auth\Entities\Area;
class ExportComment implements WithMultipleSheets
{
use Exportable;
//处理传入的参数
public function export_options(Request $request)
{
$this->started_time = $request->input('started_time', Date("Y-01-01 00:00:00"));//开始时间
$this->ended_time = $request->input('ended_time', Date("Y-m-d H:i:s"));//结束时间
$this->area_id = $request->input('area_id');
if($this->area_id){
$area = Area::find($this->area_id);
if(!$area){
throw new \Exception('区域id不存在', 400);
}
$this->level = $area->level;
}else{
$this->area_id = 2424;
$this->level = 3;
}
return $this;
}
//导出多sheet
public function sheets(): array
{
//按照实践员星级评分 获取活动风采评论排名
//活动的活动风采星级产生的活动排名
$sheets = [];
$sheets[] = new ActivityComment( $this->started_time, $this->ended_time, $this->area_id, $this->level);//按活动风采星级排序活动
$temp = $this->level + 1;
for($i = $temp; $i<= 5; $i++){
$sheets[] = new ActivityCommentLevel( $this->started_time, $this->ended_time, $this->area_id, $this->level, $i);//4、5级按照星级排名
}
return $sheets;
}
}
ActivityComment.php 导出子业务类1:
namespace Ano\Comment\Repositories\Backend;
use Ano\Activity\Entities\Activity;
use \Ano\Comment\Entities\Comment;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Modules\Auth\Entities\Area;
use Modules\Auth\Entities\Organization;
class ActivityComment implements FromCollection, WithTitle, WithHeadings
{
public function __construct($started_time, $ended_time, $area_id, $level)
{
$this->started_time = $started_time;
$this->ended_time = $ended_time;
$this->area_id = $area_id;
$this->level = $level;
}
public function headings(): array
{
if($this->level == 3){
$headings = [
['数据时间',
date('Y/m/d', strtotime($this->started_time)) . '至' . date('Y/m/d', strtotime($this->ended_time)),
],
[ '排名',
'活动名称',
'活动时间',
'组织名称',
'所属所',
'所属站',
'评分星级',
]
];
}elseif($this->level == 4){
$headings =[
['数据时间',
date('Y/m/d', strtotime($this->started_time)) . '至' . date('Y/m/d', strtotime($this->ended_time)),
],
[
'排名',
'活动名称',
'活动时间',
'组织名称',
'所属站',
'评分星级',
]
];
}else{//5级
$headings = [
['数据时间',
date('Y/m/d', strtotime($this->started_time)) . '至' . date('Y/m/d', strtotime($this->ended_time)),
],
[
'排名',
'活动名称',
'活动时间',
'组织名称',
'评分星级',
]
];
}
return $headings;
}
//导出活动风采星级的活动排名数据
public function collection()
{
$rank = 0;
switch ($this->level){
case 3 :
$area4 = 'area_4';
$area5 = 'area_5';
$areas = $area4 . ',' . $area5 . ',';
$groupBy = ['obj_id','organization_id', 'area_4', 'area_5'];
break;
case 4 :
$area5 = 'area_5';
$areas = $area5 . ',';
$groupBy = ['obj_id','organization_id', 'area_5'];
break;
case 5 :
$areas = '';
$groupBy = ['obj_id','organization_id'];
break;
default :
throw new \Exception('区域不存在', 400);
}
$current_level = $this->level;
Comment::query()
->where('area_' . $this->level, $this->area_id)
->where('type' , 2)
->where('enabled' , 1)
->where('created_at', '>=', $this->started_time)
->where('created_at', '<=', $this->ended_time)
->select(\DB::raw("obj_id,$areas organization_id, AVG(stars) as avg_star"))
->groupBy($groupBy)
->orderBy('avg_star', 'DESC')
->chunk(1000, function ($chunks) use (&$export_info, &$rank, $current_level) {
//获取区域信息
if($current_level == 3){
$area4 = $chunks->pluck('area_4')->unique()->toArray();
$area5 = $chunks->pluck('area_5')->unique()->toArray();
$areas = array_merge($area4, $area5);
}elseif($current_level == 4){
$areas = $chunks->pluck('area_5')->unique()->toArray();
}else{
//5级不需要区域数据
}
if($current_level <= 4){ //5级区域不查区域表
$area_data = Area::whereIn('id', $areas)->where('level', '>=', 4)->select('id', 'label','level')->get()->toArray();
$temp_area = array_column($area_data, 'id');
$area_data = array_combine($temp_area, $area_data);
}
//获取活动
$activities = $chunks->pluck('obj_id')->unique();
$activity_data = Activity::whereIn('id', $activities)->select('id', 'title', 'started_at')->get()->toArray();
$temp_activity = array_column($activity_data, 'id');
$activity_data = array_combine($temp_activity, $activity_data);
//获取组织
$organizations = $chunks->pluck('organization_id')->unique();
$organization_data = Organization::whereIn('id', $organizations)->pluck('name', 'id')->toArray();
$chunks = $chunks->toArray();
foreach ($chunks as $key => &$chunk) {
$rank += 1;
array_unshift($chunk, $rank);
$chunk['activity_title'] = $activity_data[$chunk['obj_id']]['title'];
$chunk['started_at'] = $activity_data[$chunk['obj_id']]['started_at'];
$chunk['organization_name'] = $organization_data[$chunk['organization_id']];
if($current_level == 3){
$chunk['area_4_name'] = isset($area_data[$chunk['area_4']]) ? $area_data[$chunk['area_4']]['label'] : '';
$chunk['area_5_name'] = isset($area_data[$chunk['area_5']]) ? $area_data[$chunk['area_5']]['label'] : '';
unset($chunk['area_4']);
unset($chunk['area_5']);
}elseif($current_level == 4){
$chunk['area_5_name'] = isset($area_data[$chunk['area_5']]) ? $area_data[$chunk['area_5']]['label'] : '';
unset($chunk['area_5']);
}
$chunk['star'] = $chunk['avg_star'];
unset($chunk['obj_id']);
unset($chunk['organization_id']);
unset($chunk['avg_star']);
}
$export_info[] = $chunks;
});
return collect($export_info);
}
//sheet title
public function title(): string
{
return '活动星级排名';
}
}
ActivityCommentLevel .php 导出子业务类2:
namespace Ano\Comment\Repositories\Backend;
use \Ano\Comment\Entities\Comment;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Modules\Auth\Entities\Area;
class ActivityCommentLevel implements FromCollection, WithTitle, WithHeadings
{
public function __construct($started_time, $ended_time, $area_id, $current_level, $next_level)
{
$this->started_time = $started_time;
$this->ended_time = $ended_time;
$this->area_id = $area_id;
$this->current_level = $current_level;
$this->next_level = $next_level;
}
public function headings(): array
{
if($this->next_level == 4){
$headings = [
'排名',
'所属所',
'平均星级'
];
}else{//5级
$headings = [
'排名',
'所属站',
'平均星级'
];
}
return $headings;
}
//导出活动风采星级的活动排名数据
public function collection()
{
$rank = 0;
if($this->next_level == 4){
$sql_area = 'area_4';
}else{
$sql_area = 'area_5';
}
Comment::query()
->where('area_' . $this->current_level, $this->area_id)
->where('area_' . $this->next_level, '>', 0)
->where('type' , 2)
->where('enabled' , 1)
->where('created_at', '>=', $this->started_time)
->where('created_at', '<=', $this->ended_time)
->select(\DB::raw("{$sql_area}, AVG(stars) as avg_star"))
->groupBy([$sql_area])
->orderBy('avg_star', 'DESC')
->chunk(1000, function ($chunks) use (&$export_info, &$rank, $sql_area) {
//获取区域信息
$areas = $chunks->pluck($sql_area)->unique()->toArray();
$area_data = Area::whereIn('id', $areas)->where('level', '>=', 4)->select('id', 'label','level')->get()->toArray();
$temp_area = array_column($area_data, 'id');
$area_data = array_combine($temp_area, $area_data);
$chunks = $chunks->toArray();
foreach ($chunks as $key => &$chunk) {
$rank += 1;
array_unshift($chunk, $rank);
$chunk[$sql_area.'_name'] = isset($area_data[$chunk[$sql_area]]) ? $area_data[$chunk[$sql_area]]['label'] : '';
$chunk['star'] = $chunk['avg_star'];
unset($chunk['obj_id']);
unset($chunk['avg_star']);
unset($chunk[$sql_area]);
}
$export_info[] = $chunks;
});
// dd($export_info);
return collect($export_info);
}
public function title(): string
{
if($this->next_level == 4){
$title = '实践所星级排行';
}else{
$title = '实践站星级排行';
}
return $title;
}
}