-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUuid.php
31 lines (23 loc) · 939 Bytes
/
Uuid.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
namespace ZhenMu\Support\Utils;
use Illuminate\Support\Facades\DB;
class Uuid
{
public static function uuid($hex = false)
{
if ($hex) {
return \Ramsey\Uuid\Uuid::uuid4()->getHex()->toString(); // like: 6b2092378b014528b30b4a9b5fab3ba7
}
return \Ramsey\Uuid\Uuid::uuid4()->toString(); // mysql uuid, like: 6b209237-8b01-4528-b30b-4a9b5fab3ba7
}
public static function getCurrentSerialNumber(string $modelClass, $serialNumberField = 'serial_number'): int
{
return $modelClass::whereDate('created_at', now())->max(DB::raw("cast(`{$serialNumberField}` as UNSIGNED INTEGER)")) ?? 0;
}
public static function generateNextSerialNumberNo(int $serialNumber, int $padLength = 3): string
{
$nextSerialNumber = $serialNumber + 1;
$no = str_pad($nextSerialNumber, $padLength, '0', STR_PAD_LEFT);
return date('ymd') . $no;
}
}