feat(notification): 實作通知輪詢與優化顯示名稱

- 新增通知輪詢 API 與前端自動更新機制
- 修正生產工單單號格式為 PRO-YYYYMMDD-XX
- 確保通知顯示實際建立者名稱而非系統
This commit is contained in:
2026-02-12 17:13:09 +08:00
parent 299602d3b1
commit 882091ce5f
14 changed files with 528 additions and 47 deletions

View File

@@ -112,13 +112,17 @@ class ProductionOrder extends Model
public static function generateCode()
{
$prefix = 'PO' . now()->format('Ymd');
$lastOrder = self::where('code', 'like', $prefix . '%')->latest()->first();
$prefix = 'PRO-' . now()->format('Ymd') . '-';
$lastOrder = self::where('code', 'like', $prefix . '%')
->lockForUpdate()
->orderBy('code', 'desc')
->first();
if ($lastOrder) {
$lastSequence = intval(substr($lastOrder->code, -3));
$sequence = str_pad($lastSequence + 1, 3, '0', STR_PAD_LEFT);
$lastSequence = intval(substr($lastOrder->code, -2));
$sequence = str_pad($lastSequence + 1, 2, '0', STR_PAD_LEFT);
} else {
$sequence = '001';
$sequence = '01';
}
return $prefix . $sequence;
}
@@ -127,4 +131,9 @@ class ProductionOrder extends Model
{
return $this->hasMany(ProductionOrderItem::class);
}
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(\App\Modules\Core\Models\User::class);
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Modules\Production\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\Modules\Production\Models\ProductionOrder;
class NewProductionOrder extends Notification
{
use Queueable;
protected $productionOrder;
protected $creatorName;
/**
* Create a new notification instance.
*/
public function __construct(ProductionOrder $productionOrder, string $creatorName)
{
$this->productionOrder = $productionOrder;
$this->creatorName = $creatorName;
}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['database'];
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'type' => 'production_order',
'action' => 'created',
'production_order_id' => $this->productionOrder->id,
'code' => $this->productionOrder->code,
'creator_name' => $this->creatorName,
'message' => "{$this->creatorName} 建立了新的生產工單:{$this->productionOrder->code}",
'link' => route('production-orders.index', ['search' => $this->productionOrder->code]),
];
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Modules\Production\Observers;
use App\Modules\Production\Models\ProductionOrder;
use App\Modules\Production\Notifications\NewProductionOrder;
use App\Modules\Core\Models\User;
use Illuminate\Support\Facades\Notification;
class ProductionOrderObserver
{
/**
* Handle the ProductionOrder "created" event.
*/
public function created(ProductionOrder $productionOrder): void
{
// 找出有檢視生產工單權限的使用者
$users = User::permission('production_orders.view')->get();
$creatorName = $productionOrder->user ? $productionOrder->user->name : '系統';
if ($users->isNotEmpty()) {
Notification::send($users, new NewProductionOrder($productionOrder, $creatorName));
}
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Modules\Production;
use Illuminate\Support\ServiceProvider;
use App\Modules\Production\Models\ProductionOrder;
use App\Modules\Production\Observers\ProductionOrderObserver;
class ProductionServiceProvider extends ServiceProvider
{
public function register(): void
{
//
}
public function boot(): void
{
ProductionOrder::observe(ProductionOrderObserver::class);
}
}