erp/app/Console/Commands/DeleteGoodsSku.php

68 lines
1.7 KiB
PHP
Raw Normal View History

2022-09-08 01:02:07 +08:00
<?php
namespace App\Console\Commands;
use App\Models\DailyStockRecord;
use App\Models\Goods;
use App\Models\GoodsSku;
use App\Models\Log;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class DeleteGoodsSku extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'delete:goods_sku {code : 完整的商品编码}';
/**
* The console command description.
*
* @var string
*/
protected $description = '删除商品规格';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$code = $this->argument('code');
if (empty($code)) {
reutrn;
}
DB::beginTransaction();
try {
2023-04-17 18:56:59 +08:00
$sku = GoodsSku::query()->where('external_sku_id', $code)->first();
$goods = Goods::query()->find($sku->goods_id);
2022-09-08 01:02:07 +08:00
$countSkus = GoodsSku::query()->where('goods_id', $goods->id)->count();
2022-09-16 11:13:51 +08:00
DailyStockRecord::query()->where('sku_id', $sku->id)->delete();
Log::query()->where('module', 'goods')->where('target_type', 'goods_sku')->where('target_id', $sku->id)->delete();
2022-09-08 01:02:07 +08:00
if (1 === $countSkus) {
$goods->delete();
}
2023-04-17 18:56:59 +08:00
$sku->delete();
2022-09-08 01:02:07 +08:00
DB::commit();
$this->info('删除成功');
} catch (\Exception $exception) {
DB::rollBack();
$this->info('删除失败' . $exception->getMessage());
}
}
}