BalanceLogService.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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\Common;
  12. use App\Models\AcceptorModel;
  13. use App\Models\AccountLogModel;
  14. use App\Models\BalanceLogModel;
  15. use App\Models\MemberModel;
  16. use App\Models\MerchantModel;
  17. use App\Services\Api\FinanceService;
  18. use App\Services\Api\MessageService;
  19. use App\Services\BaseService;
  20. use App\Services\RedisService;
  21. use App\Services\WalletService;
  22. use Illuminate\Support\Facades\DB;
  23. /**
  24. * 承兑商管理-服务类
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * @package App\Services\Common
  28. */
  29. class BalanceLogService extends BaseService
  30. {
  31. // 静态对象
  32. protected static $instance = null;
  33. /**
  34. * 构造函数
  35. * @author laravel开发员
  36. * @since 2020/11/11
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new BalanceLogModel();
  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 $params 参数
  56. * @param int $pageSize 分页大小:默认 15
  57. * @return array
  58. */
  59. public function getDataList($params, $pageSize = 10, $field = [])
  60. {
  61. $query = $this->getQuery($params);
  62. $list = $query->select($field ? $field : ['a.*', 'b.username','b.trc_url as user_trc_url', 'b.nickname','c.user_id as merchant_uid','d.user_id as acceptor_uid'])
  63. ->orderBy('a.status','asc')
  64. ->orderBy('a.create_time','desc')
  65. ->orderBy('a.pay_at','desc')
  66. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  67. $list = $list ? $list->toArray() : [];
  68. if ($list) {
  69. foreach ($list['data'] as &$item) {
  70. $item['create_time'] = datetime($item['create_time'],'Y-m-d H:i:s');
  71. $item['pay_img'] = isset($item['pay_img'])?get_image_url($item['pay_img']):'';
  72. $item['trc_url'] = $item['trc_url']?$item['trc_url'] : $item['user_trc_url'];
  73. if($item['user_type'] == 1){
  74. $item['uid'] = $item['user_id'];
  75. $item['account'] = isset($item['nickname'])&& $item['nickname']?$item['nickname'].(isset($item['username'])?' '.$item['username']:'') : $item['user_id'];
  76. }else if($item['user_type'] == 2){
  77. $item['uid'] = isset($item['merchant']['user_id'])&& $item['merchant']? $item['merchant']['user_id'] : 0;
  78. $item['account'] = isset($item['merchant']['nickname'])&& $item['merchant']?$item['merchant']['nickname'].(isset($item['merchant']['mobile'])?' '.$item['merchant']['mobile']:'') : $item['merchant_uid'];
  79. }else if($item['user_type'] == 3){
  80. $item['uid'] = isset($item['acceptor']['user_id'])&& $item['acceptor']? $item['acceptor']['user_id'] : 0;
  81. $item['account'] = isset($item['acceptor']['nickname'])&& $item['acceptor']?$item['acceptor']['nickname'].(isset($item['acceptor']['mobile'])?' '.$item['acceptor']['mobile']:'') : $item['acceptor_uid'];
  82. }
  83. }
  84. }
  85. return [
  86. 'pageSize' => $pageSize,
  87. 'total' => isset($list['total']) ? $list['total'] : 0,
  88. 'list' => isset($list['data']) ? $list['data'] : []
  89. ];
  90. }
  91. /**
  92. * 查询构造
  93. * @param $params
  94. * @return \Illuminate\Database\Eloquent\Builder
  95. */
  96. public function getQuery($params)
  97. {
  98. $where = ['a.mark' => 1];
  99. return $this->model->with(['member','acceptor','merchant','payment'])
  100. ->from('balance_logs as a')
  101. ->leftJoin('member as b', function($join) {
  102. $join->on('b.id','=', 'a.user_id')->where('a.user_type',1);
  103. })
  104. ->leftJoin('merchant as c', function($join) {
  105. $join->on('c.id','=', 'a.user_id')->where('a.user_type',2);
  106. })
  107. ->leftJoin('acceptor as d', function($join) {
  108. $join->on('d.id','=', 'a.user_id')->where('a.user_type',3);
  109. })
  110. ->where($where)
  111. ->where(function ($query) use ($params) {
  112. $userType = isset($params['user_type'])? intval($params['user_type']) : 1;
  113. $kw = isset($params['keyword']) ? trim($params['keyword']) : '';
  114. if ($kw) {
  115. if($userType == 3){
  116. $query->where('d.name', 'like', "%{$params['keyword']}%")
  117. ->orWhere('d.mobile', 'like', "%{$params['keyword']}%");
  118. }else if($userType == 2){
  119. $query->where('c.name', 'like', "%{$params['keyword']}%")
  120. ->orWhere('c.mobile', 'like', "%{$params['keyword']}%");
  121. }else if($userType == 1){
  122. $query->where('b.nickname', 'like', "%{$params['keyword']}%")
  123. ->orWhere('b.username', 'like', "%{$params['keyword']}%");
  124. }else{
  125. $query->where('b.nickname', 'like', "%{$params['keyword']}%")
  126. ->orWhere('b.username', 'like', "%{$params['keyword']}%")
  127. ->orWhere('c.name', 'like', "%{$params['keyword']}%")
  128. ->orWhere('c.mobile', 'like', "%{$params['keyword']}%")
  129. ->orWhere('d.name', 'like', "%{$params['keyword']}%")
  130. ->orWhere('d.mobile', 'like', "%{$params['keyword']}%");
  131. }
  132. }
  133. })
  134. ->where(function ($query) use($params){
  135. // 日期
  136. $date = isset($params['date']) ? $params['date'] : [];
  137. $start = isset($date[0])? $date[0] : '';
  138. $end = isset($date[1])? $date[1] : '';
  139. $end = $start>=$end? '' : $end;
  140. if ($start) {
  141. $query->where('a.create_time','>=', strtotime($start));
  142. }
  143. if($end){
  144. $query->where('a.create_time','<=', strtotime($end));
  145. }
  146. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  147. if($orderNo){
  148. $query->where('a.order_no', 'like', "%{$orderNo}%");
  149. }
  150. $trcUrl = isset($params['trc_url'])? trim($params['trc_url']) : '';
  151. if($trcUrl){
  152. $query->where('a.trc_url', '=', $trcUrl);
  153. }
  154. $hash = isset($params['hash'])? trim($params['hash']) : '';
  155. if($hash){
  156. $query->where('a.trc_url', '=', $hash);
  157. }
  158. $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
  159. if($orderNo){
  160. $query->where('a.order_no', 'like', "%{$orderNo}%");
  161. }
  162. $userType = isset($params['user_type'])? $params['user_type'] : 0;
  163. if ($userType) {
  164. $query->where('a.user_type', $userType);
  165. }
  166. $payType = isset($params['pay_type'])? $params['pay_type'] : 0;
  167. if ($payType) {
  168. $query->where('a.pay_type', $payType);
  169. }
  170. $payStatus = isset($params['pay_status'])? $params['pay_status'] : 0;
  171. if ($payStatus) {
  172. $query->where('a.pay_status', $payStatus);
  173. }
  174. $type = isset($params['type'])? $params['type'] : 0;
  175. if (is_array($type)) {
  176. $query->whereIn('a.type', $type);
  177. } else if($type){
  178. $query->where('a.type', $type);
  179. }
  180. $coinType = isset($params['coin_type'])? $params['coin_type'] : 0;
  181. if (is_array($coinType)) {
  182. $query->whereIn('a.coin_type', $coinType);
  183. } else if($coinType){
  184. $query->where('a.coin_type', $coinType);
  185. }
  186. $status = isset($params['status'])? $params['status'] : 0;
  187. if (is_array($status)) {
  188. $query->whereIn('a.status', $status);
  189. } else if($status){
  190. $query->where('a.status', $status);
  191. }
  192. });
  193. }
  194. /**
  195. * 统计
  196. * @param $params
  197. * @return array
  198. */
  199. public function count($params)
  200. {
  201. $query = $this->getQuery($params);
  202. $count = $query->count('a.id');
  203. $total = $query->sum('a.actual_money');
  204. return [
  205. 'count' => $count,
  206. 'total' => $total
  207. ];
  208. }
  209. /**
  210. * 充值审核
  211. * @param $params
  212. * @return bool
  213. */
  214. public function rechargeAuth($params)
  215. {
  216. $id = isset($params['id'])? $params['id'] : 0;
  217. $checkStatus = isset($params['status'])? $params['status'] : 0;
  218. $remark = isset($params['audit_remark'])? trim($params['audit_remark']) : '';
  219. $payImg = isset($params['pay_img'])? trim($params['pay_img']) : '';
  220. $info = $this->model->with(['member','merchant','acceptor'])->where(['id'=> $id,'mark'=>1])->first();
  221. $type = isset($info['type'])? $info['type'] : 0;
  222. $userType = isset($info['user_type'])? $info['user_type'] : 0;
  223. $coinType = isset($info['coin_type'])? $info['coin_type'] : 0;
  224. $payType = isset($info['pay_type'])? $info['pay_type'] : 0;
  225. $accountId = isset($info['user_id'])? $info['user_id'] : 0;
  226. $money = isset($info['money'])? $info['money'] : 0;
  227. $actualMoney = isset($info['actual_money'])? $info['actual_money'] : 0;
  228. $status = isset($info['status'])? $info['status'] : 0;
  229. if(empty($info) || $accountId<=0){
  230. $this->error = 4001;
  231. return false;
  232. }
  233. if($status != 1){
  234. $this->error = 4002;
  235. return false;
  236. }
  237. if($type != 1){
  238. $this->error = 1031;
  239. return false;
  240. }
  241. $cacheKey ="caches:recharge:lock_{$id}";
  242. if(RedisService::get($cacheKey)){
  243. $this->error = 1034;
  244. return false;
  245. }
  246. // 绑定的用户ID
  247. $userId = $accountId;
  248. $userInfo = isset($info['member'])? $info['member'] : [];
  249. $balance = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  250. if($userType == 2){
  251. $userInfo = isset($info['merchant'])? $info['merchant'] : [];
  252. $userId = isset($userInfo['user_id'])? $userInfo['user_id'] : 0;
  253. $balance = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  254. }else if($userType == 3){
  255. $userInfo = isset($info['acceptor'])? $info['acceptor'] : [];
  256. $userId = isset($userInfo['user_id'])? $userInfo['user_id'] : 0;
  257. $balance = isset($userInfo['quota'])? $userInfo['quota'] : 0;
  258. }
  259. if(empty($userInfo)){
  260. $this->error = 4004;
  261. return false;
  262. }
  263. // 审核处理
  264. RedisService::set($cacheKey, true);
  265. $updateData = ['status'=> $checkStatus,'audit_remark'=> $remark,'update_time'=> time()];
  266. DB::beginTransaction();
  267. if(!$this->model->where(['id'=> $id])->update($updateData)){
  268. DB::rollBack();
  269. $this->error = 1072;
  270. RedisService::clear($cacheKey);
  271. return false;
  272. }
  273. // 审核通过到账处理
  274. $title = '';
  275. $message = '';
  276. $dateTime = date('Y-m-d H:i:s');
  277. $coinTypes = [1=>'USDT余额',2=>'星豆余额',3=>'交易额度'];
  278. $accountTypes = [1=>'会员账户',2=>'商家账户',3=>'承兑商账户'];
  279. $coinName = isset($coinTypes[$coinType])? $coinTypes[$coinType] : '账户';
  280. $accountName = isset($accountTypes[$userType])? $accountTypes[$userType] : '会员账户';
  281. if($status == 2) {
  282. // 线下交易
  283. if($payType == 30){
  284. if(!in_array($userType,[1,3]) && !in_array($coinType,[1,6])){
  285. DB::rollBack();
  286. $this->error = 1021;
  287. RedisService::clear($cacheKey);
  288. return false;
  289. }
  290. // USDT入账
  291. if($userType == 1 && $coinType == 1){
  292. $updateData = ['usdt' => DB::raw("usdt + {$actualMoney}"),'update_time'=>time()];
  293. if (!MemberModel::where(['id' => $accountId])->update($updateData)){
  294. DB::rollBack();
  295. $this->error = 1072;
  296. RedisService::clear($cacheKey);
  297. return false;
  298. }
  299. }
  300. // 交易额度入账
  301. else if($userType == 3 && $coinType == 6){
  302. $updateData = ['quota' => DB::raw("quota + {$actualMoney}"),'update_time'=>time()];
  303. if (!AcceptorModel::where(['id' => $accountId])->update($updateData)){
  304. DB::rollBack();
  305. $this->error = 1072;
  306. RedisService::clear($cacheKey);
  307. return false;
  308. }
  309. }
  310. // 账户明细
  311. $log = [
  312. 'user_id' => $accountId,
  313. 'source_id' => 0,
  314. 'source_order_no' => $info['order_no'],
  315. 'type' => 5,
  316. 'coin_type' => $coinType,
  317. 'user_type'=> $userType,
  318. 'money' => $money,
  319. 'actual_money' => $actualMoney,
  320. 'balance' => $balance,
  321. 'create_time' => time(),
  322. 'update_time' => time(),
  323. 'remark' => "{$coinName}充值",
  324. 'status' => 1,
  325. 'mark' => 1,
  326. ];
  327. if(!AccountLogModel::insertGetId($log)){
  328. DB::rollBack();
  329. $this->error = 2029;
  330. RedisService::clear($cacheKey);
  331. return false;
  332. }
  333. }else{
  334. DB::rollBack();
  335. $this->error = 1021;
  336. RedisService::clear($cacheKey);
  337. return false;
  338. }
  339. $title = "{$coinName}充值审核成功通知";
  340. $message = "您的充值申请在{$dateTime}UTC+8审核成功,明细如下:\n单号:{$info['order_no']}\n账户:{$accountName}\n金额:{$money}\n到账:{$actualMoney}\n审核状态:成功\n审核说明:{$remark}";
  341. }else{
  342. $title = "{$coinName}充值审核失败通知";
  343. $message = "您的充值申请在{$dateTime}UTC+8审核失败,明细如下:\n单号:{$info['order_no']}\n账户:{$accountName}\n金额:{$money}\n到账:{$actualMoney}\n审核状态:未通过\n审核说明:{$remark}";
  344. }
  345. // 消息通知
  346. MessageService::make()->pushMessage($userId, $title, $message, 3);
  347. DB::commit();
  348. $this->error = 1071;
  349. return true;
  350. }
  351. /**
  352. * 提现审核
  353. * @param $params
  354. * @return bool
  355. */
  356. public function withdrawAuth($params)
  357. {
  358. $id = isset($params['id'])? $params['id'] : 0;
  359. $checkStatus = isset($params['status'])? $params['status'] : 0;
  360. $remark = isset($params['audit_remark'])? trim($params['audit_remark']) : '';
  361. $payImg = isset($params['pay_img'])? trim($params['pay_img']) : '';
  362. $info = $this->model->with(['member','merchant','acceptor'])->where(['id'=> $id,'mark'=>1])->first();
  363. $type = isset($info['type'])? $info['type'] : 0;
  364. $userType = isset($info['user_type'])? $info['user_type'] : 0;
  365. $coinType = isset($info['coin_type'])? $info['coin_type'] : 0;
  366. $payType = isset($info['pay_type'])? $info['pay_type'] : 0;
  367. $accountId = isset($info['user_id'])? $info['user_id'] : 0;
  368. $money = isset($info['money'])? $info['money'] : 0;
  369. $actualMoney = isset($info['actual_money'])? $info['actual_money'] : 0;
  370. $fee = isset($info['fee'])? $info['fee'] : 0;
  371. $status = isset($info['status'])? $info['status'] : 0;
  372. if(empty($info) || $accountId<=0){
  373. $this->error = 4001;
  374. return false;
  375. }
  376. if($status != 1){
  377. $this->error = 4002;
  378. return false;
  379. }
  380. if($type != 2){
  381. $this->error = 1031;
  382. return false;
  383. }
  384. $cacheKey ="caches:withdraw:lock_{$id}";
  385. if(RedisService::get($cacheKey)){
  386. $this->error = 1034;
  387. return false;
  388. }
  389. // 绑定的用户ID
  390. $userId = $accountId;
  391. $userInfo = isset($info['member'])? $info['member'] : [];
  392. $balance = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  393. if($userType == 2){
  394. $userInfo = isset($info['merchant'])? $info['merchant'] : [];
  395. $userId = isset($userInfo['user_id'])? $userInfo['user_id'] : 0;
  396. $balance = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  397. }else if($userType == 3){
  398. $userInfo = isset($info['acceptor'])? $info['acceptor'] : [];
  399. $userId = isset($userInfo['user_id'])? $userInfo['user_id'] : 0;
  400. $balance = isset($userInfo['quota'])? $userInfo['quota'] : 0;
  401. }
  402. if(empty($userInfo)){
  403. $this->error = 4004;
  404. return false;
  405. }
  406. // 审核处理
  407. RedisService::set($cacheKey, true);
  408. DB::beginTransaction();
  409. // 审核通过到账处理
  410. $title = '';
  411. $message = '';
  412. $dateTime = date('Y-m-d H:i:s');
  413. $accountTypes = [1=>'会员账户',2=>'商家账户',3=>'承兑商账户'];
  414. $coinName = 'USDT余额';
  415. if($userType == 2 && $coinType==1){
  416. $coinName = 'USDT佣金';
  417. }else if($userType == 3 && $coinType==1){
  418. $coinName = 'USDT佣金';
  419. }else if($userType == 3 && $coinType==6){
  420. $coinName = '交易额度';
  421. }
  422. $accountName = isset($accountTypes[$userType])? $accountTypes[$userType] : '会员账户';
  423. if($status == 2) {
  424. // USDT链上打款
  425. $hash = '';
  426. if($payType == 20){
  427. $trcUrl = isset($userInfo['trc_url'])? $userInfo['trc_url'] : '';
  428. if(empty($trcUrl)){
  429. DB::rollBack();
  430. $this->error = 4005;
  431. RedisService::clear($cacheKey);
  432. return false;
  433. }
  434. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $actualMoney);
  435. $hash = isset($result['txId']) ? $result['txId'] : '';
  436. if(empty($hash)){
  437. DB::rollBack();
  438. $this->error = 4005;
  439. RedisService::clear($cacheKey);
  440. return false;
  441. }
  442. $payAddress = isset($result['address']) ? $result['address'] : '';
  443. $updateData = ['status'=>2,'audit_remark'=>$remark,'pay_img'=> get_image_path($payImg),'hash' => $hash, 'wallet_url' => $payAddress,'pay_status'=>20,'pay_at'=> $dateTime, 'update_time' => time()];
  444. if(!$this->model->where(['id'=> $id])->update($updateData)){
  445. DB::rollBack();
  446. $this->error = 4006;
  447. RedisService::clear($cacheKey);
  448. return false;
  449. }
  450. }
  451. // 线下打款
  452. else if($payType == 30){
  453. $updateData = ['status'=> 2,'pay_status'=>20,'pay_at'=>$dateTime,'pay_img'=> get_image_path($payImg),'audit_remark'=> $remark,'update_time'=> time()];
  454. if(!$this->model->where(['id'=> $id])->update($updateData)){
  455. DB::rollBack();
  456. $this->error = 1072;
  457. RedisService::clear($cacheKey);
  458. return false;
  459. }
  460. }else{
  461. DB::rollBack();
  462. $this->error = 1021;
  463. RedisService::clear($cacheKey);
  464. return false;
  465. }
  466. // 平台手续费明细
  467. if($fee>0){
  468. $log = [
  469. 'user_id' => 0,
  470. 'source_id' => $accountId,
  471. 'source_order_no' => $info['order_no'],
  472. 'type' => 5,
  473. 'coin_type' => $coinType,
  474. 'user_type' => 4,
  475. 'money' => $fee,
  476. 'actual_money' => $fee,
  477. 'balance' => 0,
  478. 'create_time' => time(),
  479. 'update_time' => time(),
  480. 'hash' => $hash,
  481. 'remark' => "{$coinName}提现",
  482. 'status' => 1,
  483. 'mark' => 1,
  484. ];
  485. if(!AccountLogModel::insertGetId($log)){
  486. DB::rollBack();
  487. $this->error = 2029;
  488. RedisService::clear($cacheKey);
  489. return false;
  490. }
  491. FinanceService::make()->saveLog(0, $fee, 1);
  492. }
  493. $title = "{$coinName}提现审核成功通知";
  494. $message = "您的提现申请在{$dateTime}UTC+8审核成功,明细如下:\n单号:{$info['order_no']}\n账户:{$accountName}\n提现金额:{$money}\n到账:{$actualMoney}\n审核状态:审核成功\n审核说明:{$remark}";
  495. }
  496. // 审核失败驳回退款
  497. else{
  498. if(!in_array($userType,[1,2,3]) && !in_array($coinType,[1,6])){
  499. DB::rollBack();
  500. $this->error = 1021;
  501. RedisService::clear($cacheKey);
  502. return false;
  503. }
  504. $updateData = ['status'=> 3,'pay_img'=> get_image_path($payImg),'audit_remark'=> $remark,'update_time'=> time()];
  505. if(!$this->model->where(['id'=> $id])->update($updateData)){
  506. DB::rollBack();
  507. $this->error = 1072;
  508. RedisService::clear($cacheKey);
  509. return false;
  510. }
  511. // 会员:USDT驳回入账
  512. if($userType == 1 && $coinType == 1){
  513. $balance = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  514. $updateData = ['usdt' => DB::raw("usdt + {$money}"),'update_time'=>time()];
  515. if (!MemberModel::where(['id' => $accountId])->update($updateData)){
  516. DB::rollBack();
  517. $this->error = 1072;
  518. RedisService::clear($cacheKey);
  519. return false;
  520. }
  521. }
  522. // 商家:佣金入账
  523. else if($userType == 2 && $coinType == 1){
  524. $balance = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  525. $updateData = ['usdt' => DB::raw("usdt + {$money}"),'update_time'=>time()];
  526. if (!MerchantModel::where(['id' => $accountId])->update($updateData)){
  527. DB::rollBack();
  528. $this->error = 1072;
  529. RedisService::clear($cacheKey);
  530. return false;
  531. }
  532. }
  533. // 承兑商:佣金入账
  534. else if($userType == 3 && $coinType == 1){
  535. $balance = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  536. $updateData = ['usdt' => DB::raw("usdt + {$money}"),'update_time'=>time()];
  537. if (!AcceptorModel::where(['id' => $accountId])->update($updateData)){
  538. DB::rollBack();
  539. $this->error = 1072;
  540. RedisService::clear($cacheKey);
  541. return false;
  542. }
  543. }
  544. // 承兑商:交易额度入账
  545. else if($userType == 3 && $coinType == 6){
  546. $balance = isset($userInfo['quota'])? $userInfo['quota'] : 0;
  547. $updateData = ['quota' => DB::raw("quota + {$money}"),'update_time'=>time()];
  548. if (!AcceptorModel::where(['id' => $accountId])->update($updateData)){
  549. DB::rollBack();
  550. $this->error = 1072;
  551. RedisService::clear($cacheKey);
  552. return false;
  553. }
  554. }
  555. // 账户明细
  556. $log = [
  557. 'user_id' => $accountId,
  558. 'source_id' => 0,
  559. 'source_order_no' => $info['order_no'],
  560. 'type' => 5,
  561. 'coin_type' => $coinType,
  562. 'user_type'=> $userType,
  563. 'money' => $money,
  564. 'actual_money' => $money,
  565. 'balance' => $balance,
  566. 'create_time' => time(),
  567. 'update_time' => time(),
  568. 'remark' => "{$coinName}提现驳回",
  569. 'status' => 1,
  570. 'mark' => 1,
  571. ];
  572. if(!AccountLogModel::insertGetId($log)){
  573. DB::rollBack();
  574. $this->error = 2029;
  575. RedisService::clear($cacheKey);
  576. return false;
  577. }
  578. $title = "{$coinName}提现审核失败通知";
  579. $message = "您的提现申请在{$dateTime}UTC+8审核失败,提现金额已退回对应账户,明细如下:\n单号:{$info['order_no']}\n账户:{$accountName}\n提现金额:{$money}\n到账:{$actualMoney}\n审核状态:未通过\n审核说明:{$remark}";
  580. }
  581. // 消息通知
  582. MessageService::make()->pushMessage($userId, $title, $message, 3);
  583. DB::commit();
  584. $this->error = 1071;
  585. return true;
  586. }
  587. /**
  588. * 添加会编辑会员
  589. * @return array
  590. * @since 2020/11/11
  591. * @author laravel开发员
  592. */
  593. public function edit()
  594. {
  595. // 请求参数
  596. $data = request()->all();
  597. $balanceLog = BalanceLogModel::where(['id' => $data['id']])->first();
  598. DB::beginTransaction();
  599. try {
  600. // 提现 审核通过
  601. if ($balanceLog['type'] == 2 && isset($data['status'])) {
  602. if ($data['status'] == 2) {
  603. if ($data['user_type'] == 1) {
  604. // 会员
  605. $this->withdrawMember($data);
  606. } else if ($data['user_type'] == 2) {
  607. // 商家
  608. $this->withdrawMerchat($data);
  609. } else if ($data['user_type'] == 3) {
  610. // 承兑商
  611. $this->withdrawAcceptor($data);
  612. }
  613. } else if ($data['status'] == 3) {
  614. $money = $data['money'];
  615. $userId = $data['user_id'];
  616. $accountLog = AccountLogModel::where(['source_order_no' => $balanceLog['order_no']])->first();
  617. if ($accountLog) {
  618. AccountLogModel::where(['source_order_no' => $balanceLog])->update([
  619. 'status' => 3,
  620. 'update_time' => time(),
  621. ]);
  622. $updateData = ['usdt' => DB::raw("usdt - {$money}"), 'update_time' => time()];
  623. if (!MerchantModel::where(['user_id' => $userId, 'mark' => 1])->update($updateData)) {
  624. DB::rollBack();
  625. return message('审核不通过操作失败');
  626. }
  627. }
  628. }
  629. }
  630. } catch (\Exception $e) {
  631. DB::rollBack();
  632. return message('操作失败');
  633. }
  634. DB::commit();
  635. return parent::edit($data); // TODO: Change the autogenerated stub
  636. }
  637. private function withdrawMember(array $data)
  638. {
  639. $trcUrl = $data['trc_url'];
  640. $realUsdt = $data['actual_money'];
  641. $orderNo = $data['order_no'];
  642. $userType = $data['user_type'];
  643. $dateTime = $dateTime = date('Y-m-d H:i:s');
  644. $money = $data['money'];
  645. $userId = $data['user_id'];
  646. $fee = $data['fee'];
  647. // 打款处理
  648. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  649. $txID = isset($result['txId']) ? $result['txId'] : '';
  650. $payAddress = isset($result['address']) ? $result['address'] : '';
  651. if ($txID && $payAddress) {
  652. $updateData = ['hash' => $txID, 'wallet_url' => $payAddress, 'audit_remark' => '审核打款', 'status' => 2, 'update_time' => time()];
  653. if (BalanceLogModel::where(['order_no' => $orderNo, 'user_type' => $userType])->update($updateData)) {
  654. $title = $userType == 1 ? 'USDT余额提现审核成功' : '商户余额提现审核成功';
  655. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额审核成功,请耐心等候打款到账!!!";
  656. MessageService::make()->pushMessage($userId, $title, $message);
  657. AccountLogModel::where(['source_order_no' => $orderNo])->update(['hash' => $txID, 'update_time' => time()]);
  658. // 平台明细
  659. $log = [
  660. 'user_id' => 0,
  661. 'source_id' => $userId,
  662. 'source_order_no' => $orderNo,
  663. 'type' => 5,
  664. 'coin_type' => 1,
  665. 'user_type' => 4,
  666. 'money' => $fee,
  667. 'actual_money' => $fee,
  668. 'balance' => 0,
  669. 'create_time' => time(),
  670. 'update_time' => time(),
  671. 'hash' => $txID,
  672. 'remark' => "USDT余额提现",
  673. 'status' => 1,
  674. 'mark' => 1,
  675. ];
  676. AccountLogModel::insertGetId($log);
  677. // 平台流水
  678. FinanceService::make()->saveLog(0, $fee, 1);
  679. }
  680. }
  681. }
  682. private function withdrawMerchat(array $data)
  683. {
  684. $trcUrl = $data['trc_url'];
  685. $realUsdt = $data['actual_money'];
  686. $orderNo = $data['order_no'];
  687. $userType = $data['user_type'];
  688. $dateTime = $dateTime = date('Y-m-d H:i:s');
  689. $money = $data['money'];
  690. $userId = $data['user_id'];
  691. $fee = $data['fee'];
  692. // 打款处理
  693. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  694. $txID = isset($result['txId']) ? $result['txId'] : '';
  695. $payAddress = isset($result['address']) ? $result['address'] : '';
  696. if ($txID && $payAddress) {
  697. $updateData = ['hash' => $txID, 'wallet_url' => $payAddress, 'audit_remark' => '审核打款', 'status' => 2, 'update_time' => time()];
  698. if (BalanceLogModel::where(['order_no' => $orderNo, 'user_type' => $userType])->update($updateData)) {
  699. $title = $userType == 1 ? 'USDT余额提现审核成功' : '商户余额提现审核成功';
  700. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额审核成功,请耐心等候打款到账!!!";
  701. MessageService::make()->pushMessage($userId, $title, $message);
  702. AccountLogModel::where(['source_order_no' => $orderNo])->update(['hash' => $txID, 'update_time' => time()]);
  703. // 平台明细
  704. $log = [
  705. 'user_id' => 0,
  706. 'source_id' => $userId,
  707. 'source_order_no' => $orderNo,
  708. 'type' => 5,
  709. 'coin_type' => 1,
  710. 'user_type' => 4,
  711. 'money' => $fee,
  712. 'actual_money' => $fee,
  713. 'balance' => 0,
  714. 'create_time' => time(),
  715. 'update_time' => time(),
  716. 'hash' => $txID,
  717. 'remark' => "USDT余额提现",
  718. 'status' => 1,
  719. 'mark' => 1,
  720. ];
  721. AccountLogModel::insertGetId($log);
  722. // 平台流水
  723. FinanceService::make()->saveLog(0, $fee, 1);
  724. }
  725. }
  726. }
  727. private function withdrawAcceptor(array $data)
  728. {
  729. $trcUrl = $data['trc_url'];
  730. $realUsdt = $data['actual_money'];
  731. $orderNo = $data['order_no'];
  732. $userType = $data['user_type'];
  733. $dateTime = $dateTime = date('Y-m-d H:i:s');
  734. $money = $data['money'];
  735. $userId = $data['user_id'];
  736. $fee = $data['fee'];
  737. // 打款处理
  738. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  739. $txID = isset($result['txId']) ? $result['txId'] : '';
  740. $payAddress = isset($result['address']) ? $result['address'] : '';
  741. if ($txID && $payAddress) {
  742. $updateData = ['hash' => $txID, 'wallet_url' => $payAddress, 'audit_remark' => '审核打款', 'status' => 2, 'update_time' => time()];
  743. if (BalanceLogModel::where(['order_no' => $orderNo, 'user_type' => $userType])->update($updateData)) {
  744. $title = $userType == 1 ? 'USDT余额提现审核成功' : '承兑商余额提现审核成功';
  745. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT承兑商余额审核成功,请耐心等候打款到账!!!";
  746. MessageService::make()->pushMessage($userId, $title, $message, 3);
  747. AccountLogModel::where(['source_order_no' => $orderNo])->update(['hash' => $txID, 'update_time' => time()]);
  748. // 平台明细
  749. $log = [
  750. 'user_id' => 0,
  751. 'source_id' => $userId,
  752. 'source_order_no' => $orderNo,
  753. 'type' => 5,
  754. 'coin_type' => 1,
  755. 'user_type' => 4,
  756. 'money' => $fee,
  757. 'actual_money' => $fee,
  758. 'balance' => 0,
  759. 'create_time' => time(),
  760. 'update_time' => time(),
  761. 'hash' => $txID,
  762. 'remark' => "USDT余额提现",
  763. 'status' => 1,
  764. 'mark' => 1,
  765. ];
  766. AccountLogModel::insertGetId($log);
  767. // 平台流水
  768. FinanceService::make()->saveLog(0, $fee, 1);
  769. }
  770. }
  771. }
  772. }