478 lines
19 KiB
PHP
478 lines
19 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Constants\HttpEnumCode;
|
||
use App\Exception\BusinessException;
|
||
use App\Model\OrderInquiry;
|
||
use App\Model\OrderInquiryCoupon;
|
||
use App\Model\OrderInquiryRefund;
|
||
use App\Model\OrderPrescription;
|
||
use App\Model\OrderPrescriptionProduct;
|
||
use App\Model\OrderProduct;
|
||
use App\Model\OrderProductCoupon;
|
||
use App\Model\OrderProductItem;
|
||
use App\Model\OrderProductRefund;
|
||
use App\Model\OrderServicePackageDetail;
|
||
use App\Model\OrderServicePackageInquiry;
|
||
use App\Model\OrderServicePackageProduct;
|
||
use App\Model\Product;
|
||
use App\Model\ProductPlatformAmount;
|
||
use App\Model\UserPatient;
|
||
use Extend\Wechat\WechatPay;
|
||
use Hyperf\DbConnection\Db;
|
||
use Hyperf\Snowflake\IdGeneratorInterface;
|
||
use Psr\Container\ContainerExceptionInterface;
|
||
use Psr\Container\NotFoundExceptionInterface;
|
||
|
||
class OrderProductService extends BaseService
|
||
{
|
||
/**
|
||
* 获取患者某一状态下的商品订单数量
|
||
* @param string $patient_id 患者id
|
||
* @param int $order_product_status 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已完成 6:已取消)
|
||
* @return int
|
||
*/
|
||
public function getPatientProductWithStatus(string $patient_id,int $order_product_status): int
|
||
{
|
||
$params = array();
|
||
$params['patient_id'] = $patient_id;
|
||
$params['order_product_status'] = $order_product_status;
|
||
return OrderProduct::getCount($params);
|
||
}
|
||
|
||
/**
|
||
* 取消未支付的药品订单
|
||
* 外层需加事物
|
||
* @param string|int $order_no 订单编号
|
||
* @param string|int $cancel_reason 订单取消原因(1:主动取消 2:复核失败/库存不足 3:支付超时
|
||
* @param string $cancel_remarks 订单取消原因
|
||
* @return array
|
||
*/
|
||
public function cancelUnpayProductOrder(string|int $order_no,string|int $cancel_reason,string $cancel_remarks): array
|
||
{
|
||
$result = array();
|
||
$result['status'] = 1;
|
||
$result['message'] = "成功";
|
||
|
||
// 获取药品订单数据
|
||
$params = array();
|
||
$params['order_product_no'] = $order_no;
|
||
$order_product = OrderProduct::getOne($params);
|
||
if (empty($order_product)){
|
||
$result['status'] = 0;
|
||
$result['message'] = "未查询到对应订单数据";
|
||
return $result;
|
||
}
|
||
|
||
// 检测订单状态
|
||
if ($order_product['order_product_status'] == 5){
|
||
// 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
|
||
$result['status'] = 0;
|
||
$result['message'] = "订单已取消";
|
||
return $result;
|
||
}
|
||
|
||
if ($order_product['order_product_status'] != 1){
|
||
// 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
|
||
$result['status'] = 0;
|
||
$result['message'] = "订单取消失败";
|
||
return $result;
|
||
}
|
||
|
||
if (!in_array($order_product['refund_status'],[0,4,5])) {
|
||
// 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭)
|
||
$result['status'] = 0;
|
||
$result['message'] = "订单正在退款中";
|
||
return $result;
|
||
}
|
||
|
||
if ($order_product['pay_status'] == 2) {
|
||
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||
$result['status'] = 0;
|
||
$result['message'] = "订单已支付";
|
||
return $result;
|
||
}
|
||
|
||
// 获取患者数据
|
||
$params = array();
|
||
$params['patient_id'] = $order_product['patient_id'];
|
||
$user_patient = UserPatient::getOne($params);
|
||
if (empty($user_patient)){
|
||
$result['status'] = 0;
|
||
$result['message'] = "未查询到对应用户数据";
|
||
return $result;
|
||
}
|
||
|
||
// 获取问诊订单数据
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_product['order_inquiry_id'];
|
||
$order_inquiry = OrderInquiry::getOne($params);
|
||
if (empty($order_inquiry)){
|
||
$result['status'] = 0;
|
||
$result['message'] = "未查询到对应订单数据";
|
||
return $result;
|
||
}
|
||
|
||
// 取消药品订单
|
||
$data = array();
|
||
$data['order_product_status'] = 5;
|
||
|
||
if ($cancel_reason == 3){
|
||
$data['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; // 取消订单原因(1:医生未接诊 2:主动取消 3:无可分配医生 4:客服取消 5:支付超时)
|
||
$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);
|
||
|
||
// 获取订单商品订单列表
|
||
$params = array();
|
||
$params['order_product_id'] = $order_product['order_product_id'];
|
||
$order_product_item = OrderProductItem::getList($params);
|
||
if (empty($order_product_item)){
|
||
$result['status'] = 0;
|
||
$result['message'] = "未查询到对应订单商品订单列表";
|
||
return $result;
|
||
}
|
||
|
||
foreach ($order_product_item as $item){
|
||
// 释放锁定库存
|
||
$params = array();
|
||
$params['product_id'] = $item['product_id'];
|
||
$product = Product::getOne($params);
|
||
if (empty($product)){
|
||
$result['status'] = 0;
|
||
$result['message'] = "未查询到对应订单商品订单列表";
|
||
return $result;
|
||
}
|
||
|
||
$params = array();
|
||
$params['product_platform_code'] = $product['product_platform_code'];
|
||
$product_platform_amount = ProductPlatformAmount::getSharedLockOne($params);
|
||
if (empty($product_platform_amount)){
|
||
$result['status'] = 0;
|
||
$result['message'] = "无商品库存数据";
|
||
return $result;
|
||
}
|
||
|
||
// 库存+订单商品数量
|
||
$params = array();
|
||
$params['amount_id'] = $product_platform_amount['amount_id'];
|
||
ProductPlatformAmount::inc($params, 'stock', (float)$item['amount']);
|
||
|
||
// 获取患者家庭成员进行中的服务包订单-健康包
|
||
$OrderServicePackageService = new OrderServicePackageService();
|
||
$order_service_package = $OrderServicePackageService->getPatientFamilyInProgressServicePackage($order_inquiry['user_id'],$order_inquiry['family_id'],$order_inquiry['doctor_id'],1);
|
||
if (!empty($order_service_package)){
|
||
// 回退服务包已使用药品数量
|
||
$params = array();
|
||
$params['order_service_id'] = $order_service_package['order_service_id'];
|
||
$params['order_product_id'] = $item['order_product_id'];
|
||
$params['product_item_id'] = $item['product_item_id'];
|
||
$params['product_id'] = $item['product_id'];
|
||
$order_service_package_product = OrderServicePackageProduct::getOne($params);
|
||
if (!empty($order_service_package_product)){
|
||
$params = array();
|
||
$params['service_product_id'] = $order_service_package_product['service_product_id'];
|
||
|
||
$data = array();
|
||
$data['used_quantity'] = 0;
|
||
OrderServicePackageProduct::edit($params,$data);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 获取处方数据
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_product['order_prescription_id'];
|
||
$order_prescription = OrderPrescription::getOne($params);
|
||
if (empty($order_prescription)){
|
||
$result['status'] = 0;
|
||
$result['message'] = "未查询到对应订单处方";
|
||
return $result;
|
||
}
|
||
|
||
// 修改处方状态为未使用
|
||
if ($order_prescription['prescription_status'] == 4){
|
||
$data = array();
|
||
$data['prescription_status'] = 2;
|
||
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
||
OrderPrescription::edit($params, $data);
|
||
}
|
||
|
||
// 获取处方商品数据
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
||
$order_prescription_product = OrderPrescriptionProduct::getList($params);
|
||
if (empty($order_prescription_product)) {
|
||
$result['status'] = 0;
|
||
$result['message'] = "未查询到对应订单处方商品数据";
|
||
return $result;
|
||
}
|
||
|
||
// 修改处方商品为未使用
|
||
foreach ($order_prescription_product as $item){
|
||
$data = array();
|
||
$data['use_status'] = 0;
|
||
|
||
$params = array();
|
||
$params['prescription_product_id'] = $item['prescription_product_id'];
|
||
OrderPrescriptionProduct::edit($params, $data);
|
||
}
|
||
|
||
// 处理订单优惠卷
|
||
if (!empty($order_product['coupon_amount_total']) && $order_product['coupon_amount_total'] > 0) {
|
||
// 获取该订单全部优惠卷数据
|
||
$params = array();
|
||
$params['order_product_id'] = $order_product['order_product_id'];
|
||
$order_product_coupons = OrderProductCoupon::getList($params);
|
||
if (!empty($order_product_coupons)){
|
||
$userCouponService = new UserCouponService();
|
||
foreach ($order_product_coupons as $order_product_coupon){
|
||
// 退还优惠卷
|
||
$userCouponService->returnUserCoupon($order_product_coupon['user_coupon_id']);
|
||
|
||
// 发送站内消息-优惠卷退还
|
||
$MessagePush = new MessagePush($user_patient['user_id']);
|
||
$MessagePush->patientRefundCoupon($order_product_coupon['coupon_name']);
|
||
}
|
||
}
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 药品订单退款
|
||
* @param string $order_product_id 药品订单id
|
||
* @param string $refund_reason 退款原因
|
||
* @return void
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
/*public function OrderProductRefund(string $order_product_id, string $refund_reason): void
|
||
{
|
||
// 获取药品订单数据
|
||
$params = array();
|
||
$params['order_product_id'] = $order_product_id;
|
||
$order_product = OrderProduct::getOne($params);
|
||
if (empty($order_product)){
|
||
throw new BusinessException("订单数据为空");
|
||
}
|
||
|
||
// 检测药品订单数据状态
|
||
if ($order_product['order_product_status'] == 1){
|
||
throw new BusinessException("订单未支付");
|
||
}
|
||
|
||
if ($order_product['order_product_status'] == 4){
|
||
throw new BusinessException("订单已签收");
|
||
}
|
||
|
||
// 检测商品订单退款状态
|
||
if ($order_product['refund_status'] == 2){
|
||
// 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
|
||
throw new BusinessException("订单退款中");
|
||
}
|
||
|
||
if ($order_product['refund_status'] == 3){
|
||
// 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
|
||
throw new BusinessException("订单已退款成功");
|
||
}
|
||
|
||
if ($order_product['refund_status'] == 5){
|
||
// 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
|
||
throw new BusinessException("订单退款关闭");
|
||
}
|
||
|
||
// 检测支付状态
|
||
if ($order_product['pay_status'] != 2){
|
||
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||
throw new BusinessException("订单支付状态错误,无法退款");
|
||
}
|
||
|
||
// 获取患者数据
|
||
$params = array();
|
||
$params['patient_id'] = $order_product['patient_id'];
|
||
$user_patient = UserPatient::getOne($params);
|
||
if (empty($user_patient)){
|
||
throw new BusinessException("未查询到对应用户数据");
|
||
}
|
||
|
||
// 系统退款编号
|
||
$generator = $this->container->get(IdGeneratorInterface::class);
|
||
$product_refund_no = $generator->generate();
|
||
|
||
// 检测订单金额
|
||
if ($order_product['payment_amount_total'] <= 0){
|
||
throw new BusinessException("订单金额错误");
|
||
}
|
||
|
||
// 发起退款
|
||
$WechatPay = new WechatPay(1, 2);
|
||
|
||
$options = array();
|
||
$options['transaction_id'] = $order_product['escrow_trade_no'];
|
||
$options['out_refund_no'] = (string)$product_refund_no;
|
||
$options['reason'] = $refund_reason;
|
||
$options['amount'] = [
|
||
'refund' => (int)($order_product['payment_amount_total'] * 100),
|
||
'total' => (int)($order_product['payment_amount_total'] * 100),
|
||
'currency' => "CNY",
|
||
];
|
||
|
||
$result = $WechatPay->refund($options);
|
||
|
||
// 处理订单退款状态
|
||
// 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
|
||
$success_time = "";
|
||
if ($result['status'] == "SUCCESS") {
|
||
// 退款成功
|
||
$refund_status = 3;
|
||
$success_time = $result['success_time'];
|
||
} elseif ($result['status'] == "CLOSED") {
|
||
// 退款关闭
|
||
$refund_status = 5;
|
||
} elseif ($result['status'] == "PROCESSING") {
|
||
// 退款处理中
|
||
$refund_status = 2;
|
||
} elseif ($result['status'] == "ABNORMAL") {
|
||
// 退款异常,此情况不处理,进行短信通知
|
||
throw new BusinessException("订单退款状态异常");
|
||
} else {
|
||
throw new BusinessException("订单退款状态错误");
|
||
}
|
||
|
||
$refund_id = $result['refund_id'];
|
||
|
||
// 新增退款表
|
||
$data = array();
|
||
$data['patient_id'] = $order_product['patient_id'];
|
||
$data['order_product_id'] = $order_product['order_product_id'];
|
||
$data['order_product_no'] = $order_product['order_product_no'];
|
||
$data['product_refund_no'] = $product_refund_no;
|
||
$data['refund_id'] = $refund_id;
|
||
$data['product_refund_status'] = $refund_status;
|
||
$data['refund_total'] = $order_product['payment_amount_total'];
|
||
$data['refund_reason'] = $refund_reason;
|
||
|
||
if ($refund_status == 3 && !empty($success_time)) {
|
||
$data['success_time'] = date("Y-m-d H:i:s", strtotime($success_time)); // 退款成功时间
|
||
}
|
||
|
||
$order_product_refund = OrderProductRefund::addOrderProductRefund($data);
|
||
if (empty($order_product_refund)) {
|
||
throw new BusinessException("添加退款表失败");
|
||
}
|
||
|
||
// 修改药品订单表状态
|
||
$data = array();
|
||
$data['refund_status'] = $refund_status;
|
||
|
||
$params = array();
|
||
$params['order_product_id'] = $order_product['order_product_id'];
|
||
OrderProduct::edit($params,$data);
|
||
}*/
|
||
|
||
/**
|
||
* 药品订单退款
|
||
* @param string $order_product_id 药品订单id
|
||
* @param string $refund_reason 退款原因
|
||
* @return void
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
public function OrderProductRefund(string $order_product_id, string $refund_reason): void
|
||
{
|
||
// 获取药品订单数据
|
||
$params = array();
|
||
$params['order_product_id'] = $order_product_id;
|
||
$order_product = OrderProduct::getOne($params);
|
||
if (empty($order_product)){
|
||
throw new BusinessException("订单数据为空");
|
||
}
|
||
|
||
// 系统退款编号
|
||
$generator = $this->container->get(IdGeneratorInterface::class);
|
||
$product_refund_no = $generator->generate();
|
||
|
||
// 检测订单金额
|
||
if ($order_product['payment_amount_total'] <= 0){
|
||
throw new BusinessException("订单金额错误");
|
||
}
|
||
|
||
// 发起退款
|
||
$WechatPay = new WechatPay(1, 2);
|
||
|
||
$options = array();
|
||
$options['transaction_id'] = $order_product['escrow_trade_no'];
|
||
$options['out_refund_no'] = (string)$product_refund_no;
|
||
$options['reason'] = $refund_reason;
|
||
$options['amount'] = [
|
||
'refund' => (int)($order_product['payment_amount_total'] * 100),
|
||
'total' => (int)($order_product['payment_amount_total'] * 100),
|
||
'currency' => "CNY",
|
||
];
|
||
|
||
$result = $WechatPay->refund($options);
|
||
|
||
// 处理订单退款状态
|
||
// 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
|
||
$success_time = "";
|
||
if ($result['status'] == "SUCCESS") {
|
||
// 退款成功
|
||
$refund_status = 3;
|
||
$success_time = $result['success_time'];
|
||
} elseif ($result['status'] == "CLOSED") {
|
||
// 退款关闭
|
||
$refund_status = 5;
|
||
} elseif ($result['status'] == "PROCESSING") {
|
||
// 退款处理中
|
||
$refund_status = 2;
|
||
} elseif ($result['status'] == "ABNORMAL") {
|
||
// 退款异常,此情况不处理,进行短信通知
|
||
throw new BusinessException("订单退款状态异常");
|
||
} else {
|
||
throw new BusinessException("订单退款状态错误");
|
||
}
|
||
|
||
$refund_id = $result['refund_id'];
|
||
|
||
// 新增退款表
|
||
$data = array();
|
||
$data['patient_id'] = $order_product['patient_id'];
|
||
$data['order_product_id'] = $order_product['order_product_id'];
|
||
$data['order_product_no'] = $order_product['order_product_no'];
|
||
$data['product_refund_no'] = $product_refund_no;
|
||
$data['refund_id'] = $refund_id;
|
||
$data['product_refund_status'] = $refund_status;
|
||
$data['refund_total'] = $order_product['payment_amount_total'];
|
||
$data['refund_reason'] = $refund_reason;
|
||
|
||
if ($refund_status == 3 && !empty($success_time)) {
|
||
$data['success_time'] = date("Y-m-d H:i:s", strtotime($success_time)); // 退款成功时间
|
||
}
|
||
|
||
$order_product_refund = OrderProductRefund::addOrderProductRefund($data);
|
||
if (empty($order_product_refund)) {
|
||
throw new BusinessException("添加退款表失败");
|
||
}
|
||
|
||
// 修改药品订单表状态
|
||
$data = array();
|
||
$data['refund_status'] = $refund_status;
|
||
|
||
$params = array();
|
||
$params['order_product_id'] = $order_product['order_product_id'];
|
||
OrderProduct::edit($params,$data);
|
||
}
|
||
|
||
} |