OrderIssuePerformanceService.php 4.7 KB

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