OrderIssuePerformanceService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. $query = OrderIssueProcessLog::query()->selectRaw('user_id')->groupBy('user_id')->get();
  70. $user_ids = $query->map(function($value){
  71. return $value->user_id;
  72. });
  73. $owners = Owner::all();
  74. /* $arr = OrderIssueProcessLog::all()->groupBy('user_id');
  75. $user_ids = array_keys($arr->toArray());*/
  76. $data = [];
  77. $total = 0;
  78. foreach ($user_ids as $key => $value) {
  79. $params['user_id'] = $value;
  80. $user = User::find($value);
  81. $orderIssueProcessLog = $this->getQuery($params)->get();
  82. foreach ($owners as $owner) {
  83. $createCount = $orderIssueProcessLog->where('user_id', $value)->where('orderIssue.order.owner.id', $owner['id'])->where('type', '创建')->count(); //创建
  84. $endCount = $orderIssueProcessLog->where('user_id', $value)->where('orderIssue.order.owner.id', $owner['id'])->where('type', '结束')->count(); //结束
  85. $treatmentCount = $orderIssueProcessLog->where('user_id', $value)->where('orderIssue.order.owner.id', $owner['id'])->where('type', '处理')->count(); //处理
  86. $sum = $createCount + $endCount + $treatmentCount;
  87. if ($sum == 0) {
  88. continue;
  89. }
  90. $total++;
  91. array_push($data, ['id'=>$total,'user' => $user['name'], 'owner' => $owner['name'], 'createCount' => $createCount, 'endCount' => $endCount, 'treatmentCount' => $treatmentCount, 'sum' => $sum]);
  92. }
  93. }
  94. return $data;
  95. }
  96. public function exportPerformance(array $params)
  97. {
  98. $performance = $this->getAll($params);
  99. $row = [['user'=>'客服','owner'=>'客户','createCount'=>'创建数','treatmentCount' =>'处理数','endCount'=>'完结数','sum'=>'总数']];
  100. return Excel::download(new Export($row,$performance),date('YmdHis', time()).'-客服绩效.xlsx');
  101. }
  102. }