<?php

namespace App\Imports\User;

use App\Models\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class UserSheet implements ToCollection
{
    /**
     * @param Collection $collection
     */
    public function collection(Collection $collection)
    {
        $user_info = [];
        foreach ($collection as $row) {
            if ($row[0]=="userId")
            {
                continue;
            }
            //用户名
            if ($row[1] == null)
            {
                $user_info['user_name'] = '';
            }else{
                $user_info['user_name'] = $row[1];
            }
            //昵称
            if ($row[2] == null)
            {
                $user_info['nickname'] = '';
            }else{
                $user_info['nickname'] = $row[2];
            }
            //电话
            if ($row[3] == null)
            {
                $user_info['phone'] = '';
            }else{
                $user_info['phone'] = $row[3];
            }

            if ($row[4] == null)
            {
                $user_info['created_at'] =self::excelTime(0);
            }else{
                $user_info['created_at'] = self::excelTime($row[4]);
            }

            if ($row[5] == null)
            {
                $user_info['balance'] = '';
            }else{
                $user_info['balance'] = $row[5];
            }
            User::insert($user_info);
        }

    }
    public static function excelTime($date, $time = false) {
        $date=$date>25568?$date+1:25569;
        $ofs=(70 * 365 + 17+2) * 86400;
        $d1 = date("Y-m-d H:i:s",($date * 86400) - $ofs);
        $d1 = strtotime("-8hours",strtotime($d1));
        $d1 = date('Y-m-d H:i:s',$d1);
        $date = $d1;
        return $date;

    }
}
