Files
xk-api-yii/common/jobs/NewSendJob.php

65 lines
2.4 KiB
PHP
Raw Normal View History

2024-09-19 11:32:39 +08:00
<?php
namespace common\jobs;
use Carbon\Carbon;
use common\enums\SystemNoticeTypeEnum;
use common\models\DoctorInfo;
use common\models\DoctorPatient;
use common\models\Store;
use common\models\SystemNotice;
use yii\db\Exception;
use yii\helpers\Json;
use yii\queue\JobInterface;
class NewSendJob extends BaseJob implements JobInterface
{
public $store_id;
public $content;
public $su_id;
public $doctor_patient_ids;
/**
* @param Queue $queue which pushed and is handling the job
*/
public function execute($queue)
{
$t = \Yii::$app->db->beginTransaction();
try {
$this->setRequest();
\Yii::info('su_id为:'.$this->su_id.'的医生群发消息队列 start:'.Json::encode($this->doctor_patient_ids));
$doctorInfo = DoctorInfo::find()->select('su_id,name,depart_id,avatar')->where(['su_id' => $this->su_id])->with(['depart'])->asArray()->one();
$store = Store::find()->select('name')->where(['id' => $this->store_id])->asArray()->one();
$successCount = 0;
$array=array(
'doctor'=> $doctorInfo['name'],
'avatar'=> $doctorInfo['avatar'],
'content'=> $this->content,
);
$item=new SystemNotice();
foreach ($this->doctor_patient_ids as $v) {
$doctorPatient = DoctorPatient::find()->where(['su_id'=>$this->su_id,'up_id' => $v])->asArray()->one();
if (!$doctorPatient){
continue;
}
$item->store_id=$this->store_id;
$item->content =Json::encode($array);
$item->user_id=$doctorPatient['user_id']??0;
$item->scene_type=1;//发给用户端
$item->base_type=SystemNoticeTypeEnum::DOCTOR_NOTICE;
$item->notice_at=date('Y-m-d H:i:s',time());
$item->saveOrFail();
$successCount++;
}
$t->commit();
\Yii::info('su_id为:'.$this->su_id.'的医生群发消息队列 end: '.Carbon::now()->toDateTimeString() . "{$this->su_id}成功群发{$successCount}个用户");
} catch (\Exception $exception) {
$t->rollBack();
\Yii::error('su_id为:'.$this->su_id.'的医生群发消息队列 异常: '.$exception);
throw new Exception($exception->getMessage());
}
}
}