Files
xk-api-yii/member/models/forms/RegisterForm.php
2025-10-22 16:49:59 +08:00

196 lines
6.6 KiB
PHP

<?php
namespace member\models\forms;
use common\core\BaseModel;
use common\enums\RegisterEnum;
use common\enums\UserRoleEnum;
use common\enums\UserStatusEnum;
use common\events\RegisterEvent;
use common\helpers\FuncHelper;
use common\models\oldDb\DoctorPatient;
use common\models\oldDb\Register;
use common\models\oldDb\ServiceUser;
use common\models\oldDb\StoreDoctor;
use common\models\oldDb\UserPatient;
use common\modelsgii\oldDb\PayConfig;
use yii\db\Exception;
//挂号form
class RegisterForm extends BaseModel
{
public $store_id;
public $user_id;
public $user_patient_id;
public $service_user_id;
public $register_id;
public $register_type;
public function rules()
{
return [
[['store_id', 'user_id','register_id'], 'integer'],
[['user_patient_id', 'service_user_id', 'register_type'], 'required']
];
}
public function save()
{
if (!$this->validate()) {
throw new Exception($this->getErrorMsg());
}
$UserPatient = UserPatient::find()->where([
'user_id' => \Yii::$app->user->id,
'id' => $this->user_patient_id
])->one();
if (!$UserPatient) {
throw new Exception('就诊人不存在');
}
$today = strtotime(date('Y-m-d', time()));
$end = $today + 60 * 60 * 24;
$order_number = Register::find()->where([
'store_id' => $this->store_id,
'service_user_id' => $this->service_user_id
])->andWhere([
'between', 'created_at', $today, $end
])->select('order_number')->orderBy(['order_number' => SORT_DESC])->one();
$StoreDoctor=StoreDoctor::find()->where([
'store_id'=>\Yii::$app->store,
'su_id'=>$this->service_user_id
])->one();
if (!$StoreDoctor) {
if ($this->service_user_id != 39) {
throw new Exception('门店没有该医生');
}
}
$ServiceUser=ServiceUser::find()->where([
'id'=>$this->service_user_id,
'is_delete'=>0
])->one();
if (!$ServiceUser) throw new Exception('该医生不存在');
if ($ServiceUser->docService->register_status==0) throw new Exception('该医生没开通挂号服务');
$is_doctorPatient=DoctorPatient::find()->where([
'up_id' => $this->user_patient_id,
'su_id' => $this->service_user_id,
'user_id' => \Yii::$app->user->id,
])->one();
//添加患者
if (!$is_doctorPatient){
//医生患者
$DoctorPatient=new DoctorPatient();
$DoctorPatient->user_id=\Yii::$app->user->id;
$DoctorPatient->su_id=$this->service_user_id;
$DoctorPatient->up_id=$this->user_patient_id;
$DoctorPatient->name=$UserPatient->name;
$DoctorPatient->avatar=$UserPatient->avatar??'';
$DoctorPatient->id_card=$UserPatient->id_card;
$DoctorPatient->sex=$UserPatient->sex;
$DoctorPatient->mobile=$UserPatient->mobile;
$DoctorPatient->saveOrFail();
}
$Register = Register::find()->where([
'user_id' => \Yii::$app->user->id,
'user_patient_id' => $this->user_patient_id,
'store_id' => $this->store_id,
'service_user_id' => $this->service_user_id,
])->andWhere([
'in','status',[0,1] //未支付 待接诊
])->andWhere(['between','created_at',$today,$end])->one();
if($Register){
return [
'isHas' => 1,
'register_id' => $Register->id
];
}
// if ($Register) throw new Exception('您今天已经在该医生处挂过号,请勿重复挂号');
//获取支付方式配置
$payConfig = PayConfig::findOne(['status' => 1, 'current_use' => 1]);
$t = \Yii::$app->db->beginTransaction();
try {
$UserRegister = new Register();
$UserRegister->user_id = \Yii::$app->user->id;
$UserRegister->store_id = \Yii::$app->store;
$UserRegister->order_no = FuncHelper::generate_order_no('SN');
$UserRegister->user_patient_id = $this->user_patient_id;
$UserRegister->service_user_id = $this->service_user_id;
$UserRegister->type = $this->register_type;
$UserRegister->depart_id = $ServiceUser->docInfo->depart_id;
$UserRegister->order_number = ($order_number['order_number']??0 ) + 1;
$UserRegister->price = $ServiceUser->docService->register_price;
$UserRegister->pay_type = $payConfig->pay_type??2;//1微信 2易票联
$UserRegister->saveOrFail();
$event = new RegisterEvent();//触发订单创建事件
$event->register = $UserRegister;
\Yii::$app->trigger(Register::EVENT_CREATED, $event);
$t->commit();
} catch (\Exception $E) {
$t->rollBack();
throw new Exception($E->getMessage());
}
return ['isHas' => 0,'register_id' => $UserRegister->id];
}
public function info()
{
$ServiceUser = ServiceUser::find()->where([
'id' => $this->service_user_id,
'role' => UserRoleEnum::DOCTOR,
'status' => UserStatusEnum::OK,
'is_delete' => 0
])->one();
if (!$ServiceUser) throw new Exception('医生不存在');
\Yii::$app->response->headers->set("Content-type:text/html;charset=utf-8");
$today = strtotime(date('Y-m-d', time()));
$end = $today + 60 * 60 * 24;
$count = Register::find()->where([
'service_user_id' => $this->service_user_id
])->andWhere([
'between', 'created_at', $today, $end
])->count();
$left_number = $ServiceUser->docInfo->register_num - $count;
return [
'time' => date('Y-m-d'),
'register_price' => $ServiceUser->docService->register_price . '元/次',
'left_num' => $left_number,
'register_num' => $ServiceUser->docInfo->register_num
];
}
/**
* 取消挂号
*/
public function cancel()
{
$Register = Register::find()->where([
'id'=>$this->register_id,
'user_id' => \Yii::$app->user->id,
'is_delete' => 0,
])->one();
if (!$Register) throw new Exception('该挂号不存在');
Register::updateAll([
'is_delete' => 1,
'status'=>RegisterEnum::CANCEL
],[
'id'=>$this->register_id,
'user_id' => \Yii::$app->user->id,
]);
return ['取消成功'];
}
}