Skip to content

Commit

Permalink
激活卡时冲抵基础账户负数
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Sep 15, 2023
1 parent fd912a3 commit a35c05a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
38 changes: 38 additions & 0 deletions server/Module/Card/Aop/OffsetBaseCard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace app\Module\Card\Aop;

use app\Module\Card\Model\Card;
use app\Module\Card\Service\CardService;
use app\Module\Card\Service\MemberCardService;
use Imi\Aop\Annotation\Around;
use Imi\Aop\Annotation\Aspect;
use Imi\Aop\Annotation\PointCut;
use Imi\Aop\AroundJoinPoint;
use Imi\Aop\PointCutType;
use Imi\App;

/**
* 冲抵基础账户.
*/
#[Aspect()]
class OffsetBaseCard
{
#[
PointCut(type: PointCutType::METHOD, allow: [
CardService::class . '::activation',
]),
Around()
]
public function parse(AroundJoinPoint $point): mixed
{
/** @var Card $card */
$card = $point->proceed();
$service = App::getBean(MemberCardService::class);
$service->offsetBaseCard($card);

return $card;
}
}
3 changes: 3 additions & 0 deletions server/Module/Card/Enum/OperationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ class OperationType extends BaseEnum

#[EnumItem(__data: ['deduct' => false], text: '激活卡')]
public const ACTIVATION_CARD = 4;

#[EnumItem(__data: ['deduct' => false], text: '冲抵基础账户')]
public const OFFSET_BASE_CARD = 5;
}
26 changes: 26 additions & 0 deletions server/Module/Card/Service/MemberCardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ public function giftMemberBaseCard(int $memberId, int $amount, int $businessType
return $this->gift($this->getMemberBaseCardId($memberId), $amount, $businessType);
}

public function getMemberBaseCard(int $memberId): Card
{
return $this->cardService->get($this->getMemberBaseCardId($memberId));
}

public function getOrder(string|int $orderId): MemberCardOrder
{
if (\is_string($orderId))
Expand Down Expand Up @@ -481,4 +486,25 @@ public function adminDetails(int $memberId = 0, int $operationType = 0, int $bus

return $query->order('id', 'desc')->paginate($page, $limit)->toArray();
}

#[Transaction()]
public function offsetBaseCard(Card $card): void
{
if ($card->memberId <= 0)
{
return;
}
$baseCard = $this->getMemberBaseCard($card->memberId);
if ($baseCard->leftAmount < 0)
{
$cardDeductAmount = min($card->leftAmount, abs($baseCard->leftAmount));
if ($cardDeductAmount > 0)
{
// 基础卡加钱
$this->cardService->change($baseCard->id, OperationType::OFFSET_BASE_CARD, $cardDeductAmount);
// 卡扣钱
$this->cardService->change($card->id, OperationType::OFFSET_BASE_CARD, -$cardDeductAmount);
}
}
}
}
9 changes: 9 additions & 0 deletions server/Module/Chat/ApiController/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace app\Module\Chat\ApiController;

use app\Exception\BaseException;
use app\Module\Chat\Enum\SessionType;
use app\Module\Chat\Model\ChatSession;
use app\Module\Chat\Model\Redis\ChatConfig;
Expand Down Expand Up @@ -97,6 +98,14 @@ protected function task(): void
'finishReason' => 'rateLimit',
])));
}
catch (BaseException $baseException)
{
Log::error($baseException);
$handler->send((string) new SseMessageEvent(json_encode([
'content' => SecureFieldUtil::encode($baseException->getMessage()),
'finishReason' => 'error',
])));
}
catch (\Throwable $th)
{
Log::error($th);
Expand Down
9 changes: 9 additions & 0 deletions server/Module/Embedding/ApiController/EmbeddingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace app\Module\Embedding\ApiController;

use app\Exception\BaseException;
use app\Module\Embedding\Enum\PublicProjectStatus;
use app\Module\Embedding\Model\EmbeddingProject;
use app\Module\Embedding\Model\EmbeddingQa;
Expand Down Expand Up @@ -259,6 +260,14 @@ protected function task(): void
'finishReason' => 'rateLimit',
])));
}
catch (BaseException $baseException)
{
Log::error($baseException);
$handler->send((string) new SseMessageEvent(json_encode([
'content' => SecureFieldUtil::encode($baseException->getMessage()),
'finishReason' => 'error',
])));
}
catch (\Throwable $th)
{
Log::error($th);
Expand Down

0 comments on commit a35c05a

Please sign in to comment.