You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

168 lines
5.4 KiB

<?php
namespace App\JsonRpc;
use App\Commons\Log;
use App\Constants\v3\ErrorCode;
use App\Constants\v3\LogLabel;
use App\Constants\v3\OrderState;
use App\Exception\ErrorCodeException;
use App\Model\v3\Order;
use App\Model\v3\OrderMain;
use App\Service\v3\Interfaces\OrderOnlineServiceInterface;
use App\Service\v3\Interfaces\SeparateAccountsServiceInterface;
use Hyperf\DbConnection\Db;
use Hyperf\RpcServer\Annotation\RpcService;
use Hyperf\Di\Annotation\Inject;
use function AlibabaCloud\Client\json;
/**
* @RpcService(name="OrderService", protocol="jsonrpc-http", server="jsonrpc-http", publishTo="")
*/
class OrderService implements OrderServiceInterface
{
/**
* @Inject
* @var Log
*/
protected $log;
/**
* @Inject
* @var OrderOnlineServiceInterface
*/
protected $orderOnlineService;
/**
* @Inject
* @var SeparateAccountsServiceInterface
*/
protected $separateAccountsService;
/**
* 订单完成
* @param $global_order_id
* @param $user_id
* @return array
*/
public function onlineComplete($global_order_id, $user_id)
{
Db::beginTransaction();
try {
$this->orderOnlineService->doComplete($global_order_id, $user_id);
$this->separateAccountsService->orderOnlineCompleted($global_order_id, $user_id);
Db::commit();
return [
"status" => 200,
"code" => 0,
"result" => [],
"message" => '处理成功'
];
} catch (\Exception $e) {
Db::rollBack();
$this->log->event(LogLabel::ORDER_COMPLETE_LOG, ['jsonrpc_order_service_exception_onlineComplete' => $e->getMessage(), 'params' => json([$global_order_id, $user_id])]);
throw new ErrorCodeException(ErrorCode::ORDER_COMPLETE_FAIL);
}
}
/**
* 线上订单退款,整个订单退,这个是专门用于处理用户的申请退款的同意退款操作
* @param $global_order_id
* @param $user_id
* @return array
*/
public function onlineRefund($global_order_id, $user_id)
{
Db::beginTransaction();
try {
$result = $this->orderOnlineService->doRefund($global_order_id, $user_id);
Db::commit();
if ($result['return_code'] == 'SUCCESS' && isset($result['result_code']) && $result['result_code'] == "SUCCESS") {
return [
"status" => 200,
"code" => 0,
"result" => [],
"message" => '处理成功'
];
} else {
return [
"status" => 200,
"code" => -1,
"result" => [],
"message" => $result['err_code_des']
];
}
} catch (\Exception $e) {
Db::rollBack();
$this->log->event(LogLabel::ORDER_REFUND_LOG, ['jsonrpc_order_service_exception_onlineRefund' => $e->getMessage(), 'params' => json([$global_order_id, $user_id])]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
}
/**
* 线上订单单笔退款,主要用于后台强行操作退单退款
* 支持单商品、单店、整单
* 按比例计算红包进行退款
* 比如:两个子订单和子订单商品,分别是2元,98元,使用了10元优惠券
* 退2元商品时,退款金额为
* 红包 :(2/(98+2))*10 = 0.2
* 退款:2-0.2=1.8元
* @param $user_id *用户ID
* @param $global_order_id *全局总订单ID
* @param $child_order_id *主订单ID,
* @param $order_goods_id *订单商品ID
* @param $note
*/
public function onlineSingleRefund($user_id, $note, $global_order_id, $child_order_id = null, $order_goods_id = null)
{
if (!$user_id || !$global_order_id || !$note) {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对',
'params' => json([$global_order_id, $user_id, $note])
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
// 主订单
$orderMain = OrderMain::query()
->where(['global_order_id' => $global_order_id])
->whereIn('state', OrderState::CAN_REFUND_DIRECT)
->first();
if (empty($orderMain)) {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在',
'params' => json([$global_order_id, $user_id, $note])
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
// 子订单
if ($child_order_id) {
$orderChild = Order::query()->where(['order_main_id' => $orderMain->global_order_id])->first();
}
// 单商品退
if ($order_goods_id) {
if (!$child_order_id) {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对[单品]',
'params' => json([$global_order_id, $user_id, $note, $child_order_id])
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
}
}
}