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()); } } }