Browse Source

Merge branch 'community_report_1120'

master
liangyuyan 5 years ago
parent
commit
70b0147445
  1. 115
      app/Admin/Actions/Exporter/ReportCommunity.php
  2. 9
      app/Admin/Actions/Grid/v3/DataReportOption.php
  3. 91
      app/Admin/Actions/Tools/ReportCommunityExport.php
  4. 156
      app/Admin/Controllers/v3/ReportCommunityController.php
  5. 193
      app/Admin/Repositories/v3/ReportCommunity.php
  6. 4
      app/Admin/routes.php
  7. 12
      resources/lang/zh-CN/report-community.php

115
app/Admin/Actions/Exporter/ReportCommunity.php

@ -0,0 +1,115 @@
<?php
namespace App\Admin\Actions\Exporter;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use App\Admin\Repositories\v3\ReportCommunity as ReportCommunityRepository;
use App\Models\v3\Market as MarketModel;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class ReportCommunity implements FromArray, WithStrictNullComparison, ShouldAutoSize, WithTitle, WithHeadings, WithStyles
{
protected $params = [];
public function __construct($params)
{
$this->params = $params;
}
public function headings(): array
{
$startTime = $this->params['start_time'] ?? null;
$endTime = $this->params['end_time'] ?? null;
$nowDay = date('Y年m月d日', time());
if(empty($startTime)){
$startTime = '2020年7月1日';
}else if(empty($endTime)){
$endTime = $nowDay;
}
return [
'统计时间:'.$startTime.' 至 '.$endTime
];
}
public function styles(Worksheet $sheet)
{
$sheet->mergeCells('A1:H1');// 合并
$sheet->getRowDimension(1)->setRowHeight(40); // 行高
$sheet->getRowDimension(2)->setRowHeight(25); // 行高
$sheet->getStyle('A1:H1')->getAlignment()->setVertical('center');// 垂直居中
$sheet->getStyle('A1:H1')->getAlignment()->setHorizontal('center');// 水平居中
$sheet->getStyle('A2:H2')->getAlignment()->setVertical('center');// 垂直居中
return [
1 => ['font' => ['bold' => true,'size' => 18, 'name' => '微软雅黑 Light']],
2 => ['font' => ['bold' => true,'size' => 12, 'name' => '微软雅黑 Light']],
];
}
public function array(): array
{
$result = [[
'市场',
'懒ID',
'姓名',
'新拓展代理点数',
'新增绑定用户数',
'平台新增用户数',
'绑定用户线上订单总数',
'平台新增用户线上订单数'
]];
$data = $this->getData($this->params);
if(!isset($data['data']) ){
$data['data'] = [];
}
$markets = MarketModel::getMarketArray();
foreach ($data['data'] as $value){
$item = [
$markets[$value['market_id']] ?? '',
$value['user_id'] ?? 0,
$value['name'] ?? '',
$value['new_cs_count'] ?? 0,
$value['new_bind_user_count'] ?? 0,
$value['plat_new_user_count'] ?? 0,
$value['bound_order_online_count'] ?? 0,
$value['plat_new_user_order_online_count'] ?? 0,
];
$result[] = $item;
}
return $result;
}
/**
* 获取数据
*/
public function getData($option = [])
{
$params = $option;
$repository = new ReportCommunityRepository();
$selects = 'id,user_id,market_id,name';
$data = $repository->getDataModel($selects,$params,false);
return $data;
}
/**
* sheet 表名称
* @return string
*/
public function title(): string
{
return '统计';
}
}

9
app/Admin/Actions/Grid/v3/DataReportOption.php

@ -12,12 +12,14 @@ class DataReportOption extends AbstractTool
protected $url; protected $url;
protected $option; protected $option;
protected $title = ''; protected $title = '';
protected $moreOption = [];
public function __construct($option = '', $url = '', $title = '')
public function __construct($option = '', $url = '', $title = '', $more = [])
{ {
$this->option = $option; $this->option = $option;
$this->url = $url; $this->url = $url;
$this->title = $title; $this->title = $title;
$this->moreOption = $more;
} }
/** /**
@ -46,6 +48,11 @@ class DataReportOption extends AbstractTool
$startTime = $date['start'] ?? ''; $startTime = $date['start'] ?? '';
$endTime = $date['end'] ?? ''; $endTime = $date['end'] ?? '';
$url = 'admin/'.$this->url.'?start_time='.$startTime.'&end_time='.$endTime; $url = 'admin/'.$this->url.'?start_time='.$startTime.'&end_time='.$endTime;
if(!empty($this->moreOption) && is_array($this->moreOption)){
foreach($this->moreOption as $key => $value){
$url .= '&'. $key .'='. $value;
}
}
$this->defaultHtmlAttribute('href', url($url)); $this->defaultHtmlAttribute('href', url($url));
return <<<HTML return <<<HTML

91
app/Admin/Actions/Tools/ReportCommunityExport.php

@ -0,0 +1,91 @@
<?php
namespace App\Admin\Actions\Tools;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Traits\HasPermissions;
use Dcat\Admin\Tree\AbstractTool;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
class ReportCommunityExport extends AbstractTool
{
/**
* @return string
*/
protected $title = '<span class="feather icon-download">导出<span>';
/**
* Handle the action request.
*
* @param Request $request
*
* @return Response
*/
public function handle(Request $request)
{
$url = '/report_community_export?';
$status = $request->get('status', null);
$name = $request->get('name', '');
$marketId = $request->get('market_id',0);
$storeId = $request->get('user_id',0);
$startTime = $request->get('start_time','');
$endTime = $request->get('end_time','');
if(empty($startTime) && empty($endTime)){
return $this->response()->error('请先选择时间查询!');
}
if(!empty($status)){
$url .= '&status='.$status;
}
if(!empty($name)){
$url .= '&name='.$name;
}
if(!empty($marketId)){
$url .= '&market_id='.$marketId;
}
if(!empty($storeId)){
$url .= '&user_id='.$storeId;
}
if(!empty($startTime)){
$url .= '&start_time='.$startTime;
}
if(!empty($endTime)){
$url .= '&end_time='.$endTime;
}
return $this->response()
->success('导出中~')
->redirect($url);
}
public function parameters()
{
return [
'status' => request()->input('status', 0),
'name' => request()->input('name', ''),
'market_id' => request()->input('market_id',0),
'user_id' => request()->input('user_id',0),
'start_time' => request()->input('start_time',''),
'end_time' => request()->input('end_time',''),
];
}
/**
* @return string|array|void
*/
public function confirm()
{
return '确定导出数据吗?';
}
/**
* @param Model|Authenticatable|HasPermissions|null $user
*
* @return bool
*/
protected function authorize($user): bool
{
return true;
}
}

156
app/Admin/Controllers/v3/ReportCommunityController.php

@ -0,0 +1,156 @@
<?php
namespace App\Admin\Controllers\v3;
use App\Admin\Actions\Grid\v3\DataReportOption;
use App\Admin\Actions\Tools\ReportCommunityExport;
use App\Admin\Common\Auth;
use App\Admin\Repositories\v3\ReportCommunity;
use Dcat\Admin\Grid;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Grid\Filter;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\v3\Market as MarketModel;
use App\Models\ImsCjdcUser as UserModel;
use App\Models\v3\LanzuEmployees as EmployeesModel;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Alert;
class ReportCommunityController extends AdminController
{
/**
* 社区代理点统计
*/
protected $GoodsActivityReport = null;
public $marketId = 0;
public $newParams = ['status' => 1];
public $marketList = [];
public $storeList = [];
protected $reportDate = '';
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
$this->marketId = Auth::getMarket();
if($this->marketId){
$this->newParams['market_id'] = $this->marketId;
$builder = new ReportCommunity($this->newParams);
$this->marketList = MarketModel::getMarketArray([['id','=',$this->marketId]]);
}else{
$builder = new ReportCommunity($this->newParams);
$this->marketList = MarketModel::getMarketArray();
}
return Grid::make($builder, function (Grid $grid) {
$marketList = $this->marketList;
$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);
return $item['avatar'] ?? '';
})->image('',50);
$grid->column('name','姓名');
$grid->column('new_cs_count','新拓展代理点数');
$grid->column('new_bind_user_count','新增绑定用户数');
$grid->column('plat_new_user_count','平台新增用户数');
$grid->column('bound_order_online_count','绑定用户线上订单总数');
$grid->column('plat_new_user_order_online_count','平台新增用户线上订单数');
$grid->filter(function (Filter $filter) use($marketList) {
// 更改为 panel 布局
$filter->panel();
$filter->where('start_time',function(){
$this->newParams['start_time'] = $this->input['start_time'];
},'开始时间',)->date()->width(2);
$filter->equal('end_time','结束时间')->date()->width(2);
$filter->equal('user_id','懒ID')->width(2);
$filter->equal('name','姓名')->width(2);
if(!$this->marketId){
$filter->equal('market_id','市场')->select($marketList)->width(2);
}
$filter->equal('status','状态')->select(EmployeesModel::$_STATUS)->width(2);
});
$grid->tools([
new DataReportOption('today','report_community','今日', $this->newParams),
new DataReportOption('yesterday','report_community','昨日', $this->newParams),
new DataReportOption('this_week','report_community','本周', $this->newParams),
new DataReportOption('last_week','report_community','上周', $this->newParams),
new DataReportOption('this_month','report_community','本月', $this->newParams),
new DataReportOption('last_month','report_community','上月', $this->newParams),
new ReportCommunityExport()
]);
// 每页1条
$grid->paginate(10);
$grid->disableCreateButton();
$grid->disableBatchActions();
$grid->disableBatchDelete();
$grid->toolsWithOutline();
$grid->disableDeleteButton();
$grid->disableEditButton();
$grid->disableQuickEditButton();
$grid->disableViewButton();
$grid->disableActions();
$grid->disableRowSelector();
});
}
/**
* 页面
*/
public function index(Content $content)
{
$startTime = request()->get('start_time');
$endTime = request()->get('end_time');
if(empty($startTime) && empty($endTime)){
$content->row(Alert::make('请先选择时间查询!','')->removable());
}
return $content->title('社区代理点统计报表')
->body(function(Row $row){
$row->column(12,$this->grid());
});
}
/**
* 数据导出
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function export(Request $request)
{
$this->marketId = Auth::getMarket();
$params = [
'status' => $request->input('status',null),
'name' => $request->input('name',false),
'market_id' => $this->marketId ? $this->marketId : $request->input('market_id',null),
'user_id' => $request->input('user_id',null),
'start_time' => $request->input('start_time',null),
'end_time' => $request->input('end_time',null),
];
$name = date('Y-m-d-His',time());
$data = new \App\Admin\Actions\Exporter\ReportCommunity($params);
return Excel::download($data, $name.'.xlsx');
}
}

193
app/Admin/Repositories/v3/ReportCommunity.php

@ -0,0 +1,193 @@
<?php
namespace App\Admin\Repositories\v3;
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\LanzuCsInfo as CsInfoModel;
use App\Models\LanzuUserRelationBind as UserRelationBindModel;
use App\Models\ImsCjdcOrderMain as OrderMainModel;
class ReportCommunity extends EloquentRepository
{
protected $params = [];
protected $currentPage = 1;
protected $perPage = 10;
/**
* Model.
*
* @var string
*/
protected $eloquentClass = Model::class;
public function __construct($params = [])
{
$this->params = $params;
}
/**
* 获取统计列表数据
*/
public function get(GridModel $model)
{
// 获取当前页数
$this->currentPage = $model->getCurrentPage();
// 获取每页显示行数
$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']];
}
$selectsEmp = 'id,user_id,position,market_id,name,tel,status';
$list = $this->getDataModel($selectsEmp, $where);
return $model->makePaginator(
$list['total'] ?? 0,$list['data'] ?? []
);
}
public function getDataModel($selectsEmp, $params = [], $isPerPage = true)
{
// 获取筛选参数 如果没有时间,则用当天的数据
$startTime = $params['start_time'] ?? request()->input('start_time','');
$endTime = $params['end_time'] ?? request()->input('end_time','');
$status = request()->input('status',false);
$params = [
'status' => $status !== false ? $status : $params['status'] ?? null,
'user_id' => $params['user_id'] ?? request()->input('user_id', null),
'market_id' => $params['market_id'] ?? request()->input('market_id',null),
'name' => $params['name'] ?? request()->input('name',false),
];
if(empty($startTime) && empty($endTime)){
return [];
}
$nowDay = date('Y-m-d', time());
if(empty($startTime)){
$startTime = '2020-07-01 00:00:00';
}else{
$startTime = $startTime . '00:00:00';
}
if(empty($endTime)){
$endTime = $nowDay . '23:59:59';
}else{
$endTime = $endTime . '23:59:59';
}
// 获取懒族员工
$employeesData = $this->getEmployeesData($selectsEmp,$params);
$employeesData->orderBy('id','desc');
if($isPerPage){
$employeeList = $employeesData->paginate($this->perPage);
$employeeList = $employeeList->toArray();
}else{
$list = $employeesData->get()->toArray();
$employeeList = [
'total' => count($list),
'data' => $list
];
}
if(!isset($employeeList['data']) || count($employeeList['data']) <= 0 ) {
return $employeeList;
}
foreach ($employeeList['data'] 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['bound_order_online_count'] = $totalOrdersCount;
$employee['plat_new_user_order_online_count'] = $platNewUserOrdersCount;
}
return $employeeList;
}
/**
* 获取懒族员工 model
*/
public function getEmployeesData($selects, $params = [])
{
$model = Model::select(DB::raw($selects));
if(isset($params['status']) && is_numeric($params['status'])){
$model->where('status',$params['status']);
}
if(isset($params['user_id']) && is_numeric($params['user_id'])){
$model->where('user_id', $params['user_id']);
}
if(isset($params['name']) && $params['name']){
$model->where('name','like','%'.$params['name'].'%');
}
if(isset($params['market_id']) && is_numeric($params['market_id'])){
$model->where('market_id', $params['market_id']);
}
return $model;
}
}

4
app/Admin/routes.php

@ -109,4 +109,8 @@ Route::group([
$router->resource('/share_card_setting', 'v3\ShareCardSettingController'); $router->resource('/share_card_setting', 'v3\ShareCardSettingController');
// 关注公众号设置 // 关注公众号设置
$router->any('/official_subscribe_info', 'v3\SystemConfigController@OfficialSubscribeInfoSettingForm'); $router->any('/official_subscribe_info', 'v3\SystemConfigController@OfficialSubscribeInfoSettingForm');
// 社区代理点统计
$router->any('/report_community', 'v3\ReportCommunityController@index');
$router->any('/report_community_export', 'v3\ReportCommunityController@export');
}); });

12
resources/lang/zh-CN/report-community.php

@ -0,0 +1,12 @@
<?php
return [
'labels' => [
'ReportCommunity' => '社区代理点统计报表',
'report_community' => '社区代理点统计报表',
],
'fields' => [
'id' => 'ID',
],
'options' => [
],
];
Loading…
Cancel
Save