This commit is contained in:
root 2020-01-01 23:40:14 +08:00
parent 8bc5004654
commit 89d67279c6
3 changed files with 22 additions and 2 deletions

View File

@ -7,6 +7,7 @@ use App\Models\Order;
use App\Models\User; use App\Models\User;
use App\Models\Plan; use App\Models\Plan;
use App\Utils\Helper; use App\Utils\Helper;
use App\Models\Coupon;
class CheckOrder extends Command class CheckOrder extends Command
{ {

View File

@ -15,6 +15,9 @@ class CouponController extends Controller
if (!$coupon) { if (!$coupon) {
abort(500, '优惠券无效'); abort(500, '优惠券无效');
} }
if ($coupon->limit_use <= 0) {
abort(500, '优惠券已无可用次数');
}
if (time() < $coupon->started_at) { if (time() < $coupon->started_at) {
abort(500, '优惠券还未到可用时间'); abort(500, '优惠券还未到可用时间');
} }

View File

@ -3,10 +3,11 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Http\Requests\OrderSave; use App\Http\Requests\OrderSave;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use App\Models\Order; use App\Models\Order;
use App\Models\Plan; use App\Models\Plan;
use App\Models\User; use App\Models\User;
@ -78,6 +79,9 @@ class OrderController extends Controller
if (!$coupon) { if (!$coupon) {
abort(500, '优惠券无效'); abort(500, '优惠券无效');
} }
if ($coupon->limit_use <= 0) {
abort(500, '优惠券已无可用次数');
}
if (time() < $coupon->started_at) { if (time() < $coupon->started_at) {
abort(500, '优惠券还未到可用时间'); abort(500, '优惠券还未到可用时间');
} }
@ -86,6 +90,7 @@ class OrderController extends Controller
} }
} }
DB::beginTransaction();
$order = new Order(); $order = new Order();
$order->user_id = $request->session()->get('id'); $order->user_id = $request->session()->get('id');
$order->plan_id = $plan->id; $order->plan_id = $plan->id;
@ -115,20 +120,31 @@ class OrderController extends Controller
// coupon process // coupon process
if (isset($coupon)) { if (isset($coupon)) {
switch ($coupon->type) { switch ($coupon->type) {
case 1: $order->discount_amount = $order->total_amount - $coupon->value; case 1: $order->discount_amount = $coupon->value;
break; break;
case 2: $order->discount_amount = $order->total_amount * ($coupon->value / 100); case 2: $order->discount_amount = $order->total_amount * ($coupon->value / 100);
break; break;
} }
$order->total_amount = $order->total_amount - $order->discount_amount; $order->total_amount = $order->total_amount - $order->discount_amount;
$coupon->limit_use = $coupon->limit_use - 1;
if (!$coupon->save()) {
DB::rollback();
abort(500, '优惠券使用失败');
}
} }
// free process // free process
if ($order->total_amount <= 0) { if ($order->total_amount <= 0) {
$order->total_amount = 0;
$order->status = 1; $order->status = 1;
} }
if (!$order->save()) { if (!$order->save()) {
DB::rollback();
abort(500, '订单创建失败'); abort(500, '订单创建失败');
} }
DB::commit();
return response([ return response([
'data' => $order->trade_no 'data' => $order->trade_no
]); ]);