ClearDataController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\ClearYearDataJob;
  4. use App\Models\Contract;
  5. use App\Models\File;
  6. use App\Models\MafOrder;
  7. use App\Models\Order;
  8. use App\Models\Product;
  9. use App\Models\ProductSKU;
  10. use App\Models\Reclamation;
  11. use App\Models\Schedule;
  12. use App\Models\Ttn;
  13. use Illuminate\Http\JsonResponse;
  14. use Illuminate\Http\Request;
  15. use Illuminate\View\View;
  16. class ClearDataController extends Controller
  17. {
  18. public function index(): View
  19. {
  20. return view('clear-data.index', [
  21. 'active' => 'clear-data',
  22. 'years' => $this->getAvailableYears(),
  23. ]);
  24. }
  25. public function stats(Request $request): JsonResponse
  26. {
  27. $year = (int) $request->input('year');
  28. if ($year < 2020 || $year > 2100) {
  29. return response()->json(['error' => 'Некорректный год'], 422);
  30. }
  31. $stats = $this->collectStats($year);
  32. return response()->json([
  33. 'year' => $year,
  34. 'stats' => $stats,
  35. 'total' => array_sum($stats),
  36. ]);
  37. }
  38. public function destroy(Request $request)
  39. {
  40. $year = (int) $request->input('year');
  41. if ($year < 2020 || $year > 2100) {
  42. return redirect()->back()->with('danger', 'Некорректный год');
  43. }
  44. ClearYearDataJob::dispatch($year, $request->user()->id);
  45. return redirect()->route('clear-data.index')->with('success', 'Удаление данных за ' . $year . ' год запущено. Вы получите уведомление о завершении.');
  46. }
  47. private function getAvailableYears(): array
  48. {
  49. $years = [];
  50. $orderYears = Order::withoutGlobalScopes()->withTrashed()
  51. ->selectRaw('DISTINCT year')
  52. ->pluck('year')
  53. ->toArray();
  54. $productYears = Product::withoutGlobalScopes()->withTrashed()
  55. ->selectRaw('DISTINCT year')
  56. ->pluck('year')
  57. ->toArray();
  58. $mafOrderYears = MafOrder::withoutGlobalScopes()->withTrashed()
  59. ->selectRaw('DISTINCT year')
  60. ->pluck('year')
  61. ->toArray();
  62. $contractYears = Contract::selectRaw('DISTINCT year')
  63. ->pluck('year')
  64. ->toArray();
  65. $ttnYears = Ttn::selectRaw('DISTINCT year')
  66. ->pluck('year')
  67. ->toArray();
  68. $years = array_unique(array_merge($orderYears, $productYears, $mafOrderYears, $contractYears, $ttnYears));
  69. rsort($years);
  70. return $years;
  71. }
  72. private function collectStats(int $year): array
  73. {
  74. $orderIds = Order::withoutGlobalScopes()->withTrashed()->where('year', $year)->pluck('id');
  75. $productIds = Product::withoutGlobalScopes()->withTrashed()->where('year', $year)->pluck('id');
  76. $mafOrderIds = MafOrder::withoutGlobalScopes()->withTrashed()->where('year', $year)->pluck('id');
  77. $productSkuIds = ProductSKU::withoutGlobalScopes()->withTrashed()->where('year', $year)->pluck('id');
  78. $reclamationCount = Reclamation::whereIn('order_id', $orderIds)->count();
  79. $fileIds = $this->collectFileIds($year, $orderIds, $productIds, $productSkuIds);
  80. return [
  81. 'Заказы (Orders)' => $orderIds->count(),
  82. 'Заказы МАФ (MafOrders)' => $mafOrderIds->count(),
  83. 'Продукты (Products)' => $productIds->count(),
  84. 'SKU продуктов (ProductSKU)' => $productSkuIds->count(),
  85. 'Рекламации (Reclamations)' => $reclamationCount,
  86. 'Расписания (Schedules)' => Schedule::whereIn('order_id', $orderIds)->count(),
  87. 'ТТН (Ttns)' => Ttn::where('year', $year)->count(),
  88. 'Контракты (Contracts)' => Contract::where('year', $year)->count(),
  89. 'Файлы (Files)' => $fileIds->count(),
  90. ];
  91. }
  92. private function collectFileIds(int $year, $orderIds, $productIds, $productSkuIds): \Illuminate\Support\Collection
  93. {
  94. $fileIds = collect();
  95. $fileIds = $fileIds->merge(
  96. \DB::table('order_photo')->whereIn('order_id', $orderIds)->pluck('file_id')
  97. );
  98. $fileIds = $fileIds->merge(
  99. \DB::table('order_document')->whereIn('order_id', $orderIds)->pluck('file_id')
  100. );
  101. $fileIds = $fileIds->merge(
  102. \DB::table('order_statement')->whereIn('order_id', $orderIds)->pluck('file_id')
  103. );
  104. $reclamationIds = Reclamation::whereIn('order_id', $orderIds)->pluck('id');
  105. $fileIds = $fileIds->merge(
  106. \DB::table('reclamation_photo_before')->whereIn('reclamation_id', $reclamationIds)->pluck('file_id')
  107. );
  108. $fileIds = $fileIds->merge(
  109. \DB::table('reclamation_photo_after')->whereIn('reclamation_id', $reclamationIds)->pluck('file_id')
  110. );
  111. $fileIds = $fileIds->merge(
  112. \DB::table('reclamation_document')->whereIn('reclamation_id', $reclamationIds)->pluck('file_id')
  113. );
  114. $fileIds = $fileIds->merge(
  115. \DB::table('reclamation_act')->whereIn('reclamation_id', $reclamationIds)->pluck('file_id')
  116. );
  117. $fileIds = $fileIds->merge(
  118. Product::withoutGlobalScopes()->withTrashed()
  119. ->whereIn('id', $productIds)
  120. ->whereNotNull('certificate_id')
  121. ->pluck('certificate_id')
  122. );
  123. $fileIds = $fileIds->merge(
  124. ProductSKU::withoutGlobalScopes()->withTrashed()
  125. ->whereIn('id', $productSkuIds)
  126. ->whereNotNull('passport_id')
  127. ->pluck('passport_id')
  128. );
  129. $fileIds = $fileIds->merge(
  130. Ttn::where('year', $year)->whereNotNull('file_id')->pluck('file_id')
  131. );
  132. return $fileIds->unique();
  133. }
  134. }