Przeglądaj źródła

添加公共用户plus方法

llp 3 miesięcy temu
rodzic
commit
d0e0f0fc15
1 zmienionych plików z 116 dodań i 0 usunięć
  1. 116 0
      packages/Longyi/Member/src/Models/MemberLog.php

+ 116 - 0
packages/Longyi/Member/src/Models/MemberLog.php

@@ -5,6 +5,9 @@ namespace Longyi\Member\Models;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 use Longyi\Member\Contracts\MemberLog as MemberLogContract;
+use Webkul\Customer\Models\Customer;
+use Carbon\Carbon;
+use Illuminate\Support\Facades\DB;
 
 class MemberLog extends Model implements MemberLogContract
 {
@@ -36,4 +39,117 @@ class MemberLog extends Model implements MemberLogContract
         'amount' => 'decimal:2',
         'expirationdate' => 'date',
     ];
+
+    /**
+     * Add VIP membership to a customer.
+     *
+     * @param int $customerId Customer ID
+     * @param mixed $duration Duration in days (int) or specific date (string/Carbon instance)
+     * @param string $orderId Optional order ID for reference
+     * @param float $amount Optional amount for record
+     * @param int $type Type of membership addition (default: 1 for manual/admin addition)
+     * @return bool Success status
+     * @throws \Exception If customer not found or invalid parameters
+     */
+    public static function addVipMembership(
+        int $customerId,
+            $duration,
+        string $orderId = null,
+        float $amount = 0,
+        int $type = 1
+    ): bool {
+        if ($duration <= 0) {
+            throw new \Exception('Duration must be greater than 0');
+        }
+
+        $customer = Customer::find($customerId);
+        if (!$customer) {
+            throw new \Exception("Customer with ID {$customerId} not found");
+        }
+
+        DB::beginTransaction();
+        try {
+            // Calculate new expiration date
+            $currentExpireDate = $customer->vip_expire_date
+                ? Carbon::parse($customer->vip_expire_date)
+                : now();
+
+            // Determine the new expiration date based on duration type
+            if ($duration instanceof Carbon) {
+                // If Carbon instance is provided, use it directly
+                $newExpireDate = $duration;
+            } elseif (is_string($duration)) {
+                // If string is provided, parse it as a date
+                $newExpireDate = Carbon::parse($duration);
+            } elseif (is_int($duration)) {
+                // If integer is provided, treat as days
+                if ($currentExpireDate->isPast()) {
+                    $newExpireDate = now()->addDays($duration);
+                } else {
+                    $newExpireDate = $currentExpireDate->addDays($duration);
+                }
+            } else {
+                throw new \Exception('Invalid duration type. Must be integer (days), string (date), or Carbon instance');
+            }
+
+            // Update customer VIP expiration date
+            $customer->vip_expire_date = $newExpireDate;
+            $customer->save();
+
+            // Create member log record
+            self::create([
+                'customer_id' => $customerId,
+                'order_id' => $orderId,
+                'type' => $type,
+                'amount' => $amount,
+                'expirationdate' => $newExpireDate,
+            ]);
+
+            DB::commit();
+            return true;
+        } catch (\Exception $e) {
+            DB::rollBack();
+            throw $e;
+        }
+    }
+
+    /**
+     * Add VIP membership by days (convenience method).
+     *
+     * @param int $customerId Customer ID
+     * @param int $days Number of days to extend
+     * @param string $orderId Optional order ID
+     * @param float $amount Optional amount
+     * @param int $type Type
+     * @return bool Success status
+     */
+    public static function addVipByDays(
+        int $customerId,
+        int $days,
+        string $orderId = null,
+        float $amount = 0,
+        int $type = 1
+    ): bool {
+        return self::addVipMembership($customerId, $days, $orderId, $amount, $type);
+    }
+
+    /**
+     * Add VIP membership to a specific date (convenience method).
+     *
+     * @param int $customerId Customer ID
+     * @param string|Carbon $expireDate Expiration date
+     * @param string $orderId Optional order ID
+     * @param float $amount Optional amount
+     * @param int $type Type
+     * @return bool Success status
+     */
+    public static function addVipToDate(
+        int $customerId,
+            $expireDate,
+        string $orderId = null,
+        float $amount = 0,
+        int $type = 1
+    ): bool {
+        return self::addVipMembership($customerId, $expireDate, $orderId, $amount, $type);
+    }
 }