文件目錄
控制器方法
隊列類
Ipmorts 類
控制器方法
public function batchEdit(Request $request)
{
if ($request->isMethod('get')) {
return view('tools/batchEdit');
} else {
$file = $request->file('file');
$originName = $file->getClientOriginalName();
$fileextension = $file->getClientOriginalExtension();
$lot = 'OD' . date("YmdHis", time()) . rand(1111, 9999);
$fileName = $file->storeAs('edit', $lot . '_origin.'.$fileextension);
$add = [
'lot' => $lot,
'excel_name' => $originName,
'ua' => $_SERVER['HTTP_USER_AGENT'],
'origin_filepath' => $fileName,
'created' => time(),
'operation' => \Auth::user()->id,
];
DB::table('upload_order_log')->insert($add);
dispatch(new orderUpload($lot))->onQueue('orderUpload');
return response()->json(['code' => 200, 'desc' => 'success']);
}
}
隊列類
<?php
namespace App\Jobs;
use App\Imports\toolOrder;
use App\Imports\toolOrderV1;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\DB;
class orderUpload implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $lot;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($lot)
{
$this->lot = $lot;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$item = DB::table('upload_order_log')->where('lot', $this->lot)->first();
$toolOrder = new toolOrderV1($this->lot,$item->origin_filepath);
\Excel::import($toolOrder, $item->origin_filepath);
}
}
Ipmorts 類
<?php
namespace App\Imports;
use App\Exports\exportsOrders;
use App\Exports\pddGoods;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\ToCollection;
use App\Models\IdCard;
use App\Models\Orders;
use function Matrix\trace;
class toolOrderV1 implements ToCollection
{
public function __construct($lot, $origin_filepath)
{
$this->lot = $lot;
$this->origin_filepath = $origin_filepath;
$this->checkIdentity = new IdCard();
}
public function sheets(): array
{
// 只處理第一個sheet
return [
0 => $this,
];
}
/**
* @param Collection $collection
*/
public function collection(Collection $rows)
{
set_time_limit(0);
// 去掉表頭
unset($rows[0]);
// DB::table('upload_order_log')
// ->where('lot', $this->lot)
// ->update(['order_num'=> count($rows)]);
$i =0;
foreach ($rows as $k => &$item){
$i++;
$this->success++;
$row = current($item);
if(trim($row[$this->th['order_sn']]) == ''){
continue;
}
$insert[] = array(
'order_sn' => trim($row[$this->th['order_sn']]),
'wangwang_id' => trim($row[$this->th['wangwang_id']]),
'username' => trim($row[$this->th['username']]),
'phone' => trim($row[$this->th['phone']]),
'id_number' => trim($row[$this->th['id_number']]),
'create_time' => date("y-m-d H:i:s"),
'update_time' => date("y-m-d H:i:s"),
'lot' => $this->lot,
'status' => 8,
'status_messgae'=> '',
'message' => '',
);
$key[] = $k;
if(is_int($i / 1000)){
DB::table('orders_temporary')->insert($insert);
$insert = [];
unset($key);
$key = [];
}
}
if(!empty($insert)){
DB::table('orders_temporary')->insert($insert);
}
DB::table('upload_order_log')
->where('lot', $this->lot)
->update(['status'=> 2, 'success_order_num'=> $this->success , 'finish_time' => time()]);
}
public function getExt($filename)
{
$arr = explode('.',$filename);
return array_pop($arr);
}
}