From ff5d62211091798ab51b0a1f4dd48ecf43cd05dd Mon Sep 17 00:00:00 2001 From: wucongxing <815046773@qq.com> Date: Fri, 28 Jul 2023 14:35:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E8=AE=A2=E5=8D=95=20=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/DetectionController.php | 5 +- app/Model/DetectionProject.php | 3 + app/Model/DetectionProjectPurpose.php | 3 + app/Model/DiseaseClassDetection.php | 3 + app/Model/OrderDetection.php | 112 +++++++++++++ app/Model/OrderDetectionCase.php | 87 ++++++++++ app/Model/UserDoctor.php | 9 -- app/Request/DetectionRequest.php | 21 ++- app/Services/DetectionService.php | 210 +++++++++++++++++++++++++ 9 files changed, 441 insertions(+), 12 deletions(-) create mode 100644 app/Model/OrderDetection.php create mode 100644 app/Model/OrderDetectionCase.php diff --git a/app/Controller/DetectionController.php b/app/Controller/DetectionController.php index a9ebedc..47538d1 100644 --- a/app/Controller/DetectionController.php +++ b/app/Controller/DetectionController.php @@ -55,12 +55,13 @@ class DetectionController extends AbstractController } // 创建检测订单 - public function addDetectionOrder(){ + public function addDetectionOrder(): ResponseInterface + { $request = $this->container->get(DetectionRequest::class); $request->scene('addDetectionOrder')->validateResolved(); $detectionService = new DetectionService(); - $data = $detectionService->getDetectionDoctorList(); + $data = $detectionService->addDetectionOrder(); return $this->response->json($data); } } \ No newline at end of file diff --git a/app/Model/DetectionProject.php b/app/Model/DetectionProject.php index e295e0e..fc98473 100644 --- a/app/Model/DetectionProject.php +++ b/app/Model/DetectionProject.php @@ -7,6 +7,7 @@ namespace App\Model; use Hyperf\Database\Model\Collection; +use Hyperf\Snowflake\Concern\Snowflake; /** * @property int $detection_project_id 主键id @@ -19,6 +20,8 @@ use Hyperf\Database\Model\Collection; */ class DetectionProject extends Model { + use Snowflake; + /** * The table associated with the model. */ diff --git a/app/Model/DetectionProjectPurpose.php b/app/Model/DetectionProjectPurpose.php index 1045708..bb8bb2c 100644 --- a/app/Model/DetectionProjectPurpose.php +++ b/app/Model/DetectionProjectPurpose.php @@ -7,6 +7,7 @@ namespace App\Model; use Hyperf\Database\Model\Collection; +use Hyperf\Snowflake\Concern\Snowflake; /** * @property int $purpose_id 主键id @@ -17,6 +18,8 @@ use Hyperf\Database\Model\Collection; */ class DetectionProjectPurpose extends Model { + use Snowflake; + /** * The table associated with the model. */ diff --git a/app/Model/DiseaseClassDetection.php b/app/Model/DiseaseClassDetection.php index 0202e68..f430aef 100644 --- a/app/Model/DiseaseClassDetection.php +++ b/app/Model/DiseaseClassDetection.php @@ -7,6 +7,7 @@ namespace App\Model; use Hyperf\Database\Model\Collection; +use Hyperf\Snowflake\Concern\Snowflake; /** * @property int $id 主键id @@ -20,6 +21,8 @@ use Hyperf\Database\Model\Collection; */ class DiseaseClassDetection extends Model { + use Snowflake; + /** * The table associated with the model. */ diff --git a/app/Model/OrderDetection.php b/app/Model/OrderDetection.php new file mode 100644 index 0000000..2ab58dc --- /dev/null +++ b/app/Model/OrderDetection.php @@ -0,0 +1,112 @@ +first($fields); + } + + /** + * 获取数据-多 + * @param array $params + * @param array $fields + * @return Collection|array + */ + public static function getList(array $params = [], array $fields = ['*']): Collection|array + { + return self::where($params)->get($fields); + } + + /** + * 新增-批量 + * @param array $data 新增数据 + * @return \Hyperf\Database\Model\Model|OrderDetection + */ + public static function addOrderDetection(array $data): \Hyperf\Database\Model\Model|OrderDetection + { + return self::create($data); + } + + /** + * 修改-批量 + * @param array $params + * @param array $data + * @return int + */ + public static function editOrderDetection(array $params = [], array $data = []): int + { + return self::where($params)->update($data); + } + + /** + * 获取未完成订单 + * @param array $params + * @param array $fields + * @return object|null + */ + public static function getNotFinishedOrderDetectionOne(array $params,array $fields = ['*']): object|null + { + return self::where($params)->whereIn('detection_status',[1,2,3])->first($fields); + } +} diff --git a/app/Model/OrderDetectionCase.php b/app/Model/OrderDetectionCase.php new file mode 100644 index 0000000..e233fbb --- /dev/null +++ b/app/Model/OrderDetectionCase.php @@ -0,0 +1,87 @@ +first($fields); + } + + /** + * 获取数据-多 + * @param array $params + * @param array $fields + * @return Collection|array + */ + public static function getList(array $params = [], array $fields = ['*']): Collection|array + { + return self::where($params)->get($fields); + } + + /** + * 新增-批量 + * @param array $data 新增数据 + * @return \Hyperf\Database\Model\Model|OrderDetectionCase + */ + public static function addOrderDetectionCase(array $data): \Hyperf\Database\Model\Model|OrderDetectionCase + { + return self::create($data); + } + + /** + * 修改-批量 + * @param array $params + * @param array $data + * @return int + */ + public static function editOrderDetectionCase(array $params = [], array $data = []): int + { + return self::where($params)->update($data); + } + +} diff --git a/app/Model/UserDoctor.php b/app/Model/UserDoctor.php index 7cca6e2..9e19a4e 100644 --- a/app/Model/UserDoctor.php +++ b/app/Model/UserDoctor.php @@ -379,15 +379,6 @@ class UserDoctor extends Model ->whereHas('Hospital', function ($query) use ($hospital_params) { $query->where($hospital_params); }) -// ->when($keyword, function ($query, $keyword) { -// $query->where(function ($query) use ($keyword) { -// $query->orwhere("user_name", 'like', '%' . $keyword . '%'); -// $query->orwhere("department_custom_name", 'like', '%' . $keyword . '%'); -// $query->orWhereHas('Hospital', function ($query) use ($keyword) { -// $query->where('hospital_name', 'like', '%' . $keyword . '%'); -// }); -// }); -// }) ->where($params) ->get($fields); } diff --git a/app/Request/DetectionRequest.php b/app/Request/DetectionRequest.php index 2a4b8e8..eccc0d4 100644 --- a/app/Request/DetectionRequest.php +++ b/app/Request/DetectionRequest.php @@ -16,6 +16,10 @@ class DetectionRequest extends FormRequest 'family_id', 'nation_id', 'detection_disease_class_ids', + 'detection_project_id', // 检测项目id + 'purpose_id', // 检测项目用途id + 'doctor_id', // 医生id + 'client_type', // 客户端类型(1:手机 2:电脑) ], ]; @@ -33,7 +37,13 @@ class DetectionRequest extends FormRequest public function rules(): array { return [ + 'patient_id' => 'required', + 'family_id' => 'required', + 'nation_id' => 'required', + 'detection_disease_class_ids' => 'required', 'detection_project_id' => 'required', + 'doctor_id' => 'required', + 'client_type' => 'required|integer|min:1|max:2', ]; } @@ -43,7 +53,16 @@ class DetectionRequest extends FormRequest public function messages(): array { return [ - 'company_id.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), + 'patient_id.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), + 'family_id.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), + 'nation_id.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), + 'detection_disease_class_ids.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), + 'detection_project_id.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), + 'doctor_id.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), + 'client_type.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), + 'client_type.integer' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), + 'client_type.min' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), + 'client_type.max' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), ]; } } diff --git a/app/Services/DetectionService.php b/app/Services/DetectionService.php index 6752838..e50d2ee 100644 --- a/app/Services/DetectionService.php +++ b/app/Services/DetectionService.php @@ -2,12 +2,25 @@ 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 { @@ -257,4 +270,201 @@ class DetectionService extends BaseService 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); + } } \ No newline at end of file