GetEnergyWalletTrxUsdtTrade.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Task;
  3. use App\Service\Energy\EnergyWalletServices;
  4. use App\Service\Energy\EnergyWalletTradeTrxServices;
  5. use App\Service\Energy\EnergyWalletTradeUsdtServices;
  6. use App\Model\Energy\EnergyWalletTradeList;
  7. use App\Library\Log;
  8. // use Swoole\Coroutine\WaitGroup;
  9. use Hyperf\Utils\Coroutine\Concurrent;
  10. class GetEnergyWalletTrxUsdtTrade
  11. {
  12. public function execute()
  13. {
  14. try {
  15. $energyWallet_services = new EnergyWalletServices();
  16. $list = $energyWallet_services->getList(); //获取收款列表
  17. if(!empty($list)){
  18. try {
  19. // $this->log('getenergywallettrxtrade','-----------开始执行:拉取钱包交易列表数据,钱包总数:'.count($list).'个--------------');
  20. //协程通知
  21. // $wg = new WaitGroup();
  22. //协程数量
  23. $concurrent = new Concurrent(5);
  24. foreach ($list as $k => $v) {
  25. // $wg->add();
  26. // go(function () use ($wg,$v) {
  27. // $energyWalletTradeTrx_services = new EnergyWalletTradeTrxServices();
  28. // $res = $this->handle($wg,$v,$energyWalletTradeTrx_services);
  29. // $energyWalletTradeUsdt_services = new EnergyWalletTradeUsdtServices();
  30. // $res = $this->handleUsdt($wg,$v,$energyWalletTradeUsdt_services);
  31. // $wg->done();
  32. // });
  33. $concurrent->create(function () use ($v) {
  34. // sleep(1); //不容易被api限制
  35. $energyWalletTradeTrx_services = new EnergyWalletTradeTrxServices();
  36. $res = $this->handle($v,$energyWalletTradeTrx_services);
  37. $energyWalletTradeUsdt_services = new EnergyWalletTradeUsdtServices();
  38. $res = $this->handleUsdt($v,$energyWalletTradeUsdt_services);
  39. });
  40. }
  41. // $wg->wait();
  42. // $this->log('getenergywallettrxtrade','-----------结束执行:拉取钱包交易列表数据--------------');
  43. }catch (\Exception $e){
  44. $this->log('getenergywallettrxtrade','拉取失败'.$e->getMessage().'----------');
  45. }
  46. }else{
  47. // $this->log('getenergywallettrxtrade','-----------没有钱包需要拉取交易数据--------');
  48. }
  49. }catch (\Exception $e){
  50. $this->log('getenergywallettrxtrade','----------任务执行报错,请联系管理员。报错原因:----------'.$e->getMessage());
  51. }
  52. }
  53. public function handle($v,$energyWalletTradeTrx_services){
  54. try {
  55. $start_time = EnergyWalletTradeList::where('transferto_address',$v['receive_wallet'])->where('coin_name','trx')->orderBy('timestamp','desc')->value('timestamp');
  56. if(empty($start_time)){
  57. $start_time = 0;
  58. }
  59. $get_tx_time = strtotime($v['get_tx_time'])*1000-10;
  60. if($get_tx_time > $start_time){
  61. $start_time = $get_tx_time;
  62. }
  63. // 获取收款数据-通过tronscan获取===选一种拉取就行
  64. $res = $energyWalletTradeTrx_services->getList($v,$start_time,thirteenTime());
  65. // 获取收款数据-通过trongrid获取===选一种拉取就行
  66. $res = $energyWalletTradeTrx_services->getListByGrid($v,$start_time);
  67. }catch (\Exception $e){
  68. return ['code' => 400,'msg'=>$v['receive_wallet'].',拉取地址失败,'.$e->getMessage().'----------'];
  69. }
  70. }
  71. public function handleUsdt($v,$energyWalletTradeUsdt_services){
  72. try {
  73. $start_time = EnergyWalletTradeList::where('transferto_address',$v['receive_wallet'])->where('coin_name','usdt')->orderBy('timestamp','desc')->value('timestamp');
  74. if(empty($start_time)){
  75. $start_time = 0;
  76. }
  77. $get_tx_time = strtotime($v['get_tx_time'])*1000-10;
  78. if($get_tx_time > $start_time){
  79. $start_time = $get_tx_time;
  80. }
  81. // 获取收款数据
  82. $res = $energyWalletTradeUsdt_services->getList($v,$start_time);
  83. return ['code' => 200,'msg'=>'充值钱包地址:'.$v['receive_wallet'].',成功总数'.$res['success_count'].',失败总数'.$res['error_count']];
  84. }catch (\Exception $e){
  85. return ['code' => 400,'msg'=>$v['receive_wallet'].',拉取地址失败,'.$e->getMessage().'----------'];
  86. }
  87. }
  88. /**
  89. * 记入日志
  90. * @param $log_title [日志路径]
  91. * @param $message [内容,不支持数组]
  92. * @param $remarks [备注]
  93. */
  94. protected function log($log_title,$message,$remarks='info'){
  95. Log::get($remarks,$log_title)->info($message);
  96. }
  97. }