mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-11-30 22:20:45 +00:00
fix: #10000 分页修改,商品导入修改
This commit is contained in:
parent
3aa343aa21
commit
03f8f23c83
@ -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);
|
return GoodsBrandResource::collection($goodsBrands);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,7 +60,7 @@ class GoodsSkusController extends Controller
|
|||||||
$query->where('day', $day);
|
$query->where('day', $day);
|
||||||
}])
|
}])
|
||||||
->orderBy('updated_at', 'desc')
|
->orderBy('updated_at', 'desc')
|
||||||
->paginate();
|
->paginate($request->get('per_page'));
|
||||||
|
|
||||||
return GoodsSkuResource::collection($goodsSkus);
|
return GoodsSkuResource::collection($goodsSkus);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
return GoodsTypeResource::collection($goodsTypes);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,7 @@ class LogsController extends Controller
|
|||||||
->orderBy('id', 'desc')
|
->orderBy('id', 'desc')
|
||||||
->with(['user:id,name'])
|
->with(['user:id,name'])
|
||||||
->filter()
|
->filter()
|
||||||
->paginate();
|
->paginate($request->get('per_page'));
|
||||||
|
|
||||||
return LogsResource::collection($res);
|
return LogsResource::collection($res);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,9 +15,9 @@ use App\Models\BusinessOrderItem;
|
|||||||
|
|
||||||
class ShopsController extends Controller
|
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) {
|
foreach ($shops as $shop) {
|
||||||
$shop->authUrl = '';
|
$shop->authUrl = '';
|
||||||
if ('妙选' !== $shop->plat_id && ('未授权' === $shop->status || '重新授权' === $shop->status)) {
|
if ('妙选' !== $shop->plat_id && ('未授权' === $shop->status || '重新授权' === $shop->status)) {
|
||||||
|
|||||||
@ -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);
|
return UsersResource::collection($users);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,7 +27,7 @@ class GoodsRequest extends FormRequest
|
|||||||
return [
|
return [
|
||||||
'id' => ['sometimes', 'required', 'integer', 'exists:goods,id'],
|
'id' => ['sometimes', 'required', 'integer', 'exists:goods,id'],
|
||||||
'title' => ['required', 'string', 'max:191'],
|
'title' => ['required', 'string', 'max:191'],
|
||||||
'img_url' => ['required', 'string', 'max:191'],
|
'img_url' => ['string', 'max:191'],
|
||||||
'type_id' => ['required', 'integer', 'exists:goods_types,id'],
|
'type_id' => ['required', 'integer', 'exists:goods_types,id'],
|
||||||
'brand_id' => ['integer', 'exists:goods_brands,id'],
|
'brand_id' => ['integer', 'exists:goods_brands,id'],
|
||||||
'goods_code' => ['required', 'alpha_dash', 'max:32', Rule::unique('goods')->ignore(request('goods_id'))],
|
'goods_code' => ['required', 'alpha_dash', 'max:32', Rule::unique('goods')->ignore(request('goods_id'))],
|
||||||
|
|||||||
@ -8,6 +8,7 @@ use App\Models\GoodsBrand;
|
|||||||
use App\Models\GoodsSku;
|
use App\Models\GoodsSku;
|
||||||
use App\Models\GoodsType;
|
use App\Models\GoodsType;
|
||||||
use App\Utils\DateTimeUtils;
|
use App\Utils\DateTimeUtils;
|
||||||
|
use Exception;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
@ -27,12 +28,22 @@ class GoodsSkusImport implements ToCollection, SkipsEmptyRows
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ValidationException
|
* @throws ValidationException
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function collection(Collection $collection)
|
public function collection(Collection $rows)
|
||||||
{
|
{
|
||||||
unset($collection[0], $collection[1]);
|
unset($rows[0], $rows[1]);
|
||||||
$arr = $collection->toArray();
|
$rows = $rows->toArray();
|
||||||
$validator = Validator::make($arr, [
|
$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'],
|
'*.0' => ['required', 'string', 'max:191'],
|
||||||
'*.1' => ['required', 'string', 'max:191', 'exists:goods_types,name'],
|
'*.1' => ['required', 'string', 'max:191', 'exists:goods_types,name'],
|
||||||
'*.2' => ['string', 'max:191', 'exists:goods_brands,name'],
|
'*.2' => ['string', 'max:191', 'exists:goods_brands,name'],
|
||||||
@ -46,35 +57,32 @@ class GoodsSkusImport implements ToCollection, SkipsEmptyRows
|
|||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
throw new ValidationException($validator);
|
throw new ValidationException($validator);
|
||||||
}
|
}
|
||||||
$types = array_column($arr, 1);
|
|
||||||
$types = GoodsType::query()->whereIn('name', $types)->get(['id', 'name'])->toArray();
|
$types = GoodsType::query()->whereIn('name', $types)->get(['id', 'name'])->toArray();
|
||||||
$types = ArrayUtils::index($types, 'name');
|
$types = ArrayUtils::index($types, 'name');
|
||||||
$brands = array_column($arr, 2);
|
|
||||||
$brands = GoodsBrand::query()->whereIn('name', $brands)->get(['id', 'name'])->toArray();
|
$brands = GoodsBrand::query()->whereIn('name', $brands)->get(['id', 'name'])->toArray();
|
||||||
$brands = ArrayUtils::index($brands, 'name');
|
$brands = ArrayUtils::index($brands, 'name');
|
||||||
$goodsCodes = array_column($arr, 3);
|
|
||||||
$hasGoods = Goods::query()->whereIn('goods_code', $goodsCodes)->get(['id', 'goods_code'])->toArray();
|
$hasGoods = Goods::query()->whereIn('goods_code', $goodsCodes)->get(['id', 'goods_code'])->toArray();
|
||||||
$hasGoods = ArrayUtils::index($hasGoods, 'goods_code');
|
$hasGoods = ArrayUtils::index($hasGoods, 'goods_code');
|
||||||
$newGoods = $skus = [];
|
$newGoods = $skus = [];
|
||||||
foreach ($arr as $item) {
|
foreach ($rows as $row) {
|
||||||
$sku = [
|
$sku = [
|
||||||
'goods_id' => $item[3],
|
'goods_id' => $row[3],
|
||||||
'title' => $item[4],
|
'title' => $row[4],
|
||||||
'sku_code' => $item[5],
|
'sku_code' => $row[5],
|
||||||
'status' => $this->statusMap[$item[6]],
|
'status' => $this->statusMap[$row[6]],
|
||||||
'num' => $item[7],
|
'num' => $row[7],
|
||||||
'cost' => $item[8],
|
'cost' => $row[8],
|
||||||
];
|
];
|
||||||
// 主商品已存在
|
// 主商品已存在
|
||||||
if (isset($hasGoods[$item[3]])) {
|
if (isset($hasGoods[$row[3]])) {
|
||||||
$sku['goods_id'] = $hasGoods[$item[3]]['id'];
|
$sku['goods_id'] = $hasGoods[$row[3]]['id'];
|
||||||
} else {
|
} else {
|
||||||
// 新商品
|
// 新商品
|
||||||
$newGoods[$item[3]] = [
|
$newGoods[$row[3]] = [
|
||||||
'title' => $item[0],
|
'title' => $row[0],
|
||||||
'type_id' => $types[$item[1]]['id'],
|
'type_id' => $types[$row[1]]['id'],
|
||||||
'brand_id' => $brands[$item[2]]['id'],
|
'brand_id' => $brands[$row[2]]['id'],
|
||||||
'goods_code' => $item[3],
|
'goods_code' => $row[3],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
$skus[] = $sku;
|
$skus[] = $sku;
|
||||||
@ -93,19 +101,23 @@ class GoodsSkusImport implements ToCollection, SkipsEmptyRows
|
|||||||
}
|
}
|
||||||
$goodsSku = new GoodsSku();
|
$goodsSku = new GoodsSku();
|
||||||
$goodsSku->batchInsert(array_values($skus));
|
$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 = [];
|
$newRecords = [];
|
||||||
foreach ($collection as $sku) {
|
$day = DateTimeUtils::getToday();
|
||||||
|
foreach ($skuIds as $skuId) {
|
||||||
$newRecords[] = [
|
$newRecords[] = [
|
||||||
'sku_id' => $sku['id'],
|
'sku_id' => $skuId,
|
||||||
'day' => DateTimeUtils::getToday(),
|
'day' => $day,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
$record = new DailyStockRecord();
|
$record = new DailyStockRecord();
|
||||||
$record->batchInsert($newRecords);
|
$record->batchInsert($newRecords);
|
||||||
DB::commit();
|
DB::commit();
|
||||||
} catch (\Exception $exception) {
|
} catch (Exception $exception) {
|
||||||
DB::rollBack();
|
DB::rollBack();
|
||||||
|
// 返回错误
|
||||||
|
throw $exception;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,6 +30,6 @@ class Model extends EloquentModel
|
|||||||
$val['updated_at'] = $val['updated_at'] ?? $time;
|
$val['updated_at'] = $val['updated_at'] ?? $time;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Db::table($this->getTable())->insert($data);
|
return Db::table($this->getTable())->insertOrIgnore($data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user