AudioJob.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Redis;
  10. use Illuminate\Support\Facades\Config;
  11. use Illuminate\Support\Facades\Log;
  12. use App\Common\Library\Alltools;
  13. class AudioJob implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $msgid;
  17. /**
  18. * Create a new job instance.
  19. *
  20. * @return void
  21. */
  22. public function __construct($msgid)
  23. {
  24. //
  25. $this->msgid = $msgid;
  26. Log::info(date('Y-m-d H:i:d').':'.$msgid);
  27. }
  28. /**
  29. * Execute the job.
  30. *
  31. * @return void
  32. */
  33. public function handle()
  34. {
  35. $result = $this->goaaa();
  36. /*if($result === false){
  37. //防止失败
  38. $key = 'kgelavarel_giftjob_'.$this->msgid;
  39. $limit_rs = $this->apiLimit(2,3600000,$key);//一小时最多另执行2次
  40. if($limit_rs === true){
  41. //echo 123;
  42. dispatch((new GiftJob($this->msgid))->delay(60));//加入队列
  43. }else{
  44. //echo 345;
  45. }
  46. }*/
  47. }
  48. //主要调用
  49. public function goaaa(){
  50. //
  51. $data = DB::table('mt_user_match_audio_log')->where('id',$this->msgid)->where('plantask_status',0)->first();
  52. if(empty($data)){return true;}
  53. DB::beginTransaction();
  54. $gold = bcsub($data->money,$data->free_money,1);
  55. if($gold > 0){
  56. $Alltools = new Alltools;
  57. $result = $Alltools->shouyi($data->to_user_id,$gold,'user_match_audio_log',$this->msgid);
  58. if($result !== true){
  59. DB::rollBack();
  60. return false;
  61. }
  62. }
  63. //
  64. $update = [
  65. 'plantask_status' => 1,
  66. ];
  67. $rs_update = DB::table('mt_user_match_audio_log')->where('id',$this->msgid)->update($update);
  68. if(!$rs_update){
  69. Db::rollBack();
  70. return false;
  71. }
  72. DB::commit();
  73. return true;
  74. }
  75. private function apiLimit($apiLimit = 1, $apiLimitTime = 1000, $key = '')
  76. {
  77. $app_name = Config::get('app.name');// laravel name
  78. $key = "{$app_name}:{$key}";
  79. $redis = Redis::connection();
  80. //
  81. //指定键值新增+1 并获取
  82. $count = $redis->incr($key);
  83. if ($count > $apiLimit) {
  84. return false;
  85. }
  86. //设置过期时间
  87. if ($count == 1) {
  88. $redis->pExpire($key, $apiLimitTime);
  89. }
  90. return true;
  91. }
  92. }