OrderIssuePerformanceService.php 4.8 KB

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