OrderServices.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <?php
  2. namespace App\Services;
  3. use App\dao\Estatisticas\RechargeWithDraw;
  4. use App\Facade\TableName;
  5. use App\Game\GlobalUserInfo;
  6. use App\Game\Services\AgentService;
  7. use App\Http\Controllers\Api\AgentController;
  8. use App\Http\helper\Helper;
  9. use App\Http\helper\HttpCurl;
  10. use App\Http\helper\NumConfig;
  11. use App\Jobs\AfEvent;
  12. use App\Models\AgentUser;
  13. use App\Models\RecordScoreInfo;
  14. use App\Models\RecordUserDataStatistics;
  15. use App\Services\HolidayWheelService;
  16. use App\Util;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Log;
  19. use Illuminate\Support\Facades\Redis;
  20. class OrderServices
  21. {
  22. public $FirstCharge = 30;
  23. public $FirstChargeGive = 20;
  24. public $FirstGiftID = 301;
  25. private $first_pay = null;
  26. public function __construct()
  27. {
  28. }
  29. /**
  30. * 获取支付金额
  31. * @param int $GiftsID 礼包ID
  32. * @param int $user_id 用户ID
  33. * @param int $payAmt 支付金额(元)
  34. * @return array|void [$give, $favorable_price, $Recharge, $czReason, $cjReason] 赠送金额,赠送金额+充值金额,充值金额,充值原因,彩金原因
  35. */
  36. public function getPayInfo($GiftsID, $user_id, $payAmt)
  37. {
  38. $give = $czReason = $cjReason = $Recharge = $favorable_price = $second_give = 0;
  39. $shouldCreateGiftRecord = false; // 是否需要创建首充礼包记录
  40. // 判断是不是首冲礼包
  41. if (!empty($GiftsID)) {
  42. if ($GiftsID == 301) { // 礼包--首冲
  43. // 检查用户是否已购买过首充礼包
  44. $hasPurchased = DB::connection('write')->table('agent.dbo.first_pay_gift_records')
  45. ->where('user_id', $user_id)
  46. ->exists();
  47. if ($hasPurchased) {
  48. // 已购买首充礼包,走普通充值逻辑(只加本金)
  49. $Recharge = $payAmt;
  50. $give = 0;
  51. $favorable_price = $Recharge + $give;
  52. $czReason = 1;
  53. $cjReason = 45;
  54. } else {
  55. // 首次购买首充礼包,走赠送逻辑(立即获得bonus_instantly%)
  56. $Gifts = DB::connection('write')->table('agent.dbo.recharge_gift')->where('gift_id', $GiftsID)->first();
  57. $favorable_price = round($Gifts->bonus_instantly*$payAmt/100,2);
  58. $give = $favorable_price-$payAmt;
  59. $Recharge = $payAmt;
  60. $czReason = 50;
  61. $cjReason = 51;
  62. // 创建首充礼包记录
  63. try {
  64. $this->createFirstPayGiftRecord($user_id, $GiftsID, $payAmt);
  65. } catch (\Exception $e) {
  66. \Log::error('首充礼包记录创建失败', [
  67. 'user_id' => $user_id,
  68. 'gift_id' => $GiftsID,
  69. 'error' => $e->getMessage()
  70. ]);
  71. }
  72. }
  73. }if ($GiftsID == 302) { // 破产礼包
  74. // 首次购买首充礼包,走赠送逻辑(立即获得bonus_instantly%)
  75. $Gifts = DB::connection('write')->table('agent.dbo.recharge_gift')->where('gift_id', $GiftsID)->where('recommend', $payAmt)->first();
  76. if($Gifts){
  77. $favorable_price = round($Gifts->bonus_instantly*$payAmt/100,2);
  78. $give = $favorable_price-$payAmt;
  79. $Recharge = $payAmt;
  80. $czReason = 50;
  81. $cjReason = 51;
  82. }else{
  83. $Recharge = $payAmt;
  84. $give = 0;
  85. $favorable_price = $Recharge + $give;
  86. $czReason = 1;
  87. $cjReason = 45;
  88. }
  89. }else if ($GiftsID > 400) {
  90. $Status = 1;
  91. $recharge_gear = DB::connection('write')->table('agent.dbo.recharge_gear')->where('status', $Status)->where('money', $payAmt)->select('favorable_price', 'give')->first();
  92. if(!$recharge_gear){
  93. $Recharge=$payAmt;
  94. $give=0;
  95. }else{
  96. $Recharge = $recharge_gear->favorable_price;
  97. $give = $recharge_gear->give;
  98. }
  99. $favorable_price = $Recharge + $give;
  100. $czReason = 1;
  101. $cjReason = 45;
  102. }
  103. } else { // 普通订单
  104. $Status = 1;
  105. $recharge_gear = DB::connection('write')->table('agent.dbo.recharge_gear')->where('status', $Status)->where('money', $payAmt)->select('favorable_price', 'give')->first();
  106. if(!$recharge_gear){
  107. $Recharge=$payAmt;
  108. $give=0;
  109. }else{
  110. $Recharge = $recharge_gear->favorable_price;
  111. $give = $recharge_gear->give;
  112. }
  113. $favorable_price = $Recharge + $give;
  114. $czReason = 1;
  115. $cjReason = 45;
  116. }
  117. return [$give, $favorable_price, $Recharge, $czReason, $cjReason, $second_give];
  118. }
  119. /**
  120. * 添加玩家充值记录
  121. * @param $user_id
  122. * @param $payAmt
  123. * @param $favorable_price
  124. * @param $order_sn
  125. * @param $GiftsID
  126. * @param $Recharge
  127. * @param $czReason
  128. * @param $give
  129. * @param $cjReason
  130. * @param $AdId
  131. * @param $eventType
  132. */
  133. public function addRecord($user_id, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType)
  134. {
  135. if ($payAmt > 0) {
  136. // 增加玩家充值金额
  137. $query = DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  138. ->where('UserID', $user_id)
  139. ->value('Recharge');
  140. if ($query) {
  141. DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  142. ->where('UserID', $user_id)
  143. ->increment('Recharge', $payAmt);
  144. } else {
  145. DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  146. ->where('UserID', $user_id)
  147. ->insert(['Recharge' => $payAmt, 'UserID' => $user_id]);
  148. // 首次充值:金币银币切换 + 数据清理
  149. /*
  150. * 金币银币切换 GameScoreInfo 将score的数据复制到 InsureScore 把Score 字段置0 把 ScoreChange字段设置成1
  151. * 清理用户的数据
  152. * GameScoreInfo的MaxScore MaxWinscore清0
  153. * RecordUserTotalStatistics 表 数据清0
  154. * RecordUserDataStatisticsNew 表数据 清0
  155. * RecordUserGameCount 关于用户的数据删除
  156. *
  157. */
  158. try {
  159. $this->switchToInsureScoreAndCleanData($user_id);
  160. } catch (\Exception $e) {
  161. \Log::error('首次充值-金币银币切换失败', [
  162. 'user_id' => $user_id,
  163. 'error' => $e->getMessage()
  164. ]);
  165. }
  166. }
  167. //在这里更新 RecordUserTotalStatistics 数据 将LastRechargeValue字段更新为payAmt 如果不存在则插入
  168. DB::connection('write')->table('QPRecordDB.dbo.RecordUserTotalStatistics')
  169. ->updateOrInsert(['UserID' => $user_id], ['LastRechargeValue' => $payAmt]);
  170. // 节假日大转盘:根据充值金额增加转盘次数(首充礼包/破产礼包不计入)
  171. try {
  172. (new HolidayWheelService())->grantTimesOnRecharge($user_id, $payAmt, (int)$GiftsID);
  173. } catch (\Throwable $e) {
  174. \Log::error('holiday wheel grant times failed', [
  175. 'user_id' => $user_id,
  176. 'payAmt' => $payAmt,
  177. 'GiftsID' => $GiftsID,
  178. 'error' => $e->getMessage(),
  179. ]);
  180. }
  181. // 圣诞大转盘:根据充值金额增加转盘次数(首充礼包/破产礼包不计入)
  182. try {
  183. (new \App\Services\ChristmasWheelService())->grantTimesOnRecharge($user_id, $payAmt, (int)$GiftsID);
  184. } catch (\Throwable $e) {
  185. \Log::error('christmas wheel grant times failed', [
  186. 'user_id' => $user_id,
  187. 'payAmt' => $payAmt,
  188. 'GiftsID' => $GiftsID,
  189. 'error' => $e->getMessage(),
  190. ]);
  191. }
  192. }
  193. if ($Recharge > 0) {
  194. // 添加日志记录表
  195. StoredProcedure::addPlatformData($user_id, 3, $Recharge * NumConfig::NUM_VALUE);
  196. // 增加用户金币变化记录
  197. $AfterScore = RecordScoreInfo::addScore($user_id, ($Recharge * NumConfig::NUM_VALUE), $czReason); #充值
  198. if ($AfterScore) {
  199. RecordScoreInfo::addScore($user_id, ($give * NumConfig::NUM_VALUE), $cjReason, $AfterScore); #赠送彩金
  200. }
  201. }
  202. $favorable_price = (int) round($favorable_price * NumConfig::NUM_VALUE);
  203. $firstScore = DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user_id)->value('Score');
  204. $Score = $favorable_price + $firstScore;
  205. if ($payAmt > 0) {
  206. // 记录订单变化后金币
  207. DB::connection('write')->table('agent.dbo.order')
  208. ->where('order_sn', $order_sn)
  209. ->update(['after_amount' => $Score]);
  210. // 增加用户充值变化值
  211. RecordUserDataStatistics::updateOrAdd($user_id, 0, $payAmt);
  212. }
  213. // 不是周卡的时候执行
  214. if ($GiftsID < 100 || $GiftsID >= 200) {
  215. // 增加用户金币
  216. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user_id)->increment('Score', $favorable_price);
  217. }
  218. // 充值推广佣金
  219. // (new AgentUser())->reward($user_id, $payAmt, $order_sn);
  220. //
  221. // 周卡 -- 执行存储过程
  222. // if (isset($GiftsID) && $GiftsID > 100 && $GiftsID < 200) {
  223. // $CardID = (int)$GiftsID - 100;
  224. // StoredProcedure::BuyMonthCard($user_id, $CardID, $order_sn);
  225. // // 开始执行时间
  226. // $startTime = Helper::millisecond();
  227. // Log::info('GSP_GP_BuyMonthCard 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  228. // }
  229. //设置第二天要领取
  230. // if (isset($GiftsID) && $GiftsID >= 200 && $GiftsID < 300 ) {
  231. // $first = DB::table('agent.dbo.recharge_gear')->where('gift_id', $GiftsID)->select('gift_id', 'money', 'give', 'favorable_price', 'second_give')->first();
  232. // if ($first && $first->second_give > 0) {
  233. // $fpkey = 'Firstpay_' . $user_id;
  234. // $data = (array)$first;
  235. // $data['buytime'] = time();
  236. // $data['czReason'] = $czReason;
  237. // $data['cjReason'] = $cjReason;
  238. // Redis::set($fpkey, json_encode($data));
  239. // DB::table(TableName::agent() . 'guide_payment')->updateOrInsert([
  240. // 'UserID' => $user_id,
  241. // 'GiftID' => $GiftsID,
  242. // 'CreateTime' => now()
  243. // ], ['UserID' => $user_id]);
  244. // }
  245. // }
  246. //设置第二天要领取
  247. // if (!empty($GiftsID) && $GiftsID >= 300 && $GiftsID < 400 ) {
  248. // Redis::del('repay_temp_'.$user_id);
  249. // }
  250. // 数据统计后台 -- 充值记录添加
  251. (new RechargeWithDraw())->recharge($user_id, $payAmt);
  252. // (new RechargeWithDraw())->recharge($user_id, $Recharge);
  253. if ($AdId && $payAmt) AfEvent::dispatch([$user_id, $payAmt, $AdId, $eventType]);
  254. try {
  255. //新邀请
  256. //(new AgentController())->processDeposit($user_id, $payAmt,$order_sn);
  257. AgentService::recordPerformance($user_id, $payAmt * NumConfig::NUM_VALUE);
  258. } catch (\Exception $exception) {
  259. Util::WriteLog("AgentService", $exception->getTraceAsString());
  260. }
  261. return [$Score];
  262. }
  263. /**
  264. * 执行存储过程
  265. * @param $user_id
  266. * @param $payAmt
  267. * @param $favorable_price
  268. * @param $Score
  269. * @param $GiftsID
  270. */
  271. public function storedProcedure($user_id, $payAmt, $favorable_price, $Score, $GiftsID)
  272. {
  273. // 开始执行时间
  274. $startTime = Helper::millisecond();
  275. // 执行存储过程 -- 防刷机制
  276. StoredProcedure::SetUserTabType($user_id);
  277. Log::info('GSP_GP_SetUserTabType12 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  278. # 单控标签 -- 执行存储过程
  279. StoredProcedure::user_label($user_id, 1, $payAmt);
  280. Log::info('CheckAccountsLabel 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  281. // 服务器通知
  282. // $url = config('transfer.stock')['url'] . 'notifyPay';
  283. // Log::info('中转服参数 ' . json_encode([
  284. // 'userid' => $user_id, 'getScore' => $favorable_price, 'score' => $Score,
  285. // 'giftsid' => empty($GiftsID) ? 0 : $GiftsID,
  286. // 'url' => $url
  287. // ]));
  288. // $res = (new HttpCurl())->service($url, ['userid' => $user_id, 'getScore' => $favorable_price, 'score' => $Score, 'giftsid' => empty($GiftsID) ? 0 : $GiftsID]);
  289. //
  290. // Log::info('中转服 执行时间:' . ((Helper::millisecond() - $startTime) / 1000), [
  291. // 'res' => $res,
  292. // ]);
  293. // AF 事件
  294. // (new AppflyerEvent())->event($user_id, '', 'af_purchase_new', $payAmt);
  295. // Log::info('AF 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  296. }
  297. /**
  298. * 首次充值:金币银币切换 + 数据清理
  299. * @param int $user_id 用户ID
  300. */
  301. private function switchToInsureScoreAndCleanData($user_id)
  302. {
  303. \Log::info('开始执行金币银币切换', ['user_id' => $user_id]);
  304. try {
  305. // 1. GameScoreInfo: 将Score的数据复制到InsureScore,把Score字段置0,把ScoreChange字段设置成1
  306. $scoreInfo = DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')
  307. ->where('UserID', $user_id)
  308. ->first();
  309. if ($scoreInfo) {
  310. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')
  311. ->where('UserID', $user_id)
  312. ->update([
  313. 'InsureScore' => max($scoreInfo->Score,4000), // 将Score复制到InsureScore
  314. 'Score' => 0, // Score置0
  315. 'ScoreChange' => 1, // ScoreChange设置成1
  316. 'MaxScore' => 0, // MaxScore清0
  317. 'MaxWinScore' => 0 // MaxWinScore清0
  318. ]);
  319. \Log::info('GameScoreInfo切换成功', [
  320. 'user_id' => $user_id,
  321. 'original_score' => $scoreInfo->Score,
  322. 'insure_score' => $scoreInfo->Score
  323. ]);
  324. }
  325. // 2. RecordUserTotalStatistics 表数据清0
  326. $totalStats = DB::connection('write')->table('QPRecordDB.dbo.RecordUserTotalStatistics')
  327. ->where('UserID', $user_id)
  328. ->first();
  329. if ($totalStats) {
  330. DB::connection('write')->table('QPRecordDB.dbo.RecordUserTotalStatistics')
  331. ->where('UserID', $user_id)
  332. ->update([
  333. 'WinInning' => 0,
  334. 'LostInning' => 0,
  335. 'Revenue' => 0,
  336. 'WinScore' => 0,
  337. 'LostScore' => 0,
  338. 'Handsel' => 0,
  339. 'DrawBase' => 0,
  340. 'TotalBet' => 0,
  341. // 'TotalScore' => 0,
  342. 'MaxDrawBase' => 0,
  343. 'MaxScore' => 0,
  344. 'MaxWinScore' => 0,
  345. 'Rounds' => 0,
  346. 'ProtectedRounds' => 0
  347. ]);
  348. \Log::info('RecordUserTotalStatistics清0成功', ['user_id' => $user_id]);
  349. }
  350. // 3. RecordUserDataStatisticsNew 表数据清0
  351. DB::connection('write')->table('QPRecordDB.dbo.RecordUserDataStatisticsNew')
  352. ->where('UserID', $user_id)
  353. ->delete();
  354. \Log::info('RecordUserDataStatisticsNew清0成功', ['user_id' => $user_id]);
  355. // 4. RecordUserGameCount 关于用户的数据删除
  356. DB::connection('write')->table('QPRecordDB.dbo.RecordUserGameCount')
  357. ->where('UserID', $user_id)
  358. ->delete();
  359. \Log::info('RecordUserGameCount清除成功', ['user_id' => $user_id]);
  360. \Log::info('金币银币切换完成', [
  361. 'user_id' => $user_id,
  362. 'insure_score' => $scoreInfo->Score ?? 0
  363. ]);
  364. } catch (\Exception $e) {
  365. \Log::error('金币银币切换异常', [
  366. 'user_id' => $user_id,
  367. 'error' => $e->getMessage(),
  368. 'trace' => $e->getTraceAsString()
  369. ]);
  370. throw $e;
  371. }
  372. }
  373. /**
  374. * 创建首充礼包记录
  375. */
  376. public function createFirstPayGiftRecord($user_id, $gift_id, $payAmt)
  377. {
  378. // 检查是否已有记录
  379. $existing = DB::connection('write')->table('agent.dbo.first_pay_gift_records')
  380. ->where('user_id', $user_id)
  381. ->first();
  382. if ($existing) {
  383. \Log::info('首充礼包记录已存在', ['user_id' => $user_id]);
  384. return;
  385. }
  386. // 获取礼包配置
  387. $giftConfig = DB::connection('write')->table('agent.dbo.recharge_gift')
  388. ->where('gift_id', $gift_id)
  389. ->first();
  390. if (!$giftConfig) {
  391. \Log::error('首充礼包配置不存在', ['gift_id' => $gift_id]);
  392. return;
  393. }
  394. // 计算金额
  395. $totalBonusAmount = round($giftConfig->total_bonus * $payAmt / 100, 2);
  396. $bonusInstantlyAmount = round($giftConfig->bonus_instantly * $payAmt / 100, 2);
  397. // 解析JSON数据
  398. $dayRewards = $giftConfig->day_rewards ? json_decode($giftConfig->day_rewards, true) : null;
  399. $bettingBonus = $giftConfig->betting_bonus ? json_decode($giftConfig->betting_bonus, true) : null;
  400. $bettingTask = $giftConfig->betting_task ? json_decode($giftConfig->betting_task, true) : null;
  401. // 计算各部分金额
  402. $dayRewardsTotal = $dayRewards ? round($dayRewards['total_bonus'] * $payAmt / 100, 2) : 0;
  403. $bettingBonusTotal = $bettingBonus ? round($bettingBonus['total_bonus'] * $payAmt / 100, 2) : 0;
  404. $bettingTaskTotal = $bettingTask ? round($bettingTask['total_bonus'] * $payAmt / 100, 2) : 0;
  405. // 创建记录
  406. $data = [
  407. 'user_id' => $user_id,
  408. 'gift_id' => $gift_id,
  409. 'pay_amount' => $payAmt,
  410. 'total_bonus' => $totalBonusAmount,
  411. 'bonus_instantly' => $bonusInstantlyAmount,
  412. 'gift_name' => $giftConfig->gift_name,
  413. // 每日奖励
  414. 'day_rewards_total' => $dayRewardsTotal,
  415. 'day_rewards_claimed' => 0,
  416. 'day_rewards_data' => $giftConfig->day_rewards,
  417. 'day_last_claim_date' => null,
  418. // 下注奖励
  419. 'betting_bonus_total' => $bettingBonusTotal,
  420. 'betting_bonus_claimed' => 0,
  421. 'betting_bonus_data' => $giftConfig->betting_bonus,
  422. 'betting_current_bet' => 0,
  423. // 下注任务
  424. 'betting_task_total' => $bettingTaskTotal,
  425. 'betting_task_claimed' => 0,
  426. 'betting_task_data' => $giftConfig->betting_task,
  427. 'created_at' => date('Y-m-d H:i:s'),
  428. 'updated_at' => date('Y-m-d H:i:s')
  429. ];
  430. DB::connection('write')->table('agent.dbo.first_pay_gift_records')->insert($data);
  431. $dayRewardsTotal = $dayRewards ? round($dayRewards['total_bonus'] * $payAmt / 100, 2) : 0;
  432. $bettingBonusTotal = $bettingBonus ? round($bettingBonus['total_bonus'] * $payAmt / 100, 2) : 0;
  433. $bettingTaskTotal = $bettingTask ? round($bettingTask['total_bonus'] * $payAmt / 100, 2) : 0;
  434. \Log::info('首充礼包记录创建成功', [
  435. 'user_id' => $user_id,
  436. 'gift_id' => $gift_id,
  437. 'pay_amount' => $payAmt,
  438. 'total_bonus' => $totalBonusAmount,
  439. 'rw' => [
  440. $dayRewardsTotal,
  441. $bettingBonusTotal,
  442. $bettingTaskTotal,
  443. round($dayRewards['total_bonus'] * $payAmt / 100, 2),
  444. round($bettingBonus['total_bonus'] * $payAmt / 100, 2),
  445. round($bettingTask['total_bonus'] * $payAmt / 100, 2)
  446. ]
  447. ]);
  448. }
  449. }