Files
xk-api-yii/member/models/forms/RegisterForm.php

234 lines
8.6 KiB
PHP
Raw Normal View History

2024-09-19 11:32:39 +08:00
<?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;
2024-12-19 19:20:27 +08:00
use common\models\oldDb\DoctorPatient;
use common\models\oldDb\Register;
use common\models\oldDb\ServiceUser;
use common\models\oldDb\Store;
2024-12-19 19:20:27 +08:00
use common\models\oldDb\StoreDoctor;
use common\models\oldDb\UserPatient;
use common\modelsgii\oldDb\PayConfig;
2026-06-15 14:01:00 +08:00
use common\services\SalespersonOrderBindService;
2024-09-19 11:32:39 +08:00
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;
2026-05-22 10:24:28 +08:00
public $origin_store_id;
2024-09-19 11:32:39 +08:00
public function rules()
{
return [
2026-05-22 10:24:28 +08:00
[['store_id', 'user_id', 'register_id', 'origin_store_id'], 'integer'],
[['user_patient_id', 'service_user_id', 'register_type'], 'required']
2024-09-19 11:32:39 +08:00
];
}
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();
2025-10-22 16:49:14 +08:00
if (!$StoreDoctor) {
2025-10-22 16:49:59 +08:00
if ($this->service_user_id != 39) {
2025-10-22 16:49:14 +08:00
throw new Exception('门店没有该医生');
}
}
2024-09-19 11:32:39 +08:00
$ServiceUser=ServiceUser::find()->where([
'id'=>$this->service_user_id,
'is_delete'=>0
])->one();
2025-10-22 16:49:14 +08:00
if (!$ServiceUser) throw new Exception('该医生不存在');
2024-09-19 11:32:39 +08:00
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 {
2026-05-22 10:24:28 +08:00
// 确定实际使用的门店ID履约诊所
// 在线场景:有委托诊所配置时服务端强制写成委托诊所;发起方写入 origin_store_id
2026-05-22 10:24:28 +08:00
$actualStoreId = (int) ($this->store_id ?: \Yii::$app->store);
if (in_array((int) $this->register_type, [2, 3], true)) {
2026-05-22 10:24:28 +08:00
$currentStore = Store::findOne(\Yii::$app->store);
if ($this->register_type == 2) {
// 诊所在线复诊:发起方诊所校验(会话门店须为诊所)
if ($currentStore && $currentStore->type != 0) {
throw new Exception('诊所在线复诊只能在诊所进行');
}
}
// type=2/3配置了委托诊所则履约店强制为委托诊所不单靠前端传参
if ($currentStore && !empty($currentStore->delegate_store_id)) {
2026-05-22 10:24:28 +08:00
$actualStoreId = (int) $currentStore->delegate_store_id;
}
}
2026-05-22 10:24:28 +08:00
$originStoreId = (int) ($this->origin_store_id ?: \Yii::$app->store);
$shouldPersistOrigin = in_array((int) $this->register_type, [2, 3], true)
|| ((int) $this->register_type === 1 && (int) $this->origin_store_id > 0);
if (!$shouldPersistOrigin) {
$originStoreId = 0;
}
2024-09-19 11:32:39 +08:00
$UserRegister = new Register();
$UserRegister->user_id = \Yii::$app->user->id;
$UserRegister->store_id = $actualStoreId;
2026-05-22 10:24:28 +08:00
if ($UserRegister->hasAttribute('origin_store_id')) {
$UserRegister->origin_store_id = $originStoreId;
}
2024-09-19 11:32:39 +08:00
$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;
2024-09-19 11:32:39 +08:00
$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易票联
2026-06-15 16:20:02 +08:00
if ($UserRegister->hasAttribute('salesperson_id')) {
$bindResult = (new SalespersonOrderBindService())->resolveForOrder(
(int) \Yii::$app->user->id,
$actualStoreId,
time()
);
$UserRegister->salesperson_id = (int) ($bindResult['salesperson_id'] ?? 0);
}
2024-09-19 11:32:39 +08:00
$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 ['取消成功'];
}
}