From 8b0497dfc67c6e51650525c8709a1cfac1e2e116 Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Fri, 20 Nov 2020 18:05:03 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A4=BE=E5=8C=BA=E4=BB=A3=E7=90=86=E7=82=B9?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1--=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v3/ReportCommunityController.php | 31 ++-- app/Admin/Repositories/v3/ReportCommunity.php | 161 +++++++++++++----- app/Admin/routes.php | 3 + 3 files changed, 143 insertions(+), 52 deletions(-) diff --git a/app/Admin/Controllers/v3/ReportCommunityController.php b/app/Admin/Controllers/v3/ReportCommunityController.php index 3f493d2..5ae54c1 100644 --- a/app/Admin/Controllers/v3/ReportCommunityController.php +++ b/app/Admin/Controllers/v3/ReportCommunityController.php @@ -45,8 +45,12 @@ class ReportCommunityController extends AdminController } return Grid::make($builder, function (Grid $grid) { $marketList = $this->marketList; - $grid->column('horseman_id','ID'); + + $grid->column('market_id','市场')->display(function($marketId) use($marketList){ + return $marketList[$marketId] ?? ''; + }); $grid->column('user_id','懒ID'); + $grid->column('avatar','微信头像')->display(function(){ $userId = $this->user_id ?? 0; $item = UserModel::select('avatar')->find($userId); @@ -54,11 +58,9 @@ class ReportCommunityController extends AdminController })->image('',50); $grid->column('name','姓名'); - $grid->column('market_id','市场')->display(function($marketId) use($marketList){ - return $marketList[$marketId] ?? ''; - }); + $grid->column('total_new_agent','新拓展代理点数'); - $grid->column('total_number','配送单数') + $grid->column('total_bound_new_user','新增绑定用户数') ->modal(function($modal){ $name = $this->name; $modal->title($name.'的配送明细'); @@ -67,14 +69,17 @@ class ReportCommunityController extends AdminController $table = OrderDeliveryById::make(['horseman_id'=>$this->horseman_id,'start_time'=>$startTime, 'end_time'=>$endTime]); return $table; })->help('只统计2020年10月01日之后(包括10.01)的数据'); - $grid->column('total_delivery','总配送费'); + + $grid->column('total_add_new_user','平台新增用户数'); + $grid->column('total_bound_order_online','绑定用户线上订单总数'); + $grid->column('total_add_order_online','平台新增用户线上订单数'); $grid->filter(function (Filter $filter) use($marketList) { // 更改为 panel 布局 $filter->panel(); $filter->equal('start_time','开始时间')->date()->width(2); $filter->equal('end_time','结束时间')->date()->width(2); - $filter->equal('horseman_id','配送员ID')->width(2); + $filter->equal('user_id','懒ID')->width(2); $filter->equal('name','姓名')->width(2); if(!$this->marketId){ @@ -83,12 +88,12 @@ class ReportCommunityController extends AdminController }); $grid->tools([ - new DataReportOption('today','delivery_report','今日'), - new DataReportOption('yesterday','delivery_report','昨日'), - new DataReportOption('this_week','delivery_report','本周'), - new DataReportOption('last_week','delivery_report','上周'), - new DataReportOption('this_month','delivery_report','本月'), - new DataReportOption('last_month','delivery_report','上月'), + new DataReportOption('today','report_community','今日'), + new DataReportOption('yesterday','report_community','昨日'), + new DataReportOption('this_week','report_community','本周'), + new DataReportOption('last_week','report_community','上周'), + new DataReportOption('this_month','report_community','本月'), + new DataReportOption('last_month','report_community','上月'), new OrderDeliveryExport() ]); diff --git a/app/Admin/Repositories/v3/ReportCommunity.php b/app/Admin/Repositories/v3/ReportCommunity.php index b5413ee..68a083d 100644 --- a/app/Admin/Repositories/v3/ReportCommunity.php +++ b/app/Admin/Repositories/v3/ReportCommunity.php @@ -2,14 +2,20 @@ namespace App\Admin\Repositories\v3; -use App\Models\ImsCjdcOrderMain as Model; +use App\Models\v3\LanzuEmployees as Model; use Dcat\Admin\Grid\Model as GridModel; use Dcat\Admin\Repositories\EloquentRepository; use Illuminate\Support\Facades\DB; -use App\Models\v3\LanzuEmployees as EmployeesModel; + +use App\Models\LanzuCsInfo as CsInfoModel; +use App\Models\LanzuUserRelationBind as UserRelationBindModel; +use App\Models\ImsCjdcOrderMain as OrderMainModel; + class ReportCommunity extends EloquentRepository { - public $params = []; + protected $params = []; + protected $currentPage = 1; + protected $perPage = 10; /** * Model. * @@ -28,62 +34,139 @@ class ReportCommunity extends EloquentRepository public function get(GridModel $model) { // 获取当前页数 - $currentPage = $model->getCurrentPage(); + $this->currentPage = $model->getCurrentPage(); // 获取每页显示行数 - $perPage = $model->getPerPage(); + $this->perPage = $model->getPerPage(); $where = []; if(!empty($this->params) && isset($this->params['market_id']) && !empty($this->params['market_id'])){ $where = ['market_id'=>$this->params['market_id']]; } + + $list = $this->getDataModel($where); - $selects = 'count('.Model::$tableName.'.id) AS total_number,horseman_id,'.EmployeesModel::TABLE_NAME.'.user_id,'.EmployeesModel::TABLE_NAME.'.name,'.EmployeesModel::TABLE_NAME.'.market_id,SUM( CASE WHEN delivery_money=0 THEN 3.5 when delivery_money>0 THEN delivery_money+3.5 ELSE 0 END) as total_delivery'; - $delivery = $this->getDataModel($selects,$where); - $list = $delivery->groupBy('horseman_id',EmployeesModel::TABLE_NAME.'.market_id',EmployeesModel::TABLE_NAME.'.name',EmployeesModel::TABLE_NAME.'.user_id')->paginate($perPage); - // ->orderBy('total_number','desc') - $list = $list->toArray(); - return $model->makePaginator( $list['total'] ?? 0,$list['data'] ?? [] ); } - public function getDataModel($selects,$params = []) + public function getDataModel($params = []) { - // 获取筛选参数 - $userId = $params['user_id'] ?? request()->input('user_id',0); - $name = $params['name'] ?? request()->input('name', ''); - $marketId = $params['market_id'] ?? request()->input('market_id',0); - + // 获取筛选参数 如果没有时间,则用当天的数据 $startTime = $params['start_time'] ?? request()->input('start_time',''); $endTime = $params['end_time'] ?? request()->input('end_time',''); - // 外卖类型 type = 1 - $model = Model::select(DB::raw($selects)) - // ->with('employees:id,user_id,name,market_id') - ->join(EmployeesModel::TABLE_NAME,EmployeesModel::TABLE_NAME.'.id','=',Model::$tableName.'.horseman_id','left') - ->where(Model::$tableName.'.type',1) - ->where('shipping_type',1) - ->whereIn('state',Model::ORDER_STATE_FINISH) - ->where(Model::$tableName.'.created_at','>=',strtotime('2020-10-01 00:00:00')); - - if($userId){ - $model->where(EmployeesModel::TABLE_NAME.'.user_id',$userId); + $time = time(); + if(empty($startTime)){ + $startTime = date('Y-m-d', $time) . '00:00:00'; + }else{ + $startTime = $startTime . '00:00:00'; } - if($name){ - $model->where(EmployeesModel::TABLE_NAME.'.name','like',"%$name%"); + if(empty($endTime)){ + $endTime = date('Y-m-d', $time) . '23:59:59'; + }else{ + $endTime = $endTime . '23:59:59'; } - if($marketId){ - $model->where(EmployeesModel::TABLE_NAME.'.market_id',$marketId); + + // 获取懒族员工 + $selectsEmp = 'id,user_id,person_id,market_id,name,phone,status'; + $employeesData = $this->getEmployeesData($selectsEmp,$params); + $employeesList = $employeesData->orderBy('id','desc') + ->paginate($this->perPage) + ->map(function ($value) {return (array)$value;}) + ->toArray(); + // $employeesList = $employeesList->toArray(); + + foreach ($employeesList as $key => &$employee) { + // 根据员工信息,查询名下新增绑定的所有服务点 Statistics + $newCsCount = CsInfoModel::where(function ($query) use ($employee) { + $query->where('person_id', '=', $employee['user_id']) + ->orWhere('user_id', '=', $employee['user_id']); + }) + ->where('created_at', '>=', strtotime($startTime ?? 0)) + ->where('created_at', '<=', strtotime($endTime ?? 0)) + ->count(); + + // 查询名下所有的服务点 + $allCsInfos = CsInfoModel::where(function ($query) use ($employee) { + $query->where('person_id', '=', $employee['user_id']) + ->orWhere('user_id', '=', $employee['user_id']); + }) + ->get() + ->map(function ($value) {return (array)$value;}) + ->toArray(); + + // 新增绑定用户数 Statistics + $newBindUserCount = UserRelationBindModel::where('bind_type', '=', 1) + ->whereIn('source_id', array_values(array_column($allCsInfos, 'admin_user_id'))) + ->where('created_at', '>=', strtotime($startTime ?? 0)) + ->where('created_at', '<=', strtotime($endTime ?? 0)) + ->count(); + + // 查询名下所有服务点的所有用户 + $allCsBindUsers = UserRelationBindModel::where('bind_type', '=', 1) + ->whereIn('source_id', array_values(array_column($allCsInfos, 'admin_user_id'))) + ->get() + ->map(function ($value) {return (array)$value;}) + ->toArray(); + + // 平台新增用户数 Statistics + $platNewUsers = OrderMainModel::select('user_id') + ->where('type', '=', 1) + ->whereIn('state', [4,5,10,11]) + ->whereIn('user_id', array_values(array_column($allCsBindUsers, 'user_id'))) + ->groupBy('user_id') + ->havingRaw('MIN(created_at)>='.strtotime($startTime ?? 0).' AND MIN(created_at)<='.strtotime($endTime ?? 0).'') + ->get() + ->map(function ($value) {return (array)$value;}) + ->toArray(); + $platNewUserCount = count($platNewUsers); + + // 所有用户产生的线上订单数 Statistics + $totalOrdersCount = OrderMainModel::where('type', '=', 1) + ->whereIn('state', [4,5,10,11]) + ->whereIn('user_id', array_values(array_column($allCsBindUsers, 'user_id'))) + ->where('created_at', '>=', strtotime($startTime ?? 0)) + ->where('created_at', '<=', strtotime($endTime ?? 0)) + ->count(); + + // 平台新增用户产生的线上订单数 Statistics + $platNewUserOrdersCount =OrderMainModel::where('type', '=', 1) + ->whereIn('state', [4,5,10,11]) + ->whereIn('user_id', array_values(array_column($platNewUsers, 'user_id'))) + ->where('created_at', '>=', strtotime($startTime ?? 0)) + ->where('created_at', '<=', strtotime($endTime ?? 0)) + ->count(); + + $employee['new_cs_count'] = $newCsCount; + $employee['new_bind_user_count'] = $newBindUserCount; + $employee['plat_new_user_count'] = $platNewUserCount; + $employee['total_order_count'] = $totalOrdersCount; + $employee['plat_new_user_order_count'] = $platNewUserOrdersCount; } - if($startTime){ - $startTime = $startTime.' 00:00:00'; - $model->where(Model::$tableName.'.created_at','>=',strtotime($startTime)); + + return $employeesList; + } + + /** + * 获取懒族员工 model + */ + public function getEmployeesData($selects, $params = ['status' => 1]) + { + + $model = Model::select(DB::raw($selects)); + + if(isset($params['status']) && is_numeric($params['status'])){ + $model->where('status',$params['status']); } - if($endTime){ - $endTime = $endTime.' 23:59:59'; - $model->where(Model::$tableName.'.created_at','<=',strtotime($endTime)); + if(isset($params['user_id']) && is_numeric($params['user_id'])){ + $model->where('user_id', $params['user_id']); + } + if(isset($params['user_id'])){ + $model->where('name','like','%'.$params['user_id'].'%'); + } + if(isset($params['market_id']) && is_numeric($params['market_id'])){ + $model->where('market_id', $params['market_id']); } - return $model; } diff --git a/app/Admin/routes.php b/app/Admin/routes.php index eb22da2..4a05c22 100644 --- a/app/Admin/routes.php +++ b/app/Admin/routes.php @@ -109,4 +109,7 @@ Route::group([ $router->resource('/share_card_setting', 'v3\ShareCardSettingController'); // 关注公众号设置 $router->any('/official_subscribe_info', 'v3\SystemConfigController@OfficialSubscribeInfoSettingForm'); + + // 社区代理点统计 + $router->resource('/report_community', 'v3\ReportCommunityController'); });