|
|
@@ -2,8 +2,8 @@
|
|
|
|
|
|
namespace Longyi\Gift\Models;
|
|
|
|
|
|
-use Brainstream\Giftcard\Models\GiftCard;
|
|
|
-use Brainstream\Giftcard\Models\GiftCardBalance;
|
|
|
+use Illuminate\Support\Str;
|
|
|
+use Longyi\Gift\Models\GiftCardUsageLog;
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
use Longyi\Gift\Contracts\GiftCards as GiftCardsContract;
|
|
|
@@ -20,6 +20,7 @@ class GiftCards extends Model implements GiftCardsContract
|
|
|
'used_giftcard_amount',
|
|
|
'remaining_giftcard_amount',
|
|
|
'customer_id',
|
|
|
+ 'channel',
|
|
|
'expirationdate',
|
|
|
'giftcard_status',
|
|
|
];
|
|
|
@@ -30,8 +31,23 @@ class GiftCards extends Model implements GiftCardsContract
|
|
|
'remaining_giftcard_amount' => 'decimal:2',
|
|
|
'customer_id' => 'integer',
|
|
|
'expirationdate' => 'date',
|
|
|
+ 'channel' => 'string',
|
|
|
];
|
|
|
+ protected static function boot()
|
|
|
+ {
|
|
|
+ parent::boot();
|
|
|
+
|
|
|
+ static::created(function ($giftCard) {
|
|
|
|
|
|
+ GiftCardUsageLog::create([
|
|
|
+ 'giftcard_number' => $giftCard->giftcard_number,
|
|
|
+ 'customer_id' => $giftCard->customer_id,
|
|
|
+ 'balance_before' => $giftCard->giftcard_amount,
|
|
|
+ 'action_type' => GiftCardUsageLog::ACTION_ADD,
|
|
|
+ 'notes' => $giftCard->notes
|
|
|
+ ]);
|
|
|
+ });
|
|
|
+ }
|
|
|
public static function setGiftCardCode($giftcard)
|
|
|
{
|
|
|
if (!$giftcard) {
|
|
|
@@ -60,4 +76,43 @@ class GiftCards extends Model implements GiftCardsContract
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ public static function addGiftCard($customerId, $giftcardAmount, $num = 0, $channel = 'activity_26_04_21', $notes = '拉新活动添加')
|
|
|
+ {
|
|
|
+ $data = GiftCards::where('customer_id', $customerId)
|
|
|
+ ->where('channel', $channel)
|
|
|
+ ->count();
|
|
|
+ if ($num != 0) {
|
|
|
+ $giftNum =count($data);
|
|
|
+ if($giftNum <= $num){
|
|
|
+ return -1; // 超出
|
|
|
+ }
|
|
|
+ }
|
|
|
+ static::create([
|
|
|
+ 'giftcard_number' => self::generateGiftCardNumber(),
|
|
|
+ 'giftcard_amount' => $giftcardAmount,
|
|
|
+ 'used_giftcard_amount' => 0,
|
|
|
+ 'remaining_giftcard_amount' => $giftcardAmount,
|
|
|
+ 'customer_id' => $customerId,
|
|
|
+ 'expirationdate' => ,
|
|
|
+ 'giftcard_status' => 1,
|
|
|
+ 'channel' => $channel,
|
|
|
+ 'notes' => $notes
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ public static function generateGiftCardNumber()
|
|
|
+ {
|
|
|
+ do {
|
|
|
+ // Generate a random string of characters
|
|
|
+ $randomString = Str::upper(Str::random(16));
|
|
|
+
|
|
|
+ // Format the string into groups of four separated by dashes
|
|
|
+ $code = implode('-', str_split($randomString, 4));
|
|
|
+
|
|
|
+ // Check if the code already exists in the database
|
|
|
+ $exists = GiftCards::where('giftcard_number', $code)->exists();
|
|
|
+ } while ($exists);
|
|
|
+
|
|
|
+ return $code;
|
|
|
+ }
|
|
|
}
|