OrderServices.php 36 KB

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