mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-24 20:17:32 +08:00
fix: 规范数据库事物的使用,解决在swoole环境下可能会出现事物一直不被提交的问题
This commit is contained in:
@@ -36,53 +36,51 @@ class OrderService
|
||||
if ($order->refund_amount) {
|
||||
$this->user->balance = $this->user->balance + $order->refund_amount;
|
||||
}
|
||||
DB::beginTransaction();
|
||||
if ($order->surplus_order_ids) {
|
||||
try {
|
||||
try{
|
||||
DB::beginTransaction();
|
||||
if ($order->surplus_order_ids) {
|
||||
Order::whereIn('id', $order->surplus_order_ids)->update([
|
||||
'status' => 4
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollback();
|
||||
throw new ApiException(500, '开通失败');
|
||||
}
|
||||
}
|
||||
switch ((string)$order->period) {
|
||||
case 'onetime_price':
|
||||
$this->buyByOneTime($plan);
|
||||
break;
|
||||
case 'reset_price':
|
||||
$this->buyByResetTraffic();
|
||||
break;
|
||||
default:
|
||||
$this->buyByPeriod($order, $plan);
|
||||
}
|
||||
switch ((string)$order->period) {
|
||||
case 'onetime_price':
|
||||
$this->buyByOneTime($plan);
|
||||
break;
|
||||
case 'reset_price':
|
||||
$this->buyByResetTraffic();
|
||||
break;
|
||||
default:
|
||||
$this->buyByPeriod($order, $plan);
|
||||
}
|
||||
|
||||
switch ((int)$order->type) {
|
||||
case 1:
|
||||
$this->openEvent(admin_setting('new_order_event_id', 0));
|
||||
break;
|
||||
case 2:
|
||||
$this->openEvent(admin_setting('renew_order_event_id', 0));
|
||||
break;
|
||||
case 3:
|
||||
$this->openEvent(admin_setting('change_order_event_id', 0));
|
||||
break;
|
||||
}
|
||||
switch ((int)$order->type) {
|
||||
case 1:
|
||||
$this->openEvent(admin_setting('new_order_event_id', 0));
|
||||
break;
|
||||
case 2:
|
||||
$this->openEvent(admin_setting('renew_order_event_id', 0));
|
||||
break;
|
||||
case 3:
|
||||
$this->openEvent(admin_setting('change_order_event_id', 0));
|
||||
break;
|
||||
}
|
||||
|
||||
$this->setSpeedLimit($plan->speed_limit);
|
||||
$this->setSpeedLimit($plan->speed_limit);
|
||||
|
||||
if (!$this->user->save()) {
|
||||
if (!$this->user->save()) {
|
||||
throw new \Exception('用户信息保存失败');
|
||||
}
|
||||
$order->status = 3;
|
||||
if (!$order->save()) {
|
||||
throw new \Exception('订单信息保存失败');
|
||||
}
|
||||
DB::commit();
|
||||
}catch(\Exception $e){
|
||||
DB::rollBack();
|
||||
\Log::error($e);
|
||||
throw new ApiException(500, '开通失败');
|
||||
}
|
||||
$order->status = 3;
|
||||
if (!$order->save()) {
|
||||
DB::rollBack();
|
||||
throw new ApiException(500, '开通失败');
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
}
|
||||
|
||||
|
||||
@@ -233,21 +231,25 @@ class OrderService
|
||||
public function cancel():bool
|
||||
{
|
||||
$order = $this->order;
|
||||
DB::beginTransaction();
|
||||
$order->status = 2;
|
||||
if (!$order->save()) {
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$order->status = 2;
|
||||
if (!$order->save()) {
|
||||
throw new \Exception('Failed to save order status.');
|
||||
}
|
||||
if ($order->balance_amount) {
|
||||
$userService = new UserService();
|
||||
if (!$userService->addBalance($order->user_id, $order->balance_amount)) {
|
||||
throw new \Exception('Failed to add balance.');
|
||||
}
|
||||
}
|
||||
DB::commit();
|
||||
return true;
|
||||
}catch(\Exception $e){
|
||||
DB::rollBack();
|
||||
\Log::error($e);
|
||||
return false;
|
||||
}
|
||||
if ($order->balance_amount) {
|
||||
$userService = new UserService();
|
||||
if (!$userService->addBalance($order->user_id, $order->balance_amount)) {
|
||||
DB::rollBack();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
DB::commit();
|
||||
return true;
|
||||
}
|
||||
|
||||
private function setSpeedLimit($speedLimit)
|
||||
|
||||
@@ -13,23 +13,27 @@ use Illuminate\Support\Facades\DB;
|
||||
class TicketService {
|
||||
public function reply($ticket, $message, $userId)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
$ticketMessage = TicketMessage::create([
|
||||
'user_id' => $userId,
|
||||
'ticket_id' => $ticket->id,
|
||||
'message' => $message
|
||||
]);
|
||||
if ($userId !== $ticket->user_id) {
|
||||
$ticket->reply_status = 0;
|
||||
} else {
|
||||
$ticket->reply_status = 1;
|
||||
}
|
||||
if (!$ticketMessage || !$ticket->save()) {
|
||||
try{
|
||||
DB::beginTransaction();
|
||||
$ticketMessage = TicketMessage::create([
|
||||
'user_id' => $userId,
|
||||
'ticket_id' => $ticket->id,
|
||||
'message' => $message
|
||||
]);
|
||||
if ($userId !== $ticket->user_id) {
|
||||
$ticket->reply_status = 0;
|
||||
} else {
|
||||
$ticket->reply_status = 1;
|
||||
}
|
||||
if (!$ticketMessage || !$ticket->save()) {
|
||||
throw new \Exception();
|
||||
}
|
||||
DB::commit();
|
||||
return $ticketMessage;
|
||||
}catch(\Exception $e){
|
||||
DB::rollback();
|
||||
return false;
|
||||
}
|
||||
DB::commit();
|
||||
return $ticketMessage;
|
||||
}
|
||||
|
||||
public function replyByAdmin($ticketId, $message, $userId):void
|
||||
@@ -40,22 +44,26 @@ class TicketService {
|
||||
throw new ApiException(500, '工单不存在');
|
||||
}
|
||||
$ticket->status = 0;
|
||||
DB::beginTransaction();
|
||||
$ticketMessage = TicketMessage::create([
|
||||
'user_id' => $userId,
|
||||
'ticket_id' => $ticket->id,
|
||||
'message' => $message
|
||||
]);
|
||||
if ($userId !== $ticket->user_id) {
|
||||
$ticket->reply_status = 0;
|
||||
} else {
|
||||
$ticket->reply_status = 1;
|
||||
try{
|
||||
DB::beginTransaction();
|
||||
$ticketMessage = TicketMessage::create([
|
||||
'user_id' => $userId,
|
||||
'ticket_id' => $ticket->id,
|
||||
'message' => $message
|
||||
]);
|
||||
if ($userId !== $ticket->user_id) {
|
||||
$ticket->reply_status = 0;
|
||||
} else {
|
||||
$ticket->reply_status = 1;
|
||||
}
|
||||
if (!$ticketMessage || !$ticket->save()) {
|
||||
throw new ApiException(500, '工单回复失败');
|
||||
}
|
||||
DB::commit();
|
||||
}catch(\Exception $e){
|
||||
DB::rollBack();
|
||||
throw $e;
|
||||
}
|
||||
if (!$ticketMessage || !$ticket->save()) {
|
||||
DB::rollback();
|
||||
throw new ApiException(500, '工单回复失败');
|
||||
}
|
||||
DB::commit();
|
||||
$this->sendEmailNotify($ticket, $ticketMessage);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user