RechargeService.php 7.7 KB

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