| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- declare (strict_types = 1);
- namespace app\services\chat;
- use app\model\api\ChatBalanceLog;
- use app\model\api\ChatOrder;
- use app\model\api\Sys;
- use app\model\api\User;
- use app\model\api\UserScoreDetail;
- /**
- * 聊天次数余额服务
- */
- class ChatBalanceService
- {
- /**
- * 获取用户聊天次数
- * @param int $userId 用户ID
- * @return int
- */
- public static function getBalance(int $userId): int
- {
- $user = User::find($userId);
- return $user ? (int)($user->chat_num ?? 0) : 0;
- }
-
- /**
- * 增加聊天次数
- * @param int $userId 用户ID
- * @param int $num 增加数量
- * @param int $type 类型
- * @param string $remark 备注
- * @param int $relateId 关联ID
- * @return bool
- */
- public static function add(int $userId, int $num, int $type, string $remark = '', int $relateId = 0): bool
- {
- $user = User::find($userId);
- if (!$user) {
- return false;
- }
-
- $newBalance = (int)$user->chat_num + $num;
-
- $user->chat_num = $newBalance;
- $result = $user->save();
-
- if ($result) {
- ChatBalanceLog::record($userId, $type, $num, $newBalance, $remark, $relateId);
- }
-
- return $result;
- }
-
- /**
- * 减少聊天次数
- * @param int $userId 用户ID
- * @param int $num 减少数量
- * @param int $type 类型
- * @param string $remark 备注
- * @param int $relateId 关联ID
- * @return bool
- */
- public static function sub(int $userId, int $num, int $type, string $remark = '', int $relateId = 0): bool
- {
- $currentBalance = self::getBalance($userId);
-
- if ($currentBalance < $num) {
- return false;
- }
-
- $user = User::find($userId);
- if (!$user) {
- return false;
- }
-
- $newBalance = $currentBalance - $num;
-
- $user->chat_num = $newBalance;
- $result = $user->save();
-
- if ($result) {
- ChatBalanceLog::record($userId, $type, -$num, $newBalance, $remark, $relateId);
- }
-
- return $result;
- }
-
- /**
- * 扣除积分增加聊天次数(购买流程)
- * @param int $userId 用户ID
- * @param int $chatNum 购买次数
- * @param ChatOrder $order 订单
- * @return bool
- */
- public static function buyWithScore(int $userId, int $chatNum, ChatOrder $order): bool
- {
- $price = (int)$order->price;
-
- // 检查用户积分是否足够
- $userScore = UserScoreDetail::getUserScore($userId);
- if ($userScore < $price) {
- return false;
- }
-
- // 扣除积分
- $scoreResult = UserScoreDetail::scoreChange($userId, -$price, '购买聊天次数', $order->id);
- if (!$scoreResult) {
- return false;
- }
-
- // 增加聊天次数
- return self::add($userId, $chatNum, ChatBalanceLog::TYPE_BUY, '积分购买聊天次数', $order->id);
- }
-
- /**
- * 新用户赠送聊天次数
- * @param int $userId 用户ID
- * @param int $num 赠送次数
- * @return bool
- */
- public static function giftForNewUser(int $userId, int $num): bool
- {
- return self::add($userId, $num, ChatBalanceLog::TYPE_GIFT, '新用户赠送');
- }
-
- /**
- * 检查并扣除首次聊天次数
- * @param int $userId 用户ID(发起聊天方)
- * @param int $toUserId 对方用户ID
- * @return array ['success' => bool, 'message' => string]
- */
- public static function checkAndDeductFirstChat(int $userId, int $toUserId): array
- {
- // 检查是否已经扣过次数
- if (\app\model\api\ChatUserRelation::isDeducted($userId, $toUserId)) {
- return ['success' => true, 'message' => '已扣过次数'];
- }
-
- // 检查余额是否足够
- $balance = self::getBalance($userId);
- if ($balance <= 0) {
- return ['success' => false, 'message' => '聊天次数不足'];
- }
-
- // 扣除次数
- $result = self::sub($userId, 1, ChatBalanceLog::TYPE_FIRST_CHAT, '首次与用户' . $toUserId . '聊天', $toUserId);
-
- if ($result) {
- // 标记为已扣次数
- \app\model\api\ChatUserRelation::markDeducted($userId, $toUserId);
- return ['success' => true, 'message' => '扣除成功'];
- }
-
- return ['success' => false, 'message' => '扣除失败'];
- }
-
- /**
- * 获取购买价格
- * @return int 单次购买价格(积分)
- */
- public static function getBuyPrice(): int
- {
- $sys = Sys::find(1);
- return $sys ? (int)($sys->chat_price ?? 0) : 0;
- }
- }
|