| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 | <?phpnamespace App\Imports;use App\Http\Logics\Admin\SiteSyncLogic;use App\Http\Models\Server;use App\Http\Models\Site;use Illuminate\Support\Collection;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Log;use Illuminate\Validation\Rule;use Maatwebsite\Excel\Concerns\Importable;use Maatwebsite\Excel\Concerns\ToCollection;use Maatwebsite\Excel\Concerns\WithMappedCells;use Maatwebsite\Excel\Concerns\WithValidation;class DemoImport implements ToCollection{    use Importable;    public function __construct()    {    }    const FIELD_MAP = [        2 => 'domain',        3 => 'server_ip',        4 => 'api_url',        5 => 'webmaster_domain',        6 => 'report_day'    ];    public function collection(Collection $collections)    {//        $allSites = Site::query()->get();//        $serverMap = Server::all()->pluck('server_id', 'server_ip')->toArray();        $all = collect([]);        foreach ($collections as $key => $collection) {            if ($key < 1 || empty($collection[0]) || empty($collection[2])) {                continue;            }            $temp = $this->fieldMap($collection);            $all->push($temp);        }        $items = $all->toArray();        $siteSync = new SiteSyncLogic();        foreach ($items as $key => $item) {            $temp = $allSites->where('domain', $item['domain'])->first();            if ($temp) {                $updated = [                    'server_id' => $serverMap[$item['server_ip']] ?? 0,                    'api_url' => $item['api_url'],                    'webmaster_domain' => $item['webmaster_domain'],                    'report_day' => $item['report_day']                ];                $temp->update($updated);                $syncUpdate = $temp->toArray() + ['server_ip' => $item['server_ip']];                $siteSync->update($temp->old_id, $siteSync->fieldMap($syncUpdate));            } else {                echo $item['domain'] . '<br>';            }        }        dump('success');    }    public function fieldMap($collect): array    {        $result = [];        foreach ($collect as $inx => $val) {            if (empty(self::FIELD_MAP[$inx])) continue;            if (self::FIELD_MAP[$inx] == 'report_day') {                $result[self::FIELD_MAP[$inx]] = intval($val);            } else {                $result[self::FIELD_MAP[$inx]] = $val;            }        }        return $result;    }}
 |