调整活动领取
parent
7240f87688
commit
c0d65f6efb
|
|
@ -8,6 +8,7 @@ use App\Models\ProductPart;
|
||||||
use App\Models\ReceivePartCouponLog;
|
use App\Models\ReceivePartCouponLog;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Models\UserCoupon;
|
use App\Models\UserCoupon;
|
||||||
|
use Illuminate\Database\QueryException;
|
||||||
|
|
||||||
class CouponService
|
class CouponService
|
||||||
{
|
{
|
||||||
|
|
@ -112,17 +113,27 @@ class CouponService
|
||||||
protected function receiveSomeCoupons(User $user, array $coupons, ?int $activityId = null)
|
protected function receiveSomeCoupons(User $user, array $coupons, ?int $activityId = null)
|
||||||
{
|
{
|
||||||
$userCoupons = [];
|
$userCoupons = [];
|
||||||
foreach ($coupons as $coupon) {
|
foreach ($coupons as $key=> $coupon) {
|
||||||
for ($i = 0; $i < $coupon['num']; $i++) {
|
|
||||||
$userCoupons[] = self::createUserCouponData($user->id, $coupon['coupon'], $activityId ?? null);
|
|
||||||
}
|
|
||||||
//更新对应券发送量,余量;
|
//更新对应券发送量,余量;
|
||||||
$coupon['coupon']->increment('sent', $coupon['num']);
|
try {
|
||||||
if ($coupon['coupon']->limit > 0) {//限量才减少余量
|
$canSent = true;
|
||||||
$coupon['coupon']->decrement('stock', $coupon['num']);
|
if ($coupon['coupon']->limit > 0) {//限量才减少余量
|
||||||
|
$coupon['coupon']->decrement('stock', $coupon['num']);
|
||||||
|
}
|
||||||
|
} catch (QueryException $e) {
|
||||||
|
if (strpos($e->getMessage(), 'Numeric value out of range') !== false) {
|
||||||
|
$canSent = false;
|
||||||
|
} else {
|
||||||
|
throw new $e();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($canSent) {
|
||||||
|
for ($i = 0; $i < $coupon['num']; $i++) {
|
||||||
|
$userCoupons[] = self::createUserCouponData($user->id, $coupon['coupon'], $activityId ?? null);
|
||||||
|
}
|
||||||
|
$coupon['coupon']->increment('sent', $coupon['num']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// dd($userCoupons);
|
|
||||||
UserCoupon::insert($userCoupons);
|
UserCoupon::insert($userCoupons);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue