erp/app/Console/Commands/DeleteGoodsSku.php

69 lines
1.8 KiB
PHP

<?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;
}
[$goodsCode, $skuCode] = explode('_', $code);
DB::beginTransaction();
try {
$goods = Goods::query()->where('goods_code', $goodsCode)->first();
$countSkus = GoodsSku::query()->where('goods_id', $goods->id)->count();
$sku = GoodsSku::query()->where('goods_id', $goods->id)->where('sku_code', $skuCode)->first();
DailyStockRecord::where('sku_id', $sku->id)->delete();
Log::where('module', 'goods')->where('target_type', 'goods_sku')->where('target_id', $sku->id)->delete();
$sku->delete();
if (1 === $countSkus) {
$goods->delete();
}
DB::commit();
$this->info('删除成功');
} catch (\Exception $exception) {
DB::rollBack();
$this->info('删除失败' . $exception->getMessage());
}
}
}