llp 5 дней назад
Родитель
Сommit
bbf8a18568

+ 4 - 3
packages/Longyi/Gift/src/Http/Controllers/Shop/GiftController.php

@@ -38,10 +38,11 @@ class GiftController extends Controller
     public function add($request): JsonResponse
     {
         $id = $request->input('id');
-        $success = $this->giftCardsModel->add($id);
+        $customerId = auth()->user()->id;
+        $result = $this->giftCardsModel->add($id, $customerId);
         return response()->json([
-            'success' => $success,
-            'message' => $success ? 'Gift card added successfully' : 'Failed to add gift card',
+            'success' => $result ===  true ?: false,
+            'message' => $result === true ? 'Gift card added successfully' : $result,
         ]);
     }
     /**

+ 12 - 3
packages/Longyi/Gift/src/Models/GiftCards.php

@@ -7,6 +7,8 @@ use Longyi\Gift\Models\GiftCardUsageLog;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 use Longyi\Gift\Contracts\GiftCards as GiftCardsContract;
+use Longyi\RewardPoints\Config\TransactionType;
+use Longyi\RewardPoints\Repositories\RewardPointRepository;
 
 class GiftCards extends Model implements GiftCardsContract
 {
@@ -210,7 +212,7 @@ class GiftCards extends Model implements GiftCardsContract
         ];
     }
 
-    public function add($id)
+    public function add($id, $customerId)
     {
         $gifts = $this->giftcards();
         $giftInfo = $gifts[$id] ?? null;
@@ -219,11 +221,18 @@ class GiftCards extends Model implements GiftCardsContract
         }
         try {
             DB::beginTransaction();
-            self::addGiftCard($id, $giftInfo['amount'], $giftInfo['num'], $giftInfo['expirationdate'], $giftInfo['channel'], $giftInfo['notes']);
+            self::addGiftCard($customerId, $giftInfo['amount'], $giftInfo['num'], $giftInfo['expirationdate'], $giftInfo['channel'], $giftInfo['notes']);
+            $rewardPointRepository = app(RewardPointRepository::class);//积分兑换
+            $rewardPointRepository->deductPoints(
+                $customerId,
+                TransactionType::GIFT_CARD_REDEEM,
+                $giftInfo['points'],
+                '积分兑换礼品卡'
+            );
             DB::commit();
         } catch (\Exception $e) {
             DB::rollBack();
-            throw $e;
+            return $e->getMessage();
         }
         return true;
     }