AcceptorService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\AcceptorModel;
  13. use App\Models\AccountLogModel;
  14. use App\Models\BalanceLogModel;
  15. use App\Models\MemberBankModel;
  16. use App\Models\MemberModel;
  17. use App\Services\BaseService;
  18. use App\Services\ConfigService;
  19. use App\Services\RedisService;
  20. use App\Services\WalletService;
  21. use Illuminate\Support\Facades\DB;
  22. /**
  23. * 承兑商服务管理-服务类
  24. * @author laravel开发员
  25. * @since 2020/11/11
  26. * @package App\Services\Api
  27. */
  28. class AcceptorService extends BaseService
  29. {
  30. // 静态对象
  31. protected static $instance = null;
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. * MerchantService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new AcceptorModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 获取缓存列表
  55. * @param $position
  56. * @param int $num
  57. * @return array|mixed
  58. */
  59. public function getDataList($params, $pageSize = 15, $refresh = false, $field = '')
  60. {
  61. $page = request()->post('page', 1);
  62. $cacheKey = "caches:acceptor:page_{$page}_" . md5(json_encode($params) . $pageSize);
  63. $datas = RedisService::get($cacheKey);
  64. $data = isset($datas['data']) ? $datas['data'] : [];
  65. if ($datas && $data && !$refresh) {
  66. return [
  67. 'list' => $data,
  68. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  69. 'pageSize' => $pageSize
  70. ];
  71. }
  72. $field = $field ? $field : 'lev_a.*,lev_b.nickname,lev_b.username,lev_b.avatar';
  73. $order = 'lev_a.id desc';
  74. $datas = $this->model->from('acceptor as a')
  75. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  76. ->where(['a.mark' => 1, 'b.mark' => 1])
  77. ->where('a.quota','>',0)
  78. ->whereIn('a.trade_status', [1, 2])
  79. ->where(function ($query) use ($params) {
  80. $kw = isset($params['kw']) ? trim($params['kw']) : '';
  81. if ($kw) {
  82. $query->where('a.name', 'like', "%{$kw}%");
  83. }
  84. })
  85. ->where(function ($query) use ($params) {
  86. // 状态
  87. $status = isset($params['status']) && $params['status'] >= 0 ? intval($params['status']) : 2;
  88. if ($status > 0) {
  89. $query->where('a.status', $status);
  90. }
  91. $payType = isset($params['pay_type']) && $params['pay_type'] >0 ? intval($params['pay_type']) : 0;
  92. if($payType){
  93. $query->where('a.pay_type', $payType);
  94. }
  95. })
  96. ->selectRaw($field)
  97. ->orderByRaw($order)
  98. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  99. $datas = $datas ? $datas->toArray() : [];
  100. if ($datas) {
  101. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  102. $currency = isset($params['currency']) && $params['currency']? strtoupper($params['currency']) : 'CNY';
  103. $tradeType = isset($params['trade_type']) && $params['trade_type']? intval($params['trade_type']) : 1;
  104. $cnyPrice = $this->getRealPrice(1,$currency, $tradeType);
  105. foreach ($datas['data'] as &$item) {
  106. $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
  107. $item['currency_price'] = $this->getRealPrice(1, $currency,0);
  108. $item['currency_quota'] = $this->getRealPrice(floatval($item['quota']), $currency, 3);
  109. $item['currency_rate'] = $currency=='CNY'? 1 : WalletService::make()->getRateByCNY($currency);
  110. $item['usdt_price'] = moneyFormat(1 / $xdPrice, 2);
  111. $item['price'] = $cnyPrice;
  112. $item['time'] = $item['time']>60? intval($item['time']/60) : 20;
  113. }
  114. unset($item);
  115. RedisService::set($cacheKey, $datas, rand(3, 5));
  116. }
  117. return [
  118. 'list' => isset($datas['data']) ? $datas['data'] : [],
  119. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  120. 'pageSize' => $pageSize
  121. ];
  122. }
  123. /**
  124. * @param $price
  125. * @param $urrency
  126. * @param int $tradeType
  127. * @return string
  128. */
  129. public function getRealPrice($money,$currency='CNY', $tradeType=1)
  130. {
  131. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  132. if($usdtPrice<=0){
  133. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  134. $usdtPrice = $usdtCnyPrice>0 && $usdtCnyPrice< 100? $usdtCnyPrice : 0;
  135. }
  136. // 人民币与其他币的汇率价格
  137. $currencyPrice = $currency=='CNY'? 1 : WalletService::make()->getRateByCNY($currency);
  138. // 买价
  139. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  140. $buyPrice = moneyFormat($money/$xdPrice,4); // 星豆价格
  141. $cnyPrice = moneyFormat($buyPrice * $usdtPrice,4); // 币种价格
  142. if($tradeType == 1){
  143. $xdBuyPriceRate = ConfigService::make()->getConfigByCode('trade_buy_price_rate', 0);
  144. $xdBuyPriceRate = $xdBuyPriceRate>0 && $xdBuyPriceRate<100? $xdBuyPriceRate : 0;
  145. $cnyBuyPrice = moneyFormat($cnyPrice + ($cnyPrice * $xdBuyPriceRate/100), 4); // 上浮价格
  146. return moneyFormat($cnyBuyPrice * $currencyPrice, 4); // 汇率价格
  147. }else if($tradeType == 2){
  148. $xdSellPriceRate = ConfigService::make()->getConfigByCode('trade_sell_price_rate', 0);
  149. $xdSellPriceRate = $xdSellPriceRate>0 && $xdSellPriceRate<100? $xdSellPriceRate : 0;
  150. $cnyBuyPrice = moneyFormat($cnyPrice - ($cnyPrice * $xdSellPriceRate/100), 4); // 下浮价格
  151. return moneyFormat($cnyBuyPrice * $currencyPrice, 4); // 汇率价格
  152. }
  153. return moneyFormat($cnyPrice * $currencyPrice, 4); // 汇率价格
  154. }
  155. /**
  156. * 获取信息
  157. * @param $where
  158. * @param array $field
  159. * @return array|mixed
  160. */
  161. public function getInfo($id, $field = [], $userId=0)
  162. {
  163. if($id){
  164. $where = ['a.id'=> $id,'a.mark'=>1];
  165. }else if($userId){
  166. $where = ['a.user_id'=> $userId,'a.mark'=>1];
  167. }else{
  168. return false;
  169. }
  170. $defaultField = ['a.id', 'a.user_id', 'a.realname','a.name', 'a.mobile','a.telegram', 'a.quota','a.pay_type', 'a.usdt', 'a.status','b.nickname','b.avatar'];
  171. $field = $field ? $field : $defaultField;
  172. $info = $this->model->from('acceptor as a')
  173. ->leftjoin('member as b','b.id','=','a.user_id')
  174. ->where($where)
  175. ->select($field)
  176. ->first();
  177. $info = $info ? $info->toArray() : [];
  178. if ($info) {
  179. $currency = request()->post('currency','CNY');
  180. $tradeType = request()->post('trade_type',1);
  181. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  182. $cnyPrice = $this->getRealPrice(1,$currency, $tradeType);
  183. $info['currency_price'] = $this->getRealPrice(1, $currency,0);
  184. $info['currency_quota'] = $this->getRealPrice(floatval($info['quota']), $currency, 3);
  185. $info['currency_rate'] = $currency=='CNY'? 1 : WalletService::make()->getRateByCNY($currency);
  186. $info['usdt_price'] = moneyFormat(1 / $xdPrice, 2);
  187. $info['price'] = $cnyPrice;
  188. $info['avatar'] = get_image_url($info['avatar']);
  189. }
  190. return $info;
  191. }
  192. /**
  193. * 修改信息
  194. * @param $userId
  195. * @param $params
  196. * @return array|false|int[]
  197. */
  198. public function saveInfo($userId, $params)
  199. {
  200. // 验证是否入驻过和入驻状态
  201. $info = $this->model->where(['user_id' => $userId])->select('id', 'realname', 'trade_status', 'status', 'mark')->first();
  202. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  203. $mark = isset($info['mark']) ? $info['mark'] : 0;
  204. $id = isset($info['id']) ? $info['id'] : 0;
  205. if ($userId && empty($info)) {
  206. $this->error = 2216;
  207. return false;
  208. }
  209. // 是否被冻结
  210. if ($id && $tradeStatus == 4 && $mark) {
  211. $this->error = 2202;
  212. return false;
  213. }
  214. // 验证账户是否正常
  215. $userInfo = MemberService::make()->getCacheInfo(['id' => $userId], ['id', 'status']);
  216. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  217. if (empty($userInfo) || $status != 1) {
  218. $this->error = 2017;
  219. return false;
  220. }
  221. // 入驻数据
  222. $data = [
  223. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  224. 'user_id' => $userId,
  225. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  226. 'telegram' => isset($params['telegram']) ? $params['telegram'] : '',
  227. 'price_float' => isset($params['price_float']) ? $params['price_float'] : '',
  228. 'create_time' => time(),
  229. 'update_time' => time(),
  230. 'mark' => 1,
  231. ];
  232. // 写入数据
  233. if ($id) {
  234. if ($this->model->where(['id' => $id])->update($data)) {
  235. $this->error = 2228;
  236. RedisService::keyDel("caches:acceptor:info:temp_{$userId}*");
  237. return ['id' => $id];
  238. } else {
  239. $this->error = 2229;
  240. return false;
  241. }
  242. } else {
  243. if ($merchId = $this->model->insertGetId($data)) {
  244. $this->error = 2228;
  245. RedisService::keyDel("caches:acceptor:info:temp_{$userId}*");
  246. return ['id' => $id];
  247. } else {
  248. $this->error = 2229;
  249. return false;
  250. }
  251. }
  252. }
  253. /**
  254. * 申请入驻
  255. * @param $userId
  256. * @param $params
  257. * @return array|false|int[]
  258. */
  259. public function apply($userId, $params)
  260. {
  261. // 验证是否入驻过和入驻状态
  262. $info = $this->model->where(['user_id' => $userId])->select('id', 'name', 'trade_status', 'status', 'mark')->first();
  263. $status = isset($info['status']) ? $info['status'] : 0;
  264. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  265. $mark = isset($info['mark']) ? $info['mark'] : 0;
  266. $id = isset($info['id']) ? $info['id'] : 0;
  267. if ($id && $status == 2 && $mark) {
  268. $this->error = 2201;
  269. return false;
  270. }
  271. // 是否被冻结
  272. if ($id && $tradeStatus == 4 && $mark) {
  273. $this->error = 2202;
  274. return false;
  275. }
  276. // 验证账户是否正常
  277. $userInfo = MemberService::make()->getCacheInfo(['id' => $userId], ['id', 'status']);
  278. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  279. if (empty($userInfo) || $status != 1) {
  280. $this->error = 2017;
  281. return false;
  282. }
  283. // 支付方式验证
  284. $payType = isset($params['pay_type']) && $params['pay_type']? intval($params['pay_type']) : 1;
  285. $payment = MemberBankModel::where(['type'=> $payType,'status'=>1,'mark'=>1])->value('id');
  286. if(empty($payment)){
  287. $this->error = 3011;
  288. return false;
  289. }
  290. // 入驻数据
  291. $data = [
  292. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  293. 'user_id' => $userId,
  294. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  295. 'telegram' => isset($params['telegram']) ? $params['telegram'] : '',
  296. 'pay_type' => $payType,
  297. 'price_float' => isset($params['price_float']) ? $params['price_float'] : '',
  298. 'create_time' => time(),
  299. 'update_time' => time(),
  300. 'quota' => 0,
  301. 'trade_status' => 1,
  302. 'status' => 1,
  303. 'mark' => 1,
  304. ];
  305. // 写入数据
  306. if ($id) {
  307. if ($this->model->where(['id' => $id])->update($data)) {
  308. $this->error = 3012;
  309. return ['id' => $id];
  310. } else {
  311. $this->error = 3013;
  312. return false;
  313. }
  314. } else {
  315. if ($id = $this->model->insertGetId($data)) {
  316. $this->error = 3014;
  317. return ['id' => $id];
  318. } else {
  319. $this->error = 3013;
  320. return false;
  321. }
  322. }
  323. }
  324. /**
  325. * 获取入驻信息
  326. * @param $userId
  327. * @return mixed
  328. */
  329. public function getApplyInfo($userId)
  330. {
  331. $info = $this->model->where(['user_id' => $userId, 'mark' => 1])
  332. ->orderBy('id', 'desc')
  333. ->first();
  334. $info = $info ? $info->setHidden(['quota', 'update_time', 'mark'])->toArray() : [];
  335. if($info){
  336. $payments = [1=>'微信支付',2=>'支付宝支付',3=>'银行卡支付'];
  337. $info['pay_name'] = isset($payments[$info['pay_type']])? $payments[$info['pay_type']] : '';
  338. }
  339. return $info;
  340. }
  341. /**
  342. * 充值交易额度
  343. * @param $userId 用户ID
  344. * @param $params
  345. * @return array|false
  346. */
  347. public function rechargeQuota($userId, $params)
  348. {
  349. // $acceptId = isset($params['id'])? $params['id'] : 0;
  350. $usdt = isset($params['usdt']) ? floatval($params['usdt']) : 0;
  351. if ($usdt <= 0) {
  352. $this->error = 2031;
  353. return false;
  354. }
  355. $info = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
  356. ->select(['id', 'realname', 'mobile', 'usdt', 'quota', 'trade_status', 'status'])
  357. ->first();
  358. $quota = isset($info['quota']) ? $info['quota'] : 0;
  359. $status = isset($info['status']) ? $info['status'] : 0;
  360. $acceptId = isset($info['id']) ? $info['id'] : 0;
  361. $userInfo = isset($info['member']) ? $info['member'] : [];
  362. if ($userId <= 0 || empty($info) || $status != 2) {
  363. $this->error = 2015;
  364. return false;
  365. }
  366. // 充值订单
  367. DB::beginTransaction();
  368. $orderNo = get_order_num('DP');
  369. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  370. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  371. $money = round($xdPrice * $usdt, 2);
  372. $data = [
  373. 'order_no' => $orderNo,
  374. 'user_id' => $acceptId,
  375. 'type' => 1,
  376. 'user_type' => 3,
  377. 'coin_type' => 6,
  378. 'money' => $usdt,
  379. 'actual_money' => $money,
  380. 'pay_type' => 10,
  381. 'pay_status' => 20,
  382. 'trc_url' => isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '',
  383. 'pay_at' => date('Y-m-d H:i:s'),
  384. 'date' => date('Y-m-d'),
  385. 'create_time' => time(),
  386. 'update_time' => time(),
  387. 'status' => 2,
  388. 'mark' => 1,
  389. ];
  390. if (!$orderId = BalanceLogModel::insertGetId($data)) {
  391. $this->error = 2033;
  392. DB::rollBack();
  393. return false;
  394. }
  395. // 扣除usdt余额
  396. $updateData = ['usdt' => DB::raw("usdt - {$usdt}"), 'update_time' => time()];
  397. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  398. $this->error = 2036;
  399. DB::rollBack();
  400. return false;
  401. }
  402. // 用户明细
  403. $userUsdt = isset($userInfo['usdt']) ? $userInfo['usdt'] : 0;
  404. $log = [
  405. 'user_id' => $userId,
  406. 'source_id' => $acceptId,
  407. 'source_order_no' => $data['order_no'],
  408. 'user_type' => 1,
  409. 'type' => 5,
  410. 'coin_type' => 1,
  411. 'money' => -$usdt,
  412. 'actual_money' => -$usdt,
  413. 'balance' => $usdt,
  414. 'date' => date('Y-m-d'),
  415. 'create_time' => time(),
  416. 'remark' => '交易额度充值扣除',
  417. 'status' => 1,
  418. 'mark' => 1
  419. ];
  420. if (!AccountLogModel::insertGetId($log)) {
  421. $this->error = 2029;
  422. DB::rollBack();
  423. return false;
  424. }
  425. // 额度增加
  426. $updateData = ['quota' => DB::raw("quota + {$money}"), 'update_time' => time()];
  427. if (!$this->model->where(['id' => $userId])->update($updateData)) {
  428. $this->error = 2036;
  429. DB::rollBack();
  430. return false;
  431. }
  432. // 额度明细
  433. $log = [
  434. 'user_id' => $acceptId,
  435. 'source_id' => $userId,
  436. 'source_order_no' => $data['order_no'],
  437. 'user_type' => 3,
  438. 'type' => 5,
  439. 'coin_type' => 6,
  440. 'money' => $money,
  441. 'actual_money' => $money,
  442. 'balance' => $quota,
  443. 'date' => date('Y-m-d'),
  444. 'create_time' => time(),
  445. 'remark' => '交易额度充值',
  446. 'status' => 1,
  447. 'mark' => 1
  448. ];
  449. if (!AccountLogModel::insertGetId($log)) {
  450. $this->error = 2029;
  451. DB::rollBack();
  452. return false;
  453. }
  454. // 消息
  455. $dateTime = date('Y-m-d H:i:s');
  456. MessageService::make()->pushMessage($userId, '交易额度充值成功', "您在{$dateTime}(UTC+8)成功支付{$usdt}USDT充值{$money}交易额度", 3);
  457. DB::commit();
  458. $this->error = 2037;
  459. return ['order_id' => $orderId, 'quota' => moneyFormat($quota + $money, 2), 'usdt' => moneyFormat($userUsdt - $usdt, 2)];
  460. }
  461. /**
  462. * 交易额度提现处理
  463. * @param $userId 用户
  464. * @param $params
  465. * @return bool
  466. */
  467. public function withdrawQuota($userId, $params)
  468. {
  469. $money = isset($params['money']) ? $params['money'] : 0;
  470. $coinType = isset($params['coin_type']) ? $params['coin_type'] : 0;
  471. if ($money <= 0) {
  472. $this->error = 2039;
  473. return false;
  474. }
  475. $info = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
  476. ->select(['id', 'user_id', 'realname', 'mobile', 'usdt', 'quota', 'status'])
  477. ->first();
  478. $usdt = isset($info['usdt']) ? floatval($info['usdt']) : 0;
  479. $quota = isset($info['quota']) ? floatval($info['quota']) : 0;
  480. $status = isset($info['status']) ? $info['status'] : 0;
  481. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  482. $acceptId = isset($info['accept_id']) ? $info['accept_id'] : 0;
  483. $userInfo = isset($info['member']) ? $info['member'] : [];
  484. if ($acceptId <= 0 || empty($info) || $status != 2) {
  485. $this->error = 2039;
  486. return false;
  487. }
  488. if (in_array($tradeStatus,[2,4])) {
  489. $this->error = 2015;
  490. return false;
  491. }
  492. // 提现账户
  493. $trcUrl = isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '';
  494. if (empty($trcUrl)) {
  495. $this->error = 2403;
  496. return false;
  497. }
  498. DB::beginTransaction();
  499. // USDT 提现
  500. $payUsdt = 0;
  501. $orderNo = get_order_num('DP');
  502. if ($coinType == 1) {
  503. $payUsdt = $money;
  504. if ($money > $usdt) {
  505. $this->error = lang(2040, ['money' => $usdt]);
  506. return false;
  507. }
  508. $updateData = ['usdt' => DB::raw("usdt - {$money}"), 'update_time' => time()];
  509. if (!$this->model->where(['id' => $acceptId])->update($updateData)) {
  510. DB::rollBack();
  511. $this->error = 2042;
  512. return false;
  513. }
  514. // 明细
  515. $log = [
  516. 'user_id' => $acceptId,
  517. 'source_id' => $userId,
  518. 'source_order_no' => $orderNo,
  519. 'user_type' => 3,
  520. 'type' => 5,
  521. 'coin_type' => 1,
  522. 'money' => -$usdt,
  523. 'actual_money' => -$usdt,
  524. 'balance' => $usdt,
  525. 'date' => date('Y-m-d'),
  526. 'create_time' => time(),
  527. 'remark' => '交易余额提现',
  528. 'status' => 1,
  529. 'mark' => 1
  530. ];
  531. if (!AccountLogModel::insertGetId($log)) {
  532. $this->error = 2029;
  533. DB::rollBack();
  534. return false;
  535. }
  536. } // 额度提现
  537. else if ($coinType == 6) {
  538. // 时间限制
  539. $limitTime = ConfigService::make()->getConfigByCode('quota_withdraw_time');
  540. $limitTime = $limitTime >= 0 ? $limitTime : 0;
  541. $lockQuota = BalanceLogModel::where(['user_id' => $acceptId, 'user_type' => 3, 'coin_type' => 6, 'status' => 1, 'mark' => 1])
  542. ->where('pay_at', '>=', date('Y-m-d H:i:s', time() - $limitTime * 86400))
  543. ->sum('actual_money');
  544. if ($lockQuota > 0 && $money > ($quota - $lockQuota)) {
  545. $this->error = lang(2040, ['money' => ($quota - $lockQuota)]);
  546. return false;
  547. }
  548. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  549. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  550. $payUsdt = round($money / $xdPrice, 4);
  551. $updateData = ['quota' => DB::raw("quota - {$money}"), 'update_time' => time()];
  552. if (!$this->model->where(['id' => $acceptId])->update($updateData)) {
  553. DB::rollBack();
  554. $this->error = 2042;
  555. return false;
  556. }
  557. // 明细
  558. $log = [
  559. 'user_id' => $acceptId,
  560. 'source_id' => $userId,
  561. 'source_order_no' => $orderNo,
  562. 'user_type' => 3,
  563. 'type' => 5,
  564. 'coin_type' => 6,
  565. 'money' => -$money,
  566. 'actual_money' => -$money,
  567. 'balance' => $quota,
  568. 'date' => date('Y-m-d'),
  569. 'create_time' => time(),
  570. 'remark' => '交易额度提现',
  571. 'status' => 1,
  572. 'mark' => 1
  573. ];
  574. if (!AccountLogModel::insertGetId($log)) {
  575. $this->error = 2029;
  576. DB::rollBack();
  577. return false;
  578. }
  579. }
  580. // 提现记录
  581. $data = [
  582. 'order_no' => $orderNo,
  583. 'user_id' => $acceptId,
  584. 'type' => 2,
  585. 'coin_type' => $coinType,
  586. 'user_type' => 3,
  587. 'money' => $money,
  588. 'actual_money' => $payUsdt,
  589. 'balance' => $coinType == 1 ? $usdt : $quota,
  590. 'date' => date('Y-m-d'),
  591. 'trc_url' => $trcUrl,
  592. 'create_time' => time(),
  593. 'update_time' => time(),
  594. 'status' => 1,
  595. 'mark' => 1,
  596. ];
  597. if (!$id = BalanceLogModel::insertGetId($data)) {
  598. DB::rollBack();
  599. $this->error = 2042;
  600. return false;
  601. }
  602. DB::commit();
  603. // 站内消息
  604. $dateTime = date('Y-m-d H:i:s');
  605. $message = $coinType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT交易余额成功,请耐心等候审核!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}交易额度成功,请耐心等候审核!!!";
  606. MessageService::make()->pushMessage($userId, $coinType == 1 ? '交易余额提现申请成功' : '交易额度提现申请成功', $message);
  607. // 提现自动审核,低于该金额自动审核
  608. $autoCheckUsdt = ConfigService::make()->getConfigByCode('withdraw_auto_money', 300);
  609. $autoCheckUsdt = $autoCheckUsdt > 0 ? $autoCheckUsdt : 0;
  610. if ($payUsdt <= $autoCheckUsdt) {
  611. // 打款处理
  612. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $payUsdt);
  613. $txID = isset($result['txId']) ? $result['txId'] : '';
  614. $payAddress = isset($result['address']) ? $result['address'] : '';
  615. if ($txID && $payAddress) {
  616. $updateData = ['hash'=> $txID,'wallet_url'=> $payAddress,'audit_remark'=>'自动审核打款','status'=>2,'update_time'=>time()];
  617. if(BalanceLogModel::where(['user_id'=> $acceptId,'order_no'=> $orderNo])->update($updateData)){
  618. $message = $coinType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT交易余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}交易额度审核成功,请耐心等候打款到账!!!";
  619. MessageService::make()->pushMessage($userId, $coinType == 1 ? '交易余额提现审核成功' : '交易额度提现审核成功', $message);
  620. AccountLogModel::where(['source_order_no'=> $orderNo])->update(['hash'=> $txID,'update_time'=>time()]);
  621. }
  622. }
  623. }
  624. $this->error = 2043;
  625. return true;
  626. }
  627. }