1042 lines
43 KiB
PHP
1042 lines
43 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Amqp\Producer\AutoCompleteInquiryDelayDirectProducer;
|
||
use App\Amqp\Producer\CancelUnpayOrdersDelayDirectProducer;
|
||
use App\Constants\HttpEnumCode;
|
||
use App\Exception\BusinessException;
|
||
use App\Model\Area;
|
||
use App\Model\BasicCompany;
|
||
use App\Model\BasicDetectionOrgan;
|
||
use App\Model\BasicNation;
|
||
use App\Model\DetectionProject;
|
||
use App\Model\DetectionProjectPurpose;
|
||
use App\Model\DiseaseClassDetection;
|
||
use App\Model\Order;
|
||
use App\Model\OrderDetection;
|
||
use App\Model\OrderDetectionCase;
|
||
use App\Model\OrderDetectionRefund;
|
||
use App\Model\OrderInquiry;
|
||
use App\Model\OrderInquiryCase;
|
||
use App\Model\PatientFamily;
|
||
use App\Model\UserDoctor;
|
||
use App\Model\UserLocation;
|
||
use App\Utils\Log;
|
||
use App\Utils\PcreMatch;
|
||
use Extend\Detection\Wy;
|
||
use Extend\Wechat\WechatPay;
|
||
use Hyperf\Amqp\Producer;
|
||
use Hyperf\Amqp\Result;
|
||
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();
|
||
}
|
||
|
||
// 搜索数据
|
||
$hospital_params = array();
|
||
|
||
if (!empty($province_id)) {
|
||
$params = array();
|
||
$params['area_id'] = $province_id;
|
||
$params['area_type'] = 2;
|
||
$area_province = Area::getOne($params);
|
||
if (!empty($area_province)) {
|
||
// 搜索条件
|
||
$hospital_params['province_id'] = $area_province['area_id'];
|
||
}
|
||
}
|
||
|
||
if (!empty($city_id)) {
|
||
$params = array();
|
||
$params['area_id'] = $city_id;
|
||
$params['parent_id'] = $province_id;
|
||
$params['area_type'] = 3;
|
||
$area_city = Area::getOne($params);
|
||
if (!empty($area_city)) {
|
||
// 搜索条件
|
||
$hospital_params['city_id'] = $area_city['area_id'];
|
||
}
|
||
}
|
||
|
||
if (!empty($county_id)) {
|
||
$params = array();
|
||
$params['area_id'] = $county_id;
|
||
$params['parent_id'] = $city_id;
|
||
$params['area_type'] = 4;
|
||
$area_county = Area::getOne($params);
|
||
if (!empty($area_county)) {
|
||
// 搜索条件
|
||
$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",
|
||
"hospital_id",
|
||
];
|
||
$user_doctors = UserDoctor::getDiagnoCoopDoctorList($params, $hospital_params, $fields);
|
||
if (!empty($user_doctors)) {
|
||
foreach ($user_doctors as &$value) {
|
||
if (!empty($value['Hospital'])) {
|
||
$value['hospital_name'] = $value['Hospital']['hospital_name'];
|
||
}
|
||
|
||
unset($value['Hospital']);
|
||
|
||
$value['avatar'] = addAliyunOssWebsite($value['avatar']);
|
||
}
|
||
}
|
||
|
||
return success($user_doctors->toArray());
|
||
}
|
||
|
||
/**
|
||
* 获取检测项目用途列表
|
||
* @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'] = [
|
||
"order_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_no'] = $order_detection['detection_no'];
|
||
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;
|
||
} else {
|
||
return fail(HttpEnumCode::HTTP_ERROR, "请选择正确的支付渠道");
|
||
}
|
||
|
||
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['order_type'] = 3; // 订单类型(1:问诊订单 2:药品订单 3:检测订单 4:随访包订单 5:健康包订单)
|
||
$data['inquiry_pay_channel'] = $detection_pay_channel;// 支付渠道(1:小程序支付 2:微信扫码支付)
|
||
$data['pay_status'] = 1;// 1:待支付
|
||
$data['order_no'] = "D" . $generator->generate();// 订单编号
|
||
$data['amount_total'] = $detection_project['detection_project_price'];// 订单金额
|
||
$data['coupon_amount_total'] = 0;// 优惠卷总金额
|
||
$data['payment_amount_total'] = $detection_project['detection_project_price'];// 实际付款金额
|
||
$order = Order::addOrder($data);
|
||
if (empty($order)) {
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::SERVER_ERROR, "订单创建失败");
|
||
}
|
||
|
||
// 新增检测订单
|
||
$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'] = $order['order_no']; // 系统订单编号
|
||
$data['amount_total'] = $detection_project['detection_project_price']; // 订单金额
|
||
$data['coupon_amount_total'] = 0;// 优惠卷总金额
|
||
$data['payment_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);
|
||
}
|
||
|
||
/**
|
||
* 获取患者进行中的检测订单
|
||
* @return array
|
||
*/
|
||
public function getDetectionOrderFirst(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$family_id = $this->request->input("family_id");
|
||
$detection_project_id = $this->request->input("detection_project_id");
|
||
|
||
// 检测是否存在同类型未完成的检测订单
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['family_id'] = $family_id;
|
||
$params['detection_project_id'] = $detection_project_id;
|
||
$order_detection = OrderDetection::getNotFinishedOrderDetectionOne($params);
|
||
if (!empty($order_detection)) {
|
||
return success($order_detection['detection_no']);
|
||
}
|
||
|
||
return success("");
|
||
}
|
||
|
||
/**
|
||
* 绑定检测管
|
||
* @return array
|
||
*/
|
||
public function bindDetectionTube(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_detection_id = $this->request->route("order_detection_id");
|
||
|
||
$request_params = $this->request->all();
|
||
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['order_detection_id'] = $order_detection_id;
|
||
$order_detection = OrderDetection::getOne($params);
|
||
if (empty($order_detection)) {
|
||
return fail();
|
||
}
|
||
|
||
// 检测订单状态
|
||
if ($order_detection['detection_status'] != 2) {
|
||
return fail(HttpEnumCode::HTTP_ERROR, "订单状态错误");
|
||
}
|
||
|
||
if ($order_detection['detection_pay_status'] != 2) {
|
||
return fail(HttpEnumCode::HTTP_ERROR, "订单未支付");
|
||
}
|
||
|
||
if (!empty($order_detection['detection_bar_code'])) {
|
||
return fail(HttpEnumCode::HTTP_ERROR, "请勿重复绑定");
|
||
}
|
||
|
||
if (!empty($order_detection['detection_pic']) && !empty($request_params['detection_pic'])) {
|
||
return fail(HttpEnumCode::HTTP_ERROR, "请勿重复绑定");
|
||
}
|
||
|
||
// 处理检测管图片
|
||
if (!empty($request_params['detection_pic'])) {
|
||
$detection_pic = implode(',', $request_params['detection_pic']);
|
||
$detection_pic = PcreMatch::pregRemoveOssWebsite($detection_pic);
|
||
}
|
||
|
||
|
||
// 检测检测管编码是否已被使用
|
||
$params = array();
|
||
$params['detection_bar_code'] = $request_params['detection_bar_code'];
|
||
$result = OrderDetection::getOne($params);
|
||
if (!empty($result)) {
|
||
return fail(HttpEnumCode::HTTP_ERROR, "检测管已被使用");
|
||
}
|
||
|
||
// 获取检测码对应的检测所
|
||
$detection_organ_code = substr($request_params['detection_bar_code'], 3, 1);
|
||
if (!$detection_organ_code) {
|
||
return fail(HttpEnumCode::HTTP_ERROR, "检测码错误");
|
||
}
|
||
|
||
// 获取检测所数据
|
||
$params = array();
|
||
$params['detection_organ_code'] = $detection_organ_code;
|
||
$basic_detection_organ = BasicDetectionOrgan::getOne($params);
|
||
if (empty($basic_detection_organ)) {
|
||
return fail(HttpEnumCode::HTTP_ERROR, "检测码错误");
|
||
}
|
||
|
||
Db::beginTransaction();
|
||
try {
|
||
// 修改检测订单
|
||
$data = array();
|
||
if (isset($detection_pic)) {
|
||
$data['detection_pic'] = $detection_pic;
|
||
}
|
||
|
||
$data['detection_bar_code'] = $request_params['detection_bar_code'];
|
||
$data['detection_status'] = 3;
|
||
$data['detection_organ_id'] = $basic_detection_organ['detection_organ_id'];// 检测机构id
|
||
$data['detection_time'] = date('Y-m-d H:i:s', time());// 上传检测时间
|
||
|
||
$params = array();
|
||
$params['order_detection_id'] = $order_detection_id;
|
||
$res = OrderDetection::editOrderDetection($params, $data);
|
||
if (!$res) {
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::HTTP_ERROR, "绑定失败");
|
||
}
|
||
|
||
// 上报数据
|
||
$wy = new Wy($basic_detection_organ['detection_organ_id']);
|
||
$wy->report($order_detection_id);
|
||
|
||
Db::commit();
|
||
} catch (\Throwable $e) {
|
||
Db::rollBack();
|
||
Log::getInstance("DetectionService-bindDetectionTube")->error($e->getMessage());
|
||
return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage());
|
||
}
|
||
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 创建检测问诊订单
|
||
* @return array
|
||
*/
|
||
public function addDetectionInquiryOrder(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_detection_id = $this->request->route("order_detection_id");
|
||
|
||
// 定义返回数据
|
||
/*
|
||
* status解释
|
||
* 1:成功
|
||
* 2:存在待支付订单
|
||
* 3:存在待接诊订单
|
||
* 4:存在接诊中订单
|
||
* */
|
||
$result = array();
|
||
$result['status'] = 1;
|
||
$result['message'] = "成功";
|
||
$result['data']['order_inquiry_id'] = "";
|
||
$result['data']['order_no'] = "";
|
||
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['order_detection_id'] = $order_detection_id;
|
||
$order_detection = OrderDetection::getOne($params);
|
||
if (empty($order_detection)) {
|
||
return fail();
|
||
}
|
||
|
||
// 检测订单状态
|
||
if ($order_detection['detection_status'] != 4) {
|
||
return fail(HttpEnumCode::HTTP_ERROR, "订单状态错误");
|
||
}
|
||
|
||
// 检测是否已经创建问诊订单
|
||
if (!empty($order_detection['order_inquiry_id'])) {
|
||
$result['status'] = 1;
|
||
$result['message'] = "成功";
|
||
$result['data']['order_inquiry_id'] = (string)$order_detection['order_inquiry_id'];
|
||
$result['data']['order_no'] = (string)$order_detection['detection_no'];
|
||
|
||
return success($result);
|
||
}
|
||
|
||
Db::beginTransaction();
|
||
try {
|
||
// 检测当前医生是否和患者存在未完成问诊订单
|
||
$InquiryService = new InquiryService();
|
||
$order_inquiry = $InquiryService->checkPatientDoctorProgressInquiry($order_detection['patient_id'], $order_detection['doctor_id']);
|
||
if (!empty($order_inquiry)) {
|
||
if ($order_inquiry['inquiry_status'] == 1) {
|
||
// 待支付
|
||
Db::rollBack();
|
||
|
||
$result['status'] = 2;
|
||
$result['message'] = "当前医生有您未支付的订单,点击“继续”将为您取消订单直接进入报告解读服务。";
|
||
$result['data']['order_inquiry_id'] = (string)$order_inquiry['order_inquiry_id'];
|
||
$result['data']['order_no'] = (string)$order_inquiry['inquiry_no'];
|
||
|
||
return success($result);
|
||
}
|
||
|
||
if ($order_inquiry['inquiry_status'] == 3) {
|
||
// 待接诊
|
||
Db::rollBack();
|
||
|
||
$result['status'] = 3;
|
||
$result['message'] = "当前医生有您待接诊的订单,点击“继续”将为您取消订单直接进入报告解读服务。";
|
||
$result['data']['order_inquiry_id'] = (string)$order_inquiry['order_inquiry_id'];
|
||
$result['data']['order_no'] = (string)$order_inquiry['inquiry_no'];
|
||
|
||
return success($result);
|
||
}
|
||
|
||
if ($order_inquiry['inquiry_status'] == 4) {
|
||
// 已接诊
|
||
Db::rollBack();
|
||
|
||
$result['status'] = 4;
|
||
$result['message'] = "当前医生有您问诊中的订单,点击“继续”将进入聊天详情。";
|
||
$result['data']['order_inquiry_id'] = (string)$order_inquiry['order_inquiry_id'];
|
||
$result['data']['order_no'] = (string)$order_inquiry['inquiry_no'];
|
||
|
||
return success($result);
|
||
}
|
||
}
|
||
|
||
// 获取患者家庭成员是否存在未支付的服务包订单
|
||
$OrderServicePackageService = new OrderServicePackageService();
|
||
$order_service_package = $OrderServicePackageService->getPatientFamilyNoPayServicePackage($user_info['user_id'], $order_detection['family_id'], $order_detection['doctor_id']);
|
||
if (!empty($order_service_package)){
|
||
// 待支付
|
||
Db::rollBack();
|
||
|
||
$result['status'] = 5;
|
||
$result['message'] = "您和当前医生存在未支付的服务包订单,点击“继续”将进入订单详情。";
|
||
$result['data']['order_no'] = (string)$order_service_package['order_service_no'];
|
||
|
||
return success($result);
|
||
}
|
||
|
||
// 获取医生数据
|
||
$params = array();
|
||
$params['doctor_id'] = $order_detection['doctor_id'];
|
||
$user_doctor = UserDoctor::getOne($params);
|
||
if (empty($user_doctor)) {
|
||
Db::rollBack();
|
||
return fail();
|
||
}
|
||
|
||
// 获取检测病例数据
|
||
$params = array();
|
||
$params['order_detection_id'] = $order_detection['order_detection_id'];
|
||
$order_detection_case = OrderDetectionCase::getOne($params);
|
||
if (empty($order_detection_case)) {
|
||
Db::rollBack();
|
||
return fail();
|
||
}
|
||
|
||
// 获取检测项目数据
|
||
$params = array();
|
||
$params['detection_project_id'] = $order_detection['detection_project_id'];
|
||
$detection_project = DetectionProject::getOne($params);
|
||
if (empty($detection_project)) {
|
||
Db::rollBack();
|
||
return fail();
|
||
}
|
||
|
||
// 检测家庭成员是否存在
|
||
$params = array();
|
||
$params['family_id'] = $order_detection['family_id'];
|
||
$params['patient_id'] = $order_detection['patient_id'];
|
||
$patient_family = PatientFamily::getOne($params);
|
||
if (empty($patient_family)) {
|
||
Db::rollBack();
|
||
return fail();
|
||
}
|
||
|
||
$generator = $this->container->get(IdGeneratorInterface::class);
|
||
|
||
// 生成订单表
|
||
$data = array();
|
||
$data['user_id'] = $order_detection['user_id'];
|
||
$data['patient_id'] = $order_detection['patient_id'];
|
||
$data['doctor_id'] = $order_detection['doctor_id'];
|
||
$data['order_type'] = 1; // 订单类型(1:问诊订单 2:药品订单 3:检测订单 4:随访包订单 5:健康包订单)
|
||
$data['inquiry_pay_channel'] = 3; // 支付渠道(1:小程序支付 2:微信扫码支付)
|
||
$data['pay_status'] = 2; // 1:待支付
|
||
$data['order_no'] = "I" . $generator->generate(); // 订单编号
|
||
$data['escrow_trade_no'] = "GD" . $generator->generate(); // 第三方支付流水号
|
||
$data['amount_total'] = 0; // 订单金额
|
||
$data['coupon_amount_total'] = 0; // 优惠卷总金额
|
||
$data['payment_amount_total'] = 0; // 实际付款金额
|
||
$order = Order::addOrder($data);
|
||
if (empty($order)) {
|
||
Db::rollBack();
|
||
return fail();
|
||
}
|
||
|
||
// 创建问诊订单
|
||
$data = array();
|
||
$data['order_id'] = $order['order_id'];
|
||
$data['user_id'] = $order_detection['user_id'];
|
||
$data['patient_id'] = $order_detection['patient_id'];
|
||
$data['doctor_id'] = $order_detection['doctor_id'];
|
||
$data['family_id'] = $order_detection['family_id'];
|
||
$data['inquiry_type'] = 5; // 订单类型(1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药 5:检测)
|
||
$data['inquiry_mode'] = 1; // 订单问诊方式(1:图文 2:视频 3:语音 4:电话 5:会员)
|
||
$data['inquiry_status'] = 4; // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
$data['inquiry_pay_channel'] = 3; // 支付渠道(1:小程序支付 2:微信扫码支付 3:模拟支付)
|
||
$data['inquiry_pay_status'] = 2; // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||
$data['inquiry_no'] = $order['order_no'];// 订单编号
|
||
$data['escrow_trade_no'] = $order['escrow_trade_no']; // 第三方支付流水号
|
||
$data['amount_total'] = 0;// 订单金额
|
||
$data['coupon_amount_total'] = 0;// 优惠卷总金额
|
||
$data['payment_amount_total'] = 0;// 实际付款金额
|
||
$data['pay_time'] = date('Y-m-d H:i:s', time());// 支付时间
|
||
$data['reception_time'] = date('Y-m-d H:i:s', time());// 接诊时间
|
||
$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_inquiry = OrderInquiry::addOrderInquiry($data);
|
||
if (empty($order_inquiry)) {
|
||
Db::rollBack();
|
||
return fail();
|
||
}
|
||
|
||
// 增加患者问诊病例
|
||
$data = array();
|
||
$data['user_id'] = $order_detection['user_id'];
|
||
$data['patient_id'] = $order_detection['patient_id'];
|
||
$data['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];// 订单-问诊id
|
||
$data['family_id'] = $patient_family['family_id']; // 家庭成员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['height'] = $patient_family['height'] ?: null; // 身高(cm)
|
||
$data['weight'] = $patient_family['weight'] ?: null;; // 体重(kg)
|
||
$data['nation_id'] = $order_detection_case['nation_id'] ?: $patient_family['nation_id'] ?: null;; // 民族
|
||
$data['nation_name'] = $order_detection_case['nation_name'] ?: $patient_family['nation_name'] ?: null;; // 民族名称
|
||
$order_inquiry_case = OrderInquiryCase::addOrderInquiryCase($data);
|
||
if (empty($order_inquiry_case)) {
|
||
Db::rollBack();
|
||
return fail();
|
||
}
|
||
|
||
// 修改检测订单
|
||
$data = array();
|
||
$data['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];// 订单-问诊id
|
||
|
||
$params = array();
|
||
$params['order_detection_id'] = $order_detection['order_detection_id'];
|
||
OrderDetection::editOrderDetection($params, $data);
|
||
|
||
// 添加自动完成队列
|
||
$time = 1000 * 60 * 60 * 24 * 3;
|
||
|
||
$data = array();
|
||
$data['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
|
||
$message = new AutoCompleteInquiryDelayDirectProducer($data);
|
||
$message->setDelayMs($time);
|
||
$producer = $this->container->get(Producer::class);
|
||
$res = $producer->produce($message);
|
||
if (!$res) {
|
||
Db::rollBack();
|
||
return fail();
|
||
}
|
||
|
||
// 发送im消息
|
||
$imService = new ImService();
|
||
|
||
// 患者病例
|
||
$imService->patientCase(
|
||
$order_inquiry,
|
||
$user_doctor['user_id'],
|
||
$order_inquiry_case['disease_desc']
|
||
);
|
||
|
||
// 发送IM消息-检测报告结果
|
||
$data = [
|
||
"order_inquiry_id" => $order_inquiry['order_inquiry_id'],
|
||
"inquiry_type" => $order_inquiry['inquiry_type'],
|
||
"inquiry_mode" => $order_inquiry['inquiry_mode'],
|
||
"detection_no" => $order_detection['detection_no'],
|
||
"patient_name" => $order_detection['patient_name'],
|
||
"patient_sex" => $order_detection['patient_sex'],
|
||
"patient_age" => $order_detection['patient_age'],
|
||
"detection_project_name" => $detection_project['detection_project_name'],
|
||
"disease_class_names" => $order_detection_case['detection_disease_class_names'],
|
||
"detection_result_pdf" => $order_detection['detection_result_pdf'],
|
||
"patient_user_id" => $order_detection['user_id'],
|
||
"doctor_user_id" => $user_doctor['user_id'],
|
||
];
|
||
$imService->detectionTestReport($data);
|
||
|
||
// 发送IM消息-检测报告结果-文字
|
||
$data = [
|
||
"order_inquiry_id" => $order_inquiry['order_inquiry_id'],
|
||
"inquiry_type" => $order_inquiry['inquiry_type'],
|
||
"inquiry_mode" => $order_inquiry['inquiry_mode'],
|
||
"detection_no" => $order_detection['detection_no'],
|
||
"doctor_name" => $user_doctor['user_name'],
|
||
"patient_name" => $order_detection['patient_name'],
|
||
"patient_sex" => $order_detection['patient_sex'],
|
||
"patient_age" => $order_detection['patient_age'],
|
||
"detection_project_name" => $detection_project['detection_project_name'],
|
||
"patient_user_id" => $order_detection['user_id'],
|
||
"doctor_user_id" => $user_doctor['user_id'],
|
||
];
|
||
|
||
$imService->detectionTestReportStr($data);
|
||
|
||
Db::commit();
|
||
} catch (\Throwable $e) {
|
||
Db::rollBack();
|
||
Log::getInstance("DetectionService-bindDetectionTube")->error($e->getMessage());
|
||
return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage());
|
||
}
|
||
|
||
$result['status'] = 1;
|
||
$result['message'] = "成功";
|
||
$result['data'] = (string)$order_inquiry['order_inquiry_id'];
|
||
|
||
return success($result);
|
||
}
|
||
|
||
/**
|
||
* 取消未支付检测订单
|
||
* @param string|int $order_no
|
||
* @param string|int $cancel_reason 取消订单原因(1:主动取消 2:客服取消 3:支付超时)
|
||
* @param string|int $cancel_remarks 取消备注
|
||
* @return array
|
||
*/
|
||
public function cancelUnpayDetectionOrder(string|int $order_no, string|int $cancel_reason, string|int $cancel_remarks): array
|
||
{
|
||
$result = array();
|
||
$result['status'] = 1;
|
||
$result['message'] = "成功";
|
||
|
||
// 获取检测订单数据
|
||
$params = array();
|
||
$params['detection_no'] = $order_no;
|
||
$order_detection = OrderDetection::getOne($params);
|
||
if (empty($order_detection)) {
|
||
$result['status'] = 0;
|
||
$result['message'] = "未查询到对应订单数据";
|
||
return $result;
|
||
}
|
||
|
||
// 检测订单状态
|
||
if ($order_detection['detection_status'] == 5) {
|
||
// 检测订单状态(1:待支付 2:待绑定 3:检测中 4:检测完成 5:已取消)
|
||
$result['status'] = 0;
|
||
$result['message'] = "订单已取消";
|
||
return $result;
|
||
}
|
||
|
||
if ($order_detection['detection_status'] != 1) {
|
||
// 检测订单状态(1:待支付 2:待绑定 3:检测中 4:检测完成 5:已取消)
|
||
$result['status'] = 0;
|
||
$result['message'] = "订单取消失败";
|
||
return $result;
|
||
}
|
||
|
||
// 检测订单退款状态
|
||
if (!in_array($order_detection['detection_refund_status'], [0, 4, 5])) {
|
||
// 检测订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
|
||
$result['status'] = 0;
|
||
$result['message'] = "订单正在退款中";
|
||
return $result;
|
||
}
|
||
|
||
// 检测订单支付状态
|
||
if ($order_detection['detection_pay_status'] == 2) {
|
||
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||
$result['status'] = 0;
|
||
$result['message'] = "订单已支付";
|
||
return $result;
|
||
}
|
||
|
||
// 检测订单删除状态
|
||
if ($order_detection['is_delete'] == 1) {
|
||
// 删除状态(0:否 1:是)
|
||
$result['status'] = 2;
|
||
$result['message'] = "订单已被删除";
|
||
return $result;
|
||
}
|
||
|
||
// 取消检测订单
|
||
$data = array();
|
||
$data['detection_status'] = 5; // 检测订单状态(1:待支付 2:待绑定 3:检测中 4:检测完成 5:已取消)
|
||
if ($cancel_reason == 3) {
|
||
$data['detection_pay_status'] = 5; // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||
}
|
||
|
||
$data['cancel_time'] = date("Y-m-d H:i:s", time());
|
||
$data['cancel_reason'] = $cancel_reason; // 取消订单原因(1:主动取消 2:客服取消 3:支付超时)
|
||
$data['cancel_remarks'] = $cancel_remarks; // 取消订单备注
|
||
$data['updated_at'] = date("Y-m-d H:i:s", time());
|
||
|
||
$params = array();
|
||
$params['order_detection_id'] = $order_detection['order_detection_id'];
|
||
OrderDetection::editOrderDetection($params, $data);
|
||
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 检测订单退款接口
|
||
*/
|
||
/*public function detectionRefund(string $order_detection_id, string $refund_reason): void
|
||
{
|
||
// 获取订单数据
|
||
$params = array();
|
||
$params['order_detection_id'] = $order_detection_id;
|
||
$order_detection = OrderDetection::getOne($params);
|
||
if (empty($order_detection)) {
|
||
throw new BusinessException("订单数据为空");
|
||
}
|
||
|
||
// 检测问诊订单状态
|
||
if (!in_array($order_detection['detection_status'], [2, 5])) {
|
||
// 检测订单状态(1:待支付 2:待绑定 3:检测中 4:检测完成 5:已取消)
|
||
throw new BusinessException("订单状态错误");
|
||
}
|
||
|
||
// 检测订单退款状态
|
||
if (in_array($order_detection['detection_refund_status'], [2, 3, 5, 6])) {
|
||
// 检测订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
|
||
throw new BusinessException("订单退款状态错误");
|
||
}
|
||
|
||
// 检测支付状态
|
||
if ($order_detection['detection_pay_status'] != 2) {
|
||
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||
throw new BusinessException("订单支付状态错误");
|
||
}
|
||
|
||
// 系统退款编号
|
||
$generator = $this->container->get(IdGeneratorInterface::class);
|
||
$detection_refund_no = $generator->generate();
|
||
|
||
// 检测订单金额
|
||
if ($order_detection['payment_amount_total'] > 0) {
|
||
// 发起退款
|
||
$WechatPay = new WechatPay(1, 3);
|
||
|
||
$options = array();
|
||
$options['transaction_id'] = $order_detection['escrow_trade_no'];
|
||
$options['out_refund_no'] = (string)$detection_refund_no;
|
||
$options['reason'] = $refund_reason;
|
||
$options['amount'] = [
|
||
'refund' => (int)round($order_detection['payment_amount_total'] * 100),
|
||
'total' => (int)round($order_detection['payment_amount_total'] * 100),
|
||
'currency' => "CNY",
|
||
];
|
||
|
||
$result = $WechatPay->refund($options);
|
||
|
||
// 处理订单退款状态
|
||
// 检测订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭)
|
||
$success_time = "";
|
||
if ($result['status'] == "SUCCESS") {
|
||
// 退款成功
|
||
$detection_refund_status = 3;
|
||
$success_time = $result['success_time'];
|
||
} elseif ($result['status'] == "CLOSED") {
|
||
// 退款关闭
|
||
$detection_refund_status = 5;
|
||
} elseif ($result['status'] == "PROCESSING") {
|
||
// 退款处理中
|
||
$detection_refund_status = 2;
|
||
} elseif ($result['status'] == "ABNORMAL") {
|
||
// 退款异常,此情况不处理,进行短信通知
|
||
throw new BusinessException("订单退款状态异常");
|
||
} else {
|
||
throw new BusinessException("订单退款状态错误");
|
||
}
|
||
|
||
$refund_id = $result['refund_id'];
|
||
} else {
|
||
$detection_refund_status = 3;
|
||
$success_time = date('Y-m-d H:i:s', time());
|
||
$refund_id = $generator->generate();
|
||
}
|
||
|
||
// 新增退款表
|
||
$data = array();
|
||
$data['patient_id'] = $order_detection['patient_id'];
|
||
$data['order_detection_id'] = $order_detection['order_detection_id'];
|
||
$data['detection_no'] = $order_detection['detection_no'];
|
||
$data['detection_refund_no'] = $detection_refund_no;
|
||
$data['refund_id'] = $refund_id;
|
||
$data['detection_refund_status'] = $detection_refund_status;
|
||
$data['refund_total'] = $order_detection['payment_amount_total'];
|
||
$data['refund_reason'] = $refund_reason;
|
||
|
||
if ($detection_refund_status == 3 && !empty($success_time)) {
|
||
$data['success_time'] = date("Y-m-d H:i:s", strtotime($success_time)); // 退款成功时间
|
||
}
|
||
|
||
$order_detection_refund = OrderDetectionRefund::add($data);
|
||
if (empty($order_detection_refund)) {
|
||
throw new BusinessException("添加退款表失败");
|
||
}
|
||
|
||
// 修改问诊订单表状态
|
||
$data = array();
|
||
$data['detection_refund_status'] = $detection_refund_status;
|
||
|
||
$params = array();
|
||
$params['order_detection_id'] = $order_detection['order_detection_id'];
|
||
OrderDetection::editOrderDetection($params, $data);
|
||
}*/
|
||
|
||
/**
|
||
* 获取患者某一状态下的检测订单数量
|
||
* @param string $patient_id 患者id
|
||
* @param int $detection_status 检测订单状态(1:待支付 2:待绑定 3:检测中 4:检测完成 5:已取消)
|
||
* @return int
|
||
*/
|
||
public function getPatientDetectionWithStatus(string $patient_id, int $detection_status): int
|
||
{
|
||
$params = array();
|
||
$params['patient_id'] = $patient_id;
|
||
$params['detection_status'] = $detection_status;
|
||
return OrderDetection::getCount($params);
|
||
}
|
||
} |