OrderServices.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. namespace App\Services;
  3. use App\dao\Estatisticas\RechargeWithDraw;
  4. use App\Facade\TableName;
  5. use App\Game\GlobalUserInfo;
  6. use App\Game\Services\AgentService;
  7. use App\Http\Controllers\Api\AgentController;
  8. use App\Http\helper\Helper;
  9. use App\Http\helper\HttpCurl;
  10. use App\Http\helper\NumConfig;
  11. use App\Jobs\AfEvent;
  12. use App\Models\AgentUser;
  13. use App\Models\RecordScoreInfo;
  14. use App\Models\RecordUserDataStatistics;
  15. use App\Util;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Log;
  18. use Illuminate\Support\Facades\Redis;
  19. class OrderServices
  20. {
  21. public $FirstCharge = 30;
  22. public $FirstChargeGive = 20;
  23. public $FirstGiftID = 301;
  24. private $first_pay = null;
  25. public function __construct()
  26. {
  27. }
  28. /**
  29. * 获取支付金额
  30. * @param int $GiftsID 礼包ID
  31. * @param int $user_id 用户ID
  32. * @param int $payAmt 支付金额(元)
  33. * @return array|void [$give, $favorable_price, $Recharge, $czReason, $cjReason] 赠送金额,赠送金额+充值金额,充值金额,充值原因,彩金原因
  34. */
  35. public function getPayInfo($GiftsID, $user_id, $payAmt)
  36. {
  37. $give = $czReason = $cjReason = $Recharge = $favorable_price = $second_give = 0;
  38. // 判断是不是首冲礼包
  39. if (!empty($GiftsID)) {
  40. if ($GiftsID == 301) { // 礼包--首冲
  41. $Gifts = DB::connection('write')->table('agent.dbo.recharge_gift')->where('gift_id', $GiftsID)->first();
  42. $favorable_price = round($Gifts->bonus_instantly*$payAmt/100,2);
  43. $give = $favorable_price-$payAmt;
  44. $Recharge = $payAmt;
  45. $czReason = 50;
  46. $cjReason = 51;
  47. }else if ($GiftsID > 400) {
  48. $Status = 1;
  49. $recharge_gear = DB::connection('write')->table('agent.dbo.recharge_gear')->where('status', $Status)->where('money', $payAmt)->select('favorable_price', 'give')->first();
  50. if(!$recharge_gear){
  51. $Recharge=$payAmt;
  52. $give=0;
  53. }else{
  54. $Recharge = $recharge_gear->favorable_price;
  55. $give = $recharge_gear->give;
  56. }
  57. $favorable_price = $Recharge + $give;
  58. $czReason = 1;
  59. $cjReason = 45;
  60. }
  61. } else { // 普通订单
  62. $Status = 1;
  63. $recharge_gear = DB::connection('write')->table('agent.dbo.recharge_gear')->where('status', $Status)->where('money', $payAmt)->select('favorable_price', 'give')->first();
  64. if(!$recharge_gear){
  65. $Recharge=$payAmt;
  66. $give=0;
  67. }else{
  68. $Recharge = $recharge_gear->favorable_price;
  69. $give = $recharge_gear->give;
  70. }
  71. $favorable_price = $Recharge + $give;
  72. $czReason = 1;
  73. $cjReason = 45;
  74. }
  75. return [$give, $favorable_price, $Recharge, $czReason, $cjReason, $second_give];
  76. }
  77. /**
  78. * 添加玩家充值记录
  79. * @param $user_id
  80. * @param $payAmt
  81. * @param $favorable_price
  82. * @param $order_sn
  83. * @param $GiftsID
  84. * @param $Recharge
  85. * @param $czReason
  86. * @param $give
  87. * @param $cjReason
  88. * @param $AdId
  89. * @param $eventType
  90. */
  91. public function addRecord($user_id, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType)
  92. {
  93. if ($payAmt > 0) {
  94. // 增加玩家充值金额
  95. $query = DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  96. ->where('UserID', $user_id)
  97. ->value('Recharge');
  98. if ($query) {
  99. DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  100. ->where('UserID', $user_id)
  101. ->increment('Recharge', $payAmt);
  102. } else {
  103. DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  104. ->where('UserID', $user_id)
  105. ->insert(['Recharge' => $payAmt, 'UserID' => $user_id]);
  106. // 首次充值:金币银币切换 + 数据清理
  107. /*
  108. * 金币银币切换 GameScoreInfo 将score的数据复制到 InsureScore 把Score 字段置0 把 ScoreChange字段设置成1
  109. * 清理用户的数据
  110. * GameScoreInfo的MaxScore MaxWinscore清0
  111. * RecordUserTotalStatistics 表 数据清0
  112. * RecordUserDataStatisticsNew 表数据 清0
  113. * RecordUserGameCount 关于用户的数据删除
  114. *
  115. */
  116. try {
  117. $this->switchToInsureScoreAndCleanData($user_id);
  118. } catch (\Exception $e) {
  119. \Log::error('首次充值-金币银币切换失败', [
  120. 'user_id' => $user_id,
  121. 'error' => $e->getMessage()
  122. ]);
  123. }
  124. }
  125. //在这里更新 RecordUserTotalStatistics 数据 将LastRechargeValue字段更新为payAmt 如果不存在则插入
  126. DB::connection('write')->table('QPRecordDB.dbo.RecordUserTotalStatistics')
  127. ->updateOrInsert(['UserID' => $user_id], ['LastRechargeValue' => $payAmt]);
  128. }
  129. if ($Recharge > 0) {
  130. // 添加日志记录表
  131. StoredProcedure::addPlatformData($user_id, 3, $Recharge * NumConfig::NUM_VALUE);
  132. // 增加用户金币变化记录
  133. $AfterScore = RecordScoreInfo::addScore($user_id, ($Recharge * NumConfig::NUM_VALUE), $czReason); #充值
  134. if ($AfterScore) {
  135. RecordScoreInfo::addScore($user_id, ($give * NumConfig::NUM_VALUE), $cjReason, $AfterScore); #赠送彩金
  136. }
  137. }
  138. $favorable_price = $favorable_price * NumConfig::NUM_VALUE;
  139. $firstScore = DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user_id)->value('Score');
  140. $Score = $favorable_price + $firstScore;
  141. if ($payAmt > 0 && false) {
  142. try {
  143. $userInfo = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')
  144. ->where('UserID', $user_id)
  145. ->first();
  146. $channelConfig = DB::table('QPPlatformDB.dbo.ChannelPackageName')->where('Channel', $userInfo->Channel)->first();
  147. // Util::WriteLog('xxxxx', [$userInfo->Channel, $channelConfig, $query, $firstScore, $channelConfig->Rate > 0 && $channelConfig->ContrlScore > 0, $channelConfig->MinScore * NumConfig::NUM_VALUE > $firstScore]);
  148. if ($channelConfig && !$query && false) {
  149. if ($channelConfig->Rate > 0 && $channelConfig->ContrlScore > 0) {
  150. if ($channelConfig->MinScore * NumConfig::NUM_VALUE >= $firstScore) {
  151. $openGames = config('games.openKGame');
  152. $query = DB::table('QPTreasureDB.dbo.UserScoreControl')->where('UserID', $user_id)->first();
  153. if (!$query) {
  154. $build_sql = DB::connection('write')->table('QPTreasureDB.dbo.UserScoreControl');
  155. //Util::WriteLog('control_old_user',$list);
  156. $data = [
  157. 'ControlScore' => (int)($channelConfig->ContrlScore * NumConfig::NUM_VALUE),
  158. 'EffectiveScore' => 0,
  159. 'ControlKindID' => -1,
  160. 'Remarks' => '',
  161. 'InsertDate' => date('Y-m-d H:i:s'),
  162. 'ControlRadian' => 0
  163. ];
  164. $build_sql->updateOrInsert(['UserID' => $user_id], $data);
  165. foreach ($openGames as $GameID) {
  166. $KindData = [
  167. 'UserID' => $user_id,
  168. 'KindID' => $GameID,
  169. 'ControlRadian' => $channelConfig->Rate,
  170. 'ControlDate' => date('Y-m-d H:i:s')
  171. ];
  172. DB::connection('write')->table('QPTreasureDB.dbo.UserControlKind')->updateOrInsert(['UserID' => $user_id, 'KindID' => $GameID], $KindData);
  173. }
  174. }
  175. }
  176. }
  177. }
  178. } catch (\Exception $exception) {
  179. }
  180. // 记录订单变化后金币
  181. DB::connection('write')->table('agent.dbo.order')
  182. ->where('order_sn', $order_sn)
  183. ->update(['after_amount' => $Score]);
  184. // 增加用户充值变化值
  185. RecordUserDataStatistics::updateOrAdd($user_id, 0, $payAmt);
  186. }
  187. // 不是周卡的时候执行
  188. if ($GiftsID < 100 || $GiftsID >= 200) {
  189. // 增加用户金币
  190. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user_id)->increment('Score', $favorable_price);
  191. }
  192. // 充值推广佣金
  193. // (new AgentUser())->reward($user_id, $payAmt, $order_sn);
  194. //
  195. // 周卡 -- 执行存储过程
  196. if (isset($GiftsID) && $GiftsID > 100 && $GiftsID < 200) {
  197. $CardID = (int)$GiftsID - 100;
  198. StoredProcedure::BuyMonthCard($user_id, $CardID, $order_sn);
  199. // 开始执行时间
  200. $startTime = Helper::millisecond();
  201. Log::info('GSP_GP_BuyMonthCard 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  202. }
  203. //设置第二天要领取
  204. if (isset($GiftsID) && $GiftsID >= 200 && $GiftsID < 300 ) {
  205. $first = DB::table('agent.dbo.recharge_gear')->where('gift_id', $GiftsID)->select('gift_id', 'money', 'give', 'favorable_price', 'second_give')->first();
  206. if ($first && $first->second_give > 0) {
  207. $fpkey = 'Firstpay_' . $user_id;
  208. $data = (array)$first;
  209. $data['buytime'] = time();
  210. $data['czReason'] = $czReason;
  211. $data['cjReason'] = $cjReason;
  212. Redis::set($fpkey, json_encode($data));
  213. DB::table(TableName::agent() . 'guide_payment')->updateOrInsert([
  214. 'UserID' => $user_id,
  215. 'GiftID' => $GiftsID,
  216. 'CreateTime' => now()
  217. ], ['UserID' => $user_id]);
  218. }
  219. }
  220. //设置第二天要领取
  221. if (!empty($GiftsID) && $GiftsID >= 300 && $GiftsID < 400 ) {
  222. Redis::del('repay_temp_'.$user_id);
  223. }
  224. // 数据统计后台 -- 充值记录添加
  225. (new RechargeWithDraw())->recharge($user_id, $payAmt);
  226. // (new RechargeWithDraw())->recharge($user_id, $Recharge);
  227. if ($AdId && $payAmt) AfEvent::dispatch([$user_id, $payAmt, $AdId, $eventType]);
  228. try {
  229. //新邀请
  230. (new AgentController())->processDeposit($user_id, $payAmt,$order_sn);
  231. // AgentService::recordPerformance($user_id, $payAmt * NumConfig::NUM_VALUE);
  232. } catch (\Exception $exception) {
  233. Util::WriteLog("AgentService", $exception->getTraceAsString());
  234. }
  235. return [$Score];
  236. }
  237. /**
  238. * 执行存储过程
  239. * @param $user_id
  240. * @param $payAmt
  241. * @param $favorable_price
  242. * @param $Score
  243. * @param $GiftsID
  244. */
  245. public function storedProcedure($user_id, $payAmt, $favorable_price, $Score, $GiftsID)
  246. {
  247. // 开始执行时间
  248. $startTime = Helper::millisecond();
  249. // 执行存储过程 -- 防刷机制
  250. StoredProcedure::SetUserTabType($user_id);
  251. Log::info('GSP_GP_SetUserTabType12 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  252. # 单控标签 -- 执行存储过程
  253. StoredProcedure::user_label($user_id, 1, $payAmt);
  254. Log::info('CheckAccountsLabel 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  255. // 服务器通知
  256. // $url = config('transfer.stock')['url'] . 'notifyPay';
  257. // Log::info('中转服参数 ' . json_encode([
  258. // 'userid' => $user_id, 'getScore' => $favorable_price, 'score' => $Score,
  259. // 'giftsid' => empty($GiftsID) ? 0 : $GiftsID,
  260. // 'url' => $url
  261. // ]));
  262. // $res = (new HttpCurl())->service($url, ['userid' => $user_id, 'getScore' => $favorable_price, 'score' => $Score, 'giftsid' => empty($GiftsID) ? 0 : $GiftsID]);
  263. //
  264. // Log::info('中转服 执行时间:' . ((Helper::millisecond() - $startTime) / 1000), [
  265. // 'res' => $res,
  266. // ]);
  267. // AF 事件
  268. // (new AppflyerEvent())->event($user_id, '', 'af_purchase_new', $payAmt);
  269. // Log::info('AF 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  270. }
  271. /**
  272. * 首次充值:金币银币切换 + 数据清理
  273. * @param int $user_id 用户ID
  274. */
  275. private function switchToInsureScoreAndCleanData($user_id)
  276. {
  277. \Log::info('开始执行金币银币切换', ['user_id' => $user_id]);
  278. try {
  279. // 1. GameScoreInfo: 将Score的数据复制到InsureScore,把Score字段置0,把ScoreChange字段设置成1
  280. $scoreInfo = DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')
  281. ->where('UserID', $user_id)
  282. ->first();
  283. if ($scoreInfo) {
  284. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')
  285. ->where('UserID', $user_id)
  286. ->update([
  287. 'InsureScore' => $scoreInfo->Score, // 将Score复制到InsureScore
  288. 'Score' => 0, // Score置0
  289. 'ScoreChange' => 1, // ScoreChange设置成1
  290. 'MaxScore' => 0, // MaxScore清0
  291. 'MaxWinScore' => 0 // MaxWinScore清0
  292. ]);
  293. \Log::info('GameScoreInfo切换成功', [
  294. 'user_id' => $user_id,
  295. 'original_score' => $scoreInfo->Score,
  296. 'insure_score' => $scoreInfo->Score
  297. ]);
  298. }
  299. // 2. RecordUserTotalStatistics 表数据清0
  300. $totalStats = DB::connection('write')->table('QPRecordDB.dbo.RecordUserTotalStatistics')
  301. ->where('UserID', $user_id)
  302. ->first();
  303. if ($totalStats) {
  304. DB::connection('write')->table('QPRecordDB.dbo.RecordUserTotalStatistics')
  305. ->where('UserID', $user_id)
  306. ->update([
  307. 'Winning' => 0,
  308. 'LostInning' => 0,
  309. 'Revenue' => 0,
  310. 'WinScore' => 0,
  311. 'LostScore' => 0,
  312. 'Handsel' => 0,
  313. 'DrawBase' => 0,
  314. 'TotalBet' => 0,
  315. 'TotalScore' => 0,
  316. 'MaxDrawBase' => 0,
  317. 'MaxScore' => 0,
  318. 'MaxWinScore' => 0,
  319. 'Rounds' => 0,
  320. 'ProtectedRounds' => 0
  321. ]);
  322. \Log::info('RecordUserTotalStatistics清0成功', ['user_id' => $user_id]);
  323. }
  324. // 3. RecordUserDataStatisticsNew 表数据清0
  325. DB::connection('write')->table('QPRecordDB.dbo.RecordUserDataStatisticsNew')
  326. ->where('UserID', $user_id)
  327. ->delete();
  328. \Log::info('RecordUserDataStatisticsNew清0成功', ['user_id' => $user_id]);
  329. // 4. RecordUserGameCount 关于用户的数据删除
  330. DB::connection('write')->table('QPRecordDB.dbo.RecordUserGameCount')
  331. ->where('UserID', $user_id)
  332. ->delete();
  333. \Log::info('RecordUserGameCount清除成功', ['user_id' => $user_id]);
  334. \Log::info('金币银币切换完成', [
  335. 'user_id' => $user_id,
  336. 'insure_score' => $scoreInfo->Score ?? 0
  337. ]);
  338. } catch (\Exception $e) {
  339. \Log::error('金币银币切换异常', [
  340. 'user_id' => $user_id,
  341. 'error' => $e->getMessage(),
  342. 'trace' => $e->getTraceAsString()
  343. ]);
  344. throw $e;
  345. }
  346. }
  347. }