From a35c05a0f2e36aeeb77609a1e0353eb99b1aa3cc Mon Sep 17 00:00:00 2001 From: Yurun Date: Fri, 15 Sep 2023 10:32:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=BF=80=E6=B4=BB=E5=8D=A1=E6=97=B6=E5=86=B2?= =?UTF-8?q?=E6=8A=B5=E5=9F=BA=E7=A1=80=E8=B4=A6=E6=88=B7=E8=B4=9F=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/Module/Card/Aop/OffsetBaseCard.php | 38 +++++++++++++++++++ server/Module/Card/Enum/OperationType.php | 3 ++ .../Module/Card/Service/MemberCardService.php | 26 +++++++++++++ .../Chat/ApiController/ChatController.php | 9 +++++ .../ApiController/EmbeddingController.php | 9 +++++ 5 files changed, 85 insertions(+) create mode 100644 server/Module/Card/Aop/OffsetBaseCard.php diff --git a/server/Module/Card/Aop/OffsetBaseCard.php b/server/Module/Card/Aop/OffsetBaseCard.php new file mode 100644 index 0000000..6da4c09 --- /dev/null +++ b/server/Module/Card/Aop/OffsetBaseCard.php @@ -0,0 +1,38 @@ +proceed(); + $service = App::getBean(MemberCardService::class); + $service->offsetBaseCard($card); + + return $card; + } +} diff --git a/server/Module/Card/Enum/OperationType.php b/server/Module/Card/Enum/OperationType.php index 26ee69b..75fc1a4 100644 --- a/server/Module/Card/Enum/OperationType.php +++ b/server/Module/Card/Enum/OperationType.php @@ -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; } diff --git a/server/Module/Card/Service/MemberCardService.php b/server/Module/Card/Service/MemberCardService.php index 97df666..49e4e81 100644 --- a/server/Module/Card/Service/MemberCardService.php +++ b/server/Module/Card/Service/MemberCardService.php @@ -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)) @@ -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); + } + } + } } diff --git a/server/Module/Chat/ApiController/ChatController.php b/server/Module/Chat/ApiController/ChatController.php index 300ff1b..ada9ee9 100644 --- a/server/Module/Chat/ApiController/ChatController.php +++ b/server/Module/Chat/ApiController/ChatController.php @@ -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; @@ -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); diff --git a/server/Module/Embedding/ApiController/EmbeddingController.php b/server/Module/Embedding/ApiController/EmbeddingController.php index 0ff5a27..cedaa1d 100644 --- a/server/Module/Embedding/ApiController/EmbeddingController.php +++ b/server/Module/Embedding/ApiController/EmbeddingController.php @@ -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; @@ -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);