BalanceLogService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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\MerchantModel;
  16. use App\Services\Api\FinanceService;
  17. use App\Services\Api\MessageService;
  18. use App\Services\BaseService;
  19. use App\Services\WalletService;
  20. use Illuminate\Support\Facades\DB;
  21. /**
  22. * 承兑商管理-服务类
  23. * @author laravel开发员
  24. * @since 2020/11/11
  25. * @package App\Services\Common
  26. */
  27. class BalanceLogService extends BaseService
  28. {
  29. /**
  30. * 构造函数
  31. * @author laravel开发员
  32. * @since 2020/11/11
  33. */
  34. public function __construct()
  35. {
  36. $this->model = new BalanceLogModel();
  37. }
  38. /**
  39. * 获取列表
  40. * @param $params 参数
  41. * @param int $pageSize 分页大小:默认 15
  42. * @return array
  43. */
  44. public function getDataList($params, $pageSize = 10, $field = [])
  45. {
  46. $where = ['a.mark' => 1];
  47. $query = $this->model->with(['member'])
  48. ->from('balance_logs as a')
  49. ->leftJoin('member as b', 'b.id', 'a.user_id')
  50. ->where($where)
  51. ->select($field ? $field : ['a.*']);
  52. if (isset($params['keyword']) && $params['keyword'] != '') {
  53. $query->where(function ($query) use ($params) {
  54. $kw = isset($params['keyword']) ? trim($params['keyword']) : '';
  55. if ($kw) {
  56. $query->where('b.nickname', 'like', "%{$params['keyword']}%")->orWhere('b.realname', 'like', "%{$params['keyword']}%")->orWhere('b.username', 'like', "%{$params['keyword']}%");
  57. }
  58. });
  59. }
  60. if (isset($params['order_no']) && $params['order_no'] != '') {
  61. $query->where('a.order_no', 'like', "%{$params['order_no']}%");
  62. }
  63. if (isset($params['user_type'])) {
  64. if (is_array($params['user_type'])) {
  65. $query->whereIn('a.type', $params['user_type']);
  66. } else {
  67. if ($params['user_type'] != '') {
  68. $query->where('a.user_type', $params['user_type']);
  69. }
  70. }
  71. }
  72. if (isset($params['type'])) {
  73. if (is_array($params['type'])) {
  74. $query->whereIn('a.type', $params['type']);
  75. } else {
  76. if ($params['type'] != '') {
  77. $query->where('a.type', $params['type']);
  78. }
  79. }
  80. }
  81. if (isset($params['pay_type'])) {
  82. if (is_array($params['pay_type'])) {
  83. $query->whereIn('a.pay_type', $params['pay_type']);
  84. } else {
  85. if ($params['pay_type'] != '') {
  86. $query->where('a.pay_type', $params['pay_type']);
  87. }
  88. }
  89. }
  90. if (isset($params['pay_status'])) {
  91. if (is_array($params['pay_status'])) {
  92. $query->whereIn('a.pay_status', $params['pay_status']);
  93. } else {
  94. if ($params['pay_status'] != '') {
  95. $query->where('a.pay_status', $params['pay_status']);
  96. }
  97. }
  98. }
  99. if (isset($params['coin_type'])) {
  100. if (is_array($params['coin_type'])) {
  101. $query->whereIn('a.coin_type', $params['coin_type']);
  102. } else {
  103. if ($params['coin_type'] != '') {
  104. $query->where('a.coin_type', $params['coin_type']);
  105. }
  106. }
  107. }
  108. if (isset($params['status'])) {
  109. if (is_array($params['status'])) {
  110. $query->whereIn('a.status', $params['status']);
  111. } else {
  112. if ($params['status'] != '') {
  113. $query->where('a.status', $params['status']);
  114. }
  115. }
  116. }
  117. $list = $query->paginate($pageSize > 0 ? $pageSize : 9999999);
  118. $list = $list ? $list->toArray() : [];
  119. if ($list) {
  120. foreach ($list['data'] as &$item) {
  121. $item['create_time_text'] = $item['create_time'] ? datetime($item['create_time']) : '';
  122. }
  123. }
  124. return [
  125. 'pageSize' => $pageSize,
  126. 'total' => isset($list['total']) ? $list['total'] : 0,
  127. 'list' => isset($list['data']) ? $list['data'] : []
  128. ];
  129. }
  130. /**
  131. * 添加会编辑会员
  132. * @return array
  133. * @since 2020/11/11
  134. * @author laravel开发员
  135. */
  136. public function edit()
  137. {
  138. // 请求参数
  139. $data = request()->all();
  140. $balanceLog = BalanceLogModel::where(['id' => $data['id']])->first();
  141. DB::beginTransaction();
  142. try {
  143. // 提现 审核通过
  144. if ($balanceLog['type'] == 2 && isset($data['status'])) {
  145. if ($data['status'] == 2) {
  146. if ($data['user_type'] == 1) {
  147. // 会员
  148. $this->withdrawMember($data);
  149. } else if ($data['user_type'] == 2) {
  150. // 商家
  151. $this->withdrawMerchat($data);
  152. } else if ($data['user_type'] == 3) {
  153. // 承兑商
  154. $this->withdrawAcceptor($data);
  155. }
  156. } else if ($data['status'] == 3) {
  157. $money = $data['money'];
  158. $userId = $data['user_id'];
  159. $accountLog = AccountLogModel::where(['source_order_no' => $balanceLog['order_no']])->first();
  160. if ($accountLog) {
  161. AccountLogModel::where(['source_order_no' => $balanceLog])->update([
  162. 'status' => 3,
  163. 'update_time' => time(),
  164. ]);
  165. $updateData = ['usdt' => DB::raw("usdt - {$money}"), 'update_time' => time()];
  166. if (!MerchantModel::where(['user_id' => $userId, 'mark' => 1])->update($updateData)) {
  167. DB::rollBack();
  168. return message('审核不通过操作失败');
  169. }
  170. }
  171. }
  172. }
  173. } catch (\Exception $e) {
  174. DB::rollBack();
  175. return message('操作失败');
  176. }
  177. DB::commit();
  178. return parent::edit($data); // TODO: Change the autogenerated stub
  179. }
  180. private function withdrawMember(array $data)
  181. {
  182. $trcUrl = $data['trc_url'];
  183. $realUsdt = $data['actual_money'];
  184. $orderNo = $data['order_no'];
  185. $userType = $data['user_type'];
  186. $dateTime = $dateTime = date('Y-m-d H:i:s');
  187. $money = $data['money'];
  188. $userId = $data['user_id'];
  189. $fee = $data['fee'];
  190. // 打款处理
  191. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  192. $txID = isset($result['txId']) ? $result['txId'] : '';
  193. $payAddress = isset($result['address']) ? $result['address'] : '';
  194. if ($txID && $payAddress) {
  195. $updateData = ['hash' => $txID, 'wallet_url' => $payAddress, 'audit_remark' => '审核打款', 'status' => 2, 'update_time' => time()];
  196. if (BalanceLogModel::where(['order_no' => $orderNo, 'user_type' => $userType])->update($updateData)) {
  197. $title = $userType == 1 ? 'USDT余额提现审核成功' : '商户余额提现审核成功';
  198. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额审核成功,请耐心等候打款到账!!!";
  199. MessageService::make()->pushMessage($userId, $title, $message);
  200. AccountLogModel::where(['source_order_no' => $orderNo])->update(['hash' => $txID, 'update_time' => time()]);
  201. // 平台明细
  202. $log = [
  203. 'user_id' => 0,
  204. 'source_id' => $userId,
  205. 'source_order_no' => $orderNo,
  206. 'type' => 5,
  207. 'coin_type' => 1,
  208. 'user_type' => 4,
  209. 'money' => $fee,
  210. 'actual_money' => $fee,
  211. 'balance' => 0,
  212. 'create_time' => time(),
  213. 'update_time' => time(),
  214. 'hash' => $txID,
  215. 'remark' => "USDT余额提现",
  216. 'status' => 1,
  217. 'mark' => 1,
  218. ];
  219. AccountLogModel::insertGetId($log);
  220. // 平台流水
  221. FinanceService::make()->saveLog(0, $fee, 1);
  222. }
  223. }
  224. }
  225. private function withdrawMerchat(array $data)
  226. {
  227. $trcUrl = $data['trc_url'];
  228. $realUsdt = $data['actual_money'];
  229. $orderNo = $data['order_no'];
  230. $userType = $data['user_type'];
  231. $dateTime = $dateTime = date('Y-m-d H:i:s');
  232. $money = $data['money'];
  233. $userId = $data['user_id'];
  234. $fee = $data['fee'];
  235. // 打款处理
  236. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  237. $txID = isset($result['txId']) ? $result['txId'] : '';
  238. $payAddress = isset($result['address']) ? $result['address'] : '';
  239. if ($txID && $payAddress) {
  240. $updateData = ['hash' => $txID, 'wallet_url' => $payAddress, 'audit_remark' => '审核打款', 'status' => 2, 'update_time' => time()];
  241. if (BalanceLogModel::where(['order_no' => $orderNo, 'user_type' => $userType])->update($updateData)) {
  242. $title = $userType == 1 ? 'USDT余额提现审核成功' : '商户余额提现审核成功';
  243. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额审核成功,请耐心等候打款到账!!!";
  244. MessageService::make()->pushMessage($userId, $title, $message);
  245. AccountLogModel::where(['source_order_no' => $orderNo])->update(['hash' => $txID, 'update_time' => time()]);
  246. // 平台明细
  247. $log = [
  248. 'user_id' => 0,
  249. 'source_id' => $userId,
  250. 'source_order_no' => $orderNo,
  251. 'type' => 5,
  252. 'coin_type' => 1,
  253. 'user_type' => 4,
  254. 'money' => $fee,
  255. 'actual_money' => $fee,
  256. 'balance' => 0,
  257. 'create_time' => time(),
  258. 'update_time' => time(),
  259. 'hash' => $txID,
  260. 'remark' => "USDT余额提现",
  261. 'status' => 1,
  262. 'mark' => 1,
  263. ];
  264. AccountLogModel::insertGetId($log);
  265. // 平台流水
  266. FinanceService::make()->saveLog(0, $fee, 1);
  267. }
  268. }
  269. }
  270. private function withdrawAcceptor(array $data)
  271. {
  272. $trcUrl = $data['trc_url'];
  273. $realUsdt = $data['actual_money'];
  274. $orderNo = $data['order_no'];
  275. $userType = $data['user_type'];
  276. $dateTime = $dateTime = date('Y-m-d H:i:s');
  277. $money = $data['money'];
  278. $userId = $data['user_id'];
  279. $fee = $data['fee'];
  280. // 打款处理
  281. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  282. $txID = isset($result['txId']) ? $result['txId'] : '';
  283. $payAddress = isset($result['address']) ? $result['address'] : '';
  284. if ($txID && $payAddress) {
  285. $updateData = ['hash' => $txID, 'wallet_url' => $payAddress, 'audit_remark' => '审核打款', 'status' => 2, 'update_time' => time()];
  286. if (BalanceLogModel::where(['order_no' => $orderNo, 'user_type' => $userType])->update($updateData)) {
  287. $title = $userType == 1 ? 'USDT余额提现审核成功' : '承兑商余额提现审核成功';
  288. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT承兑商余额审核成功,请耐心等候打款到账!!!";
  289. MessageService::make()->pushMessage($userId, $title, $message, 3);
  290. AccountLogModel::where(['source_order_no' => $orderNo])->update(['hash' => $txID, 'update_time' => time()]);
  291. // 平台明细
  292. $log = [
  293. 'user_id' => 0,
  294. 'source_id' => $userId,
  295. 'source_order_no' => $orderNo,
  296. 'type' => 5,
  297. 'coin_type' => 1,
  298. 'user_type' => 4,
  299. 'money' => $fee,
  300. 'actual_money' => $fee,
  301. 'balance' => 0,
  302. 'create_time' => time(),
  303. 'update_time' => time(),
  304. 'hash' => $txID,
  305. 'remark' => "USDT余额提现",
  306. 'status' => 1,
  307. 'mark' => 1,
  308. ];
  309. AccountLogModel::insertGetId($log);
  310. // 平台流水
  311. FinanceService::make()->saveLog(0, $fee, 1);
  312. }
  313. }
  314. }
  315. }