OrderServices.php 20 KB

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