AddMessageRule.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/13
  6. * Time: 9:13
  7. */
  8. namespace App\Console\Commands;
  9. use App\Models\MessageRule;
  10. use App\Service\RabbitMqUtil;
  11. use Illuminate\Console\Command;
  12. use Illuminate\Database\QueryException;
  13. use Illuminate\Support\Carbon;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. use PhpAmqpLib\Connection\AMQPStreamConnection;
  17. class AddMessageRule extends Command
  18. {
  19. /**
  20. * The name and signature of the console command.
  21. *
  22. * @var string
  23. */
  24. protected $signature = 'message:add';
  25. /**
  26. * The console command description.
  27. *
  28. * @var string
  29. */
  30. protected $description = '添加发送消息';
  31. /**
  32. * Create a new command instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct(MessageRule $messageRule, RabbitMqUtil $rabbitMqUtil)
  37. {
  38. parent::__construct();
  39. $this->messageRule = $messageRule;
  40. $this->rabbitMqUtil = $rabbitMqUtil;
  41. }
  42. /**
  43. * Execute the console command.
  44. *
  45. * @return mixed
  46. */
  47. public function handle()
  48. {
  49. $this->line("开始添加发送消息");
  50. $this->messageRule
  51. ->where('message_status', 0)
  52. ->where('send_time', '<', Carbon::now()->toDateTimeString())
  53. ->whereNotNUll('send_time')
  54. ->chunk(100, function($messages){
  55. foreach($messages as $message){
  56. DB::beginTransaction();
  57. try{
  58. $this->rabbitMqUtil->push('add_message_rule', $message);
  59. $message->message_status = 1;
  60. $message->save();
  61. DB::commit();
  62. Log::info('添加发送消息成功:'.$message->id);
  63. }catch (QueryException $exception){
  64. DB::rollBack();
  65. Log::error('添加发送消息:'.$exception->getMessage());
  66. }
  67. }
  68. });
  69. $this->line("添加发送消息结束");
  70. }
  71. }