Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/mine-core/src/Aspect/SaveAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,12 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
} catch (\Throwable $e) {
}
}
// 生成ID
if ($instance instanceof MineModel
&& ! $instance->incrementing
&& $instance->getPrimaryKeyType() === 'int'
&& empty($instance->{$instance->getKeyName()})
// 生成雪花ID 或者 UUID
if ($instance instanceof MineModel &&
! $instance->incrementing &&
empty($instance->{$instance->getKeyName()})
) {
$instance->setPrimaryKeyValue(snowflake_id());
$instance->setPrimaryKeyValue($instance->getPrimaryKeyType() === 'int' ? snowflake_id() : uuid());
}
return $proceedingJoinPoint->process();
}
Expand Down
76 changes: 76 additions & 0 deletions src/mine-core/src/Snowflake/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact root@imoi.cn
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/

namespace Mine\Snowflake;

use Hyperf\Snowflake\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
protected int $millisecondBits = 41;

protected int $dataCenterIdBits = 2;

protected int $workerIdBits = 4;

protected int $sequenceBits = 5;

public function maxWorkerId(): int
{
return -1 ^ (-1 << $this->workerIdBits);
}

public function maxDataCenterId(): int
{
return -1 ^ (-1 << $this->dataCenterIdBits);
}

public function maxSequence(): int
{
return -1 ^ (-1 << $this->sequenceBits);
}

public function getTimestampLeftShift(): int
{
return $this->sequenceBits + $this->workerIdBits + $this->dataCenterIdBits;
}

public function getDataCenterIdShift(): int
{
return $this->sequenceBits + $this->workerIdBits;
}

public function getWorkerIdShift(): int
{
return $this->sequenceBits;
}

public function getTimestampBits(): int
{
return $this->millisecondBits;
}

public function getDataCenterIdBits(): int
{
return $this->dataCenterIdBits;
}

public function getWorkerIdBits(): int
{
return $this->workerIdBits;
}

public function getSequenceBits(): int
{
return $this->sequenceBits;
}
}
91 changes: 91 additions & 0 deletions src/mine-core/src/Snowflake/SnowflakeIdGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact root@imoi.cn
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/
/**
* __
* ____ __ __ ____/ /
* /_ / / / / / / __ /
* / /_ / /_/ / / /_/ /
* /___/ \__, / \__,_/
* /____/ 众熠达
*/
namespace Mine\Snowflake;

use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Snowflake\ConfigurationInterface;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Snowflake\Meta;
use Hyperf\Snowflake\MetaGenerator\RedisMilliSecondMetaGenerator;
use Hyperf\Snowflake\MetaGeneratorInterface;

use function Hyperf\Support\make;

class SnowflakeIdGenerator implements IdGeneratorInterface
{
protected ConfigurationInterface $config;

protected MetaGeneratorInterface $metaGenerator;

public function __construct()
{
$configuration = new Configuration();
$config = ApplicationContext::getContainer()->get(ConfigInterface::class);
$beginSecond = $config->get('snowflake.begin_second', MetaGeneratorInterface::DEFAULT_BEGIN_SECOND);
$this->metaGenerator = make(RedisMilliSecondMetaGenerator::class, [
$configuration,
$beginSecond,
$config,
]);

$this->config = $this->metaGenerator->getConfiguration();
}

public function generate(?Meta $meta = null): int
{
$meta = $this->meta($meta);

$interval = $meta->getTimeInterval() << $this->config->getTimestampLeftShift();
$dataCenterId = $meta->getDataCenterId() << $this->config->getDataCenterIdShift();
$workerId = $meta->getWorkerId() << $this->config->getWorkerIdShift();

return $interval | $dataCenterId | $workerId | $meta->getSequence();
}

public function degenerate(int $id): Meta
{
$interval = $id >> $this->config->getTimestampLeftShift();
$dataCenterId = $id >> $this->config->getDataCenterIdShift();
$workerId = $id >> $this->config->getWorkerIdShift();

return new Meta(
$interval << $this->config->getDataCenterIdBits() ^ $dataCenterId,
$dataCenterId << $this->config->getWorkerIdBits() ^ $workerId,
$workerId << $this->config->getSequenceBits() ^ $id,
$interval + $this->metaGenerator->getBeginTimestamp(),
$this->metaGenerator->getBeginTimestamp()
);
}

public function getMetaGenerator(): MetaGeneratorInterface
{
return $this->metaGenerator;
}

protected function meta(?Meta $meta = null): Meta
{
if (is_null($meta)) {
return $this->metaGenerator->generate();
}

return $meta;
}
}
12 changes: 6 additions & 6 deletions src/mine-core/src/Traits/MapperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function filterExecuteAttributes(array &$data, bool $removePk = false): v
/**
* 新增数据.
*/
public function save(array $data): int
public function save(array $data): mixed
{
$this->filterExecuteAttributes($data, $this->getModel()->incrementing);
$model = $this->model::create($data);
Expand All @@ -260,7 +260,7 @@ public function save(array $data): int
/**
* 读取一条数据.
*/
public function read(int $id, array $column = ['*']): ?MineModel
public function read(mixed $id, array $column = ['*']): ?MineModel
{
return ($model = $this->model::find($id, $column)) ? $model : null;
}
Expand Down Expand Up @@ -295,7 +295,7 @@ public function pluck(array $condition, string $columns = 'id', ?string $key = n
* 从回收站读取一条数据.
* @noinspection PhpUnused
*/
public function readByRecycle(int $id): ?MineModel
public function readByRecycle(mixed $id): ?MineModel
{
return ($model = $this->model::withTrashed()->find($id)) ? $model : null;
}
Expand All @@ -318,7 +318,7 @@ public function delete(array $ids): bool
/**
* 更新一条数据.
*/
public function update(int $id, array $data): bool
public function update(mixed $id, array $data): bool
{
$this->filterExecuteAttributes($data, true);
return $this->model::find($id)->update($data) > 0;
Expand Down Expand Up @@ -450,7 +450,7 @@ public function min(?\Closure $closure = null, string $column = '*')
/**
* 数字更新操作.
*/
public function numberOperation(int $id, string $field, int $value): bool
public function numberOperation(mixed $id, string $field, int $value): bool
{
return $this->update($id, [$field => $value]);
}
Expand Down Expand Up @@ -588,7 +588,7 @@ public function getQuery(): mixed
* 动态关联模型.
* @param $config ['name', 'model', 'type', 'localKey', 'foreignKey', 'middleTable', 'as', 'where', 'whereIn' ]
*/
public function dynamicRelations(MineModel &$model, &$config): void
public function dynamicRelations(MineModel $model, &$config): void
{
$model->resolveRelationUsing($config['name'], function ($primaryModel) use ($config) {
$namespace = str_replace('.', '\\', $config['model']);
Expand Down
12 changes: 6 additions & 6 deletions src/mine-core/src/Traits/ServiceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function getTreeListByRecycle(?array $params = null, bool $isScope = true
/**
* 新增数据.
*/
public function save(array $data): int
public function save(array $data): mixed
{
return $this->mapper->save($data);
}
Expand All @@ -126,7 +126,7 @@ public function batchSave(array $collects): bool
/**
* 读取一条数据.
*/
public function read(int $id, array $column = ['*']): ?MineModel
public function read(mixed $id, array $column = ['*']): ?MineModel
{
return $this->mapper->read($id, $column);
}
Expand Down Expand Up @@ -155,7 +155,7 @@ public function pluck(array $condition, string $columns = 'id'): array
* 从回收站读取一条数据.
* @noinspection PhpUnused
*/
public function readByRecycle(int $id): MineModel
public function readByRecycle(mixed $id): MineModel
{
return $this->mapper->readByRecycle($id);
}
Expand All @@ -171,7 +171,7 @@ public function delete(array $ids): bool
/**
* 更新一条数据.
*/
public function update(int $id, array $data): bool
public function update(mixed $id, array $data): bool
{
return $this->mapper->update($id, $data);
}
Expand Down Expand Up @@ -219,15 +219,15 @@ public function enable(array $ids, string $field = 'status'): bool
/**
* 修改数据状态
*/
public function changeStatus(int $id, string $value, string $filed = 'status'): bool
public function changeStatus(mixed $id, string $value, string $filed = 'status'): bool
{
return $value == MineModel::ENABLE ? $this->mapper->enable([$id], $filed) : $this->mapper->disable([$id], $filed);
}

/**
* 数字更新操作.
*/
public function numberOperation(int $id, string $field, int $value): bool
public function numberOperation(mixed $id, string $field, int $value): bool
{
return $this->mapper->numberOperation($id, $field, $value);
}
Expand Down
10 changes: 5 additions & 5 deletions src/mine-generator/src/Traits/MapperGeneratorTraits.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function getSearchPHPString($name, $mark, $comment): string
return <<<php

// {$comment}
if (blank(\$params['{$name}'])) {
if (filled(\$params['{$name}'])) {
\$query->where('{$name}', 'like', '%'.\$params['{$name}'].'%');
}

Expand All @@ -60,7 +60,7 @@ protected function getSearchPHPString($name, $mark, $comment): string
return <<<php

// {$comment}
if (blank(\$params['{$name}']) && is_array(\$params['{$name}']) && count(\$params['{$name}']) == 2) {
if (filled(\$params['{$name}']) && is_array(\$params['{$name}']) && count(\$params['{$name}']) == 2) {
\$query->whereBetween(
'{$name}',
[ \$params['{$name}'][0], \$params['{$name}'][1] ]
Expand All @@ -74,7 +74,7 @@ protected function getSearchPHPString($name, $mark, $comment): string
return <<<php

// {$comment}
if (blank(\$params['{$name}'])) {
if (filled(\$params['{$name}'])) {
\$query->whereIn('{$name}', \$params['{$name}']);
}

Expand All @@ -85,7 +85,7 @@ protected function getSearchPHPString($name, $mark, $comment): string
return <<<php

// {$comment}
if (blank(\$params['{$name}'])) {
if (filled(\$params['{$name}'])) {
\$query->whereNotIn('{$name}', \$params['{$name}']);
}

Expand All @@ -95,7 +95,7 @@ protected function getSearchPHPString($name, $mark, $comment): string
return <<<php

// {$comment}
if (blank(\$params['{$name}'])) {
if (filled(\$params['{$name}'])) {
\$query->where('{$name}', '{$mark}', \$params['{$name}']);
}

Expand Down
16 changes: 14 additions & 2 deletions src/mine-helpers/src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,23 @@ function app_verify(string $scene = 'api'): AppVerify

if (! function_exists('snowflake_id')) {
/**
* 生成雪花ID.
* 生成雪花ID
* @return String
*/
function snowflake_id(): string
{
return (string) container()->get(IdGeneratorInterface::class)->generate();
return container()->get(\Mine\Snowflake\SnowflakeIdGenerator::class)->generate();
}
}

if (! function_exists('uuid')) {
/**
* 生成UUID
* @return String
*/
function uuid(): string
{
return \Ramsey\Uuid\Uuid::uuid4()->toString();
}
}

Expand Down