ppp

mail@pastecode.io avatar
unknown
php
2 years ago
3.3 kB
1
Indexable
Never
<?php

namespace App\Imports;

use App\Models\UploadTarget;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Row;
use Maatwebsite\Excel\Concerns\OnEachRow;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithUpsertColumns;
use Maatwebsite\Excel\Concerns\WithUpserts;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Validators\Failure;
use Throwable;

class UploadTargetResultImport implements OnEachRow, WithHeadingRow, WithValidation, SkipsOnError, SkipsOnFailure, WithUpserts, WithUpsertColumns
{
    use Importable, SkipsErrors;

    public function onRow(Row $row)
    {
        $targetExtraIds = DB::table('targets')
            ->where('status', TARGET_ACTIVE)
            ->whereNull('deleted_at')
            ->pluck('extra_id')
            ->toArray();

        $rowData = [];

       // dump($row);

        foreach ($targetExtraIds as $key => $extraId) {
            $rowData[$key]['emp_code'] = $row['ma_nhan_vien'];
            $rowData[$key]['month_report'] = $row['thang'];
            $rowData[$key]['year_report'] = $row['nam'];
            $rowData[$key]['target_id'] = DB::table('targets')->where('extra_id', $extraId)->first()->id;
            $rowData[$key]['result'] = $row[Str::slug($extraId, '_')];
        }

        //dump($rowData);

        foreach ($rowData as $row) {
            UploadTarget::create($row);


//            UploadTarget::updateOrCreate(
//                [
//                    'target_id' => $row['target_id'],
//                    'emp_code' => $row['emp_code'],
//                    'month_report' => $row['month_report'],
//                    'year_report' => $row['year_report']
//                ],
//                [
//                    'result' => $row['result']
//                ]
//            );
        }

    }

    public function rules(): array
    {
        return [
            '*.ma_nhan_vien' => 'required',
            '*.thang' => 'required|numeric',
            '*.nam' => 'required|numeric',
        ];
    }

    /**
     * @return array
     */
    public function customValidationMessages(): array
    {
        return [
            'ma_nhan_vien.required' => 'Mã nhân viên',
            'thang.required' => 'Tháng là bắt buộc',
            'thang.numeric' => 'Tháng phải là một số',
            'nam.required' => 'Năm là bắt buộc',
            'nam.numeric' => 'Năm phải là một số',
        ];
    }

    /**
     * @return string|array
     */
    public function uniqueBy()
    {
        return ['target_id', 'emp_code', 'month_report', 'year_report'];
    }

    /**
     * @return array
     */
    public function upsertColumns()
    {
        return ['result'];
    }

    public function onError(Throwable $error)
    {
        dd($error);
    }

    public function onFailure(Failure ...$failures)
    {
        dd($failures);
        // TODO: Implement onFailure() method.
    }
}