From 03f8f23c836d2126862a3b6fd683a1a8ce506da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=B8=96=E7=95=8C?= <642747453@qq.com> Date: Fri, 19 Aug 2022 22:52:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20#10000=20=E5=88=86=E9=A1=B5=E4=BF=AE?= =?UTF-8?q?=E6=94=B9,=E5=95=86=E5=93=81=E5=AF=BC=E5=85=A5=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Goods/GoodsBrandsController.php | 4 +- .../Controllers/Goods/GoodsSkusController.php | 2 +- .../Goods/GoodsTypesController.php | 4 +- app/Http/Controllers/Log/LogsController.php | 2 +- app/Http/Controllers/Shop/ShopsController.php | 4 +- app/Http/Controllers/User/UsersController.php | 4 +- app/Http/Requests/GoodsRequest.php | 2 +- app/Imports/GoodsSkusImport.php | 64 +++++++++++-------- app/Models/Model.php | 2 +- 9 files changed, 50 insertions(+), 38 deletions(-) diff --git a/app/Http/Controllers/Goods/GoodsBrandsController.php b/app/Http/Controllers/Goods/GoodsBrandsController.php index bfe167b..7f100c8 100644 --- a/app/Http/Controllers/Goods/GoodsBrandsController.php +++ b/app/Http/Controllers/Goods/GoodsBrandsController.php @@ -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); } diff --git a/app/Http/Controllers/Goods/GoodsSkusController.php b/app/Http/Controllers/Goods/GoodsSkusController.php index cb0fecb..f1923f5 100644 --- a/app/Http/Controllers/Goods/GoodsSkusController.php +++ b/app/Http/Controllers/Goods/GoodsSkusController.php @@ -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); } diff --git a/app/Http/Controllers/Goods/GoodsTypesController.php b/app/Http/Controllers/Goods/GoodsTypesController.php index cb7afa7..a85a110 100644 --- a/app/Http/Controllers/Goods/GoodsTypesController.php +++ b/app/Http/Controllers/Goods/GoodsTypesController.php @@ -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); } diff --git a/app/Http/Controllers/Log/LogsController.php b/app/Http/Controllers/Log/LogsController.php index cd74e76..3988724 100644 --- a/app/Http/Controllers/Log/LogsController.php +++ b/app/Http/Controllers/Log/LogsController.php @@ -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); } diff --git a/app/Http/Controllers/Shop/ShopsController.php b/app/Http/Controllers/Shop/ShopsController.php index ff62287..281cc2f 100644 --- a/app/Http/Controllers/Shop/ShopsController.php +++ b/app/Http/Controllers/Shop/ShopsController.php @@ -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)) { diff --git a/app/Http/Controllers/User/UsersController.php b/app/Http/Controllers/User/UsersController.php index 32c8546..259c9ea 100644 --- a/app/Http/Controllers/User/UsersController.php +++ b/app/Http/Controllers/User/UsersController.php @@ -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); } diff --git a/app/Http/Requests/GoodsRequest.php b/app/Http/Requests/GoodsRequest.php index b807bef..b070f49 100644 --- a/app/Http/Requests/GoodsRequest.php +++ b/app/Http/Requests/GoodsRequest.php @@ -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'))], diff --git a/app/Imports/GoodsSkusImport.php b/app/Imports/GoodsSkusImport.php index f7aa3b5..e5a7a9e 100644 --- a/app/Imports/GoodsSkusImport.php +++ b/app/Imports/GoodsSkusImport.php @@ -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; } } } diff --git a/app/Models/Model.php b/app/Models/Model.php index fd85d4e..8635ba0 100644 --- a/app/Models/Model.php +++ b/app/Models/Model.php @@ -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); } }