fix: #10000 分页修改,商品导入修改

This commit is contained in:
赵世界 2022-08-19 22:52:04 +08:00
parent 3aa343aa21
commit 03f8f23c83
9 changed files with 50 additions and 38 deletions

View File

@ -21,9 +21,9 @@ class GoodsBrandsController extends Controller
]);
}
public function index()
public function index(Request $request)
{
$goodsBrands = GoodsBrand::query()->paginate();
$goodsBrands = GoodsBrand::query()->paginate($request->get('per_page'));
return GoodsBrandResource::collection($goodsBrands);
}

View File

@ -60,7 +60,7 @@ class GoodsSkusController extends Controller
$query->where('day', $day);
}])
->orderBy('updated_at', 'desc')
->paginate();
->paginate($request->get('per_page'));
return GoodsSkuResource::collection($goodsSkus);
}

View File

@ -21,9 +21,9 @@ class GoodsTypesController extends Controller
]);
}
public function index()
public function index(Request $request)
{
$goodsTypes = GoodsType::query()->paginate();
$goodsTypes = GoodsType::query()->paginate($request->get('per_page'));
return GoodsTypeResource::collection($goodsTypes);
}

View File

@ -15,7 +15,7 @@ class LogsController extends Controller
->orderBy('id', 'desc')
->with(['user:id,name'])
->filter()
->paginate();
->paginate($request->get('per_page'));
return LogsResource::collection($res);
}

View File

@ -15,9 +15,9 @@ use App\Models\BusinessOrderItem;
class ShopsController extends Controller
{
public function index()
public function index(Request $request)
{
$shops = Shop::query()->paginate();
$shops = Shop::query()->paginate($request->get('per_page'));
foreach ($shops as $shop) {
$shop->authUrl = '';
if ('妙选' !== $shop->plat_id && ('未授权' === $shop->status || '重新授权' === $shop->status)) {

View File

@ -22,9 +22,9 @@ class UsersController extends Controller
]);
}
public function index()
public function index(Request $request)
{
$users = User::query()->where('id', '<>', 1)->with('roles:id,name,guard_name')->paginate();
$users = User::query()->where('id', '<>', 1)->with('roles:id,name,guard_name')->paginate($request->get('per_page'));
return UsersResource::collection($users);
}

View File

@ -27,7 +27,7 @@ class GoodsRequest extends FormRequest
return [
'id' => ['sometimes', 'required', 'integer', 'exists:goods,id'],
'title' => ['required', 'string', 'max:191'],
'img_url' => ['required', 'string', 'max:191'],
'img_url' => ['string', 'max:191'],
'type_id' => ['required', 'integer', 'exists:goods_types,id'],
'brand_id' => ['integer', 'exists:goods_brands,id'],
'goods_code' => ['required', 'alpha_dash', 'max:32', Rule::unique('goods')->ignore(request('goods_id'))],

View File

@ -8,6 +8,7 @@ use App\Models\GoodsBrand;
use App\Models\GoodsSku;
use App\Models\GoodsType;
use App\Utils\DateTimeUtils;
use Exception;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
@ -27,12 +28,22 @@ class GoodsSkusImport implements ToCollection, SkipsEmptyRows
/**
* @throws ValidationException
* @throws Exception
*/
public function collection(Collection $collection)
public function collection(Collection $rows)
{
unset($collection[0], $collection[1]);
$arr = $collection->toArray();
$validator = Validator::make($arr, [
unset($rows[0], $rows[1]);
$rows = $rows->toArray();
$types = $brands = $goodsCodes = [];
foreach ($rows as $row) {
$row = array_map(static function ($v) {
return trim($v);
}, $row);
$types[] = $row[1];
$brands[] = $row[2];
$goodsCodes[] = $row[3];
}
$validator = Validator::make($rows, [
'*.0' => ['required', 'string', 'max:191'],
'*.1' => ['required', 'string', 'max:191', 'exists:goods_types,name'],
'*.2' => ['string', 'max:191', 'exists:goods_brands,name'],
@ -46,35 +57,32 @@ class GoodsSkusImport implements ToCollection, SkipsEmptyRows
if ($validator->fails()) {
throw new ValidationException($validator);
}
$types = array_column($arr, 1);
$types = GoodsType::query()->whereIn('name', $types)->get(['id', 'name'])->toArray();
$types = ArrayUtils::index($types, 'name');
$brands = array_column($arr, 2);
$brands = GoodsBrand::query()->whereIn('name', $brands)->get(['id', 'name'])->toArray();
$brands = ArrayUtils::index($brands, 'name');
$goodsCodes = array_column($arr, 3);
$hasGoods = Goods::query()->whereIn('goods_code', $goodsCodes)->get(['id', 'goods_code'])->toArray();
$hasGoods = ArrayUtils::index($hasGoods, 'goods_code');
$newGoods = $skus = [];
foreach ($arr as $item) {
foreach ($rows as $row) {
$sku = [
'goods_id' => $item[3],
'title' => $item[4],
'sku_code' => $item[5],
'status' => $this->statusMap[$item[6]],
'num' => $item[7],
'cost' => $item[8],
'goods_id' => $row[3],
'title' => $row[4],
'sku_code' => $row[5],
'status' => $this->statusMap[$row[6]],
'num' => $row[7],
'cost' => $row[8],
];
// 主商品已存在
if (isset($hasGoods[$item[3]])) {
$sku['goods_id'] = $hasGoods[$item[3]]['id'];
if (isset($hasGoods[$row[3]])) {
$sku['goods_id'] = $hasGoods[$row[3]]['id'];
} else {
// 新商品
$newGoods[$item[3]] = [
'title' => $item[0],
'type_id' => $types[$item[1]]['id'],
'brand_id' => $brands[$item[2]]['id'],
'goods_code' => $item[3],
$newGoods[$row[3]] = [
'title' => $row[0],
'type_id' => $types[$row[1]]['id'],
'brand_id' => $brands[$row[2]]['id'],
'goods_code' => $row[3],
];
}
$skus[] = $sku;
@ -93,19 +101,23 @@ class GoodsSkusImport implements ToCollection, SkipsEmptyRows
}
$goodsSku = new GoodsSku();
$goodsSku->batchInsert(array_values($skus));
$collection = GoodsSku::query()->whereIn('sku_code', array_column($skus, 'sku_code'))->get(['id', 'sku_code'])->toArray();
$goodsIds = Goods::query()->whereIn('goods_code', $goodsCodes)->pluck('id')->toArray();
$skuIds = GoodsSku::query()->whereIn('goods_id', $goodsIds)->pluck('id')->toArray();
$newRecords = [];
foreach ($collection as $sku) {
$day = DateTimeUtils::getToday();
foreach ($skuIds as $skuId) {
$newRecords[] = [
'sku_id' => $sku['id'],
'day' => DateTimeUtils::getToday(),
'sku_id' => $skuId,
'day' => $day,
];
}
$record = new DailyStockRecord();
$record->batchInsert($newRecords);
DB::commit();
} catch (\Exception $exception) {
} catch (Exception $exception) {
DB::rollBack();
// 返回错误
throw $exception;
}
}
}

View File

@ -30,6 +30,6 @@ class Model extends EloquentModel
$val['updated_at'] = $val['updated_at'] ?? $time;
}
return Db::table($this->getTable())->insert($data);
return Db::table($this->getTable())->insertOrIgnore($data);
}
}