2023-04-03 20:25:57 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Imports;
|
|
|
|
|
|
2023-04-21 17:46:59 +08:00
|
|
|
use App\Models\GoodsSku;
|
2023-04-03 20:25:57 +08:00
|
|
|
use App\Models\GoodsSkuLocation;
|
|
|
|
|
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
|
|
|
|
|
use Maatwebsite\Excel\Concerns\ToArray;
|
|
|
|
|
use Maatwebsite\Excel\Concerns\WithStartRow;
|
|
|
|
|
|
|
|
|
|
class GoodsSkuLocationImport implements ToArray, SkipsEmptyRows, WithStartRow
|
|
|
|
|
{
|
|
|
|
|
public function startRow(): int
|
|
|
|
|
{
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function array(array $array)
|
|
|
|
|
{
|
2023-04-21 17:46:59 +08:00
|
|
|
$externalSkuIds = $deleteLocation = [];
|
2023-04-03 20:25:57 +08:00
|
|
|
foreach ($array as &$row) {
|
|
|
|
|
$row = array_map(function ($value) {
|
|
|
|
|
return trim($value);
|
|
|
|
|
}, $row);
|
2023-04-21 17:46:59 +08:00
|
|
|
$externalSkuIds[] = $row[2];
|
|
|
|
|
$deleteLocation[] = $row[3];
|
2023-04-03 20:25:57 +08:00
|
|
|
}
|
|
|
|
|
GoodsSkuLocation::query()->whereIn('location', $deleteLocation)->delete();
|
|
|
|
|
unset($row);
|
2023-04-21 17:46:59 +08:00
|
|
|
$skus = GoodsSku::query()
|
|
|
|
|
->with('goods:id,title')
|
|
|
|
|
->whereIn('external_sku_id', $externalSkuIds)
|
|
|
|
|
->get(['id', 'goods_id', 'title', 'external_sku_id']);
|
2023-04-03 20:25:57 +08:00
|
|
|
$goodsSkus = [];
|
2023-04-21 17:46:59 +08:00
|
|
|
foreach ($skus as $sku) {
|
|
|
|
|
$goodsSkus[$sku['external_sku_id']] = [
|
|
|
|
|
'goods_id' => $sku['goods_id'],
|
|
|
|
|
'goods_sku_id' => $sku['id'],
|
|
|
|
|
'external_sku_id' => $sku['external_sku_id'],
|
|
|
|
|
'goods_name' => $sku['goods']['title'] . $sku['title'],
|
|
|
|
|
];
|
2023-04-03 20:25:57 +08:00
|
|
|
}
|
|
|
|
|
$data = [];
|
|
|
|
|
foreach ($array as $row) {
|
2023-04-21 17:46:59 +08:00
|
|
|
if (isset($goodsSkus[$row[2]])) {
|
|
|
|
|
$data[] = array_merge($goodsSkus[$row[2]], [
|
2023-04-03 20:25:57 +08:00
|
|
|
'date' => date('Y-m-d'),
|
|
|
|
|
'today_init_num' => $row[1],
|
2023-04-21 17:46:59 +08:00
|
|
|
'location' => $row[3],
|
2023-04-03 20:25:57 +08:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$model = new GoodsSkuLocation();
|
|
|
|
|
$model->batchInsert($data);
|
|
|
|
|
}
|
|
|
|
|
}
|