RechargeService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /**
  14. * 用户充值记录
  15. */
  16. class RechargeService extends BaseService
  17. {
  18. public static string $MODEL = Recharge::class;
  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 static::$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 static::$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 = static::$MODEL::where(self::getWhere($search))
  96. ->with(['member'])
  97. ->orderBy("created_at", 'desc')
  98. ->paginate($limit);
  99. $list = $paginator->items();
  100. $totalAmount = 0;
  101. $totalSuccess = 0;
  102. $totalFail = 0;
  103. foreach ($list as $item) {
  104. $item['amount'] = floatval($item['amount']);
  105. $totalAmount += $item['amount'];
  106. if ($item['status'] == 1) $totalSuccess += $item['amount'];
  107. if ($item['status'] == 2) $totalFail += $item['amount'];
  108. }
  109. return [
  110. 'total' => $paginator->total(),
  111. 'total_amount' => $totalAmount,
  112. 'total_success' => $totalSuccess,
  113. 'total_fail' => $totalFail,
  114. 'data' => $list];
  115. }
  116. /**
  117. * @description: 同步会员的USDT充值记录
  118. * @param {*} $memberId
  119. * @return {*}
  120. */
  121. public static function syncUsdtRechargeRecords($memberId)
  122. {
  123. $walletInfo = WalletService::findOne(['member_id' => $memberId]);
  124. $data = TronHelper::getTrc20UsdtRecharges($walletInfo->address);
  125. foreach ($data as $k => $v) {
  126. $v['member_id'] = $memberId;
  127. $v['net'] = $walletInfo->net;
  128. $V['type'] = static::$MODEL::TYPE_AUTO;
  129. $v['created_at'] = now();
  130. $v['updated_at'] = now();
  131. $data[$k] = $v;
  132. }
  133. $m = new (static::$MODEL);
  134. $result = $m->insertOrIgnore($data);
  135. return $result;
  136. }
  137. /**
  138. * @description: 充值确认
  139. * @param {*} $txid
  140. * @return {*}
  141. */
  142. public static function handleRechargeConfirmation($txid)
  143. {
  144. $info = self::findOne(['txid' => $txid]); // 获取充值的信息
  145. // 待处理进行充值
  146. if ($info['status'] == static::$MODEL::STATUS_STAY) {
  147. $result = TronHelper::getTransactionConfirmations($txid);
  148. if ($result['success']) {
  149. $data = [];
  150. $data['block_height'] = $result['block_number'];
  151. $data['confirmations'] = $result['latest_block'] - $result['block_number'];
  152. if ($data['confirmations'] >= TronHelper::CONFIRMED_NUMBER) {
  153. $data['status'] = static::$MODEL::STATUS_SUCCESS;
  154. $where = self::getWhere(['txid' => $txid, 'status' => static::$MODEL::STATUS_STAY]);
  155. $recharge = static::$MODEL::where($where)->update($data);
  156. // 更新成功,变动可用余额
  157. if ($recharge) {
  158. $balanceData = WalletService::updateBalance($info->member_id, $info->amount);
  159. BalanceLogService::addLog($info->member_id, $info->amount, $balanceData['before_balance'], $balanceData['after_balance'], '充值', $info->id, '');
  160. CollectService::createCollect($info->to_address, $info->coin, $info->net);
  161. $amount = floatval($info->amount);
  162. $text = " ➕ 充币到帐 +{$amount} USDT\n";
  163. $text .= "链上哈希:{$txid}\n";
  164. $text .= "当前余额:{$balanceData['after_balance']}\n";
  165. TopUpService::notifyTransferSuccess($info->member_id, $text);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * @description: 同步待处理的充值记录
  173. * @return {*}
  174. */
  175. public static function syncRechargeStay()
  176. {
  177. $list = self::findAll(['status' => static::$MODEL::STATUS_STAY, 'type' => static::$MODEL::TYPE_AUTO]);
  178. foreach ($list as $k => $v) {
  179. self::handleRechargeConfirmation($v->txid);
  180. }
  181. }
  182. }