| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- namespace App\Module\Point\Services;
- use App\Module\Point\Dto\PointAccountDto;
- use App\Module\Point\Models\PointModel;
- use App\Module\Point\Models\PointConfigModel;
- use App\Module\Point\Enums\POINT_TYPE;
- /**
- * 积分账户服务类
- *
- * 提供积分账户相关的业务功能
- */
- class AccountService
- {
- /**
- * 获取用户积分账户信息
- *
- * @param int $userId 用户ID
- * @param int|null $pointId 积分类型ID(可选)
- * @return PointAccountDto|array 单个账户返回DTO,多个账户返回数组
- */
- public static function getAccountInfo(int $userId, ?int $pointId = null)
- {
- if ($pointId !== null) {
- $account = PointModel::getAccount($userId, $pointId);
- return new PointAccountDto([
- 'user_id' => $userId,
- 'point_id' => $pointId,
- 'balance' => $account ? $account->balance : 0,
- 'create_time' => $account ? $account->create_time : 0,
- 'update_time' => $account ? $account->update_time : 0,
- ]);
- }
- $accounts = PointModel::userAccount($userId);
- $result = [];
-
- foreach ($accounts as $account) {
- $result[] = new PointAccountDto([
- 'user_id' => $account->user_id,
- 'point_id' => $account->point_id,
- 'balance' => $account->balance,
- 'create_time' => $account->create_time,
- 'update_time' => $account->update_time,
- ]);
- }
- return $result;
- }
- /**
- * 获取用户所有积分账户余额
- *
- * @param int $userId 用户ID
- * @return array 积分余额数组,key为积分类型ID,value为余额
- */
- public static function getAllBalances(int $userId): array
- {
- $accounts = PointModel::userAccount($userId);
- $balances = [];
-
- foreach ($accounts as $account) {
- $balances[$account->point_id] = $account->balance;
- }
- return $balances;
- }
- /**
- * 获取用户指定积分类型的余额
- *
- * @param int $userId 用户ID
- * @param int $pointId 积分类型ID
- * @return int 积分余额
- */
- public static function getBalance(int $userId, int $pointId): int
- {
- return PointModel::getBalance($userId, $pointId);
- }
- /**
- * 检查用户积分余额是否足够
- *
- * @param int $userId 用户ID
- * @param int $pointId 积分类型ID
- * @param int $amount 需要的积分数量
- * @return bool 是否足够
- */
- public static function checkBalance(int $userId, int $pointId, int $amount): bool
- {
- return PointModel::checkBalance($userId, $pointId, $amount);
- }
- /**
- * 创建或获取用户积分账户
- *
- * @param int $userId 用户ID
- * @param int $pointId 积分类型ID
- * @return PointModel 积分账户模型
- */
- public static function createOrGetAccount(int $userId, int $pointId): PointModel
- {
- return PointModel::createOrGet($userId, $pointId);
- }
- /**
- * 获取用户积分账户统计信息
- *
- * @param int $userId 用户ID
- * @return array 统计信息
- */
- public static function getAccountStats(int $userId): array
- {
- $accounts = PointModel::userAccount($userId);
-
- $stats = [
- 'total_accounts' => $accounts->count(),
- 'total_balance' => 0,
- 'account_details' => [],
- ];
- foreach ($accounts as $account) {
- $stats['total_balance'] += $account->balance;
- $stats['account_details'][] = [
- 'point_id' => $account->point_id,
- 'point_type_name' => $account->getPointTypeName(),
- 'balance' => $account->balance,
- 'create_time' => $account->create_time,
- 'update_time' => $account->update_time,
- ];
- }
- return $stats;
- }
- /**
- * 批量获取用户积分余额
- *
- * @param array $userIds 用户ID数组
- * @param int $pointId 积分类型ID
- * @return array 用户积分余额数组,key为用户ID,value为余额
- */
- public static function batchGetBalances(array $userIds, int $pointId): array
- {
- $accounts = PointModel::whereIn('user_id', $userIds)
- ->where('point_id', $pointId)
- ->get();
- $balances = [];
- foreach ($userIds as $userId) {
- $balances[$userId] = 0; // 默认余额为0
- }
- foreach ($accounts as $account) {
- $balances[$account->user_id] = $account->balance;
- }
- return $balances;
- }
- /**
- * 获取积分类型配置信息
- *
- * @param int|null $pointType 积分类型(可选)
- * @return array 配置信息数组
- */
- public static function getPointTypeConfigs(?int $pointType = null): array
- {
- if ($pointType !== null) {
- $config = PointConfigModel::getByType($pointType);
- return $config ? [
- 'id' => $config->id,
- 'name' => $config->name,
- 'type' => $config->type,
- 'type_name' => $config->getTypeName(),
- 'currency_id' => $config->currency_id,
- 'display_attributes' => $config->display_attributes,
- ] : [];
- }
- $configs = PointConfigModel::getAllConfigs();
- $result = [];
- foreach ($configs as $config) {
- $result[] = [
- 'id' => $config->id,
- 'name' => $config->name,
- 'type' => $config->type,
- 'type_name' => $config->getTypeName(),
- 'currency_id' => $config->currency_id,
- 'display_attributes' => $config->display_attributes,
- ];
- }
- return $result;
- }
- /**
- * 验证积分类型是否有效
- *
- * @param int $pointType 积分类型
- * @return bool 是否有效
- */
- public static function validatePointType(int $pointType): bool
- {
- return POINT_TYPE::isValid($pointType) && PointConfigModel::typeExists($pointType);
- }
- /**
- * 获取用户积分排行榜
- *
- * @param int $pointId 积分类型ID
- * @param int $limit 限制数量
- * @return array 排行榜数组
- */
- public static function getPointRanking(int $pointId, int $limit = 100): array
- {
- $accounts = PointModel::where('point_id', $pointId)
- ->where('balance', '>', 0)
- ->orderBy('balance', 'desc')
- ->limit($limit)
- ->get();
- $ranking = [];
- $rank = 1;
- foreach ($accounts as $account) {
- $ranking[] = [
- 'rank' => $rank++,
- 'user_id' => $account->user_id,
- 'balance' => $account->balance,
- 'point_id' => $account->point_id,
- 'point_type_name' => $account->getPointTypeName(),
- ];
- }
- return $ranking;
- }
- /**
- * 获取用户在指定积分类型中的排名
- *
- * @param int $userId 用户ID
- * @param int $pointId 积分类型ID
- * @return array 排名信息
- */
- public static function getUserRanking(int $userId, int $pointId): array
- {
- $userAccount = PointModel::getAccount($userId, $pointId);
- if (!$userAccount) {
- return [
- 'rank' => 0,
- 'balance' => 0,
- 'total_users' => 0,
- ];
- }
- $higherCount = PointModel::where('point_id', $pointId)
- ->where('balance', '>', $userAccount->balance)
- ->count();
- $totalUsers = PointModel::where('point_id', $pointId)
- ->where('balance', '>', 0)
- ->count();
- return [
- 'rank' => $higherCount + 1,
- 'balance' => $userAccount->balance,
- 'total_users' => $totalUsers,
- ];
- }
- }
|