OrderIssuePerformanceService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Services;
  3. use App\OrderIssueProcessLog;
  4. use App\Owner;
  5. use App\Services\common\QueryService;
  6. use App\User;
  7. use App\Exports\Export;
  8. use Illuminate\Pagination\LengthAwarePaginator;
  9. use Illuminate\Pagination\Paginator;
  10. use Maatwebsite\Excel\Facades\Excel;
  11. class OrderIssuePerformanceService
  12. {
  13. public function getQuery(array $queryParam)
  14. {
  15. $user_id = $queryParam['user_id'] ?? '';
  16. $owner_id = $queryParam['owner_id'] ?? '';
  17. $timeFrame = $queryParam['timeFrame'] ?? '';
  18. unset($queryParam['owner_id'], $queryParam['user_id'], $queryParam['timeFrame'],$queryParam['_token']);
  19. $orderIssueProcessLog = OrderIssueProcessLog::query()->with(['user' => function ($query) use ($user_id) {
  20. if ($user_id) {
  21. $query->where('id', $user_id);
  22. }
  23. }, 'orderIssue.order.owner' => function ($query) use ($owner_id) {
  24. if ($owner_id) {
  25. $query->where('id', $owner_id);
  26. }
  27. }]);
  28. $columnQueryRules = [
  29. 'create_start' => ['alias' => 'created_at', 'startDate' => ' 00:00:00'],
  30. 'create_end' => ['alias' => 'created_at', 'endDate' => ' 23:59:59'],
  31. ];
  32. if ($timeFrame ?? false) {
  33. if ($timeFrame == 'day') {
  34. $queryParam['create_start'] = date('Y-m-d');
  35. $queryParam['create_end'] = date('Y-m-d');
  36. } else if ($timeFrame == 'yesterday') {
  37. $queryParam['create_start'] = date('Y-m-d', strtotime('-1 day'));
  38. $queryParam['create_end'] = date('Y-m-d', strtotime('-1 day'));
  39. } else if ($timeFrame) {
  40. $date = date('Y-m-d', strtotime('-1 weeks', strtotime('Monday')));
  41. $queryParam['create_start'] = $date;
  42. $queryParam['create_end'] = date('Y-m-d');
  43. } else if ($timeFrame == 'month') {
  44. $queryParam['create_start'] = date('Y-m-1');
  45. $queryParam['create_end'] = date('Y-m-d');
  46. }
  47. }
  48. $orderIssueProcessLog = app(QueryService::class)->query($queryParam, $orderIssueProcessLog, $columnQueryRules);
  49. return $orderIssueProcessLog;
  50. }
  51. public function paginate(array $params)
  52. {
  53. $paginate = $params['paginate'] ?? 50;
  54. $page = $params['page'] ?? 1;
  55. $data = $this->getAll($params);
  56. $collection = collect($data);
  57. $list = $collection->slice(($page - 1) * $paginate, $paginate)->all();
  58. $option = ['path' => Paginator::resolveCurrentPath()];
  59. $paginate = new LengthAwarePaginator($list, $collection->count(), $paginate, $page,$option );
  60. return $paginate;
  61. }
  62. public function getAll($params)
  63. {
  64. $arr = OrderIssueProcessLog::all()->groupBy('user_id');
  65. $owners = Owner::all();
  66. $user_ids = array_keys($arr->toArray());
  67. $data = [];
  68. $total = 0;
  69. foreach ($user_ids as $key => $value) {
  70. $params['user_id'] = $value;
  71. $user = User::find($value);
  72. $orderIssueProcessLog = $this->getQuery($params)->get();
  73. foreach ($owners as $owner) {
  74. $createCount = $orderIssueProcessLog->where('user_id', $value)->where('orderIssue.order.owner.id', $owner['id'])->where('type', '创建')->count(); //创建
  75. $endCount = $orderIssueProcessLog->where('user_id', $value)->where('orderIssue.order.owner.id', $owner['id'])->where('type', '结束')->count(); //结束
  76. $treatmentCount = $orderIssueProcessLog->where('user_id', $value)->where('orderIssue.order.owner.id', $owner['id'])->where('type', '处理')->count(); //处理
  77. $sum = $createCount + $endCount + $treatmentCount;
  78. if ($sum == 0) {
  79. continue;
  80. }
  81. $total++;
  82. array_push($data, ['id'=>$total,'user' => $user['name'], 'owner' => $owner['name'], 'createCount' => $createCount, 'endCount' => $endCount, 'treatmentCount' => $treatmentCount, 'sum' => $sum]);
  83. }
  84. }
  85. return $data;
  86. }
  87. public function exportPerformance(array $params)
  88. {
  89. $performance = $this->getAll($params);
  90. $row = [['user'=>'客服','owner'=>'客户','createCount'=>'创建数','treatmentCount' =>'处理数','endCount'=>'完结数','sum'=>'总数']];
  91. return Excel::download(new Export($row,$performance),date('YmdHis', time()).'-客服绩效.xlsx');
  92. }
  93. }