OrderServices.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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 == 303) { // 每日首充礼包
  90. // 检查用户今日是否已充值过每日首充礼包
  91. $todayStart = date('Y-m-d') . ' 00:00:00';
  92. $todayEnd = date('Y-m-d') . ' 23:59:59';
  93. $todayRecharge = DB::connection('write')->table('agent.dbo.order')
  94. ->where('user_id', $user_id)
  95. ->where('GiftsID', 303)
  96. ->where('pay_status', 1)
  97. ->where('pay_at', '>=', $todayStart)
  98. ->where('pay_at', '<=', $todayEnd)
  99. ->first();
  100. if ($todayRecharge) {
  101. // 今日已充值过,只发放本金(不发放奖励)
  102. $Recharge = $payAmt;
  103. $give = 0;
  104. $favorable_price = $Recharge + $give;
  105. $czReason = 1;
  106. $cjReason = 45;
  107. } else {
  108. // 今日未充值过,按照礼包配置的比例发放
  109. $Gifts = DB::connection('write')->table('agent.dbo.recharge_gift')
  110. ->where('gift_id', $GiftsID)
  111. ->where('recommend', $payAmt)
  112. ->first();
  113. if($Gifts){
  114. $favorable_price = round($Gifts->bonus_instantly*$payAmt/100,2);
  115. $give = $favorable_price-$payAmt;
  116. $Recharge = $payAmt;
  117. $czReason = 50;
  118. $cjReason = 51;
  119. }else{
  120. // 如果没有找到对应的礼包配置,按照普通充值处理
  121. $Recharge = $payAmt;
  122. $give = 0;
  123. $favorable_price = $Recharge + $give;
  124. $czReason = 1;
  125. $cjReason = 45;
  126. }
  127. }
  128. }else if ($GiftsID == 304) { // 每日礼包(三档充值)
  129. // 检查用户今日是否已充值过该档位的每日礼包
  130. $todayStart = date('Y-m-d') . ' 00:00:00';
  131. $todayEnd = date('Y-m-d') . ' 23:59:59';
  132. $todayRecharge = DB::connection('write')->table('agent.dbo.order')
  133. ->where('user_id', $user_id)
  134. ->where('GiftsID', 304)
  135. ->where('amount', $payAmt) // 检查该档位金额
  136. ->where('pay_status', 1)
  137. ->where('pay_at', '>=', $todayStart)
  138. ->where('pay_at', '<=', $todayEnd)
  139. ->first();
  140. if ($todayRecharge) {
  141. // 今日已充值过该档位,只发放本金(不发放奖励)
  142. $Recharge = $payAmt;
  143. $give = 0;
  144. $favorable_price = $Recharge + $give;
  145. $czReason = 1;
  146. $cjReason = 45;
  147. } else {
  148. // 今日未充值过该档位,按照礼包配置的比例发放
  149. $Gifts = DB::connection('write')->table('agent.dbo.recharge_gift')
  150. ->where('gift_id', $GiftsID)
  151. ->where('recommend', $payAmt)
  152. ->first();
  153. if($Gifts){
  154. $favorable_price = round($Gifts->bonus_instantly*$payAmt/100,2);
  155. $give = $favorable_price-$payAmt;
  156. $Recharge = $payAmt;
  157. $czReason = 50;
  158. $cjReason = 51;
  159. }else{
  160. // 如果没有找到对应的礼包配置,按照普通充值处理
  161. $Recharge = $payAmt;
  162. $give = 0;
  163. $favorable_price = $Recharge + $give;
  164. $czReason = 1;
  165. $cjReason = 45;
  166. }
  167. }
  168. }else if ($GiftsID == 305) { // 连续未充值 VIP 新礼包
  169. // 检查用户是否已经充值过 305 礼包
  170. $hasPurchased305 = DB::connection('write')->table('agent.dbo.order')
  171. ->where('user_id', $user_id)
  172. ->where('GiftsID', 305)
  173. ->where('pay_status', 1)
  174. ->exists();
  175. if ($hasPurchased305) {
  176. // 已经充值过 305 礼包,只发放本金(不发放奖励)
  177. $Recharge = $payAmt;
  178. $give = 0;
  179. $favorable_price = $Recharge + $give;
  180. $czReason = 1;
  181. $cjReason = 45;
  182. } else {
  183. // 首次充值 305 礼包,发放 120% 奖励(固定120%,不从配置读取)
  184. $favorable_price = round($payAmt * 120 / 100, 2);
  185. $give = $favorable_price - $payAmt;
  186. $Recharge = $payAmt;
  187. $czReason = 50;
  188. $cjReason = 51;
  189. $this->createInactiveVipGiftRecord($user_id, $payAmt);
  190. }
  191. } else if ($GiftsID == 306) { // free bonus礼包
  192. $Recharge = $payAmt;
  193. $give = 0;
  194. $favorable_price = $Recharge + $give;
  195. $czReason = 50;
  196. $cjReason = 51;
  197. } else if ($GiftsID > 400) {
  198. $Status = 1;
  199. $recharge_gear = DB::connection('write')->table('agent.dbo.recharge_gear')->where('status', $Status)->where('money', $payAmt)->select('favorable_price', 'give')->first();
  200. if(!$recharge_gear){
  201. $Recharge=$payAmt;
  202. $give=0;
  203. }else{
  204. $Recharge = $recharge_gear->favorable_price;
  205. $give = $recharge_gear->give;
  206. }
  207. $favorable_price = $Recharge + $give;
  208. $czReason = 1;
  209. $cjReason = 45;
  210. }
  211. } else { // 普通订单
  212. $Status = 1;
  213. $recharge_gear = DB::connection('write')->table('agent.dbo.recharge_gear')->where('status', $Status)->where('money', $payAmt)->select('favorable_price', 'give')->first();
  214. if(!$recharge_gear){
  215. $Recharge=$payAmt;
  216. $give=0;
  217. }else{
  218. $Recharge = $recharge_gear->favorable_price;
  219. $give = $recharge_gear->give;
  220. }
  221. $favorable_price = $Recharge + $give;
  222. $czReason = 1;
  223. $cjReason = 45;
  224. }
  225. return [$give, $favorable_price, $Recharge, $czReason, $cjReason, $second_give];
  226. }
  227. /**
  228. * 添加玩家充值记录
  229. * @param $user_id
  230. * @param $payAmt
  231. * @param $favorable_price
  232. * @param $order_sn
  233. * @param $GiftsID
  234. * @param $Recharge
  235. * @param $czReason
  236. * @param $give
  237. * @param $cjReason
  238. * @param $AdId
  239. * @param $eventType
  240. */
  241. public function addRecord($user_id, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType)
  242. {
  243. if ($payAmt > 0) {
  244. // 增加玩家充值金额
  245. $query = DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  246. ->where('UserID', $user_id)
  247. ->value('Recharge');
  248. if ($query) {
  249. DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  250. ->where('UserID', $user_id)
  251. ->increment('Recharge', $payAmt);
  252. } else {
  253. DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  254. ->where('UserID', $user_id)
  255. ->insert(['Recharge' => $payAmt, 'UserID' => $user_id]);
  256. // 首次充值:金币银币切换 + 数据清理
  257. /*
  258. * 金币银币切换 GameScoreInfo 将score的数据复制到 InsureScore 把Score 字段置0 把 ScoreChange字段设置成1
  259. * 清理用户的数据
  260. * GameScoreInfo的MaxScore MaxWinscore清0
  261. * RecordUserTotalStatistics 表 数据清0
  262. * RecordUserDataStatisticsNew 表数据 清0
  263. * RecordUserGameCount 关于用户的数据删除
  264. *
  265. */
  266. try {
  267. $this->switchToInsureScoreAndCleanData($user_id);
  268. } catch (\Exception $e) {
  269. \Log::error('首次充值-金币银币切换失败', [
  270. 'user_id' => $user_id,
  271. 'error' => $e->getMessage()
  272. ]);
  273. }
  274. }
  275. //在这里更新 RecordUserTotalStatistics 数据 将LastRechargeValue字段更新为payAmt 如果不存在则插入
  276. DB::connection('write')->table('QPRecordDB.dbo.RecordUserTotalStatistics')
  277. ->updateOrInsert(['UserID' => $user_id], ['LastRechargeValue' => $payAmt]);
  278. // 节假日大转盘:根据充值金额增加转盘次数(首充礼包/破产礼包不计入)
  279. try {
  280. (new HolidayWheelService())->grantTimesOnRecharge($user_id, $payAmt, (int)$GiftsID);
  281. } catch (\Throwable $e) {
  282. \Log::error('holiday wheel grant times failed', [
  283. 'user_id' => $user_id,
  284. 'payAmt' => $payAmt,
  285. 'GiftsID' => $GiftsID,
  286. 'error' => $e->getMessage(),
  287. ]);
  288. }
  289. // 圣诞大转盘:根据充值金额增加转盘次数(首充礼包/破产礼包不计入)
  290. try {
  291. (new \App\Services\ChristmasWheelService())->grantTimesOnRecharge($user_id, $payAmt, (int)$GiftsID);
  292. } catch (\Throwable $e) {
  293. \Log::error('christmas wheel grant times failed', [
  294. 'user_id' => $user_id,
  295. 'payAmt' => $payAmt,
  296. 'GiftsID' => $GiftsID,
  297. 'error' => $e->getMessage(),
  298. ]);
  299. }
  300. }
  301. if ($Recharge > 0) {
  302. // 添加日志记录表
  303. StoredProcedure::addPlatformData($user_id, 3, $Recharge * NumConfig::NUM_VALUE);
  304. // 增加用户金币变化记录
  305. $AfterScore = RecordScoreInfo::addScore($user_id, ($Recharge * NumConfig::NUM_VALUE), $czReason); #充值
  306. if ($AfterScore) {
  307. RecordScoreInfo::addScore($user_id, ($give * NumConfig::NUM_VALUE), $cjReason, $AfterScore); #赠送彩金
  308. }
  309. }
  310. $favorable_price = (int) round($favorable_price * NumConfig::NUM_VALUE);
  311. $firstScore = DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user_id)->value('Score');
  312. $Score = $favorable_price + $firstScore;
  313. if ($payAmt > 0) {
  314. // 记录订单变化后金币
  315. DB::connection('write')->table('agent.dbo.order')
  316. ->where('order_sn', $order_sn)
  317. ->update(['after_amount' => $Score]);
  318. // 增加用户充值变化值
  319. RecordUserDataStatistics::updateOrAdd($user_id, 0, $payAmt);
  320. }
  321. // 不是周卡的时候执行
  322. if ($GiftsID < 100 || $GiftsID >= 200) {
  323. // 增加用户金币
  324. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user_id)->increment('Score', $favorable_price);
  325. }
  326. // 充值推广佣金
  327. // (new AgentUser())->reward($user_id, $payAmt, $order_sn);
  328. //
  329. // 周卡 -- 执行存储过程
  330. // if (isset($GiftsID) && $GiftsID > 100 && $GiftsID < 200) {
  331. // $CardID = (int)$GiftsID - 100;
  332. // StoredProcedure::BuyMonthCard($user_id, $CardID, $order_sn);
  333. // // 开始执行时间
  334. // $startTime = Helper::millisecond();
  335. // Log::info('GSP_GP_BuyMonthCard 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  336. // }
  337. //设置第二天要领取
  338. // if (isset($GiftsID) && $GiftsID >= 200 && $GiftsID < 300 ) {
  339. // $first = DB::table('agent.dbo.recharge_gear')->where('gift_id', $GiftsID)->select('gift_id', 'money', 'give', 'favorable_price', 'second_give')->first();
  340. // if ($first && $first->second_give > 0) {
  341. // $fpkey = 'Firstpay_' . $user_id;
  342. // $data = (array)$first;
  343. // $data['buytime'] = time();
  344. // $data['czReason'] = $czReason;
  345. // $data['cjReason'] = $cjReason;
  346. // Redis::set($fpkey, json_encode($data));
  347. // DB::table(TableName::agent() . 'guide_payment')->updateOrInsert([
  348. // 'UserID' => $user_id,
  349. // 'GiftID' => $GiftsID,
  350. // 'CreateTime' => now()
  351. // ], ['UserID' => $user_id]);
  352. // }
  353. // }
  354. //设置第二天要领取
  355. // if (!empty($GiftsID) && $GiftsID >= 300 && $GiftsID < 400 ) {
  356. // Redis::del('repay_temp_'.$user_id);
  357. // }
  358. // 数据统计后台 -- 充值记录添加
  359. (new RechargeWithDraw())->recharge($user_id, $payAmt);
  360. // (new RechargeWithDraw())->recharge($user_id, $Recharge);
  361. // 连续未充值 VIP 新礼包(gift_id=305)—— 充值成功后写入 7 日礼包记录(仅在首次充值时)
  362. // if (!empty($GiftsID) && (int)$GiftsID === 305 && $payAmt > 0) {
  363. // // 检查是否已经充值过305礼包,只有在首次充值时才创建7日礼包记录
  364. // $hasPurchased305 = DB::connection('write')->table('agent.dbo.order')
  365. // ->where('user_id', $user_id)
  366. // ->where('GiftsID', 305)
  367. // ->where('pay_status', 1)
  368. //// ->where('order_sn', '!=', $order_sn) // 排除当前订单
  369. // ->exists();
  370. //
  371. // if (!$hasPurchased305) {
  372. // // 首次充值,创建7日礼包记录
  373. // try {
  374. // $this->createInactiveVipGiftRecord($user_id, $payAmt);
  375. // } catch (\Exception $e) {
  376. // \Log::error('inactive vip gift record create failed', [
  377. // 'user_id' => $user_id,
  378. // 'payAmt' => $payAmt,
  379. // 'error' => $e->getMessage(),
  380. // ]);
  381. // }
  382. // }
  383. // }
  384. if ($AdId && $payAmt) AfEvent::dispatch([$user_id, $payAmt, $AdId, $eventType]);
  385. try {
  386. //新邀请
  387. //(new AgentController())->processDeposit($user_id, $payAmt,$order_sn);
  388. AgentService::recordPerformance($user_id, $payAmt * NumConfig::NUM_VALUE);
  389. } catch (\Exception $exception) {
  390. Util::WriteLog("AgentService", $exception->getTraceAsString());
  391. }
  392. return [$Score];
  393. }
  394. /**
  395. * 执行存储过程
  396. * @param $user_id
  397. * @param $payAmt
  398. * @param $favorable_price
  399. * @param $Score
  400. * @param $GiftsID
  401. */
  402. public function storedProcedure($user_id, $payAmt, $favorable_price, $Score, $GiftsID)
  403. {
  404. // 开始执行时间
  405. $startTime = Helper::millisecond();
  406. // 执行存储过程 -- 防刷机制
  407. StoredProcedure::SetUserTabType($user_id);
  408. Log::info('GSP_GP_SetUserTabType12 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  409. # 单控标签 -- 执行存储过程
  410. StoredProcedure::user_label($user_id, 1, $payAmt);
  411. Log::info('CheckAccountsLabel 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  412. // 服务器通知
  413. // $url = config('transfer.stock')['url'] . 'notifyPay';
  414. // Log::info('中转服参数 ' . json_encode([
  415. // 'userid' => $user_id, 'getScore' => $favorable_price, 'score' => $Score,
  416. // 'giftsid' => empty($GiftsID) ? 0 : $GiftsID,
  417. // 'url' => $url
  418. // ]));
  419. // $res = (new HttpCurl())->service($url, ['userid' => $user_id, 'getScore' => $favorable_price, 'score' => $Score, 'giftsid' => empty($GiftsID) ? 0 : $GiftsID]);
  420. //
  421. // Log::info('中转服 执行时间:' . ((Helper::millisecond() - $startTime) / 1000), [
  422. // 'res' => $res,
  423. // ]);
  424. // AF 事件
  425. // (new AppflyerEvent())->event($user_id, '', 'af_purchase_new', $payAmt);
  426. // Log::info('AF 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  427. }
  428. /**
  429. * 首次充值:金币银币切换 + 数据清理
  430. * @param int $user_id 用户ID
  431. */
  432. private function switchToInsureScoreAndCleanData($user_id)
  433. {
  434. \Log::info('开始执行金币银币切换', ['user_id' => $user_id]);
  435. try {
  436. // 1. GameScoreInfo: 将Score的数据复制到InsureScore,把Score字段置0,把ScoreChange字段设置成1
  437. $scoreInfo = DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')
  438. ->where('UserID', $user_id)
  439. ->first();
  440. if ($scoreInfo) {
  441. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')
  442. ->where('UserID', $user_id)
  443. ->update([
  444. 'InsureScore' => max($scoreInfo->Score,50000), // 将Score复制到InsureScore
  445. 'Score' => 0, // Score置0
  446. 'ScoreChange' => 1, // ScoreChange设置成1
  447. 'MaxScore' => 0, // MaxScore清0
  448. 'MaxWinScore' => 0 // MaxWinScore清0
  449. ]);
  450. \Log::info('GameScoreInfo切换成功', [
  451. 'user_id' => $user_id,
  452. 'original_score' => $scoreInfo->Score,
  453. 'insure_score' => $scoreInfo->Score
  454. ]);
  455. }
  456. // 2. RecordUserTotalStatistics 表数据清0
  457. $totalStats = DB::connection('write')->table('QPRecordDB.dbo.RecordUserTotalStatistics')
  458. ->where('UserID', $user_id)
  459. ->first();
  460. if ($totalStats) {
  461. DB::connection('write')->table('QPRecordDB.dbo.RecordUserTotalStatistics')
  462. ->where('UserID', $user_id)
  463. ->update([
  464. 'WinInning' => 0,
  465. 'LostInning' => 0,
  466. 'Revenue' => 0,
  467. 'WinScore' => 0,
  468. 'LostScore' => 0,
  469. 'Handsel' => 0,
  470. 'DrawBase' => 0,
  471. 'TotalBet' => 0,
  472. // 'TotalScore' => 0,
  473. 'MaxDrawBase' => 0,
  474. 'MaxScore' => 0,
  475. 'MaxWinScore' => 0,
  476. 'Rounds' => 0,
  477. 'ProtectedRounds' => 0
  478. ]);
  479. \Log::info('RecordUserTotalStatistics清0成功', ['user_id' => $user_id]);
  480. }
  481. // 3. RecordUserDataStatisticsNew 表数据清0
  482. DB::connection('write')->table('QPRecordDB.dbo.RecordUserDataStatisticsNew')
  483. ->where('UserID', $user_id)
  484. ->delete();
  485. \Log::info('RecordUserDataStatisticsNew清0成功', ['user_id' => $user_id]);
  486. // 4. RecordUserGameCount 关于用户的数据删除
  487. DB::connection('write')->table('QPRecordDB.dbo.RecordUserGameCount')
  488. ->where('UserID', $user_id)
  489. ->delete();
  490. \Log::info('RecordUserGameCount清除成功', ['user_id' => $user_id]);
  491. \Log::info('金币银币切换完成', [
  492. 'user_id' => $user_id,
  493. 'insure_score' => $scoreInfo->Score ?? 0
  494. ]);
  495. } catch (\Exception $e) {
  496. \Log::error('金币银币切换异常', [
  497. 'user_id' => $user_id,
  498. 'error' => $e->getMessage(),
  499. 'trace' => $e->getTraceAsString()
  500. ]);
  501. throw $e;
  502. }
  503. }
  504. /**
  505. * 连续未充值 VIP 礼包(gift_id=305)—— 创建 7 日礼包记录
  506. * @param int $user_id
  507. * @param float $payAmt 本次充值金额(元)
  508. */
  509. private function createInactiveVipGiftRecord($user_id, $payAmt)
  510. {
  511. // 获取上一次充值订单(不包括刚刚充值的这一条),用于计算连续未充值天数
  512. // 计算从最后一次充值的日期到今天的未充值天数
  513. $prevOrder = DB::connection('write')->table('agent.dbo.order')
  514. ->where('user_id', $user_id)
  515. ->where('pay_status', 1)
  516. ->where('GiftsID', '!=', 305) // 排除305礼包本身,避免重复计算
  517. ->orderBy('pay_at', 'desc')
  518. ->first();
  519. // 默认连续未充值天数至少为 3(以便至少满足条件)
  520. $inactiveDays = 3;
  521. if ($prevOrder) {
  522. // 计算从最后一次充值到今天的未充值天数
  523. $prevDate = date('Y-m-d', strtotime($prevOrder->pay_at));
  524. $today = date('Y-m-d');
  525. $diffDays = (strtotime($today) - strtotime($prevDate)) / 86400;
  526. // 连续未充值天数:如果最后一次充值是昨天,则未充值天数为0;如果是前天,则为1;以此类推
  527. $inactiveDays = max(3, (int)$diffDays - 1); // 至少为3,确保满足条件
  528. }
  529. // 七日礼包总奖励百分比:140% + (inactiveDays - 3) * 10%,上限 200%
  530. $sevenPercent = 140 + max(0, $inactiveDays - 3) * 10;
  531. $sevenPercent = min($sevenPercent, 200);
  532. // 总奖励百分比 = 120%(立即奖励) + 七日礼包百分比
  533. $totalPercent = 120 + $sevenPercent;
  534. // 七日礼包总金额 & 每日金额(7天平均分配,包括充值当天)
  535. $sevenTotalAmount = round($payAmt * $sevenPercent / 100, 2);
  536. $perDayAmount = round($sevenTotalAmount / 7, 2);
  537. $now = date('Y-m-d H:i:s');
  538. // 过期时间:从充值当天算起,共 7 天可领取(第1天为充值当天),
  539. // 因此 expired_at 设为创建时间 + 6 天
  540. $expAt = date('Y-m-d H:i:s', strtotime('+6 days'));
  541. DB::connection('write')->table('agent.dbo.inactive_vip_gift_records')->insert([
  542. 'user_id' => $user_id,
  543. 'gift_id' => 305,
  544. 'pay_amount' => $payAmt,
  545. 'total_percent' => $totalPercent,
  546. 'seven_days_percent' => $sevenPercent,
  547. 'per_day_amount' => $perDayAmount,
  548. 'inactive_days' => $inactiveDays,
  549. 'claimed_days_mask' => 0,
  550. 'created_at' => $now,
  551. 'updated_at' => $now,
  552. 'expired_at' => $expAt,
  553. ]);
  554. }
  555. /**
  556. * 创建首充礼包记录
  557. */
  558. public function createFirstPayGiftRecord($user_id, $gift_id, $payAmt)
  559. {
  560. // 检查是否已有记录
  561. $existing = DB::connection('write')->table('agent.dbo.first_pay_gift_records')
  562. ->where('user_id', $user_id)
  563. ->first();
  564. if ($existing) {
  565. \Log::info('首充礼包记录已存在', ['user_id' => $user_id]);
  566. return;
  567. }
  568. // 获取礼包配置
  569. $giftConfig = DB::connection('write')->table('agent.dbo.recharge_gift')
  570. ->where('gift_id', $gift_id)
  571. ->first();
  572. if (!$giftConfig) {
  573. \Log::error('首充礼包配置不存在', ['gift_id' => $gift_id]);
  574. return;
  575. }
  576. // 计算金额
  577. $totalBonusAmount = round($giftConfig->total_bonus * $payAmt / 100, 2);
  578. $bonusInstantlyAmount = round($giftConfig->bonus_instantly * $payAmt / 100, 2);
  579. // 解析JSON数据
  580. $dayRewards = $giftConfig->day_rewards ? json_decode($giftConfig->day_rewards, true) : null;
  581. $bettingBonus = $giftConfig->betting_bonus ? json_decode($giftConfig->betting_bonus, true) : null;
  582. $bettingTask = $giftConfig->betting_task ? json_decode($giftConfig->betting_task, true) : null;
  583. // 计算各部分金额
  584. $dayRewardsTotal = $dayRewards ? round($dayRewards['total_bonus'] * $payAmt / 100, 2) : 0;
  585. $bettingBonusTotal = $bettingBonus ? round($bettingBonus['total_bonus'] * $payAmt / 100, 2) : 0;
  586. $bettingTaskTotal = $bettingTask ? round($bettingTask['total_bonus'] * $payAmt / 100, 2) : 0;
  587. // 创建记录
  588. $data = [
  589. 'user_id' => $user_id,
  590. 'gift_id' => $gift_id,
  591. 'pay_amount' => $payAmt,
  592. 'total_bonus' => $totalBonusAmount,
  593. 'bonus_instantly' => $bonusInstantlyAmount,
  594. 'gift_name' => $giftConfig->gift_name,
  595. // 每日奖励
  596. 'day_rewards_total' => $dayRewardsTotal,
  597. 'day_rewards_claimed' => 0,
  598. 'day_rewards_data' => $giftConfig->day_rewards,
  599. 'day_last_claim_date' => null,
  600. // 下注奖励
  601. 'betting_bonus_total' => $bettingBonusTotal,
  602. 'betting_bonus_claimed' => 0,
  603. 'betting_bonus_data' => $giftConfig->betting_bonus,
  604. 'betting_current_bet' => 0,
  605. // 下注任务
  606. 'betting_task_total' => $bettingTaskTotal,
  607. 'betting_task_claimed' => 0,
  608. 'betting_task_data' => $giftConfig->betting_task,
  609. 'created_at' => date('Y-m-d H:i:s'),
  610. 'updated_at' => date('Y-m-d H:i:s')
  611. ];
  612. DB::connection('write')->table('agent.dbo.first_pay_gift_records')->insert($data);
  613. $dayRewardsTotal = $dayRewards ? round($dayRewards['total_bonus'] * $payAmt / 100, 2) : 0;
  614. $bettingBonusTotal = $bettingBonus ? round($bettingBonus['total_bonus'] * $payAmt / 100, 2) : 0;
  615. $bettingTaskTotal = $bettingTask ? round($bettingTask['total_bonus'] * $payAmt / 100, 2) : 0;
  616. \Log::info('首充礼包记录创建成功', [
  617. 'user_id' => $user_id,
  618. 'gift_id' => $gift_id,
  619. 'pay_amount' => $payAmt,
  620. 'total_bonus' => $totalBonusAmount,
  621. 'rw' => [
  622. $dayRewardsTotal,
  623. $bettingBonusTotal,
  624. $bettingTaskTotal,
  625. round($dayRewards['total_bonus'] * $payAmt / 100, 2),
  626. round($bettingBonus['total_bonus'] * $payAmt / 100, 2),
  627. round($bettingTask['total_bonus'] * $payAmt / 100, 2)
  628. ]
  629. ]);
  630. }
  631. }