MonthlySummaryCommand.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Console\Commands\Manage;
  3. use App\Repositories\Enums\Manage\DealStatusEnum;
  4. use App\Repositories\Models\Base\Department;
  5. use App\Repositories\Models\Manage\Message;
  6. use App\Repositories\Models\Manage\Examine;
  7. use Carbon\Carbon;
  8. use Illuminate\Console\Command;
  9. /**
  10. * 月度统计
  11. * @package Prettus\Repository\Generators\Commands
  12. * @author Anderson Andrade <contato@andersonandra.de>
  13. */
  14. class MonthlySummaryCommand extends Command
  15. {
  16. /**
  17. * The name of command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'manage:monthly-summary';
  22. /**
  23. * The description of command.
  24. *
  25. * @var string
  26. */
  27. protected $description = '月度统计';
  28. /**
  29. * The type of class being generated.
  30. *
  31. * @var string
  32. */
  33. protected $type = 'manage';
  34. /**
  35. * Execute the command.
  36. *
  37. * @return void
  38. * @see fire()
  39. */
  40. public function handle()
  41. {
  42. //分部门统计
  43. $firstDay = Carbon::now()->addMonths(-1)->startOfMonth()->toDateTimeString();
  44. $lastDay = Carbon::now()->addMonths(-1)->endOfMonth()->toDateTimeString();
  45. $month = $firstDay;
  46. //全部部门
  47. $departments = Department::getLast();
  48. foreach ($departments as $department) {
  49. //部门id
  50. $departmentId = $department['id'];
  51. //总数量
  52. $totalNums = Message::query()->where('import_department_id', $departmentId)->where('created_at', '>=', $firstDay)->where('created_at', '<', $lastDay)->count();
  53. //指派数
  54. $assignNums = Message::query()->where('import_department_id', $departmentId)->where('is_more_department', '!=', '0')->count();
  55. //完结数
  56. $overNums = Message::query()->where('import_department_id', $departmentId)->where('deal_status', '=', DealStatusEnum::OK)->count();
  57. //办结率
  58. $overRate = 0;
  59. if ($totalNums != 0) {
  60. $overRate = $overNums / $totalNums;
  61. }
  62. //满意数量
  63. $satisfiedNums = Message::query()->where('import_department_id', $departmentId)->where('deal_evaluation', '>=', '60')->count();
  64. //满意率
  65. $satisfiedRate = 0;
  66. if ($overNums != 0) {
  67. $satisfiedRate = $satisfiedNums / $overNums;
  68. }
  69. //超时次数
  70. $timeoutNums = Message::query()->where('import_department_id', $departmentId)->whereColumn('deal_limit_date', '<', 'deal_time')->count();
  71. //每个部门存一条数据
  72. Examine::query()->create(
  73. [
  74. 'month' => $month,
  75. 'name' => $departmentId,
  76. 'department_id' => $departmentId,
  77. 'total_nums' => $totalNums,
  78. 'assign_nums' => $assignNums,
  79. 'over_nums' => $overNums,
  80. 'over_rate' => $overRate,
  81. 'satisfaction_rate' => $satisfiedRate,
  82. 'overtime_nums' => $timeoutNums
  83. ]);
  84. }
  85. $this->line('操作成功');
  86. }
  87. }