| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace App\Services;
- use App\OrderIssueProcessLog;
- use App\Owner;
- use App\Services\common\QueryService;
- use App\User;
- use App\Exports\Export;
- use Carbon\Carbon;
- use Illuminate\Pagination\LengthAwarePaginator;
- use Illuminate\Pagination\Paginator;
- use Illuminate\Support\Facades\Auth;
- use Maatwebsite\Excel\Facades\Excel;
- class OrderIssuePerformanceService
- {
- public function getQuery(array $queryParam)
- {
- $user_id = $queryParam['user_id'] ?? '';
- $owner_id = $queryParam['owner_id'] ?? '';
- $timeFrame = $queryParam['timeFrame'] ?? '';
- unset($queryParam['owner_id'], $queryParam['user_id'], $queryParam['timeFrame'],$queryParam['_token']);
- $user = Auth::user();
- $owner_ids = $user ? $user->getPermittingOwnerIdsAttribute() : [];
- $orderIssueProcessLog = OrderIssueProcessLog::query()->with(['user' => function ($query) use ($user_id) {
- if ($user_id) {
- $query->where('id', $user_id);
- }
- }, 'orderIssue.order.owner' => function ($query) use ($owner_id,$owner_ids) {
- if ($owner_id && in_array($owner_id,$owner_ids) ) {
- $query->where('id', $owner_id);
- }else{
- $query->whereIn('id',$owner_ids);
- }
- }]);
- $columnQueryRules = [
- 'create_start' => ['alias' => 'created_at', 'startDate' => ' 00:00:00'],
- 'create_end' => ['alias' => 'created_at', 'endDate' => ' 23:59:59'],
- ];
- if ($timeFrame ?? false) {
- if ($timeFrame == 'day') {
- $queryParam['create_start'] = date('Y-m-d');
- $queryParam['create_end'] = date('Y-m-d');
- } else if ($timeFrame == 'yesterday') {
- $queryParam['create_start'] = date('Y-m-d', strtotime('-1 day'));
- $queryParam['create_end'] = date('Y-m-d', strtotime('-1 day'));
- } else if ($timeFrame) {
- $date = date('Y-m-d', strtotime('-1 weeks', strtotime('Monday')));
- $queryParam['create_start'] = $date;
- $queryParam['create_end'] = date('Y-m-d');
- } else if ($timeFrame == 'month') {
- $queryParam['create_start'] = date('Y-m-1');
- $queryParam['create_end'] = date('Y-m-d');
- }
- }
- $orderIssueProcessLog = app(QueryService::class)->query($queryParam, $orderIssueProcessLog, $columnQueryRules);
- return $orderIssueProcessLog;
- }
- public function paginate(array $params)
- {
- $paginate = $params['paginate'] ?? 50;
- $page = $params['page'] ?? 1;
- $data = $this->getAll($params);
- $collection = collect($data);
- $list = $collection->slice(($page - 1) * $paginate, $paginate)->all();
- $option = ['path' => Paginator::resolveCurrentPath()];
- $paginate = new LengthAwarePaginator($list, $collection->count(), $paginate, $page,$option );
- return $paginate;
- }
- public function getAll($params)
- {
- $query = OrderIssueProcessLog::query()->selectRaw('user_id')->groupBy('user_id')->get();
- $user_ids = $query->map(function($value){
- return $value->user_id;
- });
- $owners = Owner::query()->with('order.issue')->whereHas('order.issue')->get();
- $data = [];
- $total = 0;
- foreach ($user_ids as $key => $user_id) {
- $params['user_id'] = $user_id;
- $user = User::query()->find($user_id);
- $orderIssueProcessLog = $this->getQuery($params)->get();
- foreach ($owners as $owner) {
- $createCount = $orderIssueProcessLog->where('user_id', $user_id)->where('orderIssue.order.owner.id', $owner['id'])->where('type', '创建')->count(); //创建
- $endCount = $orderIssueProcessLog->where('user_id', $user_id)->where('orderIssue.order.owner.id', $owner['id'])->where('type', '结束')->count(); //结束
- $treatmentCount = $orderIssueProcessLog->where('user_id', $user_id)->where('orderIssue.order.owner.id', $owner['id'])->where('type', '处理')->count(); //处理
- $sum = $createCount + $endCount + $treatmentCount;
- if ($sum == 0) {continue;}
- $total++;
- $data[] = ['id'=>$total,'user' => $user['name'], 'owner' => $owner['name'], 'createCount' => $createCount, 'endCount' => $endCount, 'treatmentCount' => $treatmentCount, 'sum' => $sum];
- }
- }
- return $data;
- }
- public function exportPerformance(array $params)
- {
- $performance = $this->getAll($params);
- $row = [['user'=>'客服','owner'=>'客户','createCount'=>'创建数','treatmentCount' =>'处理数','endCount'=>'完结数','sum'=>'总数']];
- return Excel::download(new Export($row,$performance),date('YmdHis', time()).'-客服绩效.xlsx');
- }
- }
|