ReferralCodeService.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace App\Module\Promotion\Services;
  3. use App\Module\Promotion\Logics\ReferralCodeLogic;
  4. use App\Module\Promotion\Logics\ReferralLogic;
  5. use App\Module\Promotion\Models\PromotionReferralCode;
  6. use App\Module\Promotion\Models\PromotionReferralCodeUsage;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * 推荐码服务类
  10. *
  11. * 对外提供推荐码相关的服务,包括生成推荐码、验证推荐码、
  12. * 使用推荐码等功能。该类对外提供服务,内部调用逻辑层实现。
  13. */
  14. class ReferralCodeService
  15. {
  16. /**
  17. * 获取或生成用户的推荐码
  18. *
  19. * @param int $userId 用户ID
  20. * @param \DateTime|null $expireTime 过期时间
  21. * @return array|null
  22. */
  23. public static function getUserReferralCode(int $userId, ?\DateTime $expireTime = null): ?array
  24. {
  25. try {
  26. $referralLogic = new ReferralLogic();
  27. $referralCodeLogic = new ReferralCodeLogic($referralLogic);
  28. // 先尝试获取现有的推荐码
  29. $code = $referralCodeLogic->getUserActiveCode($userId);
  30. // 如果没有有效的推荐码,则生成一个新的
  31. if (!$code) {
  32. $codeStr = $referralCodeLogic->generateReferralCode($userId, $expireTime);
  33. if (!$codeStr) {
  34. return null;
  35. }
  36. $code = $referralCodeLogic->getUserActiveCode($userId);
  37. }
  38. if (!$code) {
  39. return null;
  40. }
  41. return [
  42. 'code' => $code->code,
  43. 'usage_count' => $code->usage_count,
  44. 'status' => $code->status,
  45. 'expire_time' => $code->expire_time,
  46. 'created_at' => $code->created_at,
  47. 'is_valid' => $code->isValid()
  48. ];
  49. } catch (\Exception $e) {
  50. Log::error("获取或生成用户推荐码失败: " . $e->getMessage());
  51. return null;
  52. }
  53. }
  54. /**
  55. * 验证推荐码
  56. *
  57. * @param string $code 推荐码
  58. * @return array
  59. */
  60. public static function validateReferralCode(string $code): array
  61. {
  62. try {
  63. $referralLogic = new ReferralLogic();
  64. $referralCodeLogic = new ReferralCodeLogic($referralLogic);
  65. return $referralCodeLogic->validateReferralCode($code);
  66. } catch (\Exception $e) {
  67. Log::error("验证推荐码失败: " . $e->getMessage());
  68. return [
  69. 'valid' => false,
  70. 'message' => '验证推荐码时发生错误',
  71. 'code' => null
  72. ];
  73. }
  74. }
  75. /**
  76. * 使用推荐码
  77. *
  78. * @param string $code 推荐码
  79. * @param int $userId 使用者ID
  80. * @param string $ipAddress IP地址
  81. * @param string $userAgent 用户代理
  82. * @return array
  83. */
  84. public static function useReferralCode(string $code, int $userId, string $ipAddress, string $userAgent): array
  85. {
  86. try {
  87. $referralLogic = new ReferralLogic();
  88. $referralCodeLogic = new ReferralCodeLogic($referralLogic);
  89. return $referralCodeLogic->useReferralCode($code, $userId, $ipAddress, $userAgent);
  90. } catch (\Exception $e) {
  91. Log::error("使用推荐码失败: " . $e->getMessage());
  92. return [
  93. 'success' => false,
  94. 'message' => '使用推荐码时发生错误'
  95. ];
  96. }
  97. }
  98. /**
  99. * 禁用推荐码
  100. *
  101. * @param string $code 推荐码
  102. * @return bool
  103. */
  104. public static function disableReferralCode(string $code): bool
  105. {
  106. try {
  107. $referralLogic = new ReferralLogic();
  108. $referralCodeLogic = new ReferralCodeLogic($referralLogic);
  109. return $referralCodeLogic->disableReferralCode($code);
  110. } catch (\Exception $e) {
  111. Log::error("禁用推荐码失败: " . $e->getMessage());
  112. return false;
  113. }
  114. }
  115. /**
  116. * 获取推荐码使用记录
  117. *
  118. * @param string $code 推荐码
  119. * @param int $page 页码
  120. * @param int $pageSize 每页数量
  121. * @return array
  122. */
  123. public static function getReferralCodeUsages(string $code, int $page = 1, int $pageSize = 20): array
  124. {
  125. try {
  126. $query = PromotionReferralCodeUsage::where('code', $code);
  127. $total = $query->count();
  128. $usages = $query->orderBy('created_at', 'desc')
  129. ->offset(($page - 1) * $pageSize)
  130. ->limit($pageSize)
  131. ->with(['user', 'codeOwner'])
  132. ->get();
  133. $result = [];
  134. foreach ($usages as $usage) {
  135. $result[] = [
  136. 'id' => $usage->id,
  137. 'code' => $usage->code,
  138. 'code_owner_id' => $usage->code_owner_id,
  139. 'code_owner_name' => $usage->codeOwner ? ($usage->codeOwner->username ?? '') : '',
  140. 'user_id' => $usage->user_id,
  141. 'user_name' => $usage->user ? ($usage->user->username ?? '') : '',
  142. 'ip_address' => $usage->ip_address,
  143. 'user_agent' => $usage->user_agent,
  144. 'status' => $usage->status,
  145. 'result' => $usage->result,
  146. 'remark' => $usage->remark,
  147. 'created_at' => $usage->created_at
  148. ];
  149. }
  150. return [
  151. 'total' => $total,
  152. 'page' => $page,
  153. 'page_size' => $pageSize,
  154. 'total_pages' => ceil($total / $pageSize),
  155. 'usages' => $result
  156. ];
  157. } catch (\Exception $e) {
  158. Log::error("获取推荐码使用记录失败: " . $e->getMessage());
  159. return [
  160. 'total' => 0,
  161. 'page' => $page,
  162. 'page_size' => $pageSize,
  163. 'total_pages' => 0,
  164. 'usages' => []
  165. ];
  166. }
  167. }
  168. /**
  169. * 获取用户的所有推荐码
  170. *
  171. * @param int $userId 用户ID
  172. * @param int $page 页码
  173. * @param int $pageSize 每页数量
  174. * @return array
  175. */
  176. public static function getUserAllReferralCodes(int $userId, int $page = 1, int $pageSize = 20): array
  177. {
  178. try {
  179. $query = PromotionReferralCode::where('user_id', $userId);
  180. $total = $query->count();
  181. $codes = $query->orderBy('created_at', 'desc')
  182. ->offset(($page - 1) * $pageSize)
  183. ->limit($pageSize)
  184. ->get();
  185. $result = [];
  186. foreach ($codes as $code) {
  187. $result[] = [
  188. 'code' => $code->code,
  189. 'usage_count' => $code->usage_count,
  190. 'status' => $code->status,
  191. 'expire_time' => $code->expire_time,
  192. 'created_at' => $code->created_at,
  193. 'is_valid' => $code->isValid()
  194. ];
  195. }
  196. return [
  197. 'total' => $total,
  198. 'page' => $page,
  199. 'page_size' => $pageSize,
  200. 'total_pages' => ceil($total / $pageSize),
  201. 'codes' => $result
  202. ];
  203. } catch (\Exception $e) {
  204. Log::error("获取用户所有推荐码失败: " . $e->getMessage());
  205. return [
  206. 'total' => 0,
  207. 'page' => $page,
  208. 'page_size' => $pageSize,
  209. 'total_pages' => 0,
  210. 'codes' => []
  211. ];
  212. }
  213. }
  214. /**
  215. * 清理过期的推荐码
  216. *
  217. * @return int 清理的数量
  218. */
  219. public static function cleanExpiredReferralCodes(): int
  220. {
  221. try {
  222. $referralLogic = new ReferralLogic();
  223. $referralCodeLogic = new ReferralCodeLogic($referralLogic);
  224. return $referralCodeLogic->cleanExpiredReferralCodes();
  225. } catch (\Exception $e) {
  226. Log::error("清理过期推荐码失败: " . $e->getMessage());
  227. return 0;
  228. }
  229. }
  230. }