love_php/app/Console/Commands/SendActivityQuestionnaireSurvey.php
2026-04-02 09:20:51 +08:00

73 lines
2.3 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Activity;
use App\Models\User;
use App\Models\ActivityMember;
class SendActivityQuestionnaireSurvey extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:sendActivityQuestionnaireSurvey';
/**
* The console command description.
*
* @var string
*/
protected $description = '活动结束15分钟 发送【问卷调查】服务通知';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
//每1分钟调一次接口
public function handle()
{
$date = date('Y-m-d H:i:s');//当前时间
$late_date = date('Y-m-d H:i:s',strtotime('-15 minutes',strtotime($date)));//当前时间-15分钟 防止不发通知
$activity_ids = Activity::whereBetween('end_time',[$late_date,$date])->pluck('id')->toArray();
if (!empty($activity_ids)) { //如果有在此期间结束的活动 ==> 发送【问卷调查】服务通知
$activities = Activity::whereIn('id',$activity_ids)->orderBy('end_time','desc')->get();//要发送的活动
foreach ($activities as $activity) {
//参与活动的成员id
$member_ids = ActivityMember::where('activity_id',$activity->id)->where('is_joined',1)->pluck('user_id')->toArray();
foreach ($member_ids as $member_id) {
$member = User::find($member_id);
$param = $this->getParams($activity,$member);
\WechatService::sendActivityQuestionaireSurvey($param);
}
}
}
}
public function getParams($activity,$user){
$param['activity_id'] = $activity->id;
$param['openid'] = $user->wechat ? $user->wechat->openid : null;
$param['nickname'] = $user->nickname ? $user->nickname : $user->name;
$param['theme'] = $activity->theme;
$param['time'] = $activity->start_time;
$param['desc'] = '点击参与活动问卷调查,帮助我们做得更好。';
return $param;
}
}