Advertisement
dicaribapak11

AreaFungsiSheetImport.php

May 27th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.51 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Imports\PetaOkupasiV3;
  4.  
  5. ini_set('memory_limit', '1000M');
  6.  
  7. use App\Models\AreaFungsiPetaOkupasi;
  8. use App\Models\MAreaFungsi;
  9. use App\Models\MasterAreaFungsiParent;
  10. use Illuminate\Support\Carbon;
  11. use Illuminate\Support\Facades\Cache;
  12. use Illuminate\Support\Facades\DB;
  13. use Maatwebsite\Excel\Concerns\OnEachRow;
  14. use Maatwebsite\Excel\Concerns\WithBatchInserts;
  15. use Maatwebsite\Excel\Concerns\WithStartRow;
  16. use Maatwebsite\Excel\Events\AfterImport;
  17. use Maatwebsite\Excel\Row;
  18.  
  19. class AreaFungsiSheetImport implements OnEachRow, WithBatchInserts, WithStartRow
  20. {
  21.     // Properti $keyCache digunakan untuk menyimpan cache.
  22.     protected $keyCache;
  23.  
  24.     // Properti $idVersion digunakan untuk menyimpan versi ID yang digunakan dalam sistem.
  25.     protected $idVersion;
  26.  
  27.     // Konstanta MODULE_CODE digunakan untuk menyimpan kode modul yang berkaitan dengan area, fungsi, dan peta okupasi.
  28.     const MODULE_CODE = 'area_fungsi_peta_okupasi';
  29.  
  30.     protected $modelMasterAreaFungsi;
  31.  
  32.     protected $modelMasterAreaFungsiParent;
  33.  
  34.     protected $modelAreaFungsiPO;
  35.  
  36.     /**
  37.      * Konstruktor kelas.
  38.      *
  39.      * @param  string  $keyCache  untuk penyimpanan cache.
  40.      * @param  string  $idVersion  Versi ID yang digunakan dalam sistem.
  41.      */
  42.     public function __construct(string $keyCache, string $idVersion)
  43.     {
  44.         $this->keyCache = $keyCache;
  45.         $this->idVersion = $idVersion;
  46.         $modelMasterAreaFungsi = new MAreaFungsi();
  47.         $this->modelMasterAreaFungsi = $modelMasterAreaFungsi;
  48.  
  49.         $modelMasterAreaFungsiParent = new MasterAreaFungsiParent();
  50.         $this->modelMasterAreaFungsiParent = $modelMasterAreaFungsiParent;
  51.  
  52.         $modelAreaFungsiPO = new AreaFungsiPetaOkupasi();
  53.         $this->modelAreaFungsiPO = $modelAreaFungsiPO;
  54.     }
  55.  
  56.     public function getAllMasterAreaFungsi()
  57.     {
  58.         $results = [];
  59.         foreach ($this->modelMasterAreaFungsi->get() as $val) {
  60.             $results[$val->kode] = $val;
  61.         }
  62.  
  63.         return $results;
  64.     }
  65.  
  66.     public function onRow(Row $row)
  67.     {
  68.         DB::beginTransaction();
  69.         try {
  70.             $row = $row->toArray();
  71.  
  72.             $idParent = '';
  73.  
  74.             $kodeAreaFungsi = trim($row[0]);
  75.             $judulAreaFungsi = $row[1];
  76.             $deskripsiAreaFungsi = $row[2];
  77.             $levelAreaFungsi = $row[3];
  78.             $kodeIndukAreaFungsi = $row[4];
  79.  
  80.             $dbExistingAreaFungsi = $this->getAllMasterAreaFungsi();
  81.  
  82.             $keyColumn = [
  83.                 'kode' => $kodeAreaFungsi,
  84.             ];
  85.             $updateColumn = [
  86.                 'judul' => $judulAreaFungsi,
  87.                 'deskripsi' => $deskripsiAreaFungsi,
  88.                 'is_aktif' => true,
  89.                 'created_at' => Carbon::now(),
  90.                 'updated_at' => Carbon::now(),
  91.                 'created_by' => getUser(),
  92.                 'updated_by' => getUser(),
  93.             ];
  94.  
  95.             $instData = $this->modelMasterAreaFungsi::updateOrCreate($keyColumn,
  96.                 $updateColumn);
  97.  
  98.             if (array_key_exists($kodeIndukAreaFungsi, $dbExistingAreaFungsi)) {
  99.                 $dataAreaFungsi = $dbExistingAreaFungsi[$kodeIndukAreaFungsi];
  100.                 $idParent = $dataAreaFungsi['id'];
  101.             }
  102.  
  103.             $keyColumn2 = [
  104.                 'id_area_fungsi' => $idParent,
  105.                 'id_child_fungsi' => $instData->id,
  106.             ];
  107.             $updateColumn2 = [
  108.                 'id_child_fungsi' => $instData->id,
  109.             ];
  110.  
  111.             $this->modelMasterAreaFungsiParent::updateOrCreate(
  112.                 $keyColumn2,
  113.                 $updateColumn2,
  114.             );
  115.  
  116.             $keyColumn3 = [
  117.                 'id_version' => $this->idVersion,
  118.                 'id_area_fungsi' => $instData->id,
  119.             ];
  120.  
  121.             $updateColumn3 = [
  122.                 'id_area_fungsi' => $instData->id,
  123.                 'id_parent_area_fungsi' => ! empty($idParent) ? $idParent : null,
  124.                 'id_version' => $this->idVersion,
  125.                 'kode' => ! empty($kodeAreaFungsi) ? $kodeAreaFungsi : '-',
  126.                 'judul' => ! empty($judulAreaFungsi) ? $judulAreaFungsi : '-',
  127.                 'is_aktif' => true,
  128.                 'created_at' => Carbon::now(),
  129.                 'updated_at' => Carbon::now(),
  130.                 'created_by' => getUser(),
  131.                 'updated_by' => getUser(),
  132.             ];
  133.  
  134.             $this->modelAreaFungsiPO::updateOrCreate(
  135.                 $keyColumn3,
  136.                 $updateColumn3,
  137.             );
  138.  
  139.             DB::commit();
  140.  
  141.             return true;
  142.         } catch (\Exception $e) {
  143.             DB::rollback();
  144.  
  145.             return $e;
  146.         }
  147.  
  148.     }
  149.  
  150.     public function batchSize(): int
  151.     {
  152.         return 1000;
  153.     }
  154.  
  155.     /**
  156.      * Mendaftarkan events yang terkait dengan fungsi.
  157.      *
  158.      * @return array Daftar peristiwa yang didaftarkan beserta callable-nya.
  159.      */
  160.     public function registerEvents(): array
  161.     {
  162.         return [
  163.             // Array callable, merujuk ke metode statis.
  164.             AfterImport::class => [self::class, 'afterImport'],
  165.         ];
  166.     }
  167.  
  168.     /**
  169.      * Metode yang dipanggil setelah proses impor selesai.
  170.      *
  171.      * @param  AfterImport  $event  Objek peristiwa setelah impor.
  172.      * @return bool Status keberhasilan penyelesaian proses impor.
  173.      */
  174.     public static function afterImport(AfterImport $event)
  175.     {
  176.         return true;
  177.     }
  178.  
  179.     public function startRow(): int
  180.     {
  181.         return 2;
  182.     }
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement