TalentService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace App\Module\Promotion\Services;
  3. use App\Module\Promotion\Logics\ReferralLogic;
  4. use App\Module\Promotion\Logics\TalentLogic;
  5. use App\Module\Promotion\Models\PromotionUserTalent;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 达人等级服务类
  9. *
  10. * 对外提供达人等级相关的服务,包括获取达人等级、获取达人权益、
  11. * 获取达人等级配置等功能。该类对外提供服务,内部调用逻辑层实现。
  12. */
  13. class TalentService
  14. {
  15. /**
  16. * 获取用户的达人等级信息
  17. *
  18. * @param int $userId 用户ID
  19. * @return array|null
  20. */
  21. public static function getUserTalentInfo(int $userId): ?array
  22. {
  23. try {
  24. $referralLogic = new ReferralLogic();
  25. $talentLogic = new TalentLogic($referralLogic);
  26. $talent = $talentLogic->getUserTalent($userId);
  27. if (!$talent) {
  28. return null;
  29. }
  30. $config = $talentLogic->getTalentConfig($talent->talent_level);
  31. return [
  32. 'user_id' => $talent->user_id,
  33. 'talent_level' => $talent->talent_level,
  34. 'talent_name' => $config ? $config->name : '',
  35. 'direct_count' => $talent->direct_count,
  36. 'promotion_count' => $talent->promotion_count,
  37. 'icon' => $config ? $config->icon : '',
  38. 'icon_url' => $config ? $config->icon_url : '',
  39. 'benefits' => $config ? (is_array($config->benefits) ? $config->benefits : json_decode($config->benefits, true)) : [],
  40. 'profit_rate' => $config ? $config->profit_rate : 0.0,
  41. 'created_at' => $talent->created_at,
  42. 'updated_at' => $talent->updated_at
  43. ];
  44. } catch (\Exception $e) {
  45. Log::error("获取用户达人等级信息失败: " . $e->getMessage());
  46. return null;
  47. }
  48. }
  49. /**
  50. * 获取所有达人等级配置
  51. *
  52. * @return array
  53. */
  54. public static function getAllTalentConfigs(): array
  55. {
  56. try {
  57. $referralLogic = new ReferralLogic();
  58. $talentLogic = new TalentLogic($referralLogic);
  59. $configs = $talentLogic->getAllTalentConfigs();
  60. $result = [];
  61. foreach ($configs as $config) {
  62. $result[] = [
  63. 'level' => $config->level,
  64. 'name' => $config->name,
  65. 'direct_count_required' => $config->direct_count_required,
  66. 'promotion_count_required' => $config->promotion_count_required,
  67. 'profit_rate' => $config->profit_rate,
  68. 'benefits' => is_array($config->benefits) ? $config->benefits : json_decode($config->benefits, true),
  69. 'icon' => $config->icon,
  70. 'icon_url' => $config->icon_url
  71. ];
  72. }
  73. return $result;
  74. } catch (\Exception $e) {
  75. Log::error("获取所有达人等级配置失败: " . $e->getMessage());
  76. return [];
  77. }
  78. }
  79. /**
  80. * 获取达人等级权益
  81. *
  82. * @param int $level 等级
  83. * @return array
  84. */
  85. public static function getTalentBenefits(int $level): array
  86. {
  87. try {
  88. $referralLogic = new ReferralLogic();
  89. $talentLogic = new TalentLogic($referralLogic);
  90. return $talentLogic->getTalentBenefits($level);
  91. } catch (\Exception $e) {
  92. Log::error("获取达人等级权益失败: " . $e->getMessage());
  93. return [];
  94. }
  95. }
  96. /**
  97. * 检查并更新用户的达人等级
  98. *
  99. * @param int $userId 用户ID
  100. * @return bool
  101. */
  102. public static function checkAndUpdateUserTalentLevel(int $userId): bool
  103. {
  104. try {
  105. $referralLogic = new ReferralLogic();
  106. $talentLogic = new TalentLogic($referralLogic);
  107. return $talentLogic->checkAndUpdateTalentLevel($userId);
  108. } catch (\Exception $e) {
  109. Log::error("检查并更新用户达人等级失败: " . $e->getMessage());
  110. return false;
  111. }
  112. }
  113. /**
  114. * 获取达人等级排行榜
  115. *
  116. * @param int $limit 限制数量
  117. * @return array
  118. */
  119. public static function getTalentRanking(int $limit = 10): array
  120. {
  121. try {
  122. $referralLogic = new ReferralLogic();
  123. $talentLogic = new TalentLogic($referralLogic);
  124. $talents = PromotionUserTalent::where('talent_level', '>', 0)
  125. ->orderBy('talent_level', 'desc')
  126. ->orderBy('promotion_count', 'desc')
  127. ->orderBy('direct_count', 'desc')
  128. ->limit($limit)
  129. ->with('user')
  130. ->get();
  131. $result = [];
  132. foreach ($talents as $talent) {
  133. if ($talent->user) {
  134. $config = $talentLogic->getTalentConfig($talent->talent_level);
  135. $result[] = [
  136. 'user_id' => $talent->user_id,
  137. 'username' => $talent->user->username ?? '',
  138. 'nickname' => $talent->user->nickname ?? '',
  139. 'avatar' => $talent->user->avatar ?? '',
  140. 'talent_level' => $talent->talent_level,
  141. 'talent_name' => $config ? $config->name : '',
  142. 'direct_count' => $talent->direct_count,
  143. 'promotion_count' => $talent->promotion_count,
  144. 'icon' => $config ? $config->icon : '',
  145. 'icon_url' => $config ? $config->icon_url : ''
  146. ];
  147. }
  148. }
  149. return $result;
  150. } catch (\Exception $e) {
  151. Log::error("获取达人等级排行榜失败: " . $e->getMessage());
  152. return [];
  153. }
  154. }
  155. /**
  156. * 获取用户的达人等级进度
  157. *
  158. * @param int $userId 用户ID
  159. * @return array
  160. */
  161. public static function getUserTalentProgress(int $userId): array
  162. {
  163. try {
  164. $referralLogic = new ReferralLogic();
  165. $talentLogic = new TalentLogic($referralLogic);
  166. $talent = $talentLogic->getUserTalent($userId);
  167. if (!$talent) {
  168. return [
  169. 'current_level' => 0,
  170. 'next_level' => 1,
  171. 'direct_count' => 0,
  172. 'direct_count_required' => 5,
  173. 'direct_count_progress' => 0,
  174. 'promotion_count' => 0,
  175. 'promotion_count_required' => 10,
  176. 'promotion_count_progress' => 0
  177. ];
  178. }
  179. $configs = $talentLogic->getAllTalentConfigs();
  180. // 找到下一个等级
  181. $nextLevel = null;
  182. $nextLevelConfig = null;
  183. foreach ($configs as $config) {
  184. if ($config->level > $talent->talent_level) {
  185. $nextLevel = $config->level;
  186. $nextLevelConfig = $config;
  187. break;
  188. }
  189. }
  190. if (!$nextLevel) {
  191. // 已经是最高等级
  192. $currentConfig = $talentLogic->getTalentConfig($talent->talent_level);
  193. return [
  194. 'current_level' => $talent->talent_level,
  195. 'next_level' => null,
  196. 'direct_count' => $talent->direct_count,
  197. 'direct_count_required' => $currentConfig ? $currentConfig->direct_count_required : 0,
  198. 'direct_count_progress' => 100,
  199. 'promotion_count' => $talent->promotion_count,
  200. 'promotion_count_required' => $currentConfig ? $currentConfig->promotion_count_required : 0,
  201. 'promotion_count_progress' => 100
  202. ];
  203. }
  204. // 计算进度
  205. $directCountProgress = $nextLevelConfig->direct_count_required > 0
  206. ? min(100, ($talent->direct_count / $nextLevelConfig->direct_count_required) * 100)
  207. : 0;
  208. $promotionCountProgress = $nextLevelConfig->promotion_count_required > 0
  209. ? min(100, ($talent->promotion_count / $nextLevelConfig->promotion_count_required) * 100)
  210. : 0;
  211. return [
  212. 'current_level' => $talent->talent_level,
  213. 'next_level' => $nextLevel,
  214. 'direct_count' => $talent->direct_count,
  215. 'direct_count_required' => $nextLevelConfig->direct_count_required,
  216. 'direct_count_progress' => $directCountProgress,
  217. 'promotion_count' => $talent->promotion_count,
  218. 'promotion_count_required' => $nextLevelConfig->promotion_count_required,
  219. 'promotion_count_progress' => $promotionCountProgress
  220. ];
  221. } catch (\Exception $e) {
  222. Log::error("获取用户达人等级进度失败: " . $e->getMessage());
  223. return [
  224. 'current_level' => 0,
  225. 'next_level' => 1,
  226. 'direct_count' => 0,
  227. 'direct_count_required' => 5,
  228. 'direct_count_progress' => 0,
  229. 'promotion_count' => 0,
  230. 'promotion_count_required' => 10,
  231. 'promotion_count_progress' => 0
  232. ];
  233. }
  234. }
  235. }