470 lines
18 KiB
PHP
470 lines
18 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Amqp\Producer\CancelUnpayOrdersDelayDirectProducer;
|
||
use App\Constants\HttpEnumCode;
|
||
use App\Model\Area;
|
||
use App\Model\BasicCompany;
|
||
use App\Model\BasicNation;
|
||
use App\Model\DetectionProject;
|
||
use App\Model\DetectionProjectPurpose;
|
||
use App\Model\DiseaseClassDetection;
|
||
use App\Model\OrderDetection;
|
||
use App\Model\OrderDetectionCase;
|
||
use App\Model\PatientFamily;
|
||
use App\Model\UserDoctor;
|
||
use App\Model\UserLocation;
|
||
use App\Utils\Log;
|
||
use Hyperf\Amqp\Producer;
|
||
use Hyperf\DbConnection\Db;
|
||
use Hyperf\Snowflake\IdGeneratorInterface;
|
||
use Psr\Container\ContainerExceptionInterface;
|
||
use Psr\Container\NotFoundExceptionInterface;
|
||
|
||
class DetectionService extends BaseService
|
||
{
|
||
/**
|
||
* 获取合作公司检测项目列表
|
||
* @return array
|
||
*/
|
||
public function getDetectionProjectList(): array
|
||
{
|
||
$company_id = $this->request->input("company_id",1);
|
||
|
||
// 获取合作公司数据
|
||
$params = array();
|
||
$params['company_id'] = $company_id;
|
||
$basic_company = BasicCompany::getOne($params);
|
||
if (empty($basic_company)){
|
||
return fail();
|
||
}
|
||
|
||
// 获取项目数据
|
||
$params = array();
|
||
$params['company_id'] = $company_id;
|
||
$detection_projects = DetectionProject::getList($params);
|
||
if (empty($detection_projects)){
|
||
return fail();
|
||
}
|
||
|
||
foreach ($detection_projects as &$value){
|
||
$value['img_path'] = addAliyunOssWebsite($value['img_path']);
|
||
}
|
||
|
||
return success($detection_projects->toArray());
|
||
}
|
||
|
||
/**
|
||
* 获取合作公司检测项目详情
|
||
* @return array
|
||
*/
|
||
public function getDetectionProject(): array
|
||
{
|
||
$company_id = $this->request->input("company_id",1);
|
||
$detection_project_id = $this->request->route('detection_project_id',1);
|
||
|
||
// 获取合作公司数据
|
||
$params = array();
|
||
$params['company_id'] = $company_id;
|
||
$basic_company = BasicCompany::getOne($params);
|
||
if (empty($basic_company)){
|
||
return fail();
|
||
}
|
||
|
||
// 获取项目数据
|
||
$params = array();
|
||
$params['detection_project_id'] = $detection_project_id;
|
||
$detection_project = DetectionProject::getOne($params);
|
||
if (empty($detection_project)){
|
||
return fail();
|
||
}
|
||
|
||
$detection_project['img_path'] = addAliyunOssWebsite($detection_project['img_path']);
|
||
|
||
return success($detection_project->toArray());
|
||
}
|
||
|
||
/**
|
||
* 获取检测机构合作医生列表
|
||
* @return array
|
||
*/
|
||
public function getDetectionDoctorList(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
$company_id = $this->request->input("company_id",1);
|
||
$province_id = $this->request->input("province_id");
|
||
$city_id = $this->request->input("city_id");
|
||
$county_id = $this->request->input("county_id");
|
||
|
||
|
||
// 获取合作公司数据
|
||
$params = array();
|
||
$params['company_id'] = $company_id;
|
||
$basic_company = BasicCompany::getOne($params);
|
||
if (empty($basic_company)){
|
||
return fail();
|
||
}
|
||
|
||
// 返回数据
|
||
$response_data = array(
|
||
"area" => [
|
||
"province_id" => "",
|
||
"province" => "",
|
||
"city_id" => "",
|
||
"city" => "",
|
||
"county_id" => "",
|
||
"county" => "",
|
||
],
|
||
"doctors" => [],
|
||
);
|
||
|
||
// 搜索数据
|
||
$hospital_params = array();
|
||
|
||
if (empty($province_id) && empty($city_id) && empty($county_id)){
|
||
// 获取用户定位地址数据
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_location = UserLocation::getOne($params);
|
||
if (!empty($user_location)){
|
||
// 处理省市区对应
|
||
if (!empty($user_location['province']) && !empty($user_location['city'])){
|
||
$params = array();
|
||
$params['area_name'] = $user_location['province'];
|
||
$params['area_type'] = 2;
|
||
$area_province = Area::getOne($params);
|
||
if (!empty($area_province)){
|
||
$response_data['area']['province_id'] = $area_province['area_id'];
|
||
$response_data['area']['province'] = $area_province['area_name'];
|
||
|
||
// 搜索条件
|
||
$hospital_params['province_id'] = $area_province['area_id'];
|
||
}
|
||
|
||
if (!empty($response_data['area']['province_id'])){
|
||
$params = array();
|
||
$params['area_name'] = $user_location['city'];
|
||
$params['parent_id'] = $response_data['area']['province_id'];
|
||
$params['area_type'] = 3;
|
||
$area_city = Area::getOne($params);
|
||
if (!empty($area_city)){
|
||
$response_data['area']['city_id'] = $area_city['area_id'];
|
||
$response_data['area']['city'] = $area_city['area_name'];
|
||
|
||
// 搜索条件
|
||
$hospital_params['city_id'] = $area_city['area_id'];
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!empty($response_data['area']['city_id']) && !empty($user_location['county'])){
|
||
$params = array();
|
||
$params['area_name'] = $user_location['county'];
|
||
$params['parent_id'] = $response_data['area']['city_id'];
|
||
$params['area_type'] = 4;
|
||
$area_county = Area::getOne($params);
|
||
if (!empty($area_county)){
|
||
$response_data['area']['county_id'] = $area_county['area_id'];
|
||
$response_data['area']['county'] = $area_county['area_name'];
|
||
|
||
// 搜索条件
|
||
$hospital_params['county_id'] = $area_county['area_id'];
|
||
}
|
||
}
|
||
}
|
||
} else{
|
||
if (!empty($province_id)){
|
||
$params = array();
|
||
$params['area_id'] = $province_id;
|
||
$params['area_type'] = 2;
|
||
$area_province = Area::getOne($params);
|
||
if (!empty($area_province)){
|
||
$response_data['area']['province_id'] = $area_province['area_id'];
|
||
$response_data['area']['province'] = $area_province['area_name'];
|
||
|
||
// 搜索条件
|
||
$hospital_params['province_id'] = $area_province['area_id'];
|
||
}
|
||
}
|
||
|
||
if (!empty($response_data['area']['province_id']) && !empty($city_id)){
|
||
$params = array();
|
||
$params['area_id'] = $city_id;
|
||
$params['parent_id'] = $response_data['area']['province_id'];
|
||
$params['area_type'] = 3;
|
||
$area_city = Area::getOne($params);
|
||
if (!empty($area_city)){
|
||
$response_data['area']['city_id'] = $area_city['area_id'];
|
||
$response_data['area']['city'] = $area_city['area_name'];
|
||
|
||
// 搜索条件
|
||
$hospital_params['city_id'] = $area_city['area_id'];
|
||
}
|
||
}
|
||
|
||
if (!empty($response_data['area']['city_id']) && !empty($county_id)){
|
||
$params = array();
|
||
$params['area_id'] = $county_id;
|
||
$params['parent_id'] = $response_data['area']['city_id'];
|
||
$params['area_type'] = 4;
|
||
$area_county = Area::getOne($params);
|
||
if (!empty($area_county)){
|
||
$response_data['area']['county_id'] = $area_county['area_id'];
|
||
$response_data['area']['county'] = $area_county['area_name'];
|
||
|
||
// 搜索条件
|
||
$hospital_params['county_id'] = $area_county['area_id'];
|
||
}
|
||
}
|
||
}
|
||
|
||
// 获取医生
|
||
$params = array();
|
||
$params['status'] = 1;
|
||
$params['idcard_status'] = 1;
|
||
$params['iden_auth_status'] = 1;
|
||
$params['is_sys_diagno_cooperation'] = 1;
|
||
|
||
$fields = [
|
||
"doctor_id",
|
||
"user_id",
|
||
"user_name",
|
||
"avatar",
|
||
"doctor_title",
|
||
];
|
||
$user_doctors = UserDoctor::getDiagnoCoopDoctorList($params,$hospital_params,$fields);
|
||
if (!empty($user_doctors)){
|
||
foreach ($user_doctors as &$value){
|
||
$doctor = array();
|
||
if (!empty($value['Hospital'])){
|
||
$doctor['hospital_name'] = $value['Hospital']['hospital_name'];
|
||
}
|
||
|
||
unset($value['Hospital']);
|
||
|
||
$value['avatar'] = addAliyunOssWebsite($value['avatar']);
|
||
}
|
||
|
||
$response_data['doctors'] = $user_doctors->toArray();
|
||
}
|
||
|
||
return success($response_data);
|
||
}
|
||
|
||
/**
|
||
* 获取检测项目用途列表
|
||
* @return array
|
||
*/
|
||
public function getDetectionProjectPurposeList(): array
|
||
{
|
||
$detection_project_id = $this->request->input("detection_project_id",1);
|
||
|
||
// 获取项目数据
|
||
$params = array();
|
||
$params['detection_project_id'] = $detection_project_id;
|
||
$detection_project_purpose = DetectionProjectPurpose::getList($params);
|
||
if (empty($detection_project_purpose)){
|
||
return fail();
|
||
}
|
||
|
||
return success($detection_project_purpose->toArray());
|
||
}
|
||
|
||
/**
|
||
* 创建检测订单
|
||
* @return array
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
public function addDetectionOrder(): array
|
||
{
|
||
$result = array();
|
||
$result['status'] = 1;
|
||
$result['message'] = "成功";
|
||
$result['data'] = [
|
||
"inquiry_no" => "",
|
||
"order_detection_id" => "",
|
||
];
|
||
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
$request_params = $this->request->all();
|
||
|
||
// 检测家庭成员是否存在
|
||
$params = array();
|
||
$params['family_id'] = $request_params['family_id'];
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['status'] = 1;
|
||
$patient_family = PatientFamily::getOne($params);
|
||
if (empty($patient_family)) {
|
||
return fail(HttpEnumCode::HTTP_ERROR, "患者信息错误");
|
||
}
|
||
|
||
// 检测是否存在同类型未完成的检测订单
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['family_id'] = $request_params['family_id'];
|
||
$params['detection_project_id'] = $request_params['detection_project_id'];
|
||
$order_detection = OrderDetection::getNotFinishedOrderDetectionOne($params);
|
||
if (!empty($order_detection)){
|
||
$result['status'] = 2;
|
||
$result['message'] = "当前患者存在未完成的检测订单";
|
||
$result['data']['order_detection_id'] = $order_detection['order_detection_id'];
|
||
return success($result);
|
||
}
|
||
|
||
// 检测民族
|
||
$params = array();
|
||
$params['nation_id'] = $request_params['nation_id'];
|
||
$nation = BasicNation::getOne($params);
|
||
if (empty($nation)){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"民族选择错误");
|
||
}
|
||
|
||
// 检测疾病分类
|
||
$detection_disease_class_ids = explode(',',$request_params['detection_disease_class_ids']);
|
||
if (count($detection_disease_class_ids) > 3){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"既往病史最多可选三项");
|
||
}
|
||
|
||
$detection_disease_class_names = "";
|
||
|
||
foreach ($detection_disease_class_ids as $value){
|
||
$params = array();
|
||
$params['id'] = $value;
|
||
$disease_class_detection = DiseaseClassDetection::getOne($params);
|
||
if (empty($disease_class_detection)){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"既往病史错误");
|
||
}
|
||
|
||
if ($disease_class_detection['status'] != 1){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"既往病史错误");
|
||
}
|
||
|
||
if ($disease_class_detection['enable'] != 1){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"既往病史错误");
|
||
}
|
||
|
||
if (empty($detection_disease_class_names)){
|
||
$detection_disease_class_names = $disease_class_detection['name'];
|
||
}else{
|
||
$detection_disease_class_names = $detection_disease_class_names . ',' . $disease_class_detection['name'];
|
||
}
|
||
}
|
||
|
||
// 检测项目
|
||
$params = array();
|
||
$params['detection_project_id'] = $request_params['detection_project_id'];
|
||
$detection_project = DetectionProject::getOne($params);
|
||
if (empty($detection_project)){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"检测项目错误");
|
||
}
|
||
|
||
if ($detection_project['detection_project_price'] <= 0){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单金额错误");
|
||
}
|
||
|
||
// 检测用途
|
||
$params = array();
|
||
$params['purpose_id'] = $request_params['purpose_id'];
|
||
$params['detection_project_id'] = $request_params['detection_project_id'];
|
||
$detection_project_purpose = DetectionProjectPurpose::getOne($params);
|
||
if (empty($detection_project_purpose)){
|
||
return fail();
|
||
}
|
||
|
||
// 检测医生
|
||
$params = array();
|
||
$params['doctor_id'] = $request_params['doctor_id'];
|
||
$user_doctor = UserDoctor::getOne($params);
|
||
if (empty($user_doctor)){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"医生错误");
|
||
}
|
||
|
||
if ($user_doctor['is_sys_diagno_cooperation'] != 1){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"医生错误");
|
||
}
|
||
|
||
// 确定支付渠道
|
||
// 支付渠道(1:小程序支付 2:微信扫码支付)
|
||
if ($request_params['client_type'] == 1) {
|
||
$detection_pay_channel = 1;
|
||
} elseif ($request_params['client_type'] == 2) {
|
||
$detection_pay_channel = 2;
|
||
}
|
||
|
||
// 处理订单金额
|
||
|
||
|
||
Db::beginTransaction();
|
||
|
||
$generator = $this->container->get(IdGeneratorInterface::class);
|
||
|
||
try {
|
||
// 新增检测订单
|
||
$data = array();
|
||
$data['user_id'] = $user_info['user_id'];
|
||
$data['patient_id'] = $user_info['client_user_id'];
|
||
$data['doctor_id'] = $user_doctor['doctor_id'];
|
||
$data['family_id'] = $patient_family['family_id'];
|
||
$data['detection_project_id'] = $detection_project['detection_project_id'];
|
||
$data['purpose_id'] = $detection_project_purpose['purpose_id'];
|
||
$data['detection_status'] = 1; // 检测订单状态(1:待支付 2:待绑定 3:检测中 4:检测完成 5:已取消)
|
||
$data['detection_pay_channel'] = $detection_pay_channel; // 支付渠道(1:小程序支付 2:微信扫码支付 3:模拟支付)
|
||
$data['detection_no'] = "D" . $generator->generate(); // 系统订单编号
|
||
$data['amount_total'] = $detection_project['detection_project_price']; // 订单金额
|
||
$data['patient_name'] = $patient_family['card_name'];// 患者姓名-就诊人
|
||
$data['patient_name_mask'] = $patient_family['card_name_mask'];// 患者姓名-就诊人(掩码)
|
||
$data['patient_sex'] = $patient_family['sex'];// 患者性别-就诊人(0:未知 1:男 2:女)
|
||
$data['patient_age'] = $patient_family['age'];// 患者年龄-就诊人
|
||
$order_detection = OrderDetection::addOrderDetection($data);
|
||
if (empty($order_detection)){
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::SERVER_ERROR, "订单创建失败");
|
||
}
|
||
|
||
// 新增检测订单病例
|
||
$data = array();
|
||
$data['order_detection_id'] = $order_detection['order_detection_id'];
|
||
$data['family_id'] = $patient_family['family_id'];
|
||
$data['doctor_id'] = $user_doctor['doctor_id'];
|
||
$data['relation'] = $patient_family['relation']; // 与患者关系(1:本人 2:父母 3:爱人 4:子女 5:亲戚 6:其他 )
|
||
$data['name'] = $patient_family['card_name']; // 患者名称
|
||
$data['sex'] = $patient_family['sex'] ?? 0; // 患者性别(0:未知 1:男 2:女)
|
||
$data['age'] = $patient_family['age'] ?? null; // 患者年龄
|
||
$data['nation_id'] = $nation['nation_id'] ?? null; // 民族
|
||
$data['nation_name'] = $nation['nation_name'] ?? null; // 民族名称
|
||
$data['detection_disease_class_ids'] = $request_params['detection_disease_class_ids']; // 疾病id-检测-逗号分隔
|
||
$data['detection_disease_class_names'] = $detection_disease_class_names; // 疾病名称-检测-逗号分隔
|
||
$order_detection_case = OrderDetectionCase::addOrderDetectionCase($data);
|
||
if (empty($order_detection_case)){
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::SERVER_ERROR, "订单创建失败");
|
||
}
|
||
|
||
// 增加至未支付取消订单延迟队列
|
||
// $data = array();
|
||
// $data['order_no'] = $order_detection['detection_no'];
|
||
// $data['order_type'] = 3;
|
||
//
|
||
// $message = new CancelUnpayOrdersDelayDirectProducer($data);
|
||
// $message->setDelayMs(1000 * 60 * 30);
|
||
// $producer = $this->container->get(Producer::class);
|
||
// $res = $producer->produce($message);
|
||
// if (!$res) {
|
||
// Db::rollBack();
|
||
// return fail(HttpEnumCode::SERVER_ERROR, "订单创建失败");
|
||
// }
|
||
|
||
Db::commit();
|
||
}catch (\Exception $e){
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage());
|
||
}
|
||
|
||
$result['status'] = 1;
|
||
$result['data']['order_detection_id'] = (string)$order_detection['order_detection_id']; // 订单主键id
|
||
$result['data']['inquiry_no'] = (string)$order_detection['detection_no']; // 订单编号
|
||
return success($result);
|
||
}
|
||
} |