master
parent
289976f207
commit
fe1f435a86
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Bed;
|
||||
use App\Models\Room;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CreateBed extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'create-bed {--room_id= : 每次处理的任务数量} {--total= : 床位数量} {--from=1 : 开始数字}';
|
||||
|
||||
/**
|
||||
* 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 int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$room_id = (int)$this->option('room_id');
|
||||
$total = (int) $this->option('total');
|
||||
$from = (int)$this->option('from');
|
||||
|
||||
$room = Room::find($room_id);
|
||||
if (!$room) {
|
||||
$this->error("病房不存在");
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->info("开始生成任务...");
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
for ($i = $from; $i <= $total; $i++) {
|
||||
$bed = new Bed();
|
||||
$bed->name = sprintf('%02d', $i);
|
||||
$bed->area_id = $room->area_id;
|
||||
$bed->project_id = $room->project_id;
|
||||
$bed->room_id = $room->id;
|
||||
$bed->myindex = $i;
|
||||
$bed->building_id = $room->building_id;
|
||||
$bed->save();
|
||||
}
|
||||
DB::commit();
|
||||
$this->info('Bed created successfully');
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
$this->error($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue