197 lines
7.1 KiB
PHP
197 lines
7.1 KiB
PHP
<?php
|
||
|
||
namespace App\Jobs;
|
||
|
||
use App\Models\Notice;
|
||
use App\Models\NoticeMessage;
|
||
use App\Models\ProfileCourtship;
|
||
use App\Models\User;
|
||
use Illuminate\Bus\Queueable;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Queue\SerializesModels;
|
||
use Illuminate\Queue\InteractsWithQueue;
|
||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
use Illuminate\Foundation\Bus\Dispatchable;
|
||
use App\Repositories\Eloquent\SmsRepository;
|
||
use Illuminate\Container\Container as App;
|
||
use Illuminate\Support\Facades\Redis;
|
||
|
||
ini_set('max_execution_time', 1800);
|
||
class SendNoticeMessage implements ShouldQueue
|
||
{
|
||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
||
public $tries = 3;
|
||
public $timeout = 86400;
|
||
protected $param;
|
||
|
||
/**
|
||
* Create a new job instance.
|
||
*
|
||
*/
|
||
public function __construct($param)
|
||
{
|
||
$this->param = $param;
|
||
}
|
||
|
||
/**
|
||
* Execute the job.
|
||
*
|
||
*/
|
||
public function handle()
|
||
{
|
||
$data= $this->sendNoticeMessage($this->param);
|
||
$this->storeNotcie($data);
|
||
}
|
||
|
||
public function storeNotcie($param)
|
||
{
|
||
try {
|
||
$message = $param['message'];
|
||
$message_type = $param['message_type'];
|
||
$notice_message_id = $param['notice_message_id'];
|
||
$pic = $param['pic'];
|
||
$redis_key = 'notice_message_'.$notice_message_id;
|
||
$user_id = Redis::lpop($redis_key);
|
||
while ($user_id) {
|
||
$receive_status = 1;
|
||
$profile = ProfileCourtship::where('user_id', $user_id)->first();
|
||
if ($profile && $profile->message_config == 2) {
|
||
if ($message_type == 'marketing') {
|
||
$receive_status = 0;
|
||
}
|
||
}
|
||
$param2['user_id'] = $user_id;
|
||
$param2['send_user_id'] = 1;
|
||
$param2['type'] = $message_type;
|
||
$param2['type_id'] = 0;
|
||
$param2['content'] = $message;
|
||
$param2['message'] = $message;
|
||
$param2['status'] = 0;
|
||
$param2['pic'] = $pic;
|
||
$param2['notice_message_id']= $notice_message_id;
|
||
$param2['receive_status'] = $receive_status;
|
||
SendNotice::dispatch($param2)->onQueue('send_notice')->delay(now()->addSeconds($param['delay_seconds']));
|
||
$user_id = Redis::lpop($redis_key);
|
||
}
|
||
} catch (\Exception $e) {
|
||
\Log::error($e->getMessage() . ';line-' . $e->getLine());
|
||
}
|
||
}
|
||
|
||
public function sendNoticeMessage($param)
|
||
{
|
||
//检查参数
|
||
$content = $param['content'];
|
||
$pic = $param['pic'];
|
||
//类型 ALL所有, 单身, 介绍人, 男, 女, 会员, 非会员
|
||
$type = $param;
|
||
$message_type = $param['message_type'];
|
||
$user_type = $param['user_type'];
|
||
$user_sex = $param['user_sex'];
|
||
$user_rank = $param['user_rank'];
|
||
$is_approved = $param['is_approved'];
|
||
$user_ids = $param['user_ids'];
|
||
$city = $param['city'];
|
||
$province = $param['province'];
|
||
$timing = $param['timing'];
|
||
if ($type == 'ALL') {
|
||
$user_ids = User::whereHas('wechat', function ($query) {
|
||
$query->whereNotNull('openid');
|
||
});
|
||
if (!empty($province)) {
|
||
$user_ids = $user_ids->whereHas('profileCourtship', function ($query) use ($province) {
|
||
$query->where('province', $province);
|
||
});
|
||
}
|
||
if (!empty($city)) {
|
||
$user_ids = $user_ids->whereHas('profileCourtship', function ($query) use ($city) {
|
||
$query->where('city', $city);
|
||
});
|
||
}
|
||
$user_ids = $user_ids->pluck('id')->toArray();
|
||
} else {
|
||
$type = 'PART';
|
||
if (empty($user_ids)) {
|
||
$user_ids = User::whereRaw('1=1');
|
||
if (!empty($user_type) && $user_type != 'ALL') {
|
||
$user_ids = $user_ids->where('type', $user_type);
|
||
}
|
||
if (!empty($user_sex) && $user_sex != 'ALL') {
|
||
$user_ids = $user_ids->where('sex', $user_sex);
|
||
}
|
||
if ($user_rank) {
|
||
switch ($user_rank) {
|
||
case 1:
|
||
$user_ids = $user_ids->where('rank_id', '>', 0);
|
||
break;
|
||
|
||
default:
|
||
$user_ids = $user_ids->where('rank_id', 0);
|
||
break;
|
||
}
|
||
}
|
||
if ($is_approved) {
|
||
$user_ids = $user_ids->where('is_approved', 1);
|
||
}
|
||
if (!empty($province)) {
|
||
$user_ids = $user_ids->whereHas('profileCourtship', function ($query) use ($province) {
|
||
$query->where('province', $province);
|
||
});
|
||
}
|
||
if (!empty($city)) {
|
||
$user_ids = $user_ids->whereHas('profileCourtship', function ($query) use ($city) {
|
||
$query->where('city', $city);
|
||
});
|
||
}
|
||
$user_ids = $user_ids->pluck('id')->toArray();
|
||
}
|
||
}
|
||
// 创建助手消息
|
||
if (!$content) {
|
||
if ($message_type == 'system') {
|
||
$content = '系统稍息';
|
||
} else {
|
||
$content = '营销消息';
|
||
}
|
||
}
|
||
if ($user_ids) {
|
||
if ($type == 'ALL') {
|
||
$save_ids = json_encode(['全体用户'], JSON_UNESCAPED_UNICODE);
|
||
$save_nicknames = json_encode(['全体用户'], JSON_UNESCAPED_UNICODE);
|
||
} else {
|
||
$save_ids = json_encode(array_slice($user_ids, 0, 50));
|
||
$partly_user_ids = array_slice($user_ids, 0, 50);
|
||
$nicknames = User::whereIn('id', $partly_user_ids)->whereNotNull('nickname')->pluck('nickname')->toArray();
|
||
$save_nicknames = json_encode($nicknames, JSON_UNESCAPED_UNICODE);
|
||
}
|
||
$id = NoticeMessage::insertGetId([
|
||
'content' => $content,
|
||
'pic' => $pic,
|
||
'user_ids' => $save_ids,
|
||
'type' => $type,
|
||
'nicknames' => $save_nicknames,
|
||
'message_type' => $message_type,
|
||
'timing' => $timing?strtotime($timing):null,
|
||
'status' => $timing?0:1,
|
||
'operator' => auth()->id(),
|
||
'created_at' => date('Y-m-d H:i:s'),
|
||
'updated_at' => date('Y-m-d H:i:s'),
|
||
]);
|
||
foreach ($user_ids as $key => $val) {
|
||
Redis::lpush('notice_message_' . $id, $val);
|
||
}
|
||
$data['message'] = $content;
|
||
$data['pic'] = $pic;
|
||
$data['notice_message_id'] = $id;
|
||
$data['message_type'] = $message_type;
|
||
$data['delay_seconds'] = empty($timing) || (strtotime($timing) < time())? 0:strtotime($timing) - time();
|
||
//返回
|
||
return $data;
|
||
} else {
|
||
return false;
|
||
}
|
||
|
||
}
|
||
}
|