PromotionProfitService.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace App\Module\Promotion\Services;
  3. use App\Module\Promotion\Enums\PROFIT_SOURCE_TYPE;
  4. use App\Module\Promotion\Logics\ReferralLogic;
  5. use App\Module\Promotion\Logics\TalentLogic;
  6. use App\Module\Promotion\Logics\PromotionProfitLogic;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * 团队收益服务类
  10. *
  11. * 对外提供团队收益相关的服务,包括记录收益、获取收益记录、
  12. * 统计收益等功能。该类对外提供服务,内部调用逻辑层实现。
  13. */
  14. class PromotionProfitService
  15. {
  16. /**
  17. * 记录农场收获收益
  18. *
  19. * @param int $userId 产生收益的用户ID
  20. * @param int $sourceId 收益来源ID(如收获记录ID)
  21. * @param int $itemId 物品ID
  22. * @param int $amount 收益数量
  23. * @return bool
  24. */
  25. public static function recordFarmHarvestProfit(int $userId, int $sourceId, int $itemId, int $amount): bool
  26. {
  27. try {
  28. $referralLogic = new ReferralLogic();
  29. $talentLogic = new TalentLogic($referralLogic);
  30. $promotionProfitLogic = new PromotionProfitLogic($referralLogic, $talentLogic);
  31. return $promotionProfitLogic->calculateAndRecordProfit(
  32. $userId,
  33. PROFIT_SOURCE_TYPE::FARM_HARVEST,
  34. $sourceId,
  35. $itemId,
  36. $amount
  37. );
  38. } catch (\Exception $e) {
  39. Log::error("记录农场收获收益失败: " . $e->getMessage());
  40. return false;
  41. }
  42. }
  43. /**
  44. * 记录任务完成收益
  45. *
  46. * @param int $userId 产生收益的用户ID
  47. * @param int $sourceId 收益来源ID(如任务记录ID)
  48. * @param int $itemId 物品ID
  49. * @param int $amount 收益数量
  50. * @return bool
  51. */
  52. public static function recordTaskCompleteProfit(int $userId, int $sourceId, int $itemId, int $amount): bool
  53. {
  54. try {
  55. $referralLogic = new ReferralLogic();
  56. $talentLogic = new TalentLogic($referralLogic);
  57. $promotionProfitLogic = new PromotionProfitLogic($referralLogic, $talentLogic);
  58. return $promotionProfitLogic->calculateAndRecordProfit(
  59. $userId,
  60. PROFIT_SOURCE_TYPE::TASK_COMPLETE,
  61. $sourceId,
  62. $itemId,
  63. $amount
  64. );
  65. } catch (\Exception $e) {
  66. Log::error("记录任务完成收益失败: " . $e->getMessage());
  67. return false;
  68. }
  69. }
  70. /**
  71. * 记录物品出售收益
  72. *
  73. * @param int $userId 产生收益的用户ID
  74. * @param int $sourceId 收益来源ID(如出售记录ID)
  75. * @param int $itemId 物品ID
  76. * @param int $amount 收益数量
  77. * @return bool
  78. */
  79. public static function recordItemSellProfit(int $userId, int $sourceId, int $itemId, int $amount): bool
  80. {
  81. try {
  82. $referralLogic = new ReferralLogic();
  83. $talentLogic = new TalentLogic($referralLogic);
  84. $promotionProfitLogic = new PromotionProfitLogic($referralLogic, $talentLogic);
  85. return $promotionProfitLogic->calculateAndRecordProfit(
  86. $userId,
  87. PROFIT_SOURCE_TYPE::ITEM_SELL,
  88. $sourceId,
  89. $itemId,
  90. $amount
  91. );
  92. } catch (\Exception $e) {
  93. Log::error("记录物品出售收益失败: " . $e->getMessage());
  94. return false;
  95. }
  96. }
  97. /**
  98. * 获取用户的收益记录
  99. *
  100. * @param int $userId 用户ID
  101. * @param string|null $sourceType 收益来源类型
  102. * @param int $page 页码
  103. * @param int $pageSize 每页数量
  104. * @return array
  105. */
  106. public static function getUserProfits(int $userId, ?string $sourceType = null, int $page = 1, int $pageSize = 20): array
  107. {
  108. try {
  109. $referralLogic = new ReferralLogic();
  110. $talentLogic = new TalentLogic($referralLogic);
  111. $promotionProfitLogic = new PromotionProfitLogic($referralLogic, $talentLogic);
  112. return $promotionProfitLogic->getUserProfits($userId, $sourceType, $page, $pageSize);
  113. } catch (\Exception $e) {
  114. Log::error("获取用户收益记录失败: " . $e->getMessage());
  115. return [
  116. 'total' => 0,
  117. 'page' => $page,
  118. 'page_size' => $pageSize,
  119. 'total_pages' => 0,
  120. 'profits' => []
  121. ];
  122. }
  123. }
  124. /**
  125. * 统计用户的收益
  126. *
  127. * @param int $userId 用户ID
  128. * @param string|null $sourceType 收益来源类型
  129. * @param int|null $itemId 物品ID
  130. * @return array
  131. */
  132. public static function sumUserProfits(int $userId, ?string $sourceType = null, ?int $itemId = null): array
  133. {
  134. try {
  135. $referralLogic = new ReferralLogic();
  136. $talentLogic = new TalentLogic($referralLogic);
  137. $promotionProfitLogic = new PromotionProfitLogic($referralLogic, $talentLogic);
  138. return $promotionProfitLogic->sumUserProfits($userId, $sourceType, $itemId);
  139. } catch (\Exception $e) {
  140. Log::error("统计用户收益失败: " . $e->getMessage());
  141. return [
  142. 'direct_profit' => 0,
  143. 'indirect_profit' => 0,
  144. 'total_profit' => 0
  145. ];
  146. }
  147. }
  148. /**
  149. * 获取收益分成规则
  150. *
  151. * @param string $sourceType 收益来源类型
  152. * @return array|null
  153. */
  154. public static function getProfitRule(string $sourceType): ?array
  155. {
  156. try {
  157. $referralLogic = new ReferralLogic();
  158. $talentLogic = new TalentLogic($referralLogic);
  159. $promotionProfitLogic = new PromotionProfitLogic($referralLogic, $talentLogic);
  160. $rule = $promotionProfitLogic->getProfitRule($sourceType);
  161. if (!$rule) {
  162. return null;
  163. }
  164. return [
  165. 'source_type' => $rule->source_type,
  166. 'direct_profit_rate' => $rule->direct_profit_rate,
  167. 'max_indirect_level' => $rule->max_indirect_level,
  168. 'status' => $rule->status,
  169. 'rules' => is_array($rule->rules) ? $rule->rules : json_decode($rule->rules, true)
  170. ];
  171. } catch (\Exception $e) {
  172. Log::error("获取收益分成规则失败: " . $e->getMessage());
  173. return null;
  174. }
  175. }
  176. /**
  177. * 获取所有收益来源类型
  178. *
  179. * @return array
  180. */
  181. public static function getAllProfitSourceTypes(): array
  182. {
  183. return [
  184. [
  185. 'type' => PROFIT_SOURCE_TYPE::FARM_HARVEST,
  186. 'name' => '农场收获',
  187. 'description' => '来自Farm模块的作物收获'
  188. ],
  189. [
  190. 'type' => PROFIT_SOURCE_TYPE::TASK_COMPLETE,
  191. 'name' => '任务完成',
  192. 'description' => '来自Task模块的任务奖励'
  193. ],
  194. [
  195. 'type' => PROFIT_SOURCE_TYPE::ITEM_SELL,
  196. 'name' => '物品出售',
  197. 'description' => '来自GameItems模块的物品出售'
  198. ]
  199. ];
  200. }
  201. }