feat : rank

This commit is contained in:
2025-04-09 09:40:05 +08:00
parent edb295cbe2
commit 8c284937ac
6 changed files with 163 additions and 7 deletions

View File

@@ -0,0 +1,57 @@
<?php
namespace App\Cache\Redis\Api;
use App\Cache\Redis\RedisCache;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class EvaluationCache
{
/**
* @var RedisCache
*/
#[Inject]
protected RedisCache $redis;
/**
* @param int $chefId
* @param int $score
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function addChefEvaluation(int $chefId,int $score): void
{
$key = ApiRedisKey::chefEvaluationKey($chefId);
$this->redis->hIncrBy($key, 'total_score', $score);
$this->redis->hIncrBy($key, 'total_count', 1);
}
/**
* @param int $chefId
* @return float|int
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getChefEvaluation(int $chefId): float|int
{
$key = ApiRedisKey::chefEvaluationKey($chefId);
$totalScore = $this->redis->hGet($key, 'total_score');
$totalCount = $this->redis->hGet($key, 'total_count');
if (empty($totalScore) || empty($totalCount)) return 0;
$avgScore = $totalScore / $totalCount;
if ($avgScore < 4) {
$normalized = $avgScore / 4;
$avgScore = 4 + 1 - exp(-2 * $normalized);
}
return round($avgScore,1);
}
}