diff --git a/app/Admin/Controllers/VipController.php b/app/Admin/Controllers/VipController.php index 1a32fabd..34e76b65 100644 --- a/app/Admin/Controllers/VipController.php +++ b/app/Admin/Controllers/VipController.php @@ -86,8 +86,29 @@ class VipController extends AdminController $form->number('sort')->min(0); $form->switch('status')->default(1); $form->textarea('description'); - $form->embeds('gift', function ($form) { - $form->multipleSelect('coupon')->options(Coupon::class)->ajax('api/coupons'); + + $form->table('coupon', function ($table) { + $table->select('id', '优惠券')->options(Coupon::class)->ajax('api/coupons'); + $table->number('amount', '数量')->min(1)->default(1); + })->customFormat(fn() => data_get($this->gift, 'coupon')); + + $form->hidden('gift')->customFormat(fn($v) => json_encode($v)); + + $form->saving(function (Form $form) { + $gift = $form->model()?->gift ?: []; + if ($form->coupon) { + // [id, amount, name] + $couponList = []; + foreach($form->coupon as $item) { + if (!data_get($item, '_remove_') && $item['amount'] > 0) { + $coupon = Coupon::findOrFail($item['id']); + array_push($couponList, ['id' => $coupon->id, 'amount' => $item['amount'], 'name' => $coupon->name]); + } + } + $gift['coupon'] = $couponList; + } + $form->gift = $gift; + $form->deleteInput('coupon'); }); }); } diff --git a/app/Services/CouponService.php b/app/Services/CouponService.php index bfeb39e6..4e106409 100644 --- a/app/Services/CouponService.php +++ b/app/Services/CouponService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Exceptions\BizException; use App\Models\Activity; use App\Models\Coupon; use App\Models\ProductPart; diff --git a/app/Services/VipService.php b/app/Services/VipService.php index 14cacb38..d5189392 100644 --- a/app/Services/VipService.php +++ b/app/Services/VipService.php @@ -102,11 +102,16 @@ class VipService $user = $userVip->user; $gift = $userVip->gift; // 优惠券 - if (isset($gift['coupon'])) { + if (isset($gift['coupon']) && $gift['coupon']) { $coupon_service = new CouponService(); - foreach(Coupon::whereIn('id', $gift['coupon'])->get() as $item) { + $couponId = array_column($gift['coupon'], 'id'); + foreach(Coupon::whereIn('id', $couponId)->get() as $item) { try { - $coupon_service->sendCoupon($user, $item); + $couponItem = array_filter($gift['coupon'], fn($v) => $v['id'] == $item->id); + $amount = data_get($couponItem, 'amount', 1); + if ($amount > 0) { + $coupon_service->sendCoupon($user, $item); + } } catch (BizException $e) { } } @@ -116,7 +121,7 @@ class VipService /** * 延长时限 - * + * * @param int $num 数量 * @param string $unit 单位 * @return Carbon diff --git a/resources/views/admin/vip/gift.blade.php b/resources/views/admin/vip/gift.blade.php index 5eb16d67..d8847ba0 100644 --- a/resources/views/admin/vip/gift.blade.php +++ b/resources/views/admin/vip/gift.blade.php @@ -1,4 +1,4 @@ - @if($value) -
-@if($value['coupon']) -
优惠券 - @foreach(\App\Models\Coupon::whereIn('id', $value['coupon'])->get() as $item) - {{ $item->name }} + @foreach($value as $key => $list) +
+ @foreach($list as $item) +
+ {{ $item['name'] }} + {{ $item['amount'] }} +
@endforeach
-@endif -
+ @endforeach @endif