| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- <?php
- namespace App\Services;
- use App\Services\BaseService;
- use App\Models\Collect;
- use App\Models\Recharge;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- use App\Helpers\TronHelper;
- use App\Services\WalletService;
- /**
- * 归集记录
- */
- class CollectService extends BaseService
- {
- public static string $MODEL = Collect::class;
- public static $THRESHOLD = 10; // 最小归集金额(USDT)
- /**
- * @description: 模型
- * @return {string}
- */
- public static function model() :string
- {
- return Collect::class;
- }
- /**
- * @description: 枚举
- * @return {*}
- */
- public static function enum() :string
- {
- return '';
- }
- /**
- * @description: 获取查询条件
- * @param {array} $search 查询内容
- * @return {array}
- */
- public static function getWhere(array $search = []) :array
- {
- $where = [];
- if(isset($search['coin']) && !empty($search['coin'])){
- $where[] = ['coin', '=', $search['coin']];
- }
- if(isset($search['net']) && !empty($search['net'])){
- $where[] = ['net', '=', $search['net']];
- }
- if(isset($search['to_address']) && !empty($search['to_address'])){
- $where[] = ['to_address', '=', $search['to_address']];
- }
- if(isset($search['from_address']) && !empty($search['from_address'])){
- $where[] = ['from_address', '=', $search['from_address']];
- }
- if(isset($search['id']) && !empty($search['id'])){
- $where[] = ['id', '=', $search['id']];
- }
- if(isset($search['txid']) && !empty($search['txid'])){
- $where[] = ['txid', '=', $search['txid']];
- }
- if(isset($search['status']) && $search['status'] != ''){
- $where[] = ['status', '=', $search['status']];
- }
- if (isset($search['amount']) && is_numeric($search['amount'])) {
- $where[] = ['amount', '>=', $search['amount']];
- }
- return $where;
- }
- /**
- * @description: 查询单条数据
- * @param array $search
- * @return \App\Models\Coin|null
- */
- public static function findOne(array $search): ?Collect
- {
- return self::model()::where(self::getWhere($search))->first();
- }
- /**
- * @description: 查询所有数据
- * @param array $search
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public static function findAll(array $search = [])
- {
- return self::model()::where(self::getWhere($search))->get();
- }
- /**
- * @description: 分页查询
- * @param array $search
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
- */
- public static function paginate(array $search = [])
- {
- $limit = isset($search['limit'])?$search['limit']:15;
- $paginator = self::model()::where(self::getWhere($search))->paginate($limit);
- return ['total' => $paginator->total(), 'data' => $paginator->items()];
- }
-
- /**
- * @description: 生成归集记录
- * @param {*} $address
- * @param {*} $coin
- * @param {*} $net
- * @return {*}
- */
- public static function createCollect($address ,$coin ,$net)
- {
- $amount = TronHelper::getTrc20Balance($address); // 获取地址的余额
- $info = self::findOne(['from_address' => $address ,'status' => self::model()::STATUS_STAY]);
-
- if($amount >= 0 ){
- if(empty($info)){
- $data = [];
- $data['from_address'] = $address;
- $data['amount'] = $amount;
- $data['coin'] = $coin;
- $data['net'] = $net;
- self::model()::create($data);
- }else{
- $info->amount = $amount;
- $info->save();
- }
- }
- }
- /**
- * @description: 按会员补建或刷新待归集记录,不执行链上归集
- * @param {*} $memberId
- * @return array
- */
- public static function syncMemberCollectRecords($memberId)
- {
- $recharges = Recharge::where('member_id', $memberId)
- ->where('status', Recharge::STATUS_SUCCESS)
- ->where('type', Recharge::TYPE_AUTO)
- ->orderByDesc('id')
- ->get();
- $result = [
- 'success' => true,
- 'member_id' => $memberId,
- 'recharge_count' => $recharges->count(),
- 'processed_count' => 0,
- 'created_count' => 0,
- 'updated_count' => 0,
- 'skipped_count' => 0,
- 'failed_count' => 0,
- 'items' => [],
- ];
- if ($recharges->isEmpty()) {
- $result['success'] = false;
- $result['message'] = '未找到已确认的自动充值记录';
- return $result;
- }
- $seen = [];
- foreach ($recharges as $recharge) {
- $address = $recharge->to_address;
- if (empty($address) || isset($seen[$address])) {
- continue;
- }
- $seen[$address] = true;
- self::resetInvalidStartedCollectsByAddress($address);
- $item = [
- 'from_address' => $address,
- 'recharge_txid' => $recharge->txid,
- 'action' => 'pending',
- 'collect_id' => null,
- 'amount' => null,
- 'error' => null,
- ];
- $openCollect = self::model()::where('from_address', $address)
- ->whereIn('status', [self::model()::STATUS_STAY, self::model()::STATUS_START])
- ->orderByDesc('id')
- ->first();
- if ($openCollect && intval($openCollect->status) === self::model()::STATUS_START) {
- $item['action'] = 'skipped_started';
- $item['collect_id'] = $openCollect->id;
- $item['amount'] = $openCollect->amount;
- $result['skipped_count']++;
- $result['processed_count']++;
- $result['items'][] = $item;
- continue;
- }
- $hadPending = $openCollect && intval($openCollect->status) === self::model()::STATUS_STAY;
- try {
- self::createCollect($address, $recharge->coin, $recharge->net);
- $collect = self::model()::where('from_address', $address)
- ->where('status', self::model()::STATUS_STAY)
- ->orderByDesc('id')
- ->first();
- $item['action'] = $hadPending ? 'updated' : 'created';
- $item['collect_id'] = $collect->id ?? null;
- $item['amount'] = $collect->amount ?? null;
- if ($hadPending) {
- $result['updated_count']++;
- } else {
- $result['created_count']++;
- }
- } catch (\Throwable $e) {
- $item['action'] = 'failed';
- $item['error'] = $e->getMessage();
- $result['failed_count']++;
- }
- $result['processed_count']++;
- $result['items'][] = $item;
- }
- if ($result['processed_count'] === 0) {
- $result['success'] = false;
- $result['message'] = '未找到可处理的充值地址';
- }
- return $result;
- }
- private static function resetInvalidStartedCollectsByAddress($address)
- {
- $updated = self::model()::where('from_address', $address)
- ->where('status', self::model()::STATUS_START)
- ->where(function ($query) {
- $query->whereNull('txid')
- ->orWhere('txid', '');
- })
- ->update([
- 'status' => self::model()::STATUS_STAY,
- 'to_address' => null,
- 'remark' => 'reset invalid started collect',
- 'updated_at' => now(),
- ]);
- if ($updated > 0) {
- Log::warning('reset invalid started collects', [
- 'from_address' => $address,
- 'count' => $updated,
- ]);
- }
- return $updated;
- }
- /**
- * @description: 处理指定会员待归集记录,会执行链上归集
- * @param {*} $memberId
- * @return array
- */
- public static function syncCollectStayByMember($memberId)
- {
- $result = [
- 'member_id' => $memberId,
- 'threshold' => self::$THRESHOLD,
- 'from_address' => null,
- 'to_address' => null,
- 'pending_count' => 0,
- 'handled_count' => 0,
- 'success_count' => 0,
- 'fail_count' => 0,
- 'items' => [],
- ];
- $walletInfo = WalletService::findOne(['member_id' => $memberId, 'coin' => 'USDT']);
- if (empty($walletInfo) || empty($walletInfo->address)) {
- $result['message'] = '未找到该用户的USDT钱包地址';
- Log::warning('syncCollectStayByMember skipped: wallet missing', [
- 'member_id' => $memberId,
- ]);
- return $result;
- }
- $result['from_address'] = $walletInfo->address;
- self::resetInvalidStartedCollectsByAddress($walletInfo->address);
- $to_address = self::getUsdtAddress();
- $trx_private_key = self::getTrxPrivateKey();
- $result['to_address'] = $to_address;
- Log::info('syncCollectStayByMember start', [
- 'member_id' => $memberId,
- 'from_address' => $walletInfo->address,
- 'threshold' => self::$THRESHOLD,
- 'to_address' => $to_address,
- 'has_trx_private_key' => !empty($trx_private_key),
- ]);
- if (!$to_address || !$trx_private_key) {
- $result['message'] = '归集配置不完整';
- Log::warning('syncCollectStayByMember skipped: missing config', [
- 'member_id' => $memberId,
- 'to_address' => $to_address,
- 'has_trx_private_key' => !empty($trx_private_key),
- ]);
- return $result;
- }
- $list = self::findAll([
- 'status' => self::model()::STATUS_STAY,
- 'amount' => self::$THRESHOLD,
- 'from_address' => $walletInfo->address,
- ]);
- $result['pending_count'] = $list->count();
- if ($list->isEmpty()) {
- $result['message'] = '该用户没有待归集记录';
- Log::info('syncCollectStayByMember finished: no pending collects', $result);
- return $result;
- }
- foreach ($list as $v) {
- $item = [
- 'id' => $v['id'],
- 'from_address' => $v['from_address'],
- 'amount' => $v['amount'],
- 'status' => 'pending',
- ];
- $data = [];
- $wallets = WalletService::findOne(['address' => $v['from_address']]);
- if (empty($wallets) || empty($wallets['private_key'])) {
- $item['status'] = 'wallet_not_found';
- $item['error'] = '未找到归集钱包私钥';
- $data['remark'] = $item['error'];
- $data['updated_at'] = now();
- self::model()::where(self::getWhere(['id' => $v['id']]))->update($data);
- $result['fail_count']++;
- $result['handled_count']++;
- $result['items'][] = $item;
- Log::warning('syncCollectStayByMember wallet missing', $item + ['member_id' => $memberId]);
- continue;
- }
- $privateKey = $wallets['private_key'];
- $trxBalance = TronHelper::getTrxBalance($v['from_address']);
- $item['trx_balance'] = $trxBalance;
- if ($trxBalance < 10) {
- $trxResult = TronHelper::sendTrx($trx_private_key, $v['from_address'], 10);
- $item['trx_topup_result'] = $trxResult;
- Log::info('syncCollectStayByMember topup trx', [
- 'member_id' => $memberId,
- 'from_address' => $v['from_address'],
- 'trx_balance' => $trxBalance,
- 'result' => $trxResult,
- ]);
- if ($trxResult === false || is_string($trxResult)) {
- $error = is_string($trxResult) ? $trxResult : 'TRX能量补充失败';
- $data['status'] = self::model()::STATUS_STAY;
- $data['to_address'] = null;
- $data['txid'] = null;
- $data['remark'] = $error;
- $data['updated_at'] = now();
- $item['status'] = 'trx_topup_failed';
- $item['error'] = $error;
- self::model()::where(self::getWhere(['id' => $v['id']]))->update($data);
- $result['fail_count']++;
- $result['handled_count']++;
- $result['items'][] = $item;
- Log::warning('syncCollectStayByMember topup trx failed', $item + ['member_id' => $memberId]);
- continue;
- }
- }
- $transferResult = TronHelper::transferUSDT($privateKey, $to_address, $v['amount']);
- $item['transfer_result'] = $transferResult;
- if (is_array($transferResult) && !empty($transferResult['success'])) {
- $data['to_address'] = $to_address;
- $data['txid'] = $transferResult['txid'] ?? '';
- $data['remark'] = 'success';
- $data['status'] = self::model()::STATUS_START;
- $item['status'] = 'success';
- $item['txid'] = $data['txid'];
- $result['success_count']++;
- Log::info('syncCollectStayByMember transfer success', $item + ['member_id' => $memberId]);
- } else {
- $error = is_array($transferResult)
- ? ($transferResult['error'] ?? 'USDT归集失败')
- : (is_string($transferResult) ? $transferResult : 'USDT归集失败');
- $data['status'] = self::model()::STATUS_STAY;
- $data['to_address'] = null;
- $data['txid'] = null;
- $data['remark'] = $error;
- $item['status'] = 'failed';
- $item['error'] = $error;
- $result['fail_count']++;
- Log::warning('syncCollectStayByMember transfer failed', $item + ['member_id' => $memberId]);
- }
- $data['updated_at'] = now();
- self::model()::where(self::getWhere(['id' => $v['id']]))->update($data);
- $result['handled_count']++;
- $result['items'][] = $item;
- }
- Log::info('syncCollectStayByMember finished', $result);
- return $result;
- }
- /**
- * @description: 处理待归集的
- * @return {*}
- */
- public static function syncCollectStay()
- {
- $result = [
- 'threshold' => self::$THRESHOLD,
- 'to_address' => null,
- 'pending_count' => 0,
- 'handled_count' => 0,
- 'success_count' => 0,
- 'fail_count' => 0,
- 'items' => [],
- ];
- $to_address = self::getUsdtAddress(); // 转账的接收地址
- $trx_private_key = self::getTrxPrivateKey(); // 获取TRX能量的秘钥
- $result['to_address'] = $to_address;
- Log::info('syncCollectStay start', [
- 'threshold' => self::$THRESHOLD,
- 'to_address' => $to_address,
- 'has_trx_private_key' => !empty($trx_private_key),
- ]);
- if (!$to_address || !$trx_private_key) {
- $result['message'] = '归集配置不完整';
- Log::warning('syncCollectStay skipped: missing config', [
- 'to_address' => $to_address,
- 'has_trx_private_key' => !empty($trx_private_key),
- ]);
- return $result;
- }
- $list = self::findAll(['status' => self::model()::STATUS_STAY ,'amount' => self::$THRESHOLD]);
- $result['pending_count'] = $list->count();
- if ($list->isEmpty()) {
- $result['message'] = '没有待归集记录';
- Log::info('syncCollectStay finished: no pending collects', $result);
- return $result;
- }
- foreach($list as $k => $v){
- $item = [
- 'id' => $v['id'],
- 'from_address' => $v['from_address'],
- 'amount' => $v['amount'],
- 'status' => 'pending',
- ];
- $data = [];
- $wallets = WalletService::findOne(['address' => $v['from_address']]);
- if (empty($wallets) || empty($wallets['private_key'])) {
- $item['status'] = 'wallet_not_found';
- $item['error'] = '未找到归集钱包私钥';
- $data['remark'] = $item['error'];
- $data['updated_at'] = now();
- self::model()::where(self::getWhere(['id' => $v['id']]))->update($data);
- $result['fail_count']++;
- $result['handled_count']++;
- $result['items'][] = $item;
- Log::warning('syncCollectStay wallet missing', $item);
- continue;
- }
- $privateKey = $wallets['private_key'];
- $trxBalance = TronHelper::getTrxBalance($v['from_address']);
- $item['trx_balance'] = $trxBalance;
- if($trxBalance < 10){
- $trxResult = TronHelper::sendTrx($trx_private_key,$v['from_address'],10);
- $item['trx_topup_result'] = $trxResult;
- Log::info('syncCollectStay topup trx', [
- 'from_address' => $v['from_address'],
- 'trx_balance' => $trxBalance,
- 'result' => $trxResult,
- ]);
- if ($trxResult === false || is_string($trxResult)) {
- $error = is_string($trxResult) ? $trxResult : 'TRX能量补充失败';
- $data['status'] = self::model()::STATUS_STAY;
- $data['to_address'] = null;
- $data['txid'] = null;
- $data['remark'] = $error;
- $data['updated_at'] = now();
- $item['status'] = 'trx_topup_failed';
- $item['error'] = $error;
- self::model()::where(self::getWhere(['id' => $v['id']]))->update($data);
- $result['fail_count']++;
- $result['handled_count']++;
- $result['items'][] = $item;
- Log::warning('syncCollectStay topup trx failed', $item);
- continue;
- }
- }
- $transferResult = TronHelper::transferUSDT($privateKey,$to_address,$v['amount']);
- $item['transfer_result'] = $transferResult;
- if(is_array($transferResult) && !empty($transferResult['success'])){
- $data['to_address'] = $to_address;
- $data['txid'] = $transferResult['txid'] ?? '';
- $data['remark'] = 'success';
- $data['status'] = self::model()::STATUS_START;
- $item['status'] = 'success';
- $item['txid'] = $data['txid'];
- $result['success_count']++;
- Log::info('syncCollectStay transfer success', $item);
- }else{
- $error = is_array($transferResult)
- ? ($transferResult['error'] ?? 'USDT归集失败')
- : (is_string($transferResult) ? $transferResult : 'USDT归集失败');
- $data['status'] = self::model()::STATUS_STAY;
- $data['to_address'] = null;
- $data['txid'] = null;
- $data['remark'] = $error;
- $item['status'] = 'failed';
- $item['error'] = $error;
- $result['fail_count']++;
- Log::warning('syncCollectStay transfer failed', $item);
- }
- $data['updated_at'] = now();
- self::model()::where(self::getWhere(['id' => $v['id']]))->update($data);
- $result['handled_count']++;
- $result['items'][] = $item;
- }
- Log::info('syncCollectStay finished', $result);
- return $result;
- }
- /**
- * @description: 获取归集平台的接收地址
- * @return {*}
- */
- public static function getUsdtAddress()
- {
- $usdt_address = config('app.usdt_address');
- return $usdt_address;
- }
- /**
- * @description: 获取TRX能量账号秘钥
- * @return {*}
- */
- public static function getTrxPrivateKey()
- {
- $str = config('app.trx_private_key');
- return $str;
- }
-
- }
|