| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- namespace App\Module\Promotion\Services;
- use App\Module\Promotion\Logics\ReferralCodeLogic;
- use App\Module\Promotion\Logics\ReferralLogic;
- use App\Module\Promotion\Models\PromotionReferralCode;
- use App\Module\Promotion\Models\PromotionReferralCodeUsage;
- use Illuminate\Support\Facades\Log;
- /**
- * 推荐码服务类
- *
- * 对外提供推荐码相关的服务,包括生成推荐码、验证推荐码、
- * 使用推荐码等功能。该类对外提供服务,内部调用逻辑层实现。
- */
- class ReferralCodeService
- {
- /**
- * 获取或生成用户的推荐码
- *
- * @param int $userId 用户ID
- * @param \DateTime|null $expireTime 过期时间
- * @return array|null
- */
- public static function getUserReferralCode(int $userId, ?\DateTime $expireTime = null): ?array
- {
- try {
- $referralLogic = new ReferralLogic();
- $referralCodeLogic = new ReferralCodeLogic($referralLogic);
- // 先尝试获取现有的推荐码
- $code = $referralCodeLogic->getUserActiveCode($userId);
- // 如果没有有效的推荐码,则生成一个新的
- if (!$code) {
- $codeStr = $referralCodeLogic->generateReferralCode($userId, $expireTime);
- if (!$codeStr) {
- return null;
- }
- $code = $referralCodeLogic->getUserActiveCode($userId);
- }
- if (!$code) {
- return null;
- }
- return [
- 'code' => $code->code,
- 'usage_count' => $code->usage_count,
- 'status' => $code->status,
- 'expire_time' => $code->expire_time,
- 'created_at' => $code->created_at,
- 'is_valid' => $code->isValid()
- ];
- } catch (\Exception $e) {
- Log::error("获取或生成用户推荐码失败: " . $e->getMessage());
- return null;
- }
- }
- /**
- * 验证推荐码
- *
- * @param string $code 推荐码
- * @return array
- */
- public static function validateReferralCode(string $code): array
- {
- try {
- $referralLogic = new ReferralLogic();
- $referralCodeLogic = new ReferralCodeLogic($referralLogic);
- return $referralCodeLogic->validateReferralCode($code);
- } catch (\Exception $e) {
- Log::error("验证推荐码失败: " . $e->getMessage());
- return [
- 'valid' => false,
- 'message' => '验证推荐码时发生错误',
- 'code' => null
- ];
- }
- }
- /**
- * 使用推荐码
- *
- * @param string $code 推荐码
- * @param int $userId 使用者ID
- * @param string $ipAddress IP地址
- * @param string $userAgent 用户代理
- * @return array
- */
- public static function useReferralCode(string $code, int $userId, string $ipAddress, string $userAgent): array
- {
- try {
- $referralLogic = new ReferralLogic();
- $referralCodeLogic = new ReferralCodeLogic($referralLogic);
- return $referralCodeLogic->useReferralCode($code, $userId, $ipAddress, $userAgent);
- } catch (\Exception $e) {
- Log::error("使用推荐码失败: " . $e->getMessage());
- return [
- 'success' => false,
- 'message' => '使用推荐码时发生错误'
- ];
- }
- }
- /**
- * 禁用推荐码
- *
- * @param string $code 推荐码
- * @return bool
- */
- public static function disableReferralCode(string $code): bool
- {
- try {
- $referralLogic = new ReferralLogic();
- $referralCodeLogic = new ReferralCodeLogic($referralLogic);
- return $referralCodeLogic->disableReferralCode($code);
- } catch (\Exception $e) {
- Log::error("禁用推荐码失败: " . $e->getMessage());
- return false;
- }
- }
- /**
- * 获取推荐码使用记录
- *
- * @param string $code 推荐码
- * @param int $page 页码
- * @param int $pageSize 每页数量
- * @return array
- */
- public static function getReferralCodeUsages(string $code, int $page = 1, int $pageSize = 20): array
- {
- try {
- $query = PromotionReferralCodeUsage::where('code', $code);
- $total = $query->count();
- $usages = $query->orderBy('created_at', 'desc')
- ->offset(($page - 1) * $pageSize)
- ->limit($pageSize)
- ->with(['user', 'codeOwner'])
- ->get();
- $result = [];
- foreach ($usages as $usage) {
- $result[] = [
- 'id' => $usage->id,
- 'code' => $usage->code,
- 'code_owner_id' => $usage->code_owner_id,
- 'code_owner_name' => $usage->codeOwner ? ($usage->codeOwner->username ?? '') : '',
- 'user_id' => $usage->user_id,
- 'user_name' => $usage->user ? ($usage->user->username ?? '') : '',
- 'ip_address' => $usage->ip_address,
- 'user_agent' => $usage->user_agent,
- 'status' => $usage->status,
- 'result' => $usage->result,
- 'remark' => $usage->remark,
- 'created_at' => $usage->created_at
- ];
- }
- return [
- 'total' => $total,
- 'page' => $page,
- 'page_size' => $pageSize,
- 'total_pages' => ceil($total / $pageSize),
- 'usages' => $result
- ];
- } catch (\Exception $e) {
- Log::error("获取推荐码使用记录失败: " . $e->getMessage());
- return [
- 'total' => 0,
- 'page' => $page,
- 'page_size' => $pageSize,
- 'total_pages' => 0,
- 'usages' => []
- ];
- }
- }
- /**
- * 获取用户的所有推荐码
- *
- * @param int $userId 用户ID
- * @param int $page 页码
- * @param int $pageSize 每页数量
- * @return array
- */
- public static function getUserAllReferralCodes(int $userId, int $page = 1, int $pageSize = 20): array
- {
- try {
- $query = PromotionReferralCode::where('user_id', $userId);
- $total = $query->count();
- $codes = $query->orderBy('created_at', 'desc')
- ->offset(($page - 1) * $pageSize)
- ->limit($pageSize)
- ->get();
- $result = [];
- foreach ($codes as $code) {
- $result[] = [
- 'code' => $code->code,
- 'usage_count' => $code->usage_count,
- 'status' => $code->status,
- 'expire_time' => $code->expire_time,
- 'created_at' => $code->created_at,
- 'is_valid' => $code->isValid()
- ];
- }
- return [
- 'total' => $total,
- 'page' => $page,
- 'page_size' => $pageSize,
- 'total_pages' => ceil($total / $pageSize),
- 'codes' => $result
- ];
- } catch (\Exception $e) {
- Log::error("获取用户所有推荐码失败: " . $e->getMessage());
- return [
- 'total' => 0,
- 'page' => $page,
- 'page_size' => $pageSize,
- 'total_pages' => 0,
- 'codes' => []
- ];
- }
- }
- /**
- * 清理过期的推荐码
- *
- * @return int 清理的数量
- */
- public static function cleanExpiredReferralCodes(): int
- {
- try {
- $referralLogic = new ReferralLogic();
- $referralCodeLogic = new ReferralCodeLogic($referralLogic);
- return $referralCodeLogic->cleanExpiredReferralCodes();
- } catch (\Exception $e) {
- Log::error("清理过期推荐码失败: " . $e->getMessage());
- return 0;
- }
- }
- }
|