Files
xk-api-yii/service/modules/v1/controllers/PatientController.php

591 lines
21 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace service\modules\v1\controllers;
use Carbon\Carbon;
use common\core\BaseServiceController;
use common\helpers\FuncHelper;
use common\jobs\NewSendJob;
use common\models\DoctorPatient;
use common\models\DoctorTagIll;
use common\models\Prescription;
use common\models\Register;
use common\models\User;
use common\models\UserPatient;
use common\models\UserPatientCase;
use common\models\UserPatientHealthInquiry;
use common\models\UserPatientIll;
use common\modelsgii\DoctorPatientRemark;
use common\modelsgii\PatientVisitRecord;
use common\services\ExportService;
use yii\db\Exception;
use yii\helpers\Json;
/**
* @doc-module 患者
*/
class PatientController extends BaseServiceController
{
/**
* 患者列表
* @doc-param string keyword 关键字 / optional
*/
public function actionPatientLists()
{
$keyword = \Yii::$app->request->post('keyword');
$query = DoctorPatient::find()
->with('tags')
->where(['su_id' => \Yii::$app->user->identity->getId()])
->orderBy(['id'=>SORT_DESC]);
$this->field = [
DoctorPatient::class => [
'id', 'user_id', 'su_id', 'up_id', 'name', 'sex', 'avatar',
'age' => function ($model) {
return FuncHelper::getAgeFromIdNo($model['id_card']);
},
'tags' => function ($model) {
return $model->tags;
}
]
];
if ($keyword) {
$query->andFilterWhere(['like', 'name', $keyword]);
}
return $this->create($query, \Yii::$app->request->post());
}
/**
* @doc-name 添加就诊记录
* @doc-param int up_id 就诊人id
* @doc-param string main_suit 主诉
* @doc-param string diagnose 诊断
*/
public function actionAddRecord()
{
$request = \Yii::$app->request;
$param = $request->post();
$this->requestValidate($param, [
['up_id', 'required'],
['main_suit', 'required'],
['diagnose', 'required'],
]);
\Yii::$app->db->createCommand()->insert('yii_patient_visit_record', [
'su_id' => \Yii::$app->user->identity->id,
'up_id' => $param['up_id'],
'main_suit' => $param['main_suit'],
'diagnose' => $param['diagnose'],
])->execute();
}
/**
* @doc-name 就诊记录列表
* @doc-param int up_id 就诊人id
*/
public function actionRecordList()
{
$post = \Yii::$app->request->post();
$up_id = \Yii::$app->request->post('up_id');
if (!$up_id) {
throw new Exception('就诊人id不能为空');
}
$query = PatientVisitRecord::find()
->where(['su_id' => \Yii::$app->user->identity->id])
->andWhere(['up_id' => $up_id])
->orderBy(['created_at' => SORT_DESC]);
return $this->create($query, $post);
}
/**
* @doc-name 就诊记录列表-医生端
* @doc-param int up_id 就诊人id
*/
public function actionRecordListByDoctor()
{
$post = \Yii::$app->request->post();
$up_id = \Yii::$app->request->post('up_id');
if (!$up_id) {
throw new Exception('就诊人id不能为空');
}
$Register = Register::find()->where([
'user_patient_id' => $up_id,
'store_id' => $post['store_id'],
'is_delete' => 0,
'is_cancel'=>0,
])->all();
if (!$Register) throw new Exception('暂无挂号记录');
$query = Register::find()->where([
'user_patient_id' => $up_id,
'store_id' => $post['store_id'],
'is_delete' => 0,
])->with(['store','depart','patient','doctor'])->orderBy(['id'=>SORT_DESC]);
if (!empty($post['patient_id'])) $query->andWhere(['user_patient_id'=>$post['patient_id']]);
$this->field=[
Register::class=>[
'id','user_id','order_no','service_user_id',
'store'=>'store.name',
'user_patient_id',
'depart'=>'depart.name',
'doctor'=>'doctor.name',
'order_number','price',
'patient'=>'patient.name',
'idcard'=>'patient.id_card',
'mobile'=>'patient.mobile',
'status','status_text','status_type','is_pay','created_at','is_cancel'
]
];
$list = $this->create($query,$post);
foreach ($list->models as &$model) {
$model->status_text = self::getStatusInfo($model->status, 'text');
$model->status_type = self::getStatusInfo($model->status, 'type');
}
return $list;
}
/**
* 获取挂号订单状态标签内容
*
* @param $status
* @param $type
* @return string
*/
public static function getStatusInfo($status , $type)
{
$data = [
// 0待支付1已支付待接诊2接诊中3已结束4已取消5待评价6已评价7已拒诊
'text' => [
0 => '待支付',
1 => '已支付待接诊',
2 => '接诊中',
3 => '已结束',
4 => '已取消',
5 => '待评价',
6 => '已评价',
7 => '已拒诊'
],
'type' => [
0 => 'info',
1 => 'primary',
2 => 'success',
3 => 'success',
4 => 'error',
5 => 'info',
6 => 'success',
7 => 'warning',
]
];
return $data[$type][$status];
}
/**
* @doc-name 处方记录
* @doc-param int up_id 就诊人id
* @doc-param string prescription_no 处方单号 / optional
* @doc-param string clinical_diagnose 诊断 / optional
* @doc-param string start_time 开始时间 / optional
* @doc-param string end_time 结束时间 / optional
* @doc-return mixed @List{id-int-处方id,prescription_no-int-处方编号,up_id-int-就诊人id,created_at-int-开具时间,status-int-状态0待审核1已通过2未通过3待使用4已使用5未使用6已失效7已初审,clinical_diagnose-string-临床诊断,doctor_name-string-医生,drug_name-string-药品} 处方记录
* @doc-return mixed @Pagination{total-int-总数据,totalPage-int-总页数,pageSize-int-每页数据} 分页信息
*/
public function actionPrescripRecord()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['up_id', 'required']
]);
$UserPatient = UserPatient::find()
->where([
'id' => $post['up_id']
])->one();
if (!$UserPatient) {
throw new Exception('就诊人不存在');
}
$query = Prescription::find()->alias('p')->with('userPatient')->where(['p.su_id' => \Yii::$app->user->identity->id,'p.up_id' => $post['up_id'],'p.is_deleted'=>0])->orderBy('created_at DESC');
$this->field = [
Prescription::class => [
'id','su_id','user_id','up_id', 'prescription_type','type','prescription_no','status','clinical_diagnose',
'created_at' => function ($model) {
return date('Y-m-d',$model->created_at);
},
'patient' => function ($model) {
return [
'id' => $model->userPatient['id'],
'name' =>$model->userPatient['name'],
'sex' => $model->userPatient['sex'],
'age' => FuncHelper::getAgeFromIdNo($model->userPatient['id_card']),
];
}
]
];
return $this->create($query, \Yii::$app->request->post());
}
/**
* @doc-name 基础健康信息
* @doc-param int up_id 就诊人id
* @doc-return mixed @UserPatientHealthInquiry{*} 就诊信息
*/
public function actionBaseInfo()
{
$up_id = \Yii::$app->request->post('up_id');
if (!$up_id) throw new Exception('就诊人id不能为空');
$patient = UserPatient::find()
->where(['id' => $up_id])
->asArray()->one();
if (!$patient) throw new Exception('就诊人不存在');
$UserPatientHealthInquiry = UserPatientHealthInquiry::find()->where([
'user_patient_id' => $up_id,
'is_delete' => 0
])->one();
if (!$UserPatientHealthInquiry) throw new Exception('暂无基础健康信息');
return $UserPatientHealthInquiry;
}
/**
* @doc-name 患者添加备注
* @doc-param int up_id 就诊人id
* @doc-param string remark 备注
*/
public function actionSaveRemark()
{
$up_id = \Yii::$app->request->post('up_id');
$remark = \Yii::$app->request->post('remark');
if (!$up_id) throw new Exception('就诊人id不能为空');
if (!$remark) throw new Exception('备注不能为空');
$doctorRemark = DoctorPatientRemark::find()
->where(['su_id' => \Yii::$app->user->identity->id, 'up_id' => $up_id])
->one();
if (!$doctorRemark) {
\Yii::$app->db->createCommand()->insert('yii_doctor_patient_remark', [
'su_id' => \Yii::$app->user->identity->id,
'up_id' => $up_id,
'remark' => $remark,
'created_at' => time()
])->execute();
return [];
}
throw new Exception('该患者已添加备注');
}
/**
* @doc-name 查看患者备注
* @doc-param int up_id 就诊人id
*/
public function actionPatientRemark()
{
$up_id = \Yii::$app->request->post('up_id');
if (!$up_id) throw new Exception('就诊人id不能为空');
$remark = DoctorPatientRemark::find()
->where(['su_id' => \Yii::$app->user->identity->id, 'up_id' => $up_id])
->one();
if (!$remark) throw new Exception('该就诊人还没有备注');
return $remark;
}
/**
* @doc-name 修改患者备注
* @doc-param int up_id 就诊人id
* @doc-param string remark 备注
*/
public function actionUpdatePatientRemark()
{
$up_id = \Yii::$app->request->post('up_id');
$remark = \Yii::$app->request->post('remark');
if (!$up_id) throw new Exception('患者id不能为空');
if (!$remark) throw new Exception('备注不能为空');
$doctorRemark = DoctorPatientRemark::find()
->where(['up_id' => $up_id])
->andWhere(['su_id' => \Yii::$app->user->identity->id])
->one();
if (!$doctorRemark) throw new Exception('该就诊人还没有备注,去添加备注');
\Yii::$app->db->createCommand()->update('yii_doctor_patient_remark', [
'remark' => $remark,
'updated_at' => time()
], ['up_id' => $up_id, 'su_id' => \Yii::$app->user->identity->id])->execute();
return [];
}
/**
* @doc-name 患者基本信息
* @doc-param int up_id 就诊人id
* @doc-return mixed @Patient{name-string-姓名,id_card-string-身份证,sex-int-性别0默认1男2女,mobile-string-手机号} 患者信息
* @doc-return int age 年龄
* @doc-return string remark 备注
* @doc-return array group 分组
*/
public function actionPatientInfo()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['up_id', 'required']
]);
$patient = UserPatient::find()
->select('user_id,name,sex,id_card,mobile')
->where(['id' => $post['up_id']])
->one();
if (!$patient) return ['就诊人id不存在'];
$user = User::find()
->select('avatarurl')
->where(['id' => $patient['user_id']])
->one();
$ti_ids = UserPatientIll::find()
->where([
'su_id' => \Yii::$app->user->identity->id,
'up_id' => $post['up_id']
])
->select('ti_id')
->column();
$remark = \common\models\DoctorPatientRemark::find()
->where([
'su_id' => \Yii::$app->user->identity->id,
'up_id' => $post['up_id']
])
->select('remark')->one();
if (!$ti_ids) {
return [
'patient' => $patient,
'avatar' => $user['avatarurl'],
'age' => FuncHelper::getAgeFromIdNo($patient->id_card),
'remark' => $remark,
'group' => '该就诊人还没有分组'
];
}
$group = DoctorTagIll::find()
->where([
'in', 'id', $ti_ids
])->andWhere([
'su_id' => \Yii::$app->user->identity->id,
])
->select('name')->all();
return [
'patient' => $patient,
'avatar' => $user['avatarurl'],
'age' => FuncHelper::getAgeFromIdNo($patient->id_card),
'remark' => $remark,
'group' => $group
];
}
/**
* @doc-name 患者列表
* @doc-param int status 1到店接诊患者2线上接诊患者
* @doc-param string name 搜索患者姓名 / optional
* @doc-param int sex 搜索患者性别0默认1男2女 / optional
* @doc-return mixed @Register{id-int-挂号id,patient_id-int-患者ID,,patient-string-患者,avatar-string-头像,price-float-挂号金额,pay_time-string-支付时间,age-int-年龄,sex-int-性别0默认1男2女,recent_accept-string-最近接诊时间} 患者信息
* @doc-return mixed @Pagination{total-int-总数据,totalPage-int-总页数,pageSize-int-每页数据} 分页信息
*/
public function actionPatientList()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['status', 'required']
]);
switch ($post['status']) {
case 1:
$query = Register::find()->alias('r')->where([
'r.service_user_id' => \Yii::$app->user->id,
'r.store_id' => \Yii::$app->store,
'r.is_delete' => 0,
'r.is_pay' => 1,
'r.is_cancel' => 0,
])->groupBy(['r.user_patient_id'])->orderBy(['created_at' => SORT_DESC])
->with(['patient']);
break;
case 2:
throw new Exception('暂时没写');
break;
default:
throw new Exception('参数错误');
}
if (!empty($post['name'])) {
$name = $post['name'];
$query->joinWith(['patient' => function ($model) use ($name) {
$model->alias('p');
$model->andWhere(['like', 'p.name', $name]);
}]);
}
if (!empty($post['sex'])) {
$sex = $post['sex'];
$query->joinWith(['patient' => function ($model) use ($sex) {
$model->alias('p');
$model->andWhere(['p.sex' => $sex]);
}]);
}
$this->field = [
Register::class => [
'id',
'patient_id'=>'patient.id',
'patient' => 'patient.name',
'avatar' => 'patient.avatar',
'price', 'pay_time',
'age' => function ($m) {
return FuncHelper::getAgeFromIdNo($m->patient->id_card);
},
'sex' => 'patient.sex',
'recent_accept' => 'created_at'
],
];
return $this->create($query, $post);
}
/**
* @doc-name 导出患者
*/
public function actionPatientExport()
{
$headlist = ['姓名', '身份证', '性别', '电话', '肝功能状态', '肝功能指标', '肾功能状态', '肾功能指标', '既往史有无', '既往史', '过敏史有无', '过敏史', '家庭遗传史有无', '家庭遗传史'];
$Name = '导出患者';
$class = new DoctorPatient();
return (new ExportService())->export($headlist, $Name, $class);
}
/**
* @doc-name 挂号记录
* @doc-param int patient_id 患者
* @doc-param int id 患者ID / optional
* @doc-param string order_no 挂号单号 / optional
* @doc-param string patient 患者姓名 / optional
* @doc-param string start_time 起始时间 / optional
* @doc-param string end_time 终止时间 / optional
* @doc-return mixed @List{id-int-挂号id,is_case-string-是否有病历0无1有,created_at-string-挂号时间,order_no-string-挂号单号,doctor-string-医生,patient-string-患者,main_suit-string-主诉,family-string-家族史,now_history-string-现病史,popular-string-流行病史,history-string-既往史,allergic-string-过敏史,idea-string-意见,price-string-挂号费,order_number-string-挂号序号,is_pay-int-是否支付0否1是,status-int-状态:1已支付待接诊2接诊中3已结束4已取消5待评价6已评价7已拒诊,refuse_reason-string-拒诊原因,pay_type-int-支付方式1=微信支付,pay_time-string-支付时间=微信支付,is_cancel-int-是否取消,cancel_status-int-1已取消,cancel_time-string-取消时间,user_patient_id-int-患者ID,service_user_id-int-医生IDuser_id-int-医生ID} 挂号记录
* @doc-return mixed @Pagination{total-int-总数据,totalPage-int-总页数,pageSize-int-每页多少条} 分页信息
*/
public function actionRegisterList()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['patient_id', 'required']
]);
$query = Register::find()->alias('r')->where([
'r.user_patient_id' => $post['patient_id'],
'r.service_user_id' => \Yii::$app->user->id,
'r.is_pay' => 1,
'r.is_cancel' => 0,
'r.is_delete' => 0,
])->andWhere([
'like', 'order_no', $post['order_no'] ?? '',
])->with(['case', 'doctor'])->orderBy(['id'=>SORT_DESC]);
if (!empty($post['order_no'])) {
$query->andWhere(['order_no' => $post['order_no']]);
}
if (!empty($post['id'])) {
$id = $post['id'];
$query->joinWith(['patient' => function ($p) use ($id) {
$p->alias('p');
$p->andWhere(['p.id' => $id]);
}]);
}
if (!empty($post['patient'])) {
$name = $post['patient'];
$query->joinWith(['patient' => function ($p) use ($name) {
$p->alias('p');
$p->andWhere(['like', 'p.name', $name]);
}]);
}
if (!empty($post['start_time']) && !empty($post['end_time'])) {
$query->andWhere(['between', 'r.created_at', strtotime($post['start_time']), strtotime($post['end_time'])]);
}
$this->field = [
Register::class => [
'id', 'created_at'=>function($m){
return date('Y-m-d H:i:s',$m->created_at);
},
'is_case'=>function($m){
$UserPatientCase= UserPatientCase::find()->where(['register_id'=>$m->id])->one();
if ($UserPatientCase){
return 1;
}
return 0;
},
'doctor' => 'doctor.name',
'patient' => 'patient.name',
'main_suit' => 'case.main_suit',
'family' => 'case.main_suit',
'now_history' => 'case.now_history',
'popular' => 'case.popular',
'history' => 'case.history',
'allergic' => 'case.allergic',
'idea' => 'case.idea',
'case_created_at' => 'case.created_at',
'price','order_no','order_number','is_pay','status','refuse_reason','pay_type',
'pay_time'=>function($model){
return date('Y-m-d H:i:s',$model->pay_time);
},
'is_cancel','cancel_status','cancel_time'=>function($mo){
return date('Y-m-d H:i:s',$mo->cancel_time);
},
'user_patient_id','store_id','service_user_id','user_id'
]
];
return $this->create($query, $post);
}
/**
* @doc-name 医生群发消息
* @doc-param string content 发送内容
* @doc-param string send_at 发送时间-年月日时分秒 / optional
* @doc-param array up_ids 患者ID数组
*/
public function actionSendNews()
{
$post=\Yii::$app->request->post();
\Yii::info('群发消息请求内容:'.Json::encode($post));
$this->requestValidate($post,[
[['content','up_ids'],'required']
]);
$content = \Yii::$app->request->post('content');
$doctor_patient_ids =explode(',', \Yii::$app->request->post('up_ids'));
$send_at = \Yii::$app->request->post('send_at',0);
$su_id = \Yii::$app->user->identity->id;
\Yii::$app->queue->delay($send_at ? Carbon::now()->diffInSeconds($send_at) : 0)->push(new NewSendJob([
'content' => $content,
'su_id' => $su_id,
'doctor_patient_ids' => $doctor_patient_ids,
'store_id'=>$post['store_id']
]));
return ['群发消息操作成功'];
}
}