diff --git a/src/db/Builder.php b/src/db/Builder.php index c5c0c885..a471cf05 100644 --- a/src/db/Builder.php +++ b/src/db/Builder.php @@ -85,7 +85,15 @@ protected function parseData(Query $query, array $data = [], array $fields = [], throw new Exception('fields not exists:[' . $key . ']'); } } elseif ($val instanceof Express) { - $result[$item] = $item . $this->parseExpress($query, $val); + if ($val->getLazyTime() && in_array($val->getType(), ['+', '-'])) { + $step = $query->lazyWrite($key, $val->getType() == '+' ? 'inc' : 'dec', $guid, $val->getStep(), $val->getLazyTime()); + if (false === $step) { + continue; + } + $result[$item] = $item . ' + ' . $step; + } else { + $result[$item] = $item . $this->parseExpress($query, $val); + } } elseif (is_array($val) && !empty($val) && is_string($val[0])) { if (in_array(strtoupper($val[0]), ['INC', 'DEC'])) { $result[$item] = match (strtoupper($val[0])) { diff --git a/src/db/Express.php b/src/db/Express.php index b9ff29a6..2c8f75d3 100644 --- a/src/db/Express.php +++ b/src/db/Express.php @@ -23,10 +23,11 @@ class Express * * @param string $type * @param float $value + * @param int $lazyTime * * @return void */ - public function __construct(protected string $type, protected float $step) + public function __construct(protected string $type, protected float $step = 1, protected int $lazyTime = 0) { } @@ -40,6 +41,11 @@ public function getType() return $this->type; } + public function getLazyTime() + { + return $this->lazyTime; + } + /** * 获取表达式. * diff --git a/src/db/Query.php b/src/db/Query.php index 413cff1f..3d9dce60 100644 --- a/src/db/Query.php +++ b/src/db/Query.php @@ -370,8 +370,7 @@ public function getAutoInc() public function inc(string $field, float $step = 1, int $lazyTime = 0) { if ($lazyTime > 0) { - $guid = $this->getLazyFieldCacheKey($field); - $step = $this->lazyWrite('inc', $guid, $step, $lazyTime); + $step = $this->lazyWrite($field, 'inc', $guid, $step, $lazyTime); if (false === $step) { return $this; } @@ -394,8 +393,7 @@ public function inc(string $field, float $step = 1, int $lazyTime = 0) public function dec(string $field, float $step = 1, int $lazyTime = 0) { if ($lazyTime > 0) { - $guid = $this->getLazyFieldCacheKey($field); - $step = $this->lazyWrite('dec', $guid, $step, $lazyTime); + $step = $this->lazyWrite($field, 'dec', $guid, $step, $lazyTime); if (false === $step) { return $this; } @@ -456,15 +454,17 @@ public function setDec(string $field, float $step = 1, int $lazyTime = 0) /** * 延时更新检查 返回false表示需要延时 * 否则返回实际写入的数值 - * @access protected + * @access public + * @param string $field 字段名 * @param string $type 自增或者自减 * @param string $guid 写入标识 * @param float $step 写入步进值 * @param int $lazyTime 延时时间(s) * @return false|integer */ - protected function lazyWrite(string $type, string $guid, float $step, int $lazyTime) + public function lazyWrite(string $field, string $type, string $guid, float $step, int $lazyTime) { + $guid = $this->getLazyFieldCacheKey($field); $cache = $this->getCache(); if (!$cache->has($guid . '_time')) { // 计时开始 diff --git a/src/helper.php b/src/helper.php index f59b35c5..7e2e77c7 100644 --- a/src/helper.php +++ b/src/helper.php @@ -38,15 +38,15 @@ function raw(string $value, array $bind = []): Raw } if (!function_exists('inc')) { - function inc(float $step = 1): Express + function inc(float $step = 1, int $lazyTime = 0): Express { - return new Express('+', $step); + return new Express('+', $step, $lazyTime); } } if (!function_exists('dec')) { - function dec(float $step = 1): Express + function dec(float $step = 1, int $lazyTime = 0): Express { - return new Express('-', $step); + return new Express('-', $step, $lazyTime); } }