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 = (float)(new User)->where('uid', $userId)->value('score'); if ($userScore < $price) { return false; } // 扣除积分 $scoreResult = (new UserScoreDetail)->payScore($userId, $price, 'outcome_score', $order->order_no, [], '购买聊天次数', '购买聊天次数消费' . $price . '积分'); if (!$scoreResult) { return false; } // 增加聊天次数 return self::add($userId, $chatNum, ChatBalanceLog::TYPE_BUY, '积分购买聊天次数', (int)$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; } }