2
0

AgentService.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. // 文件路径: app/Game/AgentService.php
  3. namespace App\Game\Services;
  4. use App\Game\AgentLinks;
  5. use App\Game\AgentUser;
  6. use App\Game\AgentUserRecord;
  7. use App\Game\GlobalUserInfo;
  8. use App\Http\helper\NumConfig;
  9. use Carbon\Carbon;
  10. use DB;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Str;
  13. class AgentService
  14. {
  15. /**
  16. * @param $GlobalUID
  17. * @param $UserID
  18. * @param $code
  19. * @return AgentUser
  20. */
  21. public static function SetUserAgent($GlobalUID,$UserID,$code=null)
  22. {
  23. $agentUser=AgentUser::query()->where('GlobalUID',$GlobalUID)->first();
  24. if($agentUser){
  25. return $agentUser;
  26. }
  27. $higher1GUID = null;
  28. $higher2GUID = null;
  29. $higher3GUID = null;
  30. $higher4GUID = null;
  31. $higher1ID = 0;
  32. $higher2ID = 0;
  33. $higher3ID = 0;
  34. $higher4ID = 0;
  35. // 如果提供了分享 code
  36. if ($code) {
  37. $link = AgentLinks::getByCode($code);
  38. if ($link) {
  39. $link->increment('RegNum',1);
  40. $higher1GUID = $link->GlobalUID;
  41. $higher1ID = $link->UserID;
  42. // // 查询上级的上级
  43. // $Inviter = AgentUser::where('GlobalUID', $higher1GUID)->first();
  44. // if ($Inviter) {
  45. // $higher2GUID = $Inviter->Higher1GUID;
  46. // $higher2ID = $Inviter->Higher1ID;
  47. // if($higher2GUID){
  48. // AgentUser::query()->where("GlobalUID",$higher2GUID)->increment('downCount2',1);
  49. // }
  50. //
  51. // $higher3GUID = $Inviter->Higher2GUID;
  52. // $higher3ID = $Inviter->Higher2ID;
  53. //
  54. // if($higher3GUID){
  55. // AgentUser::query()->where("GlobalUID",$higher3GUID)->increment('downCount3',1);
  56. // }
  57. //
  58. // $higher4GUID = $Inviter->Higher3GUID;
  59. // $higher4ID = $Inviter->Higher3ID;
  60. //
  61. // if($higher4GUID){
  62. // AgentUser::query()->where("GlobalUID",$higher4GUID)->increment('downCount4',1);
  63. // }
  64. // }else{
  65. // $Inviter=AgentUser::create(['GlobalUID' => $higher1GUID,
  66. // 'UserID' => $higher1ID]);
  67. // $Inviter->increment('downCount1',1);
  68. //
  69. // }
  70. }
  71. }
  72. // 插入 AgentUser 记录
  73. $agentUser=AgentUser::create([
  74. 'GlobalUID' => $GlobalUID,
  75. 'UserID' => $UserID,
  76. 'LinkCode' => $code ?? '',
  77. 'Higher1GUID' => $higher1GUID,
  78. 'Higher2GUID' => $higher2GUID,
  79. 'Higher3GUID' => $higher3GUID,
  80. 'Higher4GUID' => $higher4GUID,
  81. 'Higher1ID' => $higher1ID,
  82. 'Higher2ID' => $higher2ID,
  83. 'Higher3ID' => $higher3ID,
  84. 'Higher4ID' => $higher4ID,
  85. 'downCount1' => 0,
  86. 'downCount2' => 0,
  87. 'downCount3' => 0,
  88. 'downCount4' => 0,
  89. ]);
  90. // 为新用户创建默认的 Campaign 和 Code
  91. AgentLinks::getDefaultCampaign($GlobalUID);
  92. return $agentUser;
  93. }
  94. public static function recordPerformance($userId, $amount=0,$type=2)
  95. {
  96. try {
  97. // 获取用户的邀请链
  98. $user = AgentUser::query()->where('UserID', $userId)->first();
  99. if(!$user){
  100. $guser=GlobalUserInfo::getGameUserInfo('UserID',$userId);
  101. if(!$guser)return;
  102. $user=self::SetUserAgent($guser->GlobalUID,$userId);
  103. }
  104. // $invitationChain= [];
  105. // if($user->Higher1ID)$invitationChain[]=[$user->Higher1GUID,$user->Higher1ID];
  106. // if($user->Higher2ID)$invitationChain[]=[$user->Higher2GUID,$user->Higher2ID];
  107. // if($user->Higher3ID)$invitationChain[]=[$user->Higher3GUID,$user->Higher3ID];
  108. // if($user->Higher4ID)$invitationChain[]=[$user->Higher4GUID,$user->Higher4ID];
  109. // $hasReward=$user->TotalReward;
  110. // 更新 UserAgent 表
  111. if($user->Higher1ID) {
  112. $update = [];
  113. $reward = 10;
  114. $activity = DB::table('agent.dbo.ActivityTurnplate')
  115. ->where('UserID', $user->Higher1ID)
  116. ->first();
  117. if(!$activity){
  118. return false;
  119. }
  120. if ($type == 1){
  121. if($activity->RegisterRewardLimit<200){
  122. $reward = rand(10,min(50,max(200-intval($activity->RegisterRewardLimit),10)));
  123. $update = ['Amount' => $activity->Amount+$reward,'RegisterRewardLimit' => $activity->RegisterRewardLimit+$reward];
  124. }else{
  125. return false;
  126. }
  127. }else{
  128. $reward = rand(20,50);
  129. $update = ['Amount' => $activity->Amount+$reward];
  130. }
  131. // BonusLog 入库
  132. DB::connection('write')->table('agent.dbo.ShareBonusLog')->insert([
  133. 'UserID' => $userId,
  134. 'Higher1ID' => $user->Higher1ID,
  135. 'Reward' => $reward,
  136. 'Type' => $type,
  137. 'ActivityID' => $activity->id,
  138. 'CreateTime' => Carbon::now()->format('Y-m-d H:i:s'),
  139. ]);
  140. if ($update)
  141. DB::connection('write')->table('agent.dbo.ActivityTurnplate')->where('UserID', $user->Higher1ID)->update($update);
  142. // $user->increment('TotalReward', $amount);
  143. }
  144. return true;
  145. } catch (\Exception $exception){
  146. return false;
  147. }
  148. // 遍历上级邀请者并更新业绩
  149. // foreach ($invitationChain as $level => [$Guid,$Uid]) {
  150. //
  151. // $_real_level=$level+1;
  152. // $HigerGUID=null;
  153. // $HigerID=0;
  154. // if($_real_level<count($invitationChain)){
  155. // [$HigerGUID,$HigerID]=$invitationChain[$_real_level];
  156. // }
  157. //
  158. // // 记录到 AgentUserRecord 表
  159. // AgentUserRecord::findOrCreateAndUpdate($Guid,$Uid,$_real_level, $amount,$HigerGUID,$HigerID);
  160. //
  161. //
  162. // }
  163. // 1、每个用户充值后,您可以获得1R$
  164. // 2、当用户充值总额超过100R$,他们被视为优质用户,每个优质用户你可以获得5R$
  165. // 3、您将收到所有用户存入金额1%的现金返还,每个用户最多可以获得100R$
  166. if($user->Higher1GUID){
  167. $parentUser=AgentUser::query()->where('GlobalUID', $user->Higher1GUID)->first();
  168. //$parentUser->RewardLimit 默认值为100,可以调节
  169. if($parentUser&&$user->LinkRewards<$parentUser->RewardLimit*NumConfig::NUM_VALUE) {
  170. //找到具体活动链接
  171. if ($user->LinkCode) {
  172. $link = AgentLinks::getByCode($user->LinkCode);
  173. } else {
  174. $link = AgentLinks::getDefaultCampaign($user->Higher1GUID);
  175. }
  176. //新付费
  177. if ($hasReward == 0) {
  178. $link->increment('PayUserNum', 1);
  179. $link->increment('PayRewards', 1 * NumConfig::NUM_VALUE);
  180. $user->increment('LinkRewards', 1 * NumConfig::NUM_VALUE);
  181. }
  182. //超过100R$
  183. if ($hasReward < 100 * NumConfig::NUM_VALUE && $hasReward + $amount >= 100 * NumConfig::NUM_VALUE) {
  184. $link->increment('PayRewards', 5 * NumConfig::NUM_VALUE);
  185. $user->increment('LinkRewards', 5 * NumConfig::NUM_VALUE);
  186. }
  187. //1% $user->RewardRate 默认值100 /10000 =1%
  188. $link->increment('PayTotal', $amount);
  189. $link->increment('PayRewards', $amount *$user->RewardRate / 10000);
  190. $user->increment('LinkRewards', $amount *$user->RewardRate / 10000);
  191. }
  192. }
  193. }
  194. public static function getShareLink($GlobalUID=null,$ActName='Default')
  195. {
  196. if(!$GlobalUID){
  197. if(GlobalUserInfo::$me){
  198. $GlobalUID=GlobalUserInfo::$me->GlobalUID;
  199. }else{
  200. $GlobalUID='';
  201. }
  202. }
  203. $h5link = env('H5_LINK', 'https://www.usslot777.com/');
  204. $httpserver = $_SERVER['HTTP_ORIGIN']
  205. ?? ($_SERVER['HTTP_REFERER'] ?? $h5link);
  206. $httpserver = rtrim($httpserver, '/');
  207. $h5link = $httpserver.'/';
  208. if($ActName=='WheelFree100') {
  209. $link = $h5link .'?act='. self::encodeAct($GlobalUID, $ActName);
  210. }else{
  211. $link = $h5link .'act='.self::encodeAct($GlobalUID, $ActName);
  212. }
  213. return $link;
  214. }
  215. public static function encodeAct($GlobalUID,$ActName)
  216. {
  217. if(empty($GlobalUID))return Str::random(6);
  218. $link=AgentLinks::getCampaign($GlobalUID,$ActName);
  219. return $link->Code;
  220. }
  221. /**
  222. * @param Request $request
  223. * @return AgentLinks|int
  224. */
  225. static public function decodeAct(Request $request)
  226. {
  227. $act = $request->input("act","");
  228. if(empty($act)){
  229. return 0;
  230. }
  231. // $act=self::customDecrypt($act);
  232. // $act=explode("|",$act);
  233. // if(count($act)<2){
  234. // return -1;
  235. // }
  236. // [$ActName,$SpreadID,$SpreadGUID]=$act;
  237. $agentLink=AgentLinks::getByCode($act);
  238. if(!$agentLink)return -1;
  239. return $agentLink;
  240. }
  241. private static function getLevelName($level)
  242. {
  243. $levels = ['top', 'second', 'third', 'fourth'];
  244. return $levels[$level] ?? 'unknown';
  245. }
  246. }