| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- namespace App\Jobs;
- use App\Events\SendWebSocketMessageEvent;
- use App\Models\Contract;
- use App\Models\File;
- use App\Models\MafOrder;
- use App\Models\Order;
- use App\Models\Product;
- use App\Models\ProductSKU;
- use App\Models\Reclamation;
- use App\Models\Schedule;
- use App\Models\Ttn;
- use Exception;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Queue\Queueable;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- class ClearYearDataJob implements ShouldQueue
- {
- use Queueable;
- public function __construct(
- private readonly int $year,
- private readonly int $userId,
- ) {}
- public function handle(): void
- {
- try {
- DB::beginTransaction();
- $this->clearData();
- DB::commit();
- Log::info("Clear year data job done for year {$this->year}");
- event(new SendWebSocketMessageEvent(
- "Данные за {$this->year} год успешно удалены!",
- $this->userId,
- ['success' => true, 'year' => $this->year]
- ));
- } catch (Exception $e) {
- DB::rollBack();
- Log::error("Clear year data job failed for year {$this->year}: " . $e->getMessage());
- event(new SendWebSocketMessageEvent(
- "Ошибка удаления данных за {$this->year} год: " . $e->getMessage(),
- $this->userId,
- ['error' => $e->getMessage(), 'year' => $this->year]
- ));
- }
- }
- private function clearData(): void
- {
- $orderIds = Order::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->pluck('id');
- $productIds = Product::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->pluck('id');
- $productSkuIds = ProductSKU::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->pluck('id');
- $reclamationIds = Reclamation::whereIn('order_id', $orderIds)->pluck('id');
- $fileIds = $this->collectFileIds($orderIds, $productIds, $productSkuIds);
- // Рекламации и связанные данные
- DB::table('reclamation_details')->whereIn('reclamation_id', $reclamationIds)->delete();
- DB::table('reclamation_product_sku')->whereIn('reclamation_id', $reclamationIds)->delete();
- DB::table('reclamation_photo_before')->whereIn('reclamation_id', $reclamationIds)->delete();
- DB::table('reclamation_photo_after')->whereIn('reclamation_id', $reclamationIds)->delete();
- DB::table('reclamation_document')->whereIn('reclamation_id', $reclamationIds)->delete();
- DB::table('reclamation_act')->whereIn('reclamation_id', $reclamationIds)->delete();
- Reclamation::whereIn('id', $reclamationIds)->delete();
- // Связи заказов с файлами
- DB::table('order_photo')->whereIn('order_id', $orderIds)->delete();
- DB::table('order_document')->whereIn('order_id', $orderIds)->delete();
- DB::table('order_statement')->whereIn('order_id', $orderIds)->delete();
- // Расписания
- Schedule::whereIn('order_id', $orderIds)->delete();
- // SKU продуктов
- ProductSKU::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->forceDelete();
- // Заказы
- Order::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->forceDelete();
- // Заказы МАФ
- MafOrder::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->forceDelete();
- // Продукты
- Product::withoutGlobalScopes()->withTrashed()
- ->whereIn('id', $productIds)
- ->update(['certificate_id' => null]);
- Product::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->forceDelete();
- // ТТН
- Ttn::where('year', $this->year)->update(['file_id' => null]);
- Ttn::where('year', $this->year)->delete();
- // Контракты
- Contract::where('year', $this->year)->delete();
- // Файлы
- $this->deleteFiles($fileIds);
- }
- private function collectFileIds($orderIds, $productIds, $productSkuIds): \Illuminate\Support\Collection
- {
- $fileIds = collect();
- $fileIds = $fileIds->merge(
- DB::table('order_photo')->whereIn('order_id', $orderIds)->pluck('file_id')
- );
- $fileIds = $fileIds->merge(
- DB::table('order_document')->whereIn('order_id', $orderIds)->pluck('file_id')
- );
- $fileIds = $fileIds->merge(
- DB::table('order_statement')->whereIn('order_id', $orderIds)->pluck('file_id')
- );
- $reclamationIds = Reclamation::whereIn('order_id', $orderIds)->pluck('id');
- $fileIds = $fileIds->merge(
- DB::table('reclamation_photo_before')->whereIn('reclamation_id', $reclamationIds)->pluck('file_id')
- );
- $fileIds = $fileIds->merge(
- DB::table('reclamation_photo_after')->whereIn('reclamation_id', $reclamationIds)->pluck('file_id')
- );
- $fileIds = $fileIds->merge(
- DB::table('reclamation_document')->whereIn('reclamation_id', $reclamationIds)->pluck('file_id')
- );
- $fileIds = $fileIds->merge(
- DB::table('reclamation_act')->whereIn('reclamation_id', $reclamationIds)->pluck('file_id')
- );
- $fileIds = $fileIds->merge(
- Product::withoutGlobalScopes()->withTrashed()
- ->whereIn('id', $productIds)
- ->whereNotNull('certificate_id')
- ->pluck('certificate_id')
- );
- $fileIds = $fileIds->merge(
- ProductSKU::withoutGlobalScopes()->withTrashed()
- ->whereIn('id', $productSkuIds)
- ->whereNotNull('passport_id')
- ->pluck('passport_id')
- );
- $fileIds = $fileIds->merge(
- Ttn::where('year', $this->year)->whereNotNull('file_id')->pluck('file_id')
- );
- return $fileIds->unique();
- }
- private function deleteFiles($fileIds): void
- {
- $files = File::whereIn('id', $fileIds)->get();
- foreach ($files as $file) {
- if ($file->path && Storage::disk('public')->exists($file->path)) {
- Storage::disk('public')->delete($file->path);
- }
- }
- File::whereIn('id', $fileIds)->delete();
- }
- }
|