OrderServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 = 201;
  24. private $first_pay = null;
  25. public function __construct()
  26. {
  27. $first_pay = DB::table('agent.dbo.recharge_gear')->where('first_pay', 1)->first();
  28. $this->first_pay = $first_pay;
  29. $this->FirstCharge = (int)$first_pay->favorable_price;
  30. $this->FirstChargeGive = (int)$first_pay->give;
  31. $this->FirstGiftID = (int)$first_pay->gift_id;
  32. }
  33. /**
  34. * 获取支付金额
  35. * @param int $GiftsID 礼包ID
  36. * @param int $user_id 用户ID
  37. * @param int $payAmt 支付金额(元)
  38. * @return array|void [$give, $favorable_price, $Recharge, $czReason, $cjReason] 赠送金额,赠送金额+充值金额,充值金额,充值原因,彩金原因
  39. */
  40. public function getPayInfo($GiftsID, $user_id, $payAmt)
  41. {
  42. $give = $czReason = $cjReason = $Recharge = $favorable_price = $second_give = 0;
  43. // 判断是不是首冲礼包
  44. if (!empty($GiftsID)) {
  45. if ($GiftsID < 100) { // 礼包--首冲
  46. $GiftsData = [
  47. 'UserID' => $user_id,
  48. 'GiftsID' => $GiftsID,
  49. 'BuyCount' => 1,
  50. 'LastBuyDate' => date('Y-m-d H:i:s')
  51. ];
  52. // 卖出去数量 +1
  53. DB::connection('write')->table('QPAccountsDB.dbo.FirstRechargeGifts')->where('GiftsID', $GiftsID)->increment('TotalCount');
  54. $userGifts = DB::connection('write')->table('QPAccountsDB.dbo.UserFirstRechargeGifts')->where('UserID', $user_id)->where('GiftsID', $GiftsID)->first();
  55. if ($userGifts) {
  56. DB::connection('write')->table('QPAccountsDB.dbo.UserFirstRechargeGifts')->where('UserID', $user_id)->where('GiftsID', $GiftsID)->increment('BuyCount');
  57. } else {
  58. DB::connection('write')->table('QPAccountsDB.dbo.UserFirstRechargeGifts')->insert($GiftsData);
  59. }
  60. // 给服务端更新首冲状态
  61. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')
  62. ->where('UserID', $user_id)
  63. ->update(['RechargeGiftTime' => date('Y-m-d H:i:s')]);
  64. $Gifts = DB::connection('write')->table('QPAccountsDB.dbo.FirstRechargeGifts')->where('GiftsID', $GiftsID)->first();
  65. $favorable_price = $Gifts->AllScore;
  66. $give = $Gifts->ExtraScore;
  67. $Recharge = $Gifts->Price;
  68. $czReason = 50;
  69. $cjReason = 51;
  70. } elseif ($GiftsID > 100 && $GiftsID < 200) { // 周卡
  71. $CardID = $GiftsID - 100;
  72. $WeeklyCard = DB::connection('write')->table('QPPlatformDB.dbo.MonthCard')->where('CardID', $CardID)->first();
  73. $favorable_price = $WeeklyCard->FirstReward / 100;
  74. $Recharge = $WeeklyCard->Price;
  75. } elseif (false) {//$GiftsID == 200 || $GiftsID == 201) { // 20元首充
  76. $RechargeNum = $this->FirstCharge * NumConfig::NUM_VALUE;
  77. $give = $this->FirstChargeGive; // 赠送
  78. $firstPayExists = DB::table(TableName::QPAccountsDB() . 'UserRechargeActive')->where('UserID', $user_id)->first();
  79. if (!$firstPayExists) {
  80. DB::table(TableName::QPAccountsDB() . 'UserRechargeActive')
  81. ->insert([
  82. 'UserID' => $user_id,
  83. 'RechargeNum' => $RechargeNum,
  84. 'RewardDate' => date('Ymd'),
  85. 'FinishDate' => date('Ymd'),
  86. 'TotalReward' => $RechargeNum + ($this->FirstChargeGive * NumConfig::NUM_VALUE),
  87. 'CreateTime' => now()
  88. ]);
  89. }
  90. $favorable_price = $this->FirstCharge + $this->FirstChargeGive; // 充值
  91. $Recharge = $this->FirstCharge; // 充值
  92. $czReason = 1;
  93. $cjReason = 45;
  94. } elseif ($GiftsID == 200 || $GiftsID == 201 || $GiftsID == 211 || $GiftsID == 212) { // 引导付费礼包档位1 + 引导付费礼包档位2
  95. $first = DB::table('agent.dbo.recharge_gear')->where('gift_id', $GiftsID)->first();
  96. if ($first) {
  97. $Recharge = $first->favorable_price; // 充值
  98. $give = $first->give;
  99. $second_give = $first->second_give ?? 0;
  100. $favorable_price = $give + $Recharge; // 直接得到的金币
  101. $czReason = 1;
  102. $cjReason = 45;
  103. $RechargeNum = $Recharge * NumConfig::NUM_VALUE;
  104. if (!DB::table(TableName::QPAccountsDB() . 'UserRechargeActive')->where('UserID', $user_id)->exists()) {
  105. DB::table(TableName::QPAccountsDB() . 'UserRechargeActive')
  106. ->insert([
  107. 'UserID' => $user_id,
  108. 'RechargeNum' => $RechargeNum,
  109. 'RewardDate' => date('Ymd'),
  110. 'FinishDate' => date('Ymd'),
  111. 'TotalReward' => ($favorable_price + $second_give) * NumConfig::NUM_VALUE,
  112. 'CreateTime' => now()
  113. ]);
  114. }
  115. }
  116. } else if ($GiftsID > 300) {
  117. // 查找送的金额
  118. $recharge_gear = DB::connection('write')->table('agent.dbo.recharge_gear')->where('first_pay', 0)->where('money', $payAmt)->where('gift_id', $GiftsID)->select('favorable_price', 'give')->first();
  119. $Recharge = $recharge_gear->favorable_price;
  120. $give = $recharge_gear->give;
  121. $favorable_price = $Recharge + $give;
  122. $czReason = 1;
  123. $cjReason = 45;
  124. }
  125. } else { // 普通订单
  126. // 查找送的金额
  127. $RechargeTimes=DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->where('UserID', $user_id)->value('RechargeTimes')??0;
  128. $Status = 1;
  129. // if($RechargeTimes>=4) {
  130. // }else{
  131. // $Status=$RechargeTimes+3;
  132. // }
  133. $recharge_gear = DB::connection('write')->table('agent.dbo.recharge_gear')->where('first_pay', 0)->where('status', $Status)->where('money', $payAmt)->select('favorable_price', 'give')->first();
  134. if(!$recharge_gear){
  135. $Recharge=$payAmt;
  136. $give=0;
  137. }else{
  138. $Recharge = $recharge_gear->favorable_price;
  139. $give = $recharge_gear->give;
  140. }
  141. $favorable_price = $Recharge + $give;
  142. $czReason = 1;
  143. $cjReason = 45;
  144. }
  145. return [$give, $favorable_price, $Recharge, $czReason, $cjReason, $second_give];
  146. }
  147. /**
  148. * 添加玩家充值记录
  149. * @param $user_id
  150. * @param $payAmt
  151. * @param $favorable_price
  152. * @param $order_sn
  153. * @param $GiftsID
  154. * @param $Recharge
  155. * @param $czReason
  156. * @param $give
  157. * @param $cjReason
  158. * @param $AdId
  159. * @param $eventType
  160. */
  161. public function addRecord($user_id, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType)
  162. {
  163. if ($payAmt > 0) {
  164. // 增加玩家充值金额
  165. $query = DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  166. ->where('UserID', $user_id)
  167. ->value('Recharge');
  168. if ($query) {
  169. DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  170. ->where('UserID', $user_id)
  171. ->increment('Recharge', $payAmt);
  172. } else {
  173. DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  174. ->where('UserID', $user_id)
  175. ->insert(['Recharge' => $payAmt, 'UserID' => $user_id]);
  176. }
  177. try {
  178. if($query>=100){
  179. $user=GlobalUserInfo::getGameUserInfo('UserID',$user_id);
  180. if($user){
  181. if($user->Channel == 99){
  182. GlobalUserInfo::where('UserID', $user_id)->update([ 'Channel' => 50]);
  183. // $user->Channel = 50;
  184. // $user->save();
  185. }
  186. }
  187. }
  188. }catch (\Exception $exception){
  189. }
  190. }
  191. if ($Recharge > 0) {
  192. // 添加日志记录表
  193. StoredProcedure::addPlatformData($user_id, 3, $Recharge * NumConfig::NUM_VALUE);
  194. // 增加用户金币变化记录
  195. $AfterScore = RecordScoreInfo::addScore($user_id, ($Recharge * NumConfig::NUM_VALUE), $czReason); #充值
  196. if ($AfterScore) {
  197. RecordScoreInfo::addScore($user_id, ($give * NumConfig::NUM_VALUE), $cjReason, $AfterScore); #赠送彩金
  198. }
  199. }
  200. $favorable_price = $favorable_price * NumConfig::NUM_VALUE;
  201. $firstScore = DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user_id)->value('Score');
  202. $Score = $favorable_price + $firstScore;
  203. if ($payAmt > 0) {
  204. try {
  205. $userInfo = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')
  206. ->where('UserID', $user_id)
  207. ->first();
  208. $channelConfig = DB::table('QPPlatformDB.dbo.ChannelPackageName')->where('Channel', $userInfo->Channel)->first();
  209. // Util::WriteLog('xxxxx', [$userInfo->Channel, $channelConfig, $query, $firstScore, $channelConfig->Rate > 0 && $channelConfig->ContrlScore > 0, $channelConfig->MinScore * NumConfig::NUM_VALUE > $firstScore]);
  210. if ($channelConfig && !$query) {
  211. if ($channelConfig->Rate > 0 && $channelConfig->ContrlScore > 0) {
  212. if ($channelConfig->MinScore * NumConfig::NUM_VALUE >= $firstScore) {
  213. $openGames = config('games.openKGame');
  214. $query = DB::table('QPTreasureDB.dbo.UserScoreControl')->where('UserID', $user_id)->first();
  215. if (!$query) {
  216. $build_sql = DB::connection('write')->table('QPTreasureDB.dbo.UserScoreControl');
  217. //Util::WriteLog('control_old_user',$list);
  218. $data = [
  219. 'ControlScore' => (int)($channelConfig->ContrlScore * NumConfig::NUM_VALUE),
  220. 'EffectiveScore' => 0,
  221. 'ControlKindID' => -1,
  222. 'Remarks' => '',
  223. 'InsertDate' => date('Y-m-d H:i:s'),
  224. 'ControlRadian' => 0
  225. ];
  226. $build_sql->updateOrInsert(['UserID' => $user_id], $data);
  227. foreach ($openGames as $GameID) {
  228. $KindData = [
  229. 'UserID' => $user_id,
  230. 'KindID' => $GameID,
  231. 'ControlRadian' => $channelConfig->Rate,
  232. 'ControlDate' => date('Y-m-d H:i:s')
  233. ];
  234. DB::connection('write')->table('QPTreasureDB.dbo.UserControlKind')->updateOrInsert(['UserID' => $user_id, 'KindID' => $GameID], $KindData);
  235. }
  236. }
  237. }
  238. }
  239. }
  240. } catch (\Exception $exception) {
  241. }
  242. // 记录订单变化后金币
  243. DB::connection('write')->table('agent.dbo.order')
  244. ->where('order_sn', $order_sn)
  245. ->update(['after_amount' => $Score]);
  246. // 增加用户充值变化值
  247. RecordUserDataStatistics::updateOrAdd($user_id, 0, $payAmt);
  248. }
  249. // 不是周卡的时候执行
  250. if ($GiftsID < 100 || $GiftsID >= 200) {
  251. // 增加用户金币
  252. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user_id)->increment('Score', $favorable_price);
  253. }
  254. // 充值推广佣金
  255. // (new AgentUser())->reward($user_id, $payAmt, $order_sn);
  256. //
  257. // 周卡 -- 执行存储过程
  258. if (isset($GiftsID) && $GiftsID > 100 && $GiftsID < 200) {
  259. $CardID = (int)$GiftsID - 100;
  260. StoredProcedure::BuyMonthCard($user_id, $CardID, $order_sn);
  261. // 开始执行时间
  262. $startTime = Helper::millisecond();
  263. Log::info('GSP_GP_BuyMonthCard 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  264. }
  265. //设置第二天要领取
  266. if (isset($GiftsID) && $GiftsID >= 200 && $GiftsID < 300 ) {
  267. $first = DB::table('agent.dbo.recharge_gear')->where('gift_id', $GiftsID)->select('gift_id', 'money', 'give', 'favorable_price', 'second_give')->first();
  268. if ($first && $first->second_give > 0) {
  269. $fpkey = 'Firstpay_' . $user_id;
  270. $data = (array)$first;
  271. $data['buytime'] = time();
  272. $data['czReason'] = $czReason;
  273. $data['cjReason'] = $cjReason;
  274. Redis::set($fpkey, json_encode($data));
  275. DB::table(TableName::agent() . 'guide_payment')->updateOrInsert([
  276. 'UserID' => $user_id,
  277. 'GiftID' => $GiftsID,
  278. 'CreateTime' => now()
  279. ], ['UserID' => $user_id]);
  280. }
  281. }
  282. //设置第二天要领取
  283. if (!empty($GiftsID) && $GiftsID >= 300 && $GiftsID < 400 ) {
  284. Redis::del('repay_temp_'.$user_id);
  285. }
  286. // 数据统计后台 -- 充值记录添加
  287. (new RechargeWithDraw())->recharge($user_id, $payAmt);
  288. // (new RechargeWithDraw())->recharge($user_id, $Recharge);
  289. if ($AdId && $payAmt) AfEvent::dispatch([$user_id, $payAmt, $AdId, $eventType]);
  290. try {
  291. //新邀请
  292. (new AgentController())->processDeposit($user_id, $payAmt,$order_sn);
  293. // AgentService::recordPerformance($user_id, $payAmt * NumConfig::NUM_VALUE);
  294. } catch (\Exception $exception) {
  295. Util::WriteLog("AgentService", $exception->getTraceAsString());
  296. }
  297. return [$Score];
  298. }
  299. /**
  300. * 执行存储过程
  301. * @param $user_id
  302. * @param $payAmt
  303. * @param $favorable_price
  304. * @param $Score
  305. * @param $GiftsID
  306. */
  307. public function storedProcedure($user_id, $payAmt, $favorable_price, $Score, $GiftsID)
  308. {
  309. // 开始执行时间
  310. $startTime = Helper::millisecond();
  311. // 执行存储过程 -- 防刷机制
  312. StoredProcedure::SetUserTabType($user_id);
  313. Log::info('GSP_GP_SetUserTabType12 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  314. # 单控标签 -- 执行存储过程
  315. StoredProcedure::user_label($user_id, 1, $payAmt);
  316. Log::info('CheckAccountsLabel 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  317. // 服务器通知
  318. $url = config('transfer.stock')['url'] . 'notifyPay';
  319. Log::info('中转服参数 ' . json_encode([
  320. 'userid' => $user_id, 'getScore' => $favorable_price, 'score' => $Score,
  321. 'giftsid' => empty($GiftsID) ? 0 : $GiftsID,
  322. 'url' => $url
  323. ]));
  324. $res = (new HttpCurl())->service($url, ['userid' => $user_id, 'getScore' => $favorable_price, 'score' => $Score, 'giftsid' => empty($GiftsID) ? 0 : $GiftsID]);
  325. Log::info('中转服 执行时间:' . ((Helper::millisecond() - $startTime) / 1000), [
  326. 'res' => $res,
  327. ]);
  328. // AF 事件
  329. // (new AppflyerEvent())->event($user_id, '', 'af_purchase_new', $payAmt);
  330. // Log::info('AF 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  331. }
  332. }