975 lines
33 KiB
PHP
975 lines
33 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Constants\DoctorTitleCode;
|
||
use App\Constants\HttpEnumCode;
|
||
use App\Model\Hospital;
|
||
use App\Model\OrderInquiry;
|
||
use App\Model\OrderInquiryCase;
|
||
use App\Model\OrderPrescription;
|
||
use App\Model\OrderPrescriptionProduct;
|
||
use App\Model\OrderProduct;
|
||
use App\Model\OrderProductItem;
|
||
use App\Model\Product;
|
||
use App\Model\UserDoctor;
|
||
use App\Model\UserShipAddress;
|
||
use Extend\Wechat\WechatPay;
|
||
use Hyperf\DbConnection\Db;
|
||
use Psr\Container\ContainerExceptionInterface;
|
||
use Psr\Container\NotFoundExceptionInterface;
|
||
|
||
/**
|
||
* 患者订单
|
||
*/
|
||
class PatientOrderService extends BaseService
|
||
{
|
||
/**
|
||
* 获取患者问诊订单列表
|
||
* @return array
|
||
*/
|
||
public function getPatientInquiryOrderList(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$inquiry_status = $this->request->input('inquiry_status',0);
|
||
$family_id = $this->request->input('family_id');
|
||
$page = $this->request->input('page',1);
|
||
$per_page = $this->request->input('per_page',10);
|
||
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
if (!empty($family_id)){
|
||
$params['family_id'] = $family_id;
|
||
}
|
||
|
||
$inquiry_status_params = [];
|
||
if (!empty($inquiry_status) && $inquiry_status != 0){
|
||
// 问诊订单状态(0:全部 1:待支付 2:待接诊 3:问诊中 4:完成/取消
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
if ($inquiry_status == 1){
|
||
$inquiry_status_params = [1];
|
||
}
|
||
|
||
if ($inquiry_status == 2){
|
||
$inquiry_status_params = [2,3];
|
||
}
|
||
|
||
if ($inquiry_status == 3){
|
||
$inquiry_status_params = [4];
|
||
}
|
||
|
||
if ($inquiry_status == 4){
|
||
$inquiry_status_params = [5,6,7];
|
||
}
|
||
}
|
||
|
||
$params['is_delete'] = 0;
|
||
|
||
$fields = [
|
||
'order_inquiry_id',
|
||
'patient_id',
|
||
'doctor_id',
|
||
'family_id',
|
||
'inquiry_type',
|
||
'inquiry_mode',
|
||
'inquiry_status',
|
||
'is_delete',
|
||
'inquiry_refund_status',
|
||
'inquiry_pay_channel',
|
||
'inquiry_pay_status',
|
||
'inquiry_no',
|
||
'escrow_trade_no',
|
||
'amount_total',
|
||
'payment_amount_total',
|
||
'patient_name',
|
||
'patient_name_mask',
|
||
'patient_sex',
|
||
'patient_age',
|
||
'created_at',
|
||
];
|
||
$order_inquiry = OrderInquiry::getPatientOrderInquiryPage($params,$inquiry_status_params,$fields,$page,$per_page);
|
||
if (empty($order_inquiry['data'])){
|
||
return success();
|
||
}
|
||
|
||
foreach ($order_inquiry['data'] as &$item){
|
||
$item['disease_desc'] = $item['OrderInquiryCase']['disease_desc'];
|
||
unset($item['OrderInquiryCase']);
|
||
|
||
// 获取医生数据
|
||
$item['user_doctor'] = [];
|
||
if (!empty($item['doctor_id'])){
|
||
$fields = [
|
||
'doctor_id',
|
||
'user_name',
|
||
'doctor_title',
|
||
'hospital_id',
|
||
];
|
||
|
||
$params = array();
|
||
$params['doctor_id'] = $item['doctor_id'];
|
||
$user_doctor = UserDoctor::getOne($params,$fields);
|
||
if (empty($user_doctor)){
|
||
return fail(HttpEnumCode::SERVER_ERROR);
|
||
}
|
||
|
||
// 转换医生职称
|
||
$user_doctor['doctor_title'] = empty($user_doctor['doctor_title']) ? "" : DoctorTitleCode::getMessage($user_doctor['doctor_title']);
|
||
|
||
// 获取医生医院名称
|
||
$user_doctor['hospital_name'] = "";
|
||
$user_doctor['hospital_level_name'] = "";
|
||
|
||
$fields = [
|
||
'hospital_id',
|
||
'hospital_name',
|
||
'hospital_level_name',
|
||
];
|
||
|
||
$params = array();
|
||
$params['hospital_id'] = $user_doctor['hospital_id'];
|
||
$hospital = Hospital::getOne($params,$fields);
|
||
if (!empty($hospital)){
|
||
$user_doctor['hospital_name'] = $hospital['hospital_name'];
|
||
$user_doctor['hospital_level_name'] = $hospital['hospital_level_name'];
|
||
}
|
||
|
||
$item['user_doctor'] = $user_doctor;
|
||
unset($hospital);
|
||
unset($user_doctor);
|
||
}
|
||
}
|
||
|
||
return success($order_inquiry);
|
||
}
|
||
|
||
/**
|
||
* 获取患者问诊订单详情
|
||
* @return array
|
||
*/
|
||
public function getPatientInquiryOrderInfo(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_inquiry_id = $this->request->route('order_inquiry_id');
|
||
|
||
// 获取订单数据
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['order_inquiry_id'] = $order_inquiry_id;
|
||
$params['is_delete'] = 0;
|
||
$order_inquiry = OrderInquiry::getOne($params);
|
||
if (empty($order_inquiry)){
|
||
return fail();
|
||
}
|
||
|
||
// 获取病例信息
|
||
$fields = [
|
||
'disease_class_name',
|
||
'disease_desc',
|
||
];
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
$order_inquiry_case = OrderInquiryCase::getOne($params,$fields);
|
||
if (empty($order_inquiry_case)){
|
||
return fail();
|
||
}
|
||
|
||
$order_inquiry['case'] = $order_inquiry_case->toArray();
|
||
|
||
// 获取医生数据
|
||
$order_inquiry['user_doctor'] = [];
|
||
if (!empty($order_inquiry['doctor_id'])){
|
||
$fields = [
|
||
'doctor_id',
|
||
'user_name',
|
||
'doctor_title',
|
||
'hospital_id',
|
||
'avatar',
|
||
];
|
||
|
||
$params = array();
|
||
$params['doctor_id'] = $order_inquiry['doctor_id'];
|
||
$user_doctor = UserDoctor::getOne($params,$fields);
|
||
if (empty($user_doctor)){
|
||
return fail(HttpEnumCode::SERVER_ERROR);
|
||
}
|
||
|
||
// 转换医生职称
|
||
$user_doctor['doctor_title'] = empty($user_doctor['doctor_title']) ? "" : DoctorTitleCode::getMessage($user_doctor['doctor_title']);
|
||
|
||
// 头像
|
||
$user_doctor['avatar'] = addAliyunOssWebsite($user_doctor['avatar']);
|
||
|
||
// 获取医生医院名称
|
||
$user_doctor['hospital_name'] = "";
|
||
$user_doctor['hospital_level_name'] = "";
|
||
|
||
$fields = [
|
||
'hospital_id',
|
||
'hospital_name',
|
||
'hospital_level_name',
|
||
];
|
||
|
||
$params = array();
|
||
$params['hospital_id'] = $user_doctor['hospital_id'];
|
||
$hospital = Hospital::getOne($params,$fields);
|
||
if (!empty($hospital)){
|
||
$user_doctor['hospital_name'] = $hospital['hospital_name'];
|
||
$user_doctor['hospital_level_name'] = $hospital['hospital_level_name'];
|
||
}
|
||
|
||
$order_inquiry['user_doctor'] = $user_doctor;
|
||
unset($hospital);
|
||
unset($user_doctor);
|
||
}
|
||
|
||
return success($order_inquiry->toArray());
|
||
}
|
||
|
||
/**
|
||
* 取消患者问诊订单
|
||
* @return array
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
public function putCancelPatientInquiryOrder(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_inquiry_id = $this->request->route('order_inquiry_id');
|
||
|
||
// 获取订单数据
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['order_inquiry_id'] = $order_inquiry_id;
|
||
$params['is_delete'] = 0;
|
||
$order_inquiry = OrderInquiry::getOne($params);
|
||
if (empty($order_inquiry)){
|
||
return fail();
|
||
}
|
||
|
||
// 检测订单状态
|
||
if (!in_array($order_inquiry['inquiry_status'],[1,2,3])) {
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单无法取消");
|
||
}
|
||
|
||
if (!in_array($order_inquiry['inquiry_refund_status'],[0,4,5])) {
|
||
// 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭)
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单正在退款中");
|
||
}
|
||
|
||
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||
|
||
Db::beginTransaction();
|
||
try {
|
||
// 修改问诊订单为取消
|
||
$data = array();
|
||
$data['inquiry_status'] = 7;
|
||
$data['cancel_time'] = date("Y-m-d H:i:s",time());
|
||
$data['cancel_reason'] = 2; // 取消订单原因(1:医生未接诊 2:主动取消 3:无可分配医生 4:客服取消 5:支付超时)
|
||
$data['updated_at'] = date("Y-m-d H:i:s",time());
|
||
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
OrderInquiry::edit($params,$data);
|
||
|
||
// 检测支付状态,判断是否需要退款处理
|
||
if ($order_inquiry['inquiry_pay_status'] == 2){
|
||
// 需退款
|
||
$inquiryService = new InquiryService();
|
||
$inquiryService->inquiryRefund($order_inquiry['order_inquiry_id'],"取消问诊");
|
||
}
|
||
|
||
Db::commit();
|
||
} catch (\Exception $e) {
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage());
|
||
}
|
||
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 删除问诊订单
|
||
* 支付超时
|
||
* 取消问诊
|
||
* 问诊结束
|
||
* @return array
|
||
*/
|
||
public function deletePatientInquiryOrder(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_inquiry_id = $this->request->route('order_inquiry_id');
|
||
|
||
// 获取订单数据
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['order_inquiry_id'] = $order_inquiry_id;
|
||
$params['is_delete'] = 0;
|
||
$order_inquiry = OrderInquiry::getOne($params);
|
||
if (empty($order_inquiry)){
|
||
return fail();
|
||
}
|
||
|
||
// 检测订单状态
|
||
if (!in_array($order_inquiry['inquiry_status'],[6,7])) {
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单无法删除");
|
||
}
|
||
|
||
// 修改订单删除状态
|
||
$data = array();
|
||
$data['is_delete'] = 1;
|
||
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
|
||
OrderInquiry::edit($params,$data);
|
||
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 问诊订单取消支付
|
||
* @return array
|
||
*/
|
||
public function putPatientInquiryOrderCancelPay(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_inquiry_id = $this->request->route('order_inquiry_id');
|
||
|
||
// 获取订单数据
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['order_inquiry_id'] = $order_inquiry_id;
|
||
$params['is_delete'] = 0;
|
||
$order_inquiry = OrderInquiry::getOne($params);
|
||
if (empty($order_inquiry)){
|
||
return fail();
|
||
}
|
||
|
||
// 检测订单状态
|
||
if ($order_inquiry['inquiry_status'] != 1) {
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单无法取消支付");
|
||
}
|
||
|
||
if (!in_array($order_inquiry['inquiry_refund_status'],[0,4,5])) {
|
||
// 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭)
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单正在退款中");
|
||
}
|
||
|
||
// 修改订单状态
|
||
$data = array();
|
||
$data['inquiry_status'] = 7;
|
||
$data['cancel_time'] = date("Y-m-d H:i:s",time());
|
||
$data['cancel_reason'] = 2; // 取消订单原因(1:医生未接诊 2:主动取消 3:无可分配医生 4:客服取消 5:支付超时)
|
||
$data['cancel_remarks'] = "主动取消";
|
||
$data['updated_at'] = date("Y-m-d H:i:s",time());
|
||
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
OrderInquiry::edit($params,$data);
|
||
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 获取药品订单列表
|
||
* @return array
|
||
*/
|
||
public function getPatientProductOrderList(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_product_status = $this->request->input('order_product_status',0);
|
||
$family_id = $this->request->input('family_id');
|
||
$page = $this->request->input('page',1);
|
||
$per_page = $this->request->input('per_page',10);
|
||
|
||
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
if (!empty($family_id)){
|
||
$params['family_id'] = $family_id;
|
||
}
|
||
|
||
if (!empty($inquiry_status_params) && $inquiry_status_params != 0){
|
||
// 入参订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
|
||
// 数据库订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
|
||
$params['inquiry_status_params'] = $order_product_status;
|
||
}
|
||
|
||
$params['is_delete'] = 0;
|
||
|
||
$fields = [
|
||
'order_product_id',
|
||
'order_inquiry_id',
|
||
'order_prescription_id',
|
||
'doctor_id',
|
||
'patient_id',
|
||
'family_id',
|
||
'order_product_status',
|
||
'pay_status',
|
||
'cancel_reason',
|
||
'remarks',
|
||
'refund_status',
|
||
'cancel_remarks',
|
||
'created_at',
|
||
];
|
||
|
||
$order_product = OrderProduct::getPatientOrderProductPage($params,$fields,$page,$per_page);
|
||
if (empty($order_product['data'])){
|
||
return success();
|
||
}
|
||
|
||
foreach ($order_product['data'] as $item){
|
||
foreach ($item['orderProductItem'] as &$product_item){
|
||
$product_item['product_cover_img'] = addAliyunOssWebsite($product_item['product_cover_img']);
|
||
}
|
||
}
|
||
|
||
return success($order_product);
|
||
}
|
||
|
||
/**
|
||
* 获取药品订单详情
|
||
* @return array
|
||
*/
|
||
public function getPatientProductOrderInfo(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_product_id = $this->request->route('order_product_id');
|
||
|
||
// 获取订单数据
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['order_product_id'] = $order_product_id;
|
||
$params['is_delete'] = 0;
|
||
$order_product = OrderProduct::getOne($params);
|
||
if (empty($order_product)){
|
||
return fail();
|
||
}
|
||
|
||
$params = array();
|
||
$params['order_product_id'] = $order_product['order_product_id'];
|
||
$order_product_item = OrderProductItem::getList($params);
|
||
if (empty($order_product_item)){
|
||
return fail();
|
||
}
|
||
|
||
// 处理商品图片
|
||
foreach ($order_product_item as &$item){
|
||
$item['product_cover_img'] = addAliyunOssWebsite($item['product_cover_img']);
|
||
}
|
||
|
||
if (!empty($order_product['doctor_id'])){
|
||
// 获取问诊医生数据
|
||
$fields = [
|
||
'doctor_id',
|
||
'user_name',
|
||
'multi_point_status',
|
||
'avatar',
|
||
'doctor_title',
|
||
'department_custom_name',
|
||
'be_good_at',
|
||
];
|
||
|
||
$params = array();
|
||
$params['doctor_id'] = $order_product['doctor_id'];
|
||
$user_doctor = UserDoctor::getWithHospitalOne($params,$fields);
|
||
if (empty($user_doctor)){
|
||
return fail(HttpEnumCode::SERVER_ERROR);
|
||
}
|
||
|
||
$user_doctor['avatar'] = addAliyunOssWebsite($user_doctor['avatar']);
|
||
}
|
||
|
||
$result = array();
|
||
$result['user_doctor'] = $user_doctor ?? [];
|
||
$result['order_product'] = $order_product;
|
||
$result['order_product_item'] = $order_product_item;
|
||
|
||
return success($result);
|
||
}
|
||
|
||
/**
|
||
* 药品订单取消支付
|
||
* @return array
|
||
*/
|
||
public function putPatientProductOrderCancelPay(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_product_id = $this->request->route('order_product_id');
|
||
|
||
Db::beginTransaction();
|
||
|
||
try {
|
||
// 获取药品订单数据
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['order_product_id'] = $order_product_id;
|
||
$params['is_delete'] = 0;
|
||
$order_product = OrderProduct::getOne($params);
|
||
if (empty($order_product)){
|
||
Db::rollBack();
|
||
return fail();
|
||
}
|
||
|
||
// 检测订单状态
|
||
if ($order_product['order_product_status'] != 1) {
|
||
Db::rollBack();
|
||
// 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单无法删除");
|
||
}
|
||
|
||
if ($order_product['pay_status'] != 1) {
|
||
Db::rollBack();
|
||
// 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单无法删除");
|
||
}
|
||
|
||
if (!in_array($order_product['refund_status'],[0,4,5])) {
|
||
Db::rollBack();
|
||
// 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭)
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单正在退款中");
|
||
}
|
||
|
||
// 修改订单状态
|
||
$data = array();
|
||
$data['order_product_status'] = 5;
|
||
$data['cancel_time'] = date("Y-m-d H:i:s",time());
|
||
$data['cancel_reason'] = 1; // 订单取消原因(1:主动取消 2:复核失败/库存不足 3:支付超时
|
||
$data['cancel_remarks'] = "主动取消";
|
||
$data['updated_at'] = date("Y-m-d H:i:s",time());
|
||
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_product['order_inquiry_id'];
|
||
OrderProduct::edit($params,$data);
|
||
|
||
Db::commit();
|
||
} catch (\Exception $e) {
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage());
|
||
}
|
||
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 删除药品订单
|
||
* 已收货
|
||
* 取消订单
|
||
* @return array
|
||
*/
|
||
public function deletePatientProductOrder(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_product_id = $this->request->route('order_product_id');
|
||
|
||
// 获取订单数据
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['order_product_id'] = $order_product_id;
|
||
$params['is_delete'] = 0;
|
||
$order_product = OrderProduct::getOne($params);
|
||
if (empty($order_product)){
|
||
return fail();
|
||
}
|
||
|
||
if (!in_array($order_product['order_product_status'],[4,5])) {
|
||
Db::rollBack();
|
||
// 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单无法删除");
|
||
}
|
||
|
||
// 修改订单状态
|
||
$data = array();
|
||
$data['order_product_status'] = 5;
|
||
$data['cancel_time'] = date("Y-m-d H:i:s",time());
|
||
$data['cancel_reason'] = 1; // 订单取消原因(1:主动取消 2:复核失败/库存不足 3:支付超时
|
||
$data['cancel_remarks'] = "主动取消";
|
||
$data['updated_at'] = date("Y-m-d H:i:s",time());
|
||
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_product['order_inquiry_id'];
|
||
OrderProduct::edit($params,$data);
|
||
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 获取患者订单支付数据
|
||
* @return array
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
public function getPatientOrderPayInfo(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_type = $this->request->input('order_type');
|
||
$order_no = $this->request->input('order_no');
|
||
|
||
if ($order_type == 1){
|
||
// 问诊订单
|
||
$params = array();
|
||
$params['inquiry_no'] = $order_no;
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$order_inquiry = OrderInquiry::getOne($params);
|
||
if (empty($order_inquiry)){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"非法订单");
|
||
}
|
||
|
||
// 验证订单状态
|
||
if ($order_inquiry['inquiry_status'] != 1){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单状态错误");
|
||
}
|
||
|
||
// 验证订单支付状态
|
||
if ($order_inquiry['inquiry_pay_status'] != 1){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单支付状态错误");
|
||
}
|
||
|
||
$order_id = $order_inquiry['order_inquiry_id'];
|
||
$created_at = $order_inquiry['created_at'];
|
||
$inquiry_type = $order_inquiry['inquiry_type'];
|
||
|
||
// 获取订单金额
|
||
$amount_total = $order_inquiry['amount_total'];
|
||
$payment_amount_total = $order_inquiry['payment_amount_total'];
|
||
$coupon_amount_total = $order_inquiry['coupon_amount_total'];
|
||
|
||
}elseif ($order_type == 2){
|
||
// 药品订单
|
||
$params = array();
|
||
$params['order_product_no'] = $order_no;
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$order_product = OrderProduct::getOne($params);
|
||
if (empty($order_product)){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"非法订单");
|
||
}
|
||
|
||
// 验证订单状态
|
||
if ($order_product['order_product_status'] != 1){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单状态错误");
|
||
}
|
||
|
||
// 验证订单支付状态
|
||
if ($order_product['inquiry_pay_status'] != 1){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"订单支付状态错误");
|
||
}
|
||
|
||
$order_id = $order_product['order_product_id'];
|
||
$created_at = $order_product['created_at'];
|
||
$inquiry_type = 0;
|
||
|
||
// 获取订单金额
|
||
$amount_total = $order_product['amount_total'];
|
||
$payment_amount_total = $order_product['payment_amount_total'];
|
||
$coupon_amount_total = 0;
|
||
}else{
|
||
return fail();
|
||
}
|
||
|
||
// 发起支付
|
||
$WechatPay = new WechatPay(1);
|
||
|
||
// 获取预支付交易会话标识
|
||
$prepay = $WechatPay->getJsapiPrepayId($order_no, $payment_amount_total * 100, $user_info['open_id']);
|
||
if (empty($prepay)) {
|
||
return fail(HttpEnumCode::SERVER_ERROR);
|
||
}
|
||
|
||
// 获取小程序支付配置
|
||
$pay_config = $WechatPay->getAppletsPayConfig($prepay['prepay_id']);
|
||
|
||
$result = array();
|
||
$result['amount_total'] = $amount_total; // 订单金额
|
||
$result['payment_amount_total'] = $payment_amount_total; // 实际订单金额
|
||
$result['coupon_amount_total'] = $coupon_amount_total; // 优惠金额
|
||
$result['order_no'] = $order_no; // 订单编号
|
||
$result['order_id'] = $order_id; // 订单主键id(问诊订单:order_inquiry_id 药品订单:order_product_id)
|
||
$result['created_at'] = $created_at; // 创建时间
|
||
$result['inquiry_type'] = $inquiry_type; // 订单类型(1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药)
|
||
$result['pay_config'] = $pay_config; // 小程序支付配置
|
||
|
||
return success($result);
|
||
}
|
||
|
||
// 创建药品订单
|
||
public function addPatientProductOrder(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_prescription_id = $this->request->input('order_prescription_id');
|
||
$address_id = $this->request->input('address_id');
|
||
|
||
// 获取处方数据
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['is_delete'] = 0;
|
||
$order_prescription = OrderPrescription::getOne($params);
|
||
if (empty($order_prescription)){
|
||
return fail();
|
||
}
|
||
|
||
// 验证处方状态
|
||
if ($order_prescription['prescription_status'] == 1){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"处方未审核");
|
||
}
|
||
|
||
if ($order_prescription['prescription_status'] == 3){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"已失效");
|
||
}
|
||
if ($order_prescription['prescription_status'] == 4){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"已使用");
|
||
}
|
||
|
||
if ($order_prescription['pharmacist_audit_status'] != 1){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"处方未审核");
|
||
}
|
||
|
||
// 检测是否已经存在药品订单数据
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_prescription['order_inquiry_id'];
|
||
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$order_product = OrderProduct::getExists($params);
|
||
if ($order_product){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"处方已使用");
|
||
}
|
||
|
||
// 获取处方药品数据
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
||
$order_prescription_product = OrderPrescriptionProduct::getList($params);
|
||
if (empty($order_prescription_product)){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"处方药品错误");
|
||
}
|
||
|
||
// 检测药品是否存在,库存是否足够
|
||
foreach ($order_prescription_product as $value){
|
||
$params = array();
|
||
$params['product_id'] = $value['product_id'];
|
||
$product = Product::getOne($params);
|
||
if (empty($product)){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"处方存在未知药品");
|
||
}
|
||
}
|
||
|
||
Db::beginTransaction();
|
||
try {
|
||
// 新增药品订单
|
||
$data = array();
|
||
$data['order_inquiry_id'] = $order_prescription['order_inquiry_id'];
|
||
$data['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
||
$data['doctor_id'] = $order_prescription['doctor_id'];
|
||
$data['patient_id'] = $order_prescription['patient_id'];
|
||
|
||
// 新增药品订单详情
|
||
// 修正处方使用状态
|
||
// 新增取消订单队列
|
||
|
||
Db::commit();
|
||
} catch (\Exception $e) {
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::SERVER_ERROR, $e->getMessage());
|
||
}
|
||
|
||
|
||
|
||
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 获取处方订单列表
|
||
* @return array
|
||
*/
|
||
public function getPatientPrescriptionOrderList(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
$page = $this->request->input('page',1);
|
||
$per_page = $this->request->input('per_page',10);
|
||
|
||
// 获取处方数据
|
||
$params = array();
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['pharmacist_audit_status'] = 1;
|
||
$params['platform_audit_status'] = 1;
|
||
$params['is_delete'] = 0;
|
||
$order_prescription = OrderPrescription::getWithPage($params,['*'],$page,$per_page);
|
||
if (!empty($order_prescription['data'])){
|
||
foreach ($order_prescription['data'] as &$item){
|
||
if (!empty($item['UserDoctor'])){
|
||
$item['UserDoctor']['doctor_title'] = DoctorTitleCode::getMessage($item['UserDoctor']['doctor_title']);
|
||
}
|
||
}
|
||
}
|
||
|
||
return success($order_prescription);
|
||
}
|
||
|
||
/**
|
||
* 获取处方订单详情
|
||
* @return array
|
||
*/
|
||
public function getPatientPrescriptionOrderInfo(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_prescription_id = $this->request->route('order_prescription_id');
|
||
|
||
// 获取处方数据
|
||
$fields = [
|
||
'order_prescription_id',
|
||
'prescription_status',
|
||
'prescription_img',
|
||
];
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['pharmacist_audit_status'] = 1;
|
||
$params['platform_audit_status'] = 1;
|
||
$params['is_delete'] = 0;
|
||
$order_prescription = OrderPrescription::getOne($params,$fields);
|
||
if (empty($order_prescription)){
|
||
return fail();
|
||
}
|
||
|
||
$order_prescription['prescription_img'] = addAliyunOssWebsite($order_prescription['prescription_img']);
|
||
|
||
return success($order_prescription->toArray());
|
||
}
|
||
|
||
/**
|
||
* 获取处方订单支付页详情
|
||
* @return array
|
||
*/
|
||
public function getPatientPrescriptionOrderPayInfo(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_prescription_id = $this->request->route('order_prescription_id');
|
||
|
||
$fields = [
|
||
"order_prescription_id"
|
||
];
|
||
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$params['is_delete'] = 0;
|
||
$order_prescription = OrderPrescription::getOne($params,$fields);
|
||
if (empty($order_prescription)){
|
||
return fail();
|
||
}
|
||
|
||
// 验证处方状态
|
||
if ($order_prescription['prescription_status'] == 1){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"处方未审核");
|
||
}
|
||
|
||
if ($order_prescription['prescription_status'] == 3){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"处方已失效");
|
||
}
|
||
|
||
if ($order_prescription['prescription_status'] == 4){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"处方已使用");
|
||
}
|
||
|
||
// 获取处方药品信息
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
||
$order_prescription_product = OrderPrescriptionProduct::getWithProductList($params);
|
||
if (empty($order_prescription_product)){
|
||
return fail(HttpEnumCode::SERVER_ERROR);
|
||
}
|
||
|
||
$amount_total = 0;
|
||
$coupon_amount_total = 0;
|
||
$logistics_fee = 0; // 运费金额
|
||
foreach ($order_prescription_product as $item){
|
||
if (!empty($item['Product']['product_price'])){
|
||
$amount_total += $item['Product']['product_price'];
|
||
}
|
||
}
|
||
|
||
// 获取运费金额
|
||
|
||
// 实际支付金额
|
||
$payment_amount_total = $amount_total + $logistics_fee;
|
||
|
||
// 获取收货地址
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_ship_addresss = UserShipAddress::getList($params);
|
||
|
||
foreach ($user_ship_addresss as $item){
|
||
if ($item['is_default'] == 1){
|
||
$user_ship_address = $item;
|
||
}
|
||
}
|
||
|
||
// 无默认地址,选择第一个
|
||
if (empty($user_ship_address)){
|
||
$user_ship_address = $user_ship_addresss[0] ?? [];
|
||
}
|
||
|
||
$result = array();
|
||
$result['amount_total'] = $amount_total;
|
||
$result['coupon_amount_total'] = $coupon_amount_total;
|
||
$result['payment_amount_total'] = $payment_amount_total;
|
||
$result['logistics_fee'] = $logistics_fee;
|
||
$result['user_ship_address'] = $user_ship_address;
|
||
|
||
return success($result);
|
||
}
|
||
|
||
/**
|
||
* 删除处方订单记录
|
||
* @return array
|
||
*/
|
||
public function deletePatientPrescriptionOrder(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$order_prescription_id = $this->request->route('order_prescription_id');
|
||
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
$params['patient_id'] = $user_info['client_user_id'];
|
||
$order_prescription = OrderPrescription::getOne($params);
|
||
if (empty($order_prescription)){
|
||
return fail();
|
||
}
|
||
|
||
// 验证处方状态
|
||
if ($order_prescription['prescription_status'] == 1){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"处方无法删除");
|
||
}
|
||
|
||
if ($order_prescription['is_delete'] == 1){
|
||
return success();
|
||
}
|
||
|
||
$data = array();
|
||
$data['is_delete'] = 1;
|
||
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
||
OrderPrescription::edit($params,$data);
|
||
|
||
return success();
|
||
}
|
||
} |