WithdrawalLogic.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <?php
  2. namespace App\Http\logic\admin;
  3. use App\dao\Estatisticas\RechargeWithDraw;
  4. use App\Facade\RedisConnect;
  5. use App\Facade\TableName;
  6. use App\Game\Services\AgentSystemService;
  7. use App\Http\helper\Helper;
  8. use App\Http\helper\NumConfig;
  9. use App\Models\AccountsInfo;
  10. use App\Models\AccountWithDrawInfo;
  11. use App\Models\PrivateMail;
  12. use App\Models\Cpf;
  13. use App\Models\RecordUserDataStatistics;
  14. use App\Models\Withdrawal;
  15. use App\Models\WithdrawalChannelPositionConfig;
  16. use App\Services\CashService;
  17. use App\Services\LogDayStatisticalByDayAndChannel;
  18. use App\Services\StoredProcedure;
  19. use App\Util;
  20. use Carbon\Carbon;
  21. use Illuminate\Support\Facades\DB;
  22. use Illuminate\Support\Facades\Redis;
  23. use Illuminate\Support\Facades\Request;
  24. use Mockery\Exception;
  25. class WithdrawalLogic extends BaseLogicController
  26. {
  27. protected $agent; // 代付方
  28. public function __construct()
  29. {
  30. $this->agent = DB::table('agent.dbo.admin_configs')
  31. ->where('type', 'cash')
  32. ->where('status', 1)
  33. ->get();
  34. }
  35. public function waitWithdrawal($GameID, $withdraw_search, $withdraw, $state, $start_time, $end_time, $agent = '', $flag = false, $create_time_sort = '', $orderID = '', $final_start_time = '', $final_end_time = '', $excel = '', $Channel = '', $payState = '', $take_effect = '', $isEmpty = 0, $register_start_time = '', $register_end_time = '', $PackgeName = '',$firstWithDraw=0)
  36. {
  37. $SQL = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw as ow')
  38. ->join('QPAccountsDB.dbo.AccountsInfo as ai', 'ow.UserID', 'ai.UserID')
  39. ->leftJoin('QPAccountsDB.dbo.withdraw_notify as wn', 'ow.OrderID', '=', 'wn.order_sn')
  40. ->leftJoin('QPAccountsDB.dbo.AccountsRecord as ar', function ($join) {
  41. $join->on('ow.RecordID', 'ar.RecordID');
  42. $join->where('ar.type', 1);
  43. });
  44. if($firstWithDraw<2){
  45. $SQL->join(TableName::QPRecordDB() . 'RecordUserTotalStatistics as rds', function ($join) use ($firstWithDraw) {
  46. $join->on('rds.UserID', 'ow.UserID');
  47. $join->where('rds.Withdraw', $firstWithDraw>0?'=':'<>',0);
  48. });
  49. }
  50. if (!$flag) {
  51. empty($state) && $state = 1;
  52. } else {
  53. empty($state) && $state = 100;
  54. if ($isEmpty == 0) {
  55. empty($start_time) && $start_time = date('Y-m-d 00:00:00');
  56. empty($end_time) && $end_time = date('Y-m-d 23:59:59');
  57. } elseif ($isEmpty == 2) {
  58. empty($final_start_time) && $final_start_time = date('Y-m-d 00:00:00');
  59. empty($final_end_time) && $final_end_time = date('Y-m-d 23:59:59');
  60. }
  61. }
  62. !empty($GameID) && $where[] = ['ai.GameID', $GameID];
  63. // !empty($withdraw_search) && $SQL->whereRaw("ow.WithDraw+ow.ServiceFee=$withdraw_search");
  64. // switch ($withdraw) {
  65. // case 100:
  66. // $SQL = $SQL->havingRaw('sum(ow.WithDraw + ow.ServiceFee) < ?', [100]);
  67. // break;
  68. // case 500:
  69. // $SQL = $SQL->havingRaw('sum(ow.WithDraw + ow.ServiceFee) >= ?', [100]);
  70. // $SQL = $SQL->havingRaw('sum(ow.WithDraw + ow.ServiceFee) <= ?', [500]);
  71. // break;
  72. // case 1000:
  73. // $SQL = $SQL->havingRaw('sum(ow.WithDraw + ow.ServiceFee) >= ?', [500]);
  74. // $SQL = $SQL->havingRaw('sum(ow.WithDraw + ow.ServiceFee) <= ?', [1000]);
  75. // break;
  76. // case 1001:
  77. // $SQL = $SQL->havingRaw('sum(ow.WithDraw + ow.ServiceFee) > ?', [1000]);
  78. // break;
  79. // }
  80. if (empty($state)) {
  81. $where[] = ['ow.State', '=', 0];
  82. } elseif ($state == 100) {
  83. $where[] = ['ow.State', '<>', ''];
  84. } elseif ($state == 4) {
  85. $where[] = ['ow.State', '=', $state];
  86. } else {
  87. $where[] = ['ow.State', '=', $state];
  88. }
  89. !empty($start_time) && $where[] = ['CreateDate', '>=', str_replace('T', ' ', $start_time)];
  90. !empty($end_time) && $where[] = ['CreateDate', '<=', str_replace('T', ' ', $end_time)];
  91. if (!empty($agent)) {
  92. $SQL = $SQL->join('agent.dbo.admin_configs as ac', 'ow.agent', '=', 'ac.id');
  93. $where[] = ['agent', $agent];
  94. }
  95. !empty($orderID) && $where[] = ['OrderID', $orderID];
  96. !empty($final_start_time) && $where[] = ['finishDate', '>=', str_replace('T', ' ', $final_start_time)];
  97. !empty($final_end_time) && $where[] = ['finishDate', '<=', str_replace('T', ' ', $final_end_time)];
  98. if(Request::input('final_start_time_cn')) {
  99. $where[] = ['finishDate', '>=',
  100. date('Y-m-d H:i:s', strtotime(
  101. str_replace('T', ' ', Request::input('final_start_time_cn')). ' - 11 hours'
  102. ))
  103. ];
  104. }
  105. if(Request::input('final_end_time_cn')) {
  106. $where[] = ['finishDate', '<=',
  107. date('Y-m-d H:i:s', strtotime(
  108. str_replace('T', ' ', Request::input('final_end_time_cn')). ' - 11 hours'
  109. ))
  110. ];
  111. }
  112. (!empty($Channel) || $Channel === '0') && $where[] = ['ai.Channel', $Channel];
  113. if ($payState == 1) {
  114. $SQL = $SQL->join('QPRecordDB.dbo.RecordUserTotalStatistics as rs', 'ow.UserID', 'rs.UserID')->where('Recharge', '>', 0);
  115. }
  116. if ($take_effect == -1) {
  117. $SQL = $SQL->join('agent.dbo.withdrawal_position_log as log', function ($join) {
  118. $join->on('log.order_sn', '=', 'ow.OrderID');
  119. });
  120. } else {
  121. if (!empty($take_effect)) {
  122. $SQL = $SQL->Join('agent.dbo.admin_users as us', 'ar.admin_id', '=', 'us.id');
  123. $where[] = ['ar.admin_id', '=', $take_effect];
  124. }
  125. }
  126. !empty($register_start_time) && $where[] = ['RegisterDate', '>=', str_replace('T', ' ', $register_start_time) ];
  127. !empty($register_end_time) && $where[] = ['RegisterDate', '<=', str_replace('T', ' ', $register_end_time)];
  128. if (!empty($PackgeName)) {
  129. $SQL = $SQL->leftJoin('QPRecordDB.dbo.RecordPackageName as rn', 'ai.UserID', 'rn.UserID');
  130. $where[] = ['PackgeName', $PackgeName];
  131. }
  132. $adminChannels=session('admin_channels');
  133. if(count($adminChannels)<5)$SQL=$SQL->whereIn('ai.Channel', $adminChannels);
  134. // dd($where);
  135. if (!$flag && $excel) {
  136. AccountWithDrawInfo::verifyOrderDownExcel($SQL, $where);
  137. } elseif ($flag && $excel) {
  138. AccountWithDrawInfo::orderOverDownExcel($SQL, $where);
  139. } else {
  140. $field = ['ai.RegisterIP','ow.locking', 'ar.remarks','ow.remark', 'ow.RecordID', 'ar.RecordID as ar_RecordID', 'wn.state as wn_state', 'ai.NickName', 'ow.AccountsBank', 'ow.BankUserName', 'ow.WithDraw', 'ow.State', 'ow.BankNO', 'ow.ServiceFee', 'ow.withdraw_fee', 'OrderId', 'ai.GameID', 'ai.UserID', 'ai.Channel', 'ar.admin_id', 'ow.agent', 'finishDate', 'PixNum', 'PixType', 'ow.EmailAddress', 'ow.PhoneNumber', 'ow.AdhaarNumber', 'ow.IFSCNumber'];
  141. $SQL1 = clone $SQL;
  142. $SQL2 = clone $SQL;
  143. $SQL3 = clone $SQL;
  144. $model = new AccountWithDrawInfo();
  145. $list = $SQL
  146. ->where($where)
  147. ->lock('with(nolock)')
  148. ->select($field)
  149. ->selectRaw('ar.update_at, CreateDate')
  150. ->orderBy('ow.RecordID', 'desc')
  151. ->paginate(10);
  152. // 申请人数,笔数,金额
  153. $applyUserCount = $SQL1
  154. ->where($where)
  155. ->lock('with(nolock)')
  156. ->selectRaw('count(distinct(ow.UserID)) userCount,(sum(cast(ow.WithDraw as bigint))+sum(cast(ow.ServiceFee as bigint))) WithDraw,count(ow.RecordID) count')->lock('with(nolock)')->first();
  157. if (isset($applyUserCount->WithDraw)) {
  158. $applyUserCount->WithDraw = number_float($applyUserCount->WithDraw / NumConfig::NUM_VALUE);
  159. }
  160. // 到账人数,金额
  161. $overUserCount = $SQL2->where($where)->where('ow.State', 2)->selectRaw('count(distinct(ow.UserID)) userCount,sum(cast(ow.WithDraw as bigint)) WithDraw,count(ow.RecordID) count')
  162. ->lock('with(nolock)')
  163. ->first();
  164. if (isset($overUserCount->WithDraw)) {
  165. $overUserCount->WithDraw = number_float($overUserCount->WithDraw / NumConfig::NUM_VALUE);
  166. }
  167. // 手续费汇总(当前筛选条件下)
  168. $totalWithdrawFeeRow = $SQL3->where($where)->selectRaw('sum(cast(ISNULL(ow.withdraw_fee,0) as bigint)) as total_withdraw_fee')->lock('with(nolock)')->first();
  169. $totalWithdrawFee = isset($totalWithdrawFeeRow->total_withdraw_fee) ? number_float($totalWithdrawFeeRow->total_withdraw_fee / NumConfig::NUM_VALUE) : 0;
  170. $userIDs = [];
  171. $adminIDs = [];
  172. $agentIDs = [];
  173. $accountsInfo=new AccountsInfo();
  174. foreach ($list as &$val) {
  175. $val->actual_arrival = number_float(($val->WithDraw + $val->ServiceFee) / 100); // 实际提现金额
  176. $val->ServiceFee = number_float($val->ServiceFee / 100);
  177. $val->WithDraw = number_float($val->WithDraw / 100);
  178. $val->withdraw_fee_display = number_float(($val->withdraw_fee ?? 0) / NumConfig::NUM_VALUE);
  179. $val->sameNameNum=$accountsInfo->sameWithDrawBankName($val->BankUserName);
  180. $val->sameEmailNum=$accountsInfo->sameWithDrawEmail($val->EmailAddress);
  181. $val->sameMac=$accountsInfo->sameLoginMacCount($val->UserID);
  182. $val->sameCpfCount=Cpf::getCpfCount($val->UserID);
  183. $val->sameIP=$accountsInfo->sameRegisterIPCount($val->RegisterIP);
  184. // $val->sameLIP=$accountsInfo->sameLoginIPCount($val->UserID);
  185. $userIDs[] = $val->UserID;
  186. !empty($val->admin_id) && $adminIDs[] = $val->admin_id;
  187. !empty($val->agent) && $agentIDs[] = $val->agent;
  188. }
  189. unset($val);
  190. $agentName = DB::connection('write')->table('agent.dbo.admin_configs')
  191. ->whereIn('id', $agentIDs)
  192. ->pluck('name', 'id')->toArray();
  193. $adminName = DB::connection('write')->table('agent.dbo.admin_users')
  194. ->whereIn('id', $adminIDs)
  195. ->pluck('account', 'id')->toArray();
  196. $userIDs = array_unique($userIDs);
  197. // 银行卡绑定信息评估
  198. $BankNO = $model->BankNO($userIDs);
  199. $IP = $model->ip($userIDs);
  200. $orders = DB::connection('read')->table('QPAccountsDB.dbo.YN_VIPAccount')->whereIn('UserID', $userIDs)
  201. ->selectRaw('Recharge,UserID')
  202. ->lock('with(nolock)')
  203. ->pluck('Recharge', 'UserID')->toArray();
  204. $Withdrawal_M = new Withdrawal();
  205. // 评估档位
  206. foreach ($list as &$val) {
  207. /*
  208. * 评估档位 累计充值金额 获得佣金
  209. * 低 ≥100 ≥100
  210. * 普通 >0<100 >0<100
  211. * 较高 0 >0
  212. * 高 0 0
  213. * */
  214. $cz_money = $orders[$val->UserID] ?? 0;
  215. if (isset($IP[$val->UserID]) && $IP[$val->UserID] > 1) {
  216. $gear = '高';
  217. } elseif (isset($BankNO[$val->UserID]) && $BankNO[$val->UserID] > 1) {
  218. $gear = '高';
  219. } elseif ($cz_money >= 100) {
  220. $gear = '低';
  221. } elseif ($cz_money > 0 && $cz_money < 100) {
  222. $gear = '普通';
  223. } elseif ($cz_money > 0) {
  224. $gear = '较高';
  225. } elseif ($cz_money == 0) {
  226. $gear = '高';
  227. }
  228. $val->gear = $gear ?? '';
  229. $val->assessment = $Withdrawal_M->assessment($cz_money);
  230. $val->States = $Withdrawal_M->stateText($val->State, $val->locking);
  231. $val->account = isset($adminName[$val->admin_id]) ? $adminName[$val->admin_id] : '';
  232. $val->name = isset($agentName[$val->agent]) ? $agentName[$val->agent] : '';
  233. }
  234. }
  235. // 改已读
  236. // DB::table('QPAccountsDB.dbo.OrderWithDraw')->update(['admin_read' => 1]);
  237. $allChannel = $adminChannels;//DB::connection('read')->table('QPPlatformDB.dbo.ChannelPackageName')->selectRaw('Channel')->groupBy('Channel')->pluck('Channel');
  238. $start_time = Helper::timeChange($start_time);
  239. $end_time = Helper::timeChange($end_time);
  240. $final_start_time = Helper::timeChange($final_start_time);
  241. $final_end_time = Helper::timeChange($final_end_time);
  242. // 提现管理员
  243. $withdrawal_administrator = DB::connection('write')->table('agent.dbo.withdrawal_administrator')->pluck('account', 'admin_id');
  244. // 获取包名
  245. $ChannelPackageName = DB::connection('write')->table('QPPlatformDB.dbo.ChannelPackageName')
  246. ->select('PackageName', 'Channel')
  247. ->pluck('PackageName', 'Channel');
  248. return compact('applyUserCount', 'overUserCount', 'list', 'totalWithdrawFee', 'payState', 'allChannel', 'Channel', 'GameID', 'withdraw_search', 'withdraw', 'state', 'start_time', 'end_time', 'agent', 'orderID', 'final_start_time', 'final_end_time', 'take_effect', 'withdrawal_administrator', 'isEmpty', 'register_start_time', 'register_end_time', 'PackgeName', 'ChannelPackageName');
  249. }
  250. public function verify_agree($ids)
  251. {
  252. $id_arr = explode(',', $ids);
  253. $order = ['WithDraw', 'ServiceFee', 'ow.RecordID', 'ai.GameID', 'ai.UserID', 'ai.Channel','PixType'];
  254. $list = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw as ow')
  255. ->join('QPAccountsDB.dbo.AccountsInfo as ai', 'ow.UserID', 'ai.UserID')
  256. ->whereIn('RecordID', $id_arr)
  257. ->select($order)
  258. ->lock('with(nolock)')
  259. ->paginate(10);
  260. $channelConfigs = [];
  261. $res = WithdrawalChannelPositionConfig::where('status', 1)->get();
  262. foreach ($res as $v) {
  263. $channelConfigs[$v->channel] = $v;
  264. }
  265. // $config = DB::table(TableName::agent() . 'withdrawal_position_config')
  266. // ->where('status', 1)
  267. // ->first();
  268. foreach ($list as &$value) {
  269. $value->WithDraw = number_float(($value->WithDraw + $value->ServiceFee) / 100);
  270. if($value->PixType==66){
  271. //代理订单
  272. $value->agent=
  273. $this->agent=[(object)[
  274. "id" => "6666",
  275. "name" => "代理申请为玩家提现",
  276. "config_key" => "AgentCashOut",
  277. "config_value" => "66",
  278. "type" => "cash",
  279. "created_at" => null,
  280. "updated_at" => null,
  281. "admin_id" => "11",
  282. "status" => "1",
  283. "sort" => "0",
  284. "remarks" => null,
  285. "new_pay_type" => "0",
  286. "pic_num" => null,
  287. "pay_error" => "0",
  288. "cpf_first" => "0"
  289. ]];
  290. continue;
  291. }
  292. if(isset($channelConfigs[$value->Channel]) && $channelConfigs[$v->channel]->limit_manual_review_show) {
  293. $c = $channelConfigs[$value->Channel];
  294. $value->agent = array_filter($this->agent->toArray(), function ($item) use ($c) {
  295. return $item->config_value == $c->agent;
  296. });
  297. }
  298. }
  299. $agent = $this->agent;
  300. return compact('list', 'agent');
  301. }
  302. public function save($data, $admin_id)
  303. {
  304. $redis = new RedisConnect();
  305. $limitKey = 'withdraw';
  306. $res = $redis->redis()->set($limitKey, 1, 3);
  307. if (empty($res)) return apiReturnFail('频繁操作');
  308. $WithdrawalModel = new Withdrawal();
  309. $id_arr = explode(',', $data['ids']);
  310. // 代付方
  311. $agent = $data['agent'] ?? '';
  312. if (empty($agent) && $data['agree'] == 'true') {
  313. return apiReturnFail('请选择代付方');
  314. }
  315. if ($data['agree'] == 'true') {
  316. $state = 2;
  317. } else {
  318. $state = -1;
  319. }
  320. try {
  321. foreach ($id_arr as $val) {
  322. $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('RecordID', $val)->first();
  323. if($query->PixType!=66) {
  324. if ($query->State != 1) {
  325. return apiReturnFail('订单重复操作');
  326. }
  327. if ($query->locking == 1) {
  328. return apiReturnFail('订单已被锁定');
  329. }
  330. $redis1 = Redis::connection();
  331. $order_sn = $query->OrderId;
  332. if ($redis1->exists($order_sn . 'key1')) {
  333. return apiReturnFail('订单已被锁定new');
  334. }
  335. $redis1->set($order_sn.'key1', $order_sn, 3600 * 24);
  336. }
  337. // 拒绝提现 ,发邮件给用户
  338. if ($state == -1) {
  339. // 发送邮件给玩家
  340. $WithDraw = $query->WithDraw + $query->ServiceFee;
  341. PrivateMail::ClearDrawMail($query->UserID, $query->OrderId, $WithDraw);
  342. // 修改订单状态
  343. DB::table('QPAccountsDB.dbo.OrderWithDraw')->where('RecordID', $val)->update(['State' => $state, 'locking' => 2]);
  344. $remarks = $data['remarks'] ?? '';
  345. // 添加用户提现操作记录
  346. (new AccountsRecordLogic())->create_record($val, 1, $state, $remarks, $admin_id, 1);
  347. if($query->PixType==66)AgentSystemService::FailWithdraw($query);
  348. } else if($query->PixType==66){
  349. DB::table('QPAccountsDB.dbo.OrderWithDraw')->where('RecordID', $val)->update(['State' => 5, 'agent' => 660000+$query->AccountsBank]);
  350. //通知AgentSystem完成,并锁定
  351. AgentSystemService::FinishWithdraw($query);
  352. } else { // 发起提现
  353. // 验证用户提现方式
  354. $verifyAccountWithdrawal = $WithdrawalModel->AccountWithDrawInfo($query->PixNum, $agent);
  355. if (!$verifyAccountWithdrawal) return apiReturnFail('不支持的提现格式!');
  356. $state = 5;
  357. # 获取哪个三方提现
  358. // $orderAgent = $WithdrawalModel->getAgentChannel($agent);
  359. $agentID = DB::connection('write')->table('agent.dbo.admin_configs')->where('config_value', strval($agent))
  360. ->select('id')->first()->id ?? '';
  361. $orderAgent = strval($agentID);
  362. // 改变状态处理中
  363. DB::table('QPAccountsDB.dbo.OrderWithDraw')->where('RecordID', $val)->update(['State' => $state, 'agent' => $orderAgent]);
  364. // 添加用户提现操作记录
  365. (new AccountsRecordLogic())->create_record($val, 1, $state, '', $admin_id, 1);
  366. $RecordID = $query->RecordID;
  367. $amount = $query->WithDraw;
  368. $accountName = $query->BankUserName;
  369. $email = $query->EmailAddress;
  370. $phone = $query->PhoneNumber;
  371. $PixNum = $query->PixNum;
  372. $PixType = $query->PixType;
  373. $OrderId = $query->OrderId;
  374. $IFSCNumber = $query->IFSCNumber;
  375. $BranchBank = $query->BranchBank;
  376. $BankNO = $query->BankNO;
  377. if($PixType>10){
  378. Util::WriteLog("pixerror",$query);
  379. $oldRecord=DB::table('QPAccountsDB.dbo.OrderWithDraw')->where('UserID', $query->UserID)->where('State',2)->first();
  380. if(isset($oldRecord)&&isset($oldRecord->PixType)){
  381. $PixType=$oldRecord->PixType;
  382. }else{
  383. $PixType=1;
  384. }
  385. }
  386. $service = CashService::payment($agent);
  387. $result = $service->payment($RecordID, $amount, $accountName, $phone, $email, $OrderId, $PixNum, $PixType, $IFSCNumber, $BranchBank, $BankNO);
  388. if ($result === 'fail') {
  389. return apiReturnFail('订单提现失败');
  390. }
  391. }
  392. // 判断管理员是否已经存在
  393. $withdrawal_administrator = DB::connection('write')->table('agent.dbo.withdrawal_administrator')->where('admin_id', $admin_id)->first();
  394. if (!$withdrawal_administrator) {
  395. $dataArr = [
  396. 'admin_id' => $admin_id,
  397. 'account' => session('admin')->account ?? ''
  398. ];
  399. DB::connection('write')->table('agent.dbo.withdrawal_administrator')->insert($dataArr);
  400. }
  401. }
  402. $redis->redis()->del($limitKey);
  403. return apiReturnSuc();
  404. } catch (Exception $e) {
  405. $redis->redis()->del($limitKey);
  406. return apiReturnFail($e->getMessage());
  407. }
  408. }
  409. // 用户提充统计
  410. public function userWithdrawalStatistics($date, $excel, $gameID, $channel)
  411. {
  412. $dateID = Carbon::parse($date)->format('Ymd');
  413. $where[] = ['Withdraw', '>', 0];
  414. !empty($date) && $where[] = ['DateID', $dateID];
  415. !empty($gameID) && $where[] = ['GameID', $gameID];
  416. (!empty($channel) || $channel === 0) && $where[] = ['Channel', $channel];
  417. $buildSql = DB::table(TableName::QPRecordDB() . 'RecordUserDataStatisticsNew as rn')
  418. ->where($where)
  419. ->join(TableName::QPAccountsDB() . 'AccountsInfo as ai', 'rn.UserID', 'ai.UserID')
  420. ->select('GameID', 'rn.UserID', 'Withdraw', 'Recharge')
  421. ->orderBy('Withdraw', 'desc');
  422. // 昨天的提现用户,昨日提现多少,昨日充值多少,历史充值多少,历史提现多少
  423. $list = $excel ? $buildSql->get() : $buildSql->paginate(15);
  424. $UserIDs = [];
  425. foreach ($list as $value) {
  426. $UserIDs[] = $value->UserID;
  427. }
  428. $total = DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')
  429. ->whereIn('UserID', $UserIDs)
  430. ->select('Withdraw', 'Recharge', 'UserID')
  431. ->get();
  432. foreach ($list as &$value) {
  433. $value->Withdraw = number_float($value->Withdraw / 100);
  434. foreach ($total as $val) {
  435. if ($val->UserID == $value->UserID) {
  436. $value->HisWithdraw = number_float($val->Withdraw / 100);
  437. $value->HisRecharge = $val->Recharge;
  438. }
  439. }
  440. }
  441. if ($excel) {
  442. $title = ['游戏ID', '用户ID', '今日提现', '今日充值', '历史提现', '历史充值'];
  443. $list = json_decode(json_encode($list), true);
  444. downloadExcel($list, $title, '统计' . date('YmdHis'));
  445. return;
  446. } else {
  447. $channelList = DB::table(TableName::QPPlatformDB().'ChannelPackageName')
  448. ->select('Channel')
  449. ->groupBy('Channel')
  450. ->pluck('Channel','Channel')->toArray();
  451. return [$list,$channelList];
  452. }
  453. }
  454. /**
  455. * 后台模拟触发提现回调
  456. * @param $withdrawOrder
  457. * @return bool
  458. */
  459. public function compensateNotify($withdrawOrder)
  460. {
  461. $withdraw_data = [
  462. 'State' => 2,
  463. 'finishDate' => now(),
  464. ];
  465. $UserID = $withdrawOrder->UserID;
  466. $TakeMoney = $withdrawOrder->WithDraw + $withdrawOrder->ServiceFee;
  467. // 增加提现记录
  468. $first = DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $UserID)->first();
  469. if ($first) {
  470. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $UserID)->increment('TakeMoney', $TakeMoney);
  471. } else {
  472. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->insert(['TakeMoney' => $TakeMoney, 'UserID' => $UserID]);
  473. }
  474. // 免审的时候,修改免审状态
  475. $withdrawal_position_log = DB::connection('write')
  476. ->table('agent.dbo.withdrawal_position_log')->where('order_sn', $withdrawOrder->OrderId)
  477. ->first();
  478. if ($withdrawal_position_log) {
  479. DB::connection('write')->table('agent.dbo.withdrawal_position_log')
  480. ->where('order_sn', $withdrawOrder->OrderId)
  481. ->update(['take_effect' => 2, 'update_at' => date('Y-m-d H:i:s')]);
  482. }
  483. $ServiceFee = $withdrawOrder->ServiceFee;
  484. // 增加用户提现值
  485. RecordUserDataStatistics::updateOrAdd($UserID, $TakeMoney, 0, $ServiceFee);
  486. // 给用户发邮件
  487. PrivateMail::successMail($UserID, $withdrawOrder->OrderId, $TakeMoney);
  488. StoredProcedure::addPlatformData($UserID, 4, $TakeMoney);
  489. // 数据统计后台 -- 提现记录添加
  490. (new RechargeWithDraw())->withDraw($UserID, $TakeMoney);
  491. $redis = Redis::connection();
  492. $redis->incr('draw_'.date('Ymd').$UserID);
  493. $RecordData = [
  494. 'before_state' => $withdrawOrder->State,
  495. 'after_state' => $withdraw_data['State'] ?? 0,
  496. 'RecordID' => $withdrawOrder->RecordID,
  497. 'update_at' => date('Y-m-d H:i:s')
  498. ];
  499. $notify_data = [
  500. 'state' => 1,
  501. 'finish_at' => now(),
  502. 'casOrdNo' => '',
  503. 'extra' => '{}',
  504. 'created_at' => now(),
  505. 'updated_at' => now(),
  506. 'order_sn' => $withdrawOrder->OrderId,
  507. 'amount' => $TakeMoney,
  508. ];
  509. // 添加用户提现操作记录
  510. DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')
  511. ->updateOrInsert(['RecordID' => $withdrawOrder->RecordID, 'type' => 1], $RecordData);
  512. DB::connection('write')->table('QPAccountsDB.dbo.withdraw_notify')
  513. ->updateOrInsert(['order_sn' => $withdrawOrder->OrderId], $notify_data);
  514. DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')
  515. ->where('OrderId', $withdrawOrder->OrderId)
  516. ->update($withdraw_data);
  517. if (isset($withdraw_data['State']) && $withdraw_data['State'] == 2) {
  518. // 单控标签
  519. StoredProcedure::user_label($UserID, 2, $TakeMoney);
  520. // 渠道后台埋点
  521. (new LogDayStatisticalByDayAndChannel())->updateData($UserID, 2);
  522. }
  523. return true;
  524. }
  525. }