初始化仓库
This commit is contained in:
293
member/models/forms/AskForm.php
Normal file
293
member/models/forms/AskForm.php
Normal file
@@ -0,0 +1,293 @@
|
||||
<?php
|
||||
namespace member\models\forms;
|
||||
|
||||
use common\core\BaseModel;
|
||||
use common\enums\UserStatusEnum;
|
||||
use common\events\OrderEvent;
|
||||
use common\helpers\ArrayHelper;
|
||||
use common\helpers\FuncHelper;
|
||||
use common\models\DoctorPatient;
|
||||
use common\models\Order;
|
||||
use common\models\OrderVideoInfo;
|
||||
use common\models\ServiceUser;
|
||||
use common\models\UserInquiry;
|
||||
use common\models\UserPatient;
|
||||
use common\models\UserPatientHealthInquiry;
|
||||
use yii\base\Exception;
|
||||
|
||||
//问诊form
|
||||
class AskForm extends BaseModel
|
||||
{
|
||||
public $doctor_id;
|
||||
public $desc;
|
||||
public $images;
|
||||
public $patient_id;
|
||||
public $is_visit;
|
||||
public $visit_desc;
|
||||
|
||||
public $liver_function;
|
||||
public $renal_function;
|
||||
public $allergic_status;
|
||||
public $allergic_history;
|
||||
public $person_status;
|
||||
public $person_history;
|
||||
public $family_status;
|
||||
public $family_history;
|
||||
public $type;
|
||||
public $store_id;
|
||||
|
||||
public $patient_data;
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['doctor_id','desc','images','patient_id','type'],'required'],
|
||||
['desc', 'string', 'length' => [10, 255]],
|
||||
['images', 'checkImages'],
|
||||
['patient_id','checkPatient'],
|
||||
['is_visit','default','value'=> 0],
|
||||
['visit_desc','required','when'=>function($model){
|
||||
return $model->is_visit == 1;
|
||||
}],
|
||||
['visit_desc','default','value'=>''],
|
||||
|
||||
[['liver_function', 'renal_function', 'allergic_status', 'person_status','family_status'], 'default', 'value' => 0],
|
||||
[['allergic_history', 'person_history', 'family_history'], 'default', 'value' => '[]'],
|
||||
|
||||
['allergic_history','required','when'=>function($model){
|
||||
return $model->allergic_status == 1;
|
||||
}],
|
||||
['person_history','required','when'=>function($model){
|
||||
return $model->person_status == 1;
|
||||
}],
|
||||
['family_history','required','when'=>function($model){
|
||||
return $model->family_status == 1;
|
||||
}],
|
||||
];
|
||||
}
|
||||
|
||||
public function checkImages($attribute, $params)
|
||||
{
|
||||
$images = json_decode($this->images,true);
|
||||
if(empty($images)){
|
||||
$this->addError($attribute, '检查报告或患处图片不能为空');
|
||||
}
|
||||
}
|
||||
|
||||
public function checkPatient($attribute, $params)
|
||||
{
|
||||
$patient = UserPatient::find()->where([
|
||||
'id' => $this->patient_id,
|
||||
'user_id' => \Yii::$app->user->identity->id,
|
||||
'is_delete' => 0,
|
||||
])->one();
|
||||
if(!$patient){
|
||||
$this->addError($attribute, '患者不存在');
|
||||
}
|
||||
$this->patient_data = ArrayHelper::toArray($patient,[
|
||||
UserPatient::class => [
|
||||
'id','name','id_card','sex','relation','mobile','is_default','avatar'
|
||||
// 'age' => function($model){
|
||||
// return FuncHelper::getAgeFromIdNo($model->id_card);
|
||||
// },
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'desc' => '病情',
|
||||
'images' => '报告或患者图片',
|
||||
'patient_id' => '就诊人',
|
||||
'is_visit' => '是否就诊过',
|
||||
'visit_desc' => '实体医院就诊确诊病例名称',
|
||||
'liver_function' => '肝功能',
|
||||
'renal_function' => '肾功能',
|
||||
'allergic_history' => '过敏史',
|
||||
'person_history' => '个人病史',
|
||||
'family_history' => '家庭病史',
|
||||
];
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
if (!$this->validate()) {
|
||||
throw new Exception($this->getErrorMsg());
|
||||
}
|
||||
|
||||
//咨询的医生
|
||||
$doctor = ServiceUser::find()->where([
|
||||
'id' => $this->doctor_id,
|
||||
'is_delete' => 0,
|
||||
'status' => UserStatusEnum::OK
|
||||
])->with('docService')->one();
|
||||
if(!$doctor){
|
||||
throw new Exception('医生不存在');
|
||||
}
|
||||
if(!$doctor->docService->image_limit_status){
|
||||
throw new Exception('医生未开通图文问诊服务');
|
||||
}
|
||||
$t = \Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$UserInquiry = new UserInquiry();
|
||||
$UserInquiry->attributes = $this->attributes;
|
||||
$UserInquiry->su_id = $this->doctor_id;
|
||||
$UserInquiry->user_id = \Yii::$app->user->identity->id;
|
||||
$UserInquiry->patient_data = json_encode($this->patient_data);
|
||||
$UserInquiry->saveOrFail();
|
||||
|
||||
$order = new Order();
|
||||
$order->user_id = \Yii::$app->user->identity->id;
|
||||
$order->su_id = $this->doctor_id;
|
||||
$order->up_id=$this->patient_id;
|
||||
$order->type = $this->type;
|
||||
$order->ui_id = $UserInquiry->id;
|
||||
$order->order_no = FuncHelper::generate_order_no('SN');
|
||||
$order->total_pay_price = $doctor->docService->register_price;
|
||||
$order->saveOrFail();
|
||||
|
||||
$event = new OrderEvent();//触发订单创建事件
|
||||
$event->order = $order;
|
||||
$event->sender = $this;
|
||||
\Yii::$app->trigger(Order::EVENT_CREATED, $event);
|
||||
|
||||
$t->commit();
|
||||
}catch (Exception $exception){
|
||||
$t->rollBack();
|
||||
throw $exception;
|
||||
}
|
||||
return ['order_id' => $order->id];
|
||||
}
|
||||
|
||||
public function saves()
|
||||
{
|
||||
if (!$this->validate()) {
|
||||
throw new Exception($this->getErrorMsg());
|
||||
}
|
||||
|
||||
//咨询的医生
|
||||
$doctor = ServiceUser::find()->where([
|
||||
'id' => $this->doctor_id,
|
||||
'is_delete' => 0,
|
||||
'status' => UserStatusEnum::OK
|
||||
])->with('docService')->one();
|
||||
if(!$doctor){
|
||||
throw new Exception('医生不存在');
|
||||
}
|
||||
switch ($this->type){
|
||||
case 1:
|
||||
if(!$doctor->docService->image_limit_status){
|
||||
throw new Exception('医生未开通图文问诊服务');
|
||||
}
|
||||
$t = \Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$UserInquiry = new UserInquiry();
|
||||
$UserInquiry->attributes = $this->attributes;
|
||||
$UserInquiry->su_id = $this->doctor_id;
|
||||
$UserInquiry->up_id=$this->patient_id;
|
||||
$UserInquiry->store_id=$this->store_id;
|
||||
$UserInquiry->user_id = \Yii::$app->user->identity->id;
|
||||
$UserInquiry->patient_data = json_encode($this->patient_data);
|
||||
$UserInquiry->saveOrFail();
|
||||
//医生患者
|
||||
$DoctorPatient=new DoctorPatient();
|
||||
$DoctorPatient->user_id=\Yii::$app->user->identity->id;
|
||||
$DoctorPatient->su_id=$this->doctor_id;
|
||||
$DoctorPatient->up_id=$this->patient_id;
|
||||
$data=$this->patient_data;
|
||||
$DoctorPatient->name=$data['name'];
|
||||
$DoctorPatient->avatar=$data['avatar']??'';
|
||||
$DoctorPatient->id_card=$data['id_card'];
|
||||
$DoctorPatient->sex=$data['sex'];
|
||||
$DoctorPatient->saveOrFail();
|
||||
|
||||
|
||||
//创建订单
|
||||
$order = new Order();
|
||||
$order->user_id = \Yii::$app->user->identity->id;
|
||||
$order->su_id = $this->doctor_id;
|
||||
$order->up_id=$this->patient_id;
|
||||
$order->type = $this->type;
|
||||
$order->ui_id = $UserInquiry->id;
|
||||
$order->store_id=$this->store_id;
|
||||
$order->order_no = FuncHelper::generate_order_no('SN');
|
||||
$order->total_pay_price = $doctor->docService->register_price;
|
||||
$order->saveOrFail();
|
||||
|
||||
$event = new OrderEvent();//触发订单创建事件
|
||||
$event->order = $order;
|
||||
$event->sender = $this;
|
||||
\Yii::$app->trigger(Order::EVENT_CREATED, $event);
|
||||
|
||||
$t->commit();
|
||||
}catch (Exception $exception){
|
||||
$t->rollBack();
|
||||
throw $exception;
|
||||
}
|
||||
return ['order_id' => $order->id];
|
||||
|
||||
case 2:
|
||||
if(!$doctor->docService->video_limit_status){
|
||||
throw new Exception('医生未开通视频问诊服务');
|
||||
}
|
||||
|
||||
$t = \Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$UserInquiry = new UserInquiry();
|
||||
$UserInquiry->attributes = $this->attributes;
|
||||
$UserInquiry->su_id = $this->doctor_id;
|
||||
$UserInquiry->up_id=$this->patient_id;
|
||||
$UserInquiry->user_id = \Yii::$app->user->identity->id;
|
||||
$UserInquiry->store_id = $this->store_id;
|
||||
$UserInquiry->patient_data = json_encode($this->patient_data);
|
||||
$UserInquiry->saveOrFail();
|
||||
//创建订单
|
||||
$order = new Order();
|
||||
$order->user_id = \Yii::$app->user->identity->id;
|
||||
$order->su_id = $this->doctor_id;
|
||||
$order->up_id=$this->patient_id;
|
||||
$order->type = $this->type;
|
||||
$order->ui_id = $UserInquiry->id;
|
||||
$order->store_id=$this->store_id;
|
||||
$order->order_no = FuncHelper::generate_order_no('SN');
|
||||
$order->saveOrFail();
|
||||
|
||||
//医生患者
|
||||
$DoctorPatient=new DoctorPatient();
|
||||
$DoctorPatient->user_id=\Yii::$app->user->identity->id;
|
||||
$DoctorPatient->su_id=$this->doctor_id;
|
||||
$DoctorPatient->up_id=$this->patient_id;
|
||||
$data=$this->patient_data;
|
||||
$DoctorPatient->name=$data['name'];
|
||||
$DoctorPatient->avatar=$data['avatar']??'';
|
||||
$DoctorPatient->id_card=$data['id_card'];
|
||||
$DoctorPatient->sex=$data['sex'];
|
||||
$DoctorPatient->saveOrFail();
|
||||
|
||||
|
||||
//创建视频问诊视频通话信息
|
||||
$OrderVideoInfo=new OrderVideoInfo();
|
||||
$OrderVideoInfo->order_id=$order->id;
|
||||
$OrderVideoInfo->user_id=\Yii::$app->user->identity->getId();
|
||||
$OrderVideoInfo->su_id=$this->doctor_id;
|
||||
$OrderVideoInfo->is_limit=1;
|
||||
$OrderVideoInfo->order_limit_minutes=10;
|
||||
$OrderVideoInfo->left_minutes=10;
|
||||
$OrderVideoInfo->saveOrFail();
|
||||
|
||||
$event = new OrderEvent();//触发订单创建事件
|
||||
$event->order = $order;
|
||||
$event->sender = $this;
|
||||
\Yii::$app->trigger(Order::EVENT_CREATED, $event);
|
||||
|
||||
$t->commit();
|
||||
}catch (Exception $exception){
|
||||
$t->rollBack();
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
return ['order_id' => $order->id];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user