RechargeService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\Recharge;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Services\WalletService;
  9. use App\Services\CollectService;
  10. use App\Services\UserService;
  11. use App\Services\BalanceLogService;
  12. use App\Helpers\TronHelper;
  13. use App\Models\Config;
  14. /**
  15. * 用户充值记录
  16. */
  17. class RechargeService extends BaseService
  18. {
  19. /**
  20. * @description: 模型
  21. * @return {string}
  22. */
  23. public static function model(): string
  24. {
  25. return Recharge::class;
  26. }
  27. /**
  28. * @description: 枚举
  29. * @return {*}
  30. */
  31. public static function enum(): string
  32. {
  33. return '';
  34. }
  35. /**
  36. * @description: 获取查询条件
  37. * @param {array} $search 查询内容
  38. * @return {array}
  39. */
  40. public static function getWhere(array $search = []): array
  41. {
  42. $where = [];
  43. if (isset($search['coin']) && !empty($search['coin'])) {
  44. $where[] = ['coin', '=', $search['coin']];
  45. }
  46. if (isset($search['net']) && !empty($search['net'])) {
  47. $where[] = ['net', '=', $search['net']];
  48. }
  49. if (isset($search['to_address']) && !empty($search['to_address'])) {
  50. $where[] = ['to_address', '=', $search['to_address']];
  51. }
  52. if (isset($search['id']) && !empty($search['id'])) {
  53. $where[] = ['id', '=', $search['id']];
  54. }
  55. if (isset($search['member_id']) && !empty($search['member_id'])) {
  56. $where[] = ['member_id', '=', $search['member_id']];
  57. }
  58. if (isset($search['txid']) && !empty($search['txid'])) {
  59. $where[] = ['txid', '=', $search['txid']];
  60. }
  61. if (isset($search['status']) && $search['status'] != '') {
  62. $where[] = ['status', '=', $search['status']];
  63. }
  64. if (isset($search['type']) && $search['type'] != '') {
  65. $where[] = ['type', '=', $search['type']];
  66. }
  67. return $where;
  68. }
  69. /**
  70. * @description: 查询单条数据
  71. * @param array $search
  72. * @return \App\Models\Coin|null
  73. */
  74. public static function findOne(array $search): ?Recharge
  75. {
  76. return self::model()::where(self::getWhere($search))->first();
  77. }
  78. /**
  79. * @description: 查询所有数据
  80. * @param array $search
  81. * @return \Illuminate\Database\Eloquent\Collection
  82. */
  83. public static function findAll(array $search = [])
  84. {
  85. return self::model()::where(self::getWhere($search))->get();
  86. }
  87. /**
  88. * @description: 分页查询
  89. * @param array $search
  90. * @return array
  91. */
  92. public static function paginate(array $search = [])
  93. {
  94. $limit = isset($search['limit']) ? $search['limit'] : 15;
  95. $paginator = self::model()::where(self::getWhere($search))
  96. ->orderBy("created_at", 'desc')
  97. ->paginate($limit);
  98. $list = $paginator->items();
  99. $totalAmount = 0;
  100. $totalSuccess = 0;
  101. $totalFail = 0;
  102. foreach ($list as $item) {
  103. $item['amount'] = floatval($item['amount']);
  104. $totalAmount += $item['amount'];
  105. if ($item['status'] == 1) $totalSuccess += $item['amount'];
  106. if ($item['status'] == 2) $totalFail += $item['amount'];
  107. }
  108. return [
  109. 'total' => $paginator->total(),
  110. 'total_amount' => $totalAmount,
  111. 'total_success' => $totalSuccess,
  112. 'total_fail' => $totalFail,
  113. 'data' => $list];
  114. }
  115. /**
  116. * @description: 同步会员的USDT充值记录
  117. * @param {*} $memberId
  118. * @return {*}
  119. */
  120. public static function syncUsdtRechargeRecords($memberId)
  121. {
  122. $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  123. $data = TronHelper::getTrc20UsdtRecharges($walletInfo->address);
  124. foreach ($data as $k => $v) {
  125. $v['member_id'] = $memberId;
  126. $v['net'] = $walletInfo->net;
  127. $V['type'] = self::model()::TYPE_AUTO;
  128. $v['created_at'] = now();
  129. $v['updated_at'] = now();
  130. $data[$k] = $v;
  131. }
  132. $m = new (self::model());
  133. $result = $m->insertOrIgnore($data);
  134. return $result;
  135. }
  136. /**
  137. * @description: 充值确认
  138. * @param {*} $txid
  139. * @return {*}
  140. */
  141. public static function handleRechargeConfirmation($txid)
  142. {
  143. $info = self::findOne(['txid' => $txid]); // 获取充值的信息
  144. // 汇率
  145. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  146. // 待处理进行充值
  147. if ($info['status'] == self::model()::STATUS_STAY) {
  148. $result = TronHelper::getTransactionConfirmations($txid);
  149. if ($result['success']) {
  150. $data = [];
  151. $data['block_height'] = $result['block_number'];
  152. $data['confirmations'] = $result['latest_block'] - $result['block_number'];
  153. $data['exchange_rate'] = $rate;
  154. if ($data['confirmations'] >= TronHelper::CONFIRMED_NUMBER) {
  155. $data['status'] = self::model()::STATUS_SUCCESS;
  156. $where = self::getWhere(['txid' => $txid, 'status' => self::model()::STATUS_STAY]);
  157. $recharge = self::model()::where($where)->update($data);
  158. $amount = floatval($info->amount);
  159. $rate_amount = bcmul($amount, $rate, 10); // 汇率转换后分数
  160. // 更新成功,变动可用余额
  161. if ($recharge) {
  162. $balanceData = WalletService::updateBalance($info->member_id, $rate_amount);
  163. BalanceLogService::addLog($info->member_id, $rate_amount, $balanceData['before_balance'], $balanceData['after_balance'], '充值', $info->id, '');
  164. CollectService::createCollect($info->to_address, $info->coin, $info->net);
  165. $amount = floatval($info->amount);
  166. $text = "充值结果通知\n";
  167. $text .= "充值数量:{$amount} USDT\n";
  168. $text .= "充值地址:{$info->to_address}\n";
  169. $text .= "汇率:1 USDT = {$rate} RMB\n";
  170. $text .= "折合金额:" . number_format($rate_amount, 2) . " RMB\n";
  171. $text .= "状态:成功\n";
  172. TopUpService::notifyTransferSuccess($info->member_id, $text);
  173. }
  174. }
  175. }
  176. }
  177. }
  178. /**
  179. * @description: 同步待处理的充值记录
  180. * @return {*}
  181. */
  182. public static function syncRechargeStay()
  183. {
  184. $list = self::findAll(['status' => self::model()::STATUS_STAY, 'type' => self::model()::TYPE_AUTO]);
  185. foreach ($list as $k => $v) {
  186. self::handleRechargeConfirmation($v->txid);
  187. }
  188. }
  189. }