Untitled

mail@pastecode.io avatar
unknown
php
3 years ago
3.4 kB
1
Indexable
Never
<?php /** @noinspection ALL */

namespace App\Imports;

use App\Models\Well;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\WithBatchInserts;

class ImportExcelFileWell implements ToModel,WithHeadingRow, WithBatchInserts,WithChunkReading,WithValidation,SkipsOnError
{
    use Importable,SkipsErrors;
    protected $auth;
    protected $model_feedback;

    public function __construct($slug)
    {
        $this->auth=Auth::user();
        if($slug == 'well') $this->model_feedback=Well::class;
    }

    public function model(array  $value)
    {
        $time=Carbon::now();

        $update_feedback = [
            'fl_name'                          => $value['fl_name'],
            'scheduling_location'              => $value['scheduling_location'],
        ];

        $data_feedback=[
            'well'                             => $value['well'],
            'department'                       => $value['department'],
            'sap_location'                     => $value['sap_location'],
            'fl_description'                   => $value['fl_description'],
            'last_change_by'                   => $this->auth->name,
            'updated_at'                       => Carbon::parse(Carbon::now())->setTime($time->hour, $time->minute),
            'history'                          => true,
            'status_update_input_by_user'     => "Update using Excel file by ".$this->auth->name
        ];

        DB::transaction(function () use($update_feedback,$data_feedback,$value) {
            $this->model_feedback::where('fl_name', $value['fl_name'])
                ->update(['history' => false]);
            $this->model_feedback::updateOrCreate($update_feedback, $data_feedback);
        });

    }

    public function batchSize(): int
    {
        return 500;
    }
    public function chunkSize(): int
    {
        return 500;
    }
    public function rules(): array
    {


        return [
            'fl_name'                          => 'required',
            'well'                             => 'required',
            'department'                       => 'required',
            'fl_description'                   => 'required',
            'scheduling_location'              => 'required',
            'sap_location'                     => 'required',
        ];
    }
    public function prepareForValidation($value, $index)
    {
        // strtoupper
        $value['fl_name']                = strtoupper($value['fl_name']);
        $value['well']                   = strtoupper($value['well']);
        $value['department']             = strtoupper($value['department']);
        $value['scheduling_location']    = strtoupper($value['scheduling_location']);
        $value['sap_location']           = strtoupper($value['sap_location']);

        return $value;
    }


}