12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace App\Console\Commands;
- use App\Models\NoticeRule;
- use App\Service\RabbitMqUtil;
- use App\Traits\PostTrait;
- use Illuminate\Console\Command;
- use Illuminate\Database\QueryException;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use PhpAmqpLib\Connection\AMQPStreamConnection;
- class AddNoticeRule extends Command
- {
- use PostTrait;
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'notice:add';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '添加发送通知';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct(NoticeRule $noticeRule, RabbitMqUtil $rabbitMqUtil)
- {
- parent::__construct();
- $this->noticeRule = $noticeRule;
- $this->rabbitMqUtil = $rabbitMqUtil;
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->line("开始添加发送通知");
- $this->noticeRule
- ->where('notice_type', 0)
- ->where('notice_status', 0)
- ->where('send_time', '<', Carbon::now()->toDateTimeString())
- ->whereNotNUll('send_time')
- ->chunk(100, function($notices){
- foreach($notices as $notice){
- DB::beginTransaction();
- try{
- $notice->notice_status = 1;
- $noticeUsers = json_decode($notice->notice_users, true);
- if(isset($noticeUsers['category']) && $noticeUsers['category']){
- $uids = $this->getCategoryUids(json_encode($noticeUsers['category']));
- Log::debug('uids'.json_encode($uids));
- if(!$uids){
- DB::rollBack();
- Log::error('获取关注分类用户失败:');
- }
- $noticeUsers['category_uids'] = $uids;
- $notice->notice_users = json_encode($noticeUsers);
- }
- $notice->save();
- DB::commit();
- $this->rabbitMqUtil->push('add_notice_rule', $notice);
- Log::info('添加发送通知成功:'.$notice->id);
- }catch (QueryException $exception){
- DB::rollBack();
- Log::error('添加发送通知失败:'.$exception->getMessage());
- }
- }
- });
- $this->line("添加发送消息结束");
- }
- }
|