233 lines
8.5 KiB
PHP
233 lines
8.5 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\Store;
|
||
use common\models\oldDb\StoreDoctor;
|
||
use common\models\oldDb\UserPatient;
|
||
use common\modelsgii\oldDb\PayConfig;
|
||
use common\services\SalespersonOrderBindService;
|
||
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 $origin_store_id;
|
||
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['store_id', 'user_id', 'register_id', 'origin_store_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 {
|
||
// 确定实际使用的门店ID(履约诊所)
|
||
// 在线场景优先使用前端传入的 store_id(如委托诊所);药店复诊再按配置覆盖为委托诊所
|
||
$actualStoreId = (int) ($this->store_id ?: \Yii::$app->store);
|
||
if ($this->register_type == 3) {
|
||
$currentStore = Store::findOne(\Yii::$app->store);
|
||
if ($currentStore && !empty($currentStore->delegate_store_id)) {
|
||
$actualStoreId = (int) $currentStore->delegate_store_id;
|
||
}
|
||
} elseif ($this->register_type == 2) {
|
||
// 诊所在线复诊:发起方诊所校验(会话门店须为诊所)
|
||
$currentStore = Store::findOne(\Yii::$app->store);
|
||
if ($currentStore && $currentStore->type != 0) {
|
||
throw new Exception('诊所在线复诊只能在诊所进行');
|
||
}
|
||
}
|
||
|
||
$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;
|
||
}
|
||
|
||
$UserRegister = new Register();
|
||
$UserRegister->user_id = \Yii::$app->user->id;
|
||
$UserRegister->store_id = $actualStoreId;
|
||
if ($UserRegister->hasAttribute('origin_store_id')) {
|
||
$UserRegister->origin_store_id = $originStoreId;
|
||
}
|
||
$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易票联
|
||
if ($UserRegister->hasAttribute('salesperson_id')) {
|
||
$bindResult = (new SalespersonOrderBindService())->resolveForOrder(
|
||
(int) \Yii::$app->user->id,
|
||
$actualStoreId,
|
||
time()
|
||
);
|
||
$UserRegister->salesperson_id = (int) ($bindResult['salesperson_id'] ?? 0);
|
||
}
|
||
$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 ['取消成功'];
|
||
}
|
||
|
||
} |