|
|
@@ -0,0 +1,102 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services;
|
|
|
+
|
|
|
+use App\OrderIssueProcessLog;
|
|
|
+use App\Owner;
|
|
|
+use App\Services\common\QueryService;
|
|
|
+use App\User;
|
|
|
+use App\Exports\Export;
|
|
|
+use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
+use Illuminate\Pagination\Paginator;
|
|
|
+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']);
|
|
|
+ $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) {
|
|
|
+ if ($owner_id) {
|
|
|
+ $query->where('id', $owner_id);
|
|
|
+ }
|
|
|
+ }]);
|
|
|
+ $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)
|
|
|
+ {
|
|
|
+ $arr = OrderIssueProcessLog::all()->groupBy('user_id');
|
|
|
+ $owners = Owner::all();
|
|
|
+ $user_ids = array_keys($arr->toArray());
|
|
|
+ $data = [];
|
|
|
+ $total = 0;
|
|
|
+ foreach ($user_ids as $key => $value) {
|
|
|
+ $params['user_id'] = $value;
|
|
|
+ $user = User::find($value);
|
|
|
+ $orderIssueProcessLog = $this->getQuery($params)->get();
|
|
|
+ foreach ($owners as $owner) {
|
|
|
+ $createCount = $orderIssueProcessLog->where('user_id', $value)->where('orderIssue.order.owner.id', $owner['id'])->where('type', '创建')->count(); //创建
|
|
|
+ $endCount = $orderIssueProcessLog->where('user_id', $value)->where('orderIssue.order.owner.id', $owner['id'])->where('type', '结束')->count(); //结束
|
|
|
+ $treatmentCount = $orderIssueProcessLog->where('user_id', $value)->where('orderIssue.order.owner.id', $owner['id'])->where('type', '处理')->count(); //处理
|
|
|
+ $sum = $createCount + $endCount + $treatmentCount;
|
|
|
+ if ($sum == 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $total++;
|
|
|
+ array_push($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');
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|