OrderServices.php 33 KB

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