OrderServices.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 == 301) { // 礼包--首冲
  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. }else if ($GiftsID > 400) {
  71. $Status = 1;
  72. $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();
  73. if(!$recharge_gear){
  74. $Recharge=$payAmt;
  75. $give=0;
  76. }else{
  77. $Recharge = $recharge_gear->favorable_price;
  78. $give = $recharge_gear->give;
  79. }
  80. $favorable_price = $Recharge + $give;
  81. $czReason = 1;
  82. $cjReason = 45;
  83. }
  84. } else { // 普通订单
  85. $Status = 1;
  86. $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();
  87. if(!$recharge_gear){
  88. $Recharge=$payAmt;
  89. $give=0;
  90. }else{
  91. $Recharge = $recharge_gear->favorable_price;
  92. $give = $recharge_gear->give;
  93. }
  94. $favorable_price = $Recharge + $give;
  95. $czReason = 1;
  96. $cjReason = 45;
  97. }
  98. return [$give, $favorable_price, $Recharge, $czReason, $cjReason, $second_give];
  99. }
  100. /**
  101. * 添加玩家充值记录
  102. * @param $user_id
  103. * @param $payAmt
  104. * @param $favorable_price
  105. * @param $order_sn
  106. * @param $GiftsID
  107. * @param $Recharge
  108. * @param $czReason
  109. * @param $give
  110. * @param $cjReason
  111. * @param $AdId
  112. * @param $eventType
  113. */
  114. public function addRecord($user_id, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType)
  115. {
  116. if ($payAmt > 0) {
  117. // 增加玩家充值金额
  118. $query = DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  119. ->where('UserID', $user_id)
  120. ->value('Recharge');
  121. if ($query) {
  122. DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  123. ->where('UserID', $user_id)
  124. ->increment('Recharge', $payAmt);
  125. } else {
  126. DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
  127. ->where('UserID', $user_id)
  128. ->insert(['Recharge' => $payAmt, 'UserID' => $user_id]);
  129. /* TODO
  130. * 金币银币切换 GameScoreInfo 将score的数据复制到 InsureScore 把Score 字段置0 把 ScoreChange字段设置成1
  131. * 清理用户的数据
  132. * GameScoreInfo的MaxScore MaxWinscore清0
  133. * RecordUserTotalStatistics 表 数据清0
  134. * RecordUserDataStatisticsNew 表数据 清0
  135. * RecordUserGameCount 关于用户的数据删除
  136. *
  137. */
  138. }
  139. }
  140. if ($Recharge > 0) {
  141. // 添加日志记录表
  142. StoredProcedure::addPlatformData($user_id, 3, $Recharge * NumConfig::NUM_VALUE);
  143. // 增加用户金币变化记录
  144. $AfterScore = RecordScoreInfo::addScore($user_id, ($Recharge * NumConfig::NUM_VALUE), $czReason); #充值
  145. if ($AfterScore) {
  146. RecordScoreInfo::addScore($user_id, ($give * NumConfig::NUM_VALUE), $cjReason, $AfterScore); #赠送彩金
  147. }
  148. }
  149. $favorable_price = $favorable_price * NumConfig::NUM_VALUE;
  150. $firstScore = DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user_id)->value('Score');
  151. $Score = $favorable_price + $firstScore;
  152. if ($payAmt > 0) {
  153. try {
  154. $userInfo = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')
  155. ->where('UserID', $user_id)
  156. ->first();
  157. $channelConfig = DB::table('QPPlatformDB.dbo.ChannelPackageName')->where('Channel', $userInfo->Channel)->first();
  158. // Util::WriteLog('xxxxx', [$userInfo->Channel, $channelConfig, $query, $firstScore, $channelConfig->Rate > 0 && $channelConfig->ContrlScore > 0, $channelConfig->MinScore * NumConfig::NUM_VALUE > $firstScore]);
  159. if ($channelConfig && !$query && false) {
  160. if ($channelConfig->Rate > 0 && $channelConfig->ContrlScore > 0) {
  161. if ($channelConfig->MinScore * NumConfig::NUM_VALUE >= $firstScore) {
  162. $openGames = config('games.openKGame');
  163. $query = DB::table('QPTreasureDB.dbo.UserScoreControl')->where('UserID', $user_id)->first();
  164. if (!$query) {
  165. $build_sql = DB::connection('write')->table('QPTreasureDB.dbo.UserScoreControl');
  166. //Util::WriteLog('control_old_user',$list);
  167. $data = [
  168. 'ControlScore' => (int)($channelConfig->ContrlScore * NumConfig::NUM_VALUE),
  169. 'EffectiveScore' => 0,
  170. 'ControlKindID' => -1,
  171. 'Remarks' => '',
  172. 'InsertDate' => date('Y-m-d H:i:s'),
  173. 'ControlRadian' => 0
  174. ];
  175. $build_sql->updateOrInsert(['UserID' => $user_id], $data);
  176. foreach ($openGames as $GameID) {
  177. $KindData = [
  178. 'UserID' => $user_id,
  179. 'KindID' => $GameID,
  180. 'ControlRadian' => $channelConfig->Rate,
  181. 'ControlDate' => date('Y-m-d H:i:s')
  182. ];
  183. DB::connection('write')->table('QPTreasureDB.dbo.UserControlKind')->updateOrInsert(['UserID' => $user_id, 'KindID' => $GameID], $KindData);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. } catch (\Exception $exception) {
  190. }
  191. // 记录订单变化后金币
  192. DB::connection('write')->table('agent.dbo.order')
  193. ->where('order_sn', $order_sn)
  194. ->update(['after_amount' => $Score]);
  195. // 增加用户充值变化值
  196. RecordUserDataStatistics::updateOrAdd($user_id, 0, $payAmt);
  197. }
  198. // 不是周卡的时候执行
  199. if ($GiftsID < 100 || $GiftsID >= 200) {
  200. // 增加用户金币
  201. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user_id)->increment('Score', $favorable_price);
  202. }
  203. // 充值推广佣金
  204. // (new AgentUser())->reward($user_id, $payAmt, $order_sn);
  205. //
  206. // 周卡 -- 执行存储过程
  207. if (isset($GiftsID) && $GiftsID > 100 && $GiftsID < 200) {
  208. $CardID = (int)$GiftsID - 100;
  209. StoredProcedure::BuyMonthCard($user_id, $CardID, $order_sn);
  210. // 开始执行时间
  211. $startTime = Helper::millisecond();
  212. Log::info('GSP_GP_BuyMonthCard 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  213. }
  214. //设置第二天要领取
  215. if (isset($GiftsID) && $GiftsID >= 200 && $GiftsID < 300 ) {
  216. $first = DB::table('agent.dbo.recharge_gear')->where('gift_id', $GiftsID)->select('gift_id', 'money', 'give', 'favorable_price', 'second_give')->first();
  217. if ($first && $first->second_give > 0) {
  218. $fpkey = 'Firstpay_' . $user_id;
  219. $data = (array)$first;
  220. $data['buytime'] = time();
  221. $data['czReason'] = $czReason;
  222. $data['cjReason'] = $cjReason;
  223. Redis::set($fpkey, json_encode($data));
  224. DB::table(TableName::agent() . 'guide_payment')->updateOrInsert([
  225. 'UserID' => $user_id,
  226. 'GiftID' => $GiftsID,
  227. 'CreateTime' => now()
  228. ], ['UserID' => $user_id]);
  229. }
  230. }
  231. //设置第二天要领取
  232. if (!empty($GiftsID) && $GiftsID >= 300 && $GiftsID < 400 ) {
  233. Redis::del('repay_temp_'.$user_id);
  234. }
  235. // 数据统计后台 -- 充值记录添加
  236. (new RechargeWithDraw())->recharge($user_id, $payAmt);
  237. // (new RechargeWithDraw())->recharge($user_id, $Recharge);
  238. if ($AdId && $payAmt) AfEvent::dispatch([$user_id, $payAmt, $AdId, $eventType]);
  239. try {
  240. //新邀请
  241. (new AgentController())->processDeposit($user_id, $payAmt,$order_sn);
  242. // AgentService::recordPerformance($user_id, $payAmt * NumConfig::NUM_VALUE);
  243. } catch (\Exception $exception) {
  244. Util::WriteLog("AgentService", $exception->getTraceAsString());
  245. }
  246. return [$Score];
  247. }
  248. /**
  249. * 执行存储过程
  250. * @param $user_id
  251. * @param $payAmt
  252. * @param $favorable_price
  253. * @param $Score
  254. * @param $GiftsID
  255. */
  256. public function storedProcedure($user_id, $payAmt, $favorable_price, $Score, $GiftsID)
  257. {
  258. // 开始执行时间
  259. $startTime = Helper::millisecond();
  260. // 执行存储过程 -- 防刷机制
  261. StoredProcedure::SetUserTabType($user_id);
  262. Log::info('GSP_GP_SetUserTabType12 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  263. # 单控标签 -- 执行存储过程
  264. StoredProcedure::user_label($user_id, 1, $payAmt);
  265. Log::info('CheckAccountsLabel 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  266. // 服务器通知
  267. // $url = config('transfer.stock')['url'] . 'notifyPay';
  268. // Log::info('中转服参数 ' . json_encode([
  269. // 'userid' => $user_id, 'getScore' => $favorable_price, 'score' => $Score,
  270. // 'giftsid' => empty($GiftsID) ? 0 : $GiftsID,
  271. // 'url' => $url
  272. // ]));
  273. // $res = (new HttpCurl())->service($url, ['userid' => $user_id, 'getScore' => $favorable_price, 'score' => $Score, 'giftsid' => empty($GiftsID) ? 0 : $GiftsID]);
  274. //
  275. // Log::info('中转服 执行时间:' . ((Helper::millisecond() - $startTime) / 1000), [
  276. // 'res' => $res,
  277. // ]);
  278. // AF 事件
  279. // (new AppflyerEvent())->event($user_id, '', 'af_purchase_new', $payAmt);
  280. // Log::info('AF 执行时间:' . ((Helper::millisecond() - $startTime) / 1000));
  281. }
  282. }