OrderServices.php 35 KB

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