我有一个停车系统,我使用 Angular 6 + Laravel 作为后端,但我有一个具体问题,我不知道正确的方法。
公园有两个图腾,它们向我的服务器发送一个条目。只有当客户从出口走出图腾并进入付款区时,我才会检查客户是否无效。
这是我的代码,当他把有 EAN_13 条形码读取的票时。这是我的退出图腾准备好的代码:
public function getEntrysDataByEan(Request $request)
{
if (isset($request)) {
$entryean = $request->input('entryean13');
$entry = $this->mdMovEntry->getEntrysDataByEan($entryean);
if (empty($entry)) {
$response["success"] = 0;
$response["message"] = "Não existe nenhuma entrada correspondente";
} else {
$nowHour = Carbon::now();
$enterHour = Carbon::parse($entry[0]->updated_at);
$difmin = $enterHour->diffInMinutes($nowHour);
$dif = $enterHour->diffInHours($nowHour);
if ($difmin <= 20) {
$this->mdMovEntry->validatedEntryByEan($entryean, Carbon::parse($entry[0]->updated_at), $nowHour);
$entry[0]->validated = 'S';
} else {
$this->mdMovEntry->devaluedEntryByEan($entryean, Carbon::parse($entry[0]->updated_at));
$entry[0]->validated = 'N';
}
$response["success"] = 1;
$response["message"] = "Entrada retornada com sucesso";
$response["entry"] = $entry;
}
} else {
$response["success"] = 0;
$response["message"] = "Nenhum dado enviado";
}
return $response;
}
问题是,我认为仅读取客户端是否可以出去需要太多处理,因此我从 Laravel 中搜索任务调度方法和作业方法,但没有找到任何适合我的问题的信息。
我想要的是,当输入区域中的图腾在我的数据库中进行插入时,我创建一些作业或任务,20 分钟后,他将每条新记录置为无效。
当客户端出去时,系统只是在数据库上执行 getEntry 并检查他是否经过验证。
我怎样才能做到这一点?我问这个是因为任务的每个示例都是全局执行,但我只想为每个新记录分配一个任务,这可能吗?
考虑使用模型事件 https://laravel.com/docs/5.7/eloquent#events和这个结合延迟发货 https://laravel.com/docs/5.7/queues#delayed-dispatching对于排队的作业。
下面就来听听created
当新条目插入数据库并调度延迟 20 分钟的作业以使条目稍后失效时发生的事件。
使条目作业无效
class InvalidateEntry implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $entry;
/**
* Create a new job instance.
*
* @param Entry $entry
* @return void
*/
public function __construct(Entry $entry)
{
$this->entry = $entry;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// you'll probably need to add some conditional logic
// before updating the entry to determine if the
// action is still required.
$isEntryInvalid = ...;
if ($isEntryInvalid) {
$this->entry->update([
'validated' => 'N'
]);
} else {
// delete the job from the queue
$this->delete();
}
}
}
入门型号
class Entry extends Model
{
public static function boot()
{
parent::boot();
static::created(function (Entry $entry) {
InvalidateEntry::dispatch($entry)->delay(now()->addMinutes(20));
});
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)