feat : rank
This commit is contained in:
@@ -84,4 +84,13 @@ class ApiRedisKey
|
|||||||
{
|
{
|
||||||
return 'chef:leaderboard:city_id:'.$cityId.':type:'.$type.':time_key:'.$timeKey;
|
return 'chef:leaderboard:city_id:'.$cityId.':type:'.$type.':time_key:'.$timeKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $chefId
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function chefEvaluationKey(int $chefId): string
|
||||||
|
{
|
||||||
|
return 'evaluation:chef:id:'.$chefId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
57
app/Cache/Redis/Api/EvaluationCache.php
Normal file
57
app/Cache/Redis/Api/EvaluationCache.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -409,16 +409,15 @@ class RedisCache
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 单个字段的 hash 值原子增加
|
* 单个字段的 hash 值原子增加
|
||||||
* @param $key
|
* @param string $key
|
||||||
* @param $hashKey
|
* @param string $hashKey
|
||||||
* @param $hashValue
|
* @param int $hashValue
|
||||||
* @param string $poolName
|
* @param string $poolName
|
||||||
* @return false|int|Redis
|
* @return false|int|Redis
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFoundExceptionInterface
|
* @throws NotFoundExceptionInterface
|
||||||
* @throws RedisException
|
|
||||||
*/
|
*/
|
||||||
public function HIncrBy($key, $hashKey, $hashValue, string $poolName = RedisCode::DEFAULT_DB)
|
public function hIncrBy(string $key, string $hashKey, int $hashValue, string $poolName = RedisCode::DEFAULT_DB): false|int|Redis
|
||||||
{
|
{
|
||||||
return $this->getRedis($poolName)->hincrby($key, $hashKey, $hashValue);
|
return $this->getRedis($poolName)->hincrby($key, $hashKey, $hashValue);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,12 +10,19 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Service\Api\Chef;
|
namespace App\Service\Api\Chef;
|
||||||
|
|
||||||
|
use App\Cache\Redis\Api\EvaluationCache;
|
||||||
use App\Exception\ErrException;
|
use App\Exception\ErrException;
|
||||||
use App\Model\AdminUser;
|
use App\Model\AdminUser;
|
||||||
use App\Model\Chef;
|
use App\Model\Chef;
|
||||||
|
use App\Model\Evaluation;
|
||||||
|
use App\Model\Order;
|
||||||
|
use App\Model\Sku;
|
||||||
|
use App\Model\User;
|
||||||
use App\Service\Api\BaseService;
|
use App\Service\Api\BaseService;
|
||||||
use App\Service\ServiceTrait\Common\OssTrait;
|
use App\Service\ServiceTrait\Common\OssTrait;
|
||||||
use Hyperf\Di\Annotation\Inject;
|
use Hyperf\Di\Annotation\Inject;
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
|
|
||||||
class IndexService extends BaseService
|
class IndexService extends BaseService
|
||||||
{
|
{
|
||||||
@@ -33,8 +40,15 @@ class IndexService extends BaseService
|
|||||||
#[Inject]
|
#[Inject]
|
||||||
protected Chef $chefModel;
|
protected Chef $chefModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var EvaluationCache
|
||||||
|
*/
|
||||||
|
protected EvaluationCache $evaluationCache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
public function handle(): array
|
public function handle(): array
|
||||||
{
|
{
|
||||||
@@ -58,9 +72,77 @@ class IndexService extends BaseService
|
|||||||
$avatarUrl = $this->getOssObjectById($chef->avatar);
|
$avatarUrl = $this->getOssObjectById($chef->avatar);
|
||||||
$res = $chef->toArray();
|
$res = $chef->toArray();
|
||||||
$res['avatar_url'] = $avatarUrl;
|
$res['avatar_url'] = $avatarUrl;
|
||||||
|
$res['avg_score'] = $this->evaluationCache->getChefEvaluation($chefId) ?? 0;
|
||||||
|
|
||||||
// todo 评价补全
|
// 评价补全
|
||||||
|
$res['evaluation_list'] = $this->getEvaluationList($chefId);
|
||||||
|
|
||||||
return $this->return->success('success', $res);
|
return $this->return->success('success', $res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Evaluation
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected Evaluation $evaluationModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Sku
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected Sku $skuModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Order
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected Order $orderModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var User
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected User $userModel;
|
||||||
|
|
||||||
|
private function getEvaluationList(int $chefId): array
|
||||||
|
{
|
||||||
|
$limit = $this->request->input('limit') ?? 10;
|
||||||
|
|
||||||
|
$data = $this->evaluationModel
|
||||||
|
->where('chef_id', $chefId)
|
||||||
|
->orderByDesc('id')
|
||||||
|
->paginate($limit,['content','image_ids','id','order_id','user_id','sku_id','score','content'])
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
if (empty($data['data'])) return [];
|
||||||
|
|
||||||
|
$skuIds = array_column($data['data'], 'sku_id');
|
||||||
|
$orderIds = array_column($data['data'], 'order_id');
|
||||||
|
$userIds = array_column($data['data'], 'user_id');
|
||||||
|
$skuList = $this->skuModel->whereIn('id', $skuIds)->pluck('title','id')->toArray();
|
||||||
|
$orderTimes = $this->orderModel->whereIn('id', $orderIds)->pluck('create_time','id')->toArray();
|
||||||
|
// $skuList = array_column($skuList, null,'id');
|
||||||
|
$userList = $this->userModel->whereIn('id', $userIds)->select(['nickname','id','avatar_id'])->get()->toArray();
|
||||||
|
$userList = array_column($userList, null,'id');
|
||||||
|
$imageIds = array_column($userList, 'avatar_id');
|
||||||
|
$imageIdArr = array_column($data['data'],'image_ids');
|
||||||
|
$listImageIds = array_unique(explode(',',implode(',',$imageIdArr)));
|
||||||
|
$totalImages = array_merge($imageIds,$listImageIds);
|
||||||
|
unset($listImageIds,$imageIds);
|
||||||
|
$imageList = $this->getOssObjects($totalImages);
|
||||||
|
|
||||||
|
foreach ($data['data'] as &$item) {
|
||||||
|
$item['sku_title'] = $skuList[$item['sku_id']]['title'] ?? '';
|
||||||
|
$item['order_time'] = $orderTimes[$item['order_id']] ?? '';
|
||||||
|
$oneImage = [];
|
||||||
|
foreach (explode(',',$item['image_ids']) as $imageId) {
|
||||||
|
$oneImage[] = $imageList[$imageId]['url'] ?? '';
|
||||||
|
}
|
||||||
|
$item['content_image_list'] = $oneImage;
|
||||||
|
$item['nickname'] = $userList[$item['user_id']]['nickname'] ?? '';
|
||||||
|
$item['avatar'] = $imageList[$userList[$item['user_id']]['avatar_id']]['url'] ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Service\Api\Evaluation;
|
namespace App\Service\Api\Evaluation;
|
||||||
|
|
||||||
|
use App\Cache\Redis\Api\EvaluationCache;
|
||||||
use App\Constants\Common\OrderCode;
|
use App\Constants\Common\OrderCode;
|
||||||
use App\Exception\ErrException;
|
use App\Exception\ErrException;
|
||||||
use App\Model\Evaluation;
|
use App\Model\Evaluation;
|
||||||
@@ -44,6 +45,12 @@ class EvaluationService extends BaseService
|
|||||||
#[Inject]
|
#[Inject]
|
||||||
protected Sku $skuModel;
|
protected Sku $skuModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var EvaluationCache
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected EvaluationCache $evaluationCache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -89,6 +96,8 @@ class EvaluationService extends BaseService
|
|||||||
return $insertModel;
|
return $insertModel;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$this->evaluationCache->addChefEvaluation($skuInfo->chef_id, (int)$this->request->input('score'));
|
||||||
|
|
||||||
return $this->return->success('success', ['id' => $insertModel->id]);
|
return $this->return->success('success', ['id' => $insertModel->id]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,7 +82,7 @@ class GetLeaderboardService extends BaseService
|
|||||||
$v = 4 + 1 - exp(-2 * $normalized);
|
$v = 4 + 1 - exp(-2 * $normalized);
|
||||||
}
|
}
|
||||||
|
|
||||||
$v = round($v,2);
|
$v = round($v,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->redis->zAdd($redisKey,$v,$k);
|
$this->redis->zAdd($redisKey,$v,$k);
|
||||||
|
|||||||
Reference in New Issue
Block a user