链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

173 lines
6.7 KiB

<?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']];
}
$list = $this->getDataModel($where);
return $model->makePaginator(
$list['total'] ?? 0,$list['data'] ?? []
);
}
public function getDataModel($params = [])
{
// 获取筛选参数 如果没有时间,则用当天的数据
$startTime = $params['start_time'] ?? request()->input('start_time','');
$endTime = $params['end_time'] ?? request()->input('end_time','');
$time = time();
if(empty($startTime)){
$startTime = date('Y-m-d', $time) . '00:00:00';
}else{
$startTime = $startTime . '00:00:00';
}
if(empty($endTime)){
$endTime = date('Y-m-d', $time) . '23:59:59';
}else{
$endTime = $endTime . '23:59:59';
}
// 获取懒族员工
$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;
}
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(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;
}
}