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

280 lines
10 KiB
PHP

<?php
namespace member\models\forms;
use common\core\BaseModel;
use common\models\oldDb\UserPatient;
use common\models\oldDb\UserPatientHealthInquiry;
use yii\base\Exception;
//就诊人form
class PatientForm extends BaseModel
{
public $id;
public $up_id;
public $name;
public $id_card;
public $age;
public $sex;
public $relation;
public $mobile;
public $is_default;
public $user_patient_id;
public $family_status;
public $family_history;
public $allergic_status;
public $allergic_history;
public $person_status;
public $person_history;
public $liver_function;
public $liver_index;
public $renal_function;
public $renal_index;
public function rules()
{
return [
['id', 'default', 'value' => 0],
['up_id', 'integer'],
[['name', 'id_card', 'age', 'sex', 'relation', 'mobile'], 'required'],
[['is_default'], 'default', 'value' => 0],
['mobile', 'match', 'pattern' => '/^\d{11}$/i'],
// ['id_card', 'match', 'pattern' => '/^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/i'],
// ['id_card','checkCard'],
[['family_status', 'allergic_status', 'person_status', 'liver_function', 'renal_function'], 'default', 'value' => 0],
[['family_history', 'allergic_history', 'person_history', 'liver_index', 'renal_index'], 'default', 'value' => ''],
['family_history', 'required', 'when' => function ($model) {
return $model->family_status == 1;
}],
['allergic_history', 'required', 'when' => function ($model) {
return $model->allergic_status ==1;
}],
['person_history', 'required', 'when' => function ($model) {
return $model->person_status == 1;
}],
// ['liver_index', 'required', 'when' => function ($model) {
// return $model->liver_function == 1;
// }],
// ['renal_index', 'required', 'when' => function ($model) {
// return $model->renal_function == 1;
// }],
];
}
// public function checkUnique($attribute, $params)
// {
// $exist = UserPatient::find()->where([
// 'user_id' => \Yii::$app->user->id,
// 'is_delete' => 0
// ])->andWhere(['<>', 'id', $this->id])->andWhere([
// 'or',
// ['name' => $this->name],
// ['id_card' => $this->id_card],
// ])->one();
// if ($exist) {
// $this->addError($attribute, '就诊人姓名或身份证已存在');
// }
//
// if ($this->relation == 0) {
// $exist = UserPatient::find()->where([
// 'user_id' => \Yii::$app->user->id,
// 'is_delete' => 0
// ])->andWhere(['<>', 'id', $this->id])->andWhere([
// 'relation' => $this->relation,//本人
// ])->one();
// if ($exist) {
// $this->addError($attribute, '本人的信息已存在,请选择其他关系');
// }
// }
// }
public function checkCard($attribute, $params)
{
// $response = (new Client(['http_errors' => false]))->post("https://eid.shumaidata.com/eid/check", [
// 'headers' => ['Authorization' => "APPCODE f98bba3251714c759f5bd29d00e1c46e"],
// 'query' => [
// 'idcard' => $this->id_card,
// 'name' => $this->name
// ],
// ]);
// $result = json_decode($response->getBody(), true);
// if (!($result['code'] == 0 && $result['result']['res'] == 1)) {
// $this->addError($attribute, '身份证名字不匹配');
// }
}
public function attributeLabels()
{
return [
'name' => '姓名',
'id_card' => '身份证号',
'age' => '年龄',
'sex' => '性别',
'relation' => '与本人关系',
'mobile' => '手机号',
'liver_function' => '肝功能',
'renal_function' => '肾功能',
'allergic_history' => '过敏史',
'person_history' => '个人病史',
'family_history' => '家庭病史',
];
}
public function save()
{
if (!$this->validate()) {
throw new Exception($this->getErrorMsg());
}
$user_id = \Yii::$app->user->identity->id;
//修改
if ($this->id) {
$patient = UserPatient::findOne([
'id' => $this->id,
'user_id' => \Yii::$app->user->identity->id,
'is_delete' => 0
]);
if (!$patient) {
throw new Exception('就诊人不存在');
}
} else {//新增
$patient = new UserPatient();
}
$t = \Yii::$app->db->beginTransaction();
try {
if ($this->is_default) {
UserPatient::updateAll(['is_default' => 0], ['user_id' => $user_id]);
}
$patient->user_id = \Yii::$app->user->id;
$patient->attributes = $this->attributes;
$patient->saveOrFail();
$UserPatientHealthInquiry = UserPatientHealthInquiry::find()->where([
'user_patient_id' => $patient->id,
'is_delete' => 0
])->one();
if (!$UserPatientHealthInquiry) {//新增
$UserPatientHealthInquiry = new UserPatientHealthInquiry();
}
$UserPatientHealthInquiry->user_patient_id = $patient->attributes['id'];
$UserPatientHealthInquiry->family_status = $this->family_status;
$UserPatientHealthInquiry->allergic_status = $this->allergic_status;
$UserPatientHealthInquiry->person_status = $this->person_status;
$UserPatientHealthInquiry->liver_function = $this->liver_function;
$UserPatientHealthInquiry->renal_function = $this->renal_function;
$UserPatientHealthInquiry->family_history = $this->family_history;
$UserPatientHealthInquiry->allergic_history = $this->allergic_history;
$UserPatientHealthInquiry->person_history = $this->person_history;
$UserPatientHealthInquiry->liver_index = $this->liver_index;
$UserPatientHealthInquiry->renal_index = $this->renal_index;
$UserPatientHealthInquiry->saveOrFail();
// $params = [
// 'user_id' => \Yii::$app->user->id,
// 'avatar' => '',
// 'name' => $this->name,
// 'id_card' => $this->id_card,
// 'age' => $this->age,
// 'sex' => $this->sex,
// 'relation' => $this->relation,
// 'mobile' => $this->mobile,
// 'is_default' => $this->is_default,
// 'is_delete' =>0,
// 'userPatientHealthInquiry'=>json_encode($UserPatientHealthInquiry,true),
// 'user_patient_id' => $patient->id,
// 'family_status' => $this->family_status,
// 'allergic_status' => $this->allergic_status,
// 'person_status' => $this->person_status,
// 'liver_function' => $this->liver_function,
// 'renal_function' => $this->renal_function,
// 'family_history' => $this->family_history,
// 'allergic_history' => $this->allergic_history,
// 'person_history' => $this->person_history,
// 'liver_index' => $this->liver_index,
// 'renal_index' => $this->renal_index,
// ];
// $curl = new Curl();
// $url = 'http://hy.api.ctkj88.com/platform/v1/sync/patient';
// $response = $curl->setPostParams($params)
// ->setHeaders([
// 'multipart/form-data' => 'application/json',
// 'Authorization' => 'Bearer 1234567890',
// ])->post($url);
//
// $response=json_decode($response,true);
// if ($response['errcode']==-1){
// return [$response['msg']];
// }
$t->commit();
return ['添加或编辑成功'];
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
}
public function del()
{
$user_id = \Yii::$app->user->identity->getId();
$patient = UserPatient::findOne([
'id' => $this->id,
'user_id' => $user_id,
'is_delete' => 0
]);
if (!$patient) {
throw new Exception('就诊人不存在');
}
// $curl = new Curl();
// $params = [
// 'user_id'=>$user_id,
// 'id_card'=>$patient['id_card'],
// ];
// $response = (new Client(['http_errors' => false]))->post("https://hy.api.ctkj88.com/platform/v1/sync/patient-delete", [
// 'headers' => ['Authorization' =>"Bearer 1234567890"],
// 'form_params' => [
// 'user_id'=>$user_id,
// 'id_card'=>$patient['id_card'],
// ],
// ]);
// $result = json_decode($response->getBody(),true);
// if ($result['errcode'] == -1) {
// throw new Exception($result['msg']);
// }
//删除萧康就诊人
$patient->delete();
}
//切换就诊人
public function change()
{
$patient = UserPatient::findOne([
'id' => $this->up_id,
'user_id' => \Yii::$app->user->id,
'is_delete' => 0
]);
if (!$patient) {
throw new Exception('就诊人不存在');
}else{
UserPatient::updateAll(['is_default'=>0],
['user_id' => \Yii::$app->user->id, 'is_delete' => 0]);
}
UserPatient::updateAll(['is_default'=>1],
['id' => $this->up_id,'user_id' => \Yii::$app->user->id, 'is_delete' => 0]);
return ['切换成功'];
}
}