| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- namespace App\Module\Friend\Logics;
- use App\Module\Friend\Enums\ERROR_CODE;
- use App\Module\Friend\Enums\FRIEND_STATUS;
- use App\Module\Friend\Models\FriendRelation;
- use App\Module\User\Models\User;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 好友关系逻辑类
- *
- * 实现好友关系的核心业务逻辑,包括好友添加、删除、查询等功能。
- * 该类仅供内部使用,不对外提供服务。
- */
- class Friend
- {
- /**
- * 好友数量上限
- *
- * @var int
- */
- protected $friendLimit = 100;
- /**
- * 获取用户的好友列表
- *
- * @param int $userId 用户ID
- * @param int $page 页码
- * @param int $limit 每页数量
- * @param array $filters 过滤条件
- * @return array 好友列表数据
- */
- public function getFriendList(int $userId, int $page = 1, int $limit = 20, array $filters = []): array
- {
- // 构建查询条件
- $query = FriendRelation::where('user_id', $userId);
-
- // 应用状态过滤
- if (isset($filters['status'])) {
- $query->where('status', $filters['status']);
- } else {
- // 默认只查询正常状态的好友
- $query->where('status', FRIEND_STATUS::NORMAL);
- }
-
- // 应用分组过滤
- if (isset($filters['group_id'])) {
- $query->where('group_id', $filters['group_id']);
- }
-
- // 应用关键词搜索(好友备注)
- if (isset($filters['keyword']) && !empty($filters['keyword'])) {
- $query->where('remark', 'like', '%' . $filters['keyword'] . '%');
- }
-
- // 应用排序
- if (isset($filters['sort_by']) && isset($filters['sort_order'])) {
- $query->orderBy($filters['sort_by'], $filters['sort_order']);
- } else {
- // 默认按亲密度降序排列
- $query->orderBy('intimacy', 'desc');
- }
-
- // 执行分页查询
- $relations = $query->with('friend')->paginate($limit, ['*'], 'page', $page);
-
- // 格式化数据
- $items = [];
- foreach ($relations as $relation) {
- if (!$relation->friend) {
- continue;
- }
-
- $items[] = [
- 'user_id' => $relation->friend_id,
- 'nickname' => $relation->friend->nickname ?? '',
- 'avatar' => $relation->friend->avatar ?? '',
- 'level' => $relation->friend->level ?? 1,
- 'status' => $this->getUserOnlineStatus($relation->friend_id),
- 'last_login' => $relation->friend->last_login_at ?? 0,
- 'remark' => $relation->remark,
- 'intimacy' => $relation->intimacy,
- ];
- }
-
- return [
- 'total' => $relations->total(),
- 'per_page' => $relations->perPage(),
- 'current_page' => $relations->currentPage(),
- 'last_page' => $relations->lastPage(),
- 'items' => $items
- ];
- }
- /**
- * 创建好友关系
- *
- * @param int $userId 用户ID
- * @param int $friendId 好友ID
- * @param string|null $remark 备注名
- * @return array 操作结果
- */
- public function createFriendRelation(int $userId, int $friendId, ?string $remark = null): array
- {
- try {
- // 检查是否已经是好友
- $existingRelation = FriendRelation::where('user_id', $userId)
- ->where('friend_id', $friendId)
- ->first();
-
- if ($existingRelation) {
- if ($existingRelation->status == FRIEND_STATUS::NORMAL) {
- return [
- 'success' => false,
- 'code' => ERROR_CODE::ALREADY_FRIEND,
- 'message' => '已经是好友关系'
- ];
- } else {
- // 如果是黑名单状态,则更新为正常状态
- $existingRelation->status = FRIEND_STATUS::NORMAL;
- $existingRelation->save();
-
- return [
- 'success' => true,
- 'data' => $existingRelation
- ];
- }
- }
-
- // 检查好友数量是否达到上限
- $friendCount = FriendRelation::where('user_id', $userId)
- ->where('status', FRIEND_STATUS::NORMAL)
- ->count();
-
- if ($friendCount >= $this->friendLimit) {
- return [
- 'success' => false,
- 'code' => ERROR_CODE::FRIEND_LIMIT_REACHED,
- 'message' => '好友数量已达上限'
- ];
- }
-
- // 创建好友关系(双向)
- DB::beginTransaction();
-
- // 创建A->B的关系
- $relationAB = FriendRelation::create([
- 'user_id' => $userId,
- 'friend_id' => $friendId,
- 'remark' => $remark,
- 'group_id' => 0,
- 'intimacy' => 0,
- 'status' => FRIEND_STATUS::NORMAL,
- ]);
-
- // 创建B->A的关系
- $relationBA = FriendRelation::create([
- 'user_id' => $friendId,
- 'friend_id' => $userId,
- 'remark' => null,
- 'group_id' => 0,
- 'intimacy' => 0,
- 'status' => FRIEND_STATUS::NORMAL,
- ]);
-
- DB::commit();
-
- return [
- 'success' => true,
- 'data' => $relationAB
- ];
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error('创建好友关系失败: ' . $e->getMessage());
-
- return [
- 'success' => false,
- 'code' => ERROR_CODE::PARAM_ERROR,
- 'message' => '创建好友关系失败'
- ];
- }
- }
- /**
- * 删除好友关系
- *
- * @param int $userId 用户ID
- * @param int $friendId 好友ID
- * @return array 操作结果
- */
- public function deleteFriend(int $userId, int $friendId): array
- {
- try {
- // 检查是否存在好友关系
- $relation = FriendRelation::where('user_id', $userId)
- ->where('friend_id', $friendId)
- ->where('status', FRIEND_STATUS::NORMAL)
- ->first();
-
- if (!$relation) {
- return [
- 'success' => false,
- 'code' => ERROR_CODE::RELATION_NOT_EXIST,
- 'message' => '好友关系不存在'
- ];
- }
-
- // 删除双向好友关系
- DB::beginTransaction();
-
- // 删除A->B的关系
- FriendRelation::where('user_id', $userId)
- ->where('friend_id', $friendId)
- ->delete();
-
- // 删除B->A的关系
- FriendRelation::where('user_id', $friendId)
- ->where('friend_id', $userId)
- ->delete();
-
- DB::commit();
-
- return [
- 'success' => true
- ];
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error('删除好友关系失败: ' . $e->getMessage());
-
- return [
- 'success' => false,
- 'code' => ERROR_CODE::PARAM_ERROR,
- 'message' => '删除好友关系失败'
- ];
- }
- }
- /**
- * 更新好友备注
- *
- * @param int $userId 用户ID
- * @param int $friendId 好友ID
- * @param string $remark 备注名
- * @return array 操作结果
- */
- public function updateFriendRemark(int $userId, int $friendId, string $remark): array
- {
- try {
- // 检查是否存在好友关系
- $relation = FriendRelation::where('user_id', $userId)
- ->where('friend_id', $friendId)
- ->where('status', FRIEND_STATUS::NORMAL)
- ->first();
-
- if (!$relation) {
- return [
- 'success' => false,
- 'code' => ERROR_CODE::RELATION_NOT_EXIST,
- 'message' => '好友关系不存在'
- ];
- }
-
- // 更新备注
- $relation->remark = $remark;
- $relation->save();
-
- return [
- 'success' => true
- ];
- } catch (\Exception $e) {
- Log::error('更新好友备注失败: ' . $e->getMessage());
-
- return [
- 'success' => false,
- 'code' => ERROR_CODE::PARAM_ERROR,
- 'message' => '更新好友备注失败'
- ];
- }
- }
- /**
- * 获取用户在线状态
- *
- * @param int $userId 用户ID
- * @return int 状态:1在线,2离线
- */
- protected function getUserOnlineStatus(int $userId): int
- {
- // TODO: 实现获取用户在线状态的逻辑
- // 这里可以调用用户模块的相关服务或者查询缓存
- return 2; // 默认离线
- }
- }
|