fix(coupon): correct knows issues

This commit is contained in:
xboard
2025-01-23 14:48:12 +08:00
parent 25c71c60ac
commit 8f88e11403
7 changed files with 971 additions and 929 deletions
@@ -11,12 +11,18 @@ class CouponController extends Controller
{
public function check(Request $request)
{
$request->validate([
'code' => 'required|string',
'plan_id' => 'required|integer',
'period' => 'nullable|string',
]);
if (empty($request->input('code'))) {
return $this->fail([422,__('Coupon cannot be empty')]);
}
$couponService = new CouponService($request->input('code'));
$couponService->setPlanId($request->input('plan_id'));
$couponService->setUserId($request->user()->id);
$couponService->setPeriod($request->input('period'));
$couponService->check();
return $this->success($couponService->getCoupon());
}
+8
View File
@@ -2,6 +2,7 @@
namespace App\Models;
use App\Services\PlanService;
use Illuminate\Database\Eloquent\Model;
class Coupon extends Model
@@ -15,4 +16,11 @@ class Coupon extends Model
'limit_plan_ids' => 'array',
'limit_period' => 'array'
];
public function getLimitPeriodAttribute($value)
{
return collect(json_decode($value, true))->map(function ($item) {
return PlanService::getPeriodKey($item);
})->toArray();
}
}
+9 -5
View File
@@ -21,7 +21,7 @@ class CouponService
->first();
}
public function use(Order $order):bool
public function use(Order $order): bool
{
$this->setPlanId($order->plan_id);
$this->setUserId($order->user_id);
@@ -39,7 +39,8 @@ class CouponService
$order->discount_amount = $order->total_amount;
}
if ($this->coupon->limit_use !== NULL) {
if ($this->coupon->limit_use <= 0) return false;
if ($this->coupon->limit_use <= 0)
return false;
$this->coupon->limit_use = $this->coupon->limit_use - 1;
if (!$this->coupon->save()) {
return false;
@@ -70,16 +71,19 @@ class CouponService
public function setPeriod($period)
{
$this->period = $period;
if ($period) {
$this->period = PlanService::getPeriodKey($period);
}
}
public function checkLimitUseWithUser():bool
public function checkLimitUseWithUser(): bool
{
$usedCount = Order::where('coupon_id', $this->coupon->id)
->where('user_id', $this->userId)
->whereNotIn('status', [0, 2])
->count();
if ($usedCount >= $this->coupon->limit_use_with_user) return false;
if ($usedCount >= $this->coupon->limit_use_with_user)
return false;
return true;
}
File diff suppressed because one or more lines are too long
+944 -920
View File
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.