251 lines
7.8 KiB
PHP
251 lines
7.8 KiB
PHP
<?php
|
||
|
||
namespace App\Endpoint\Api\Http\Controllers;
|
||
|
||
use App\Endpoint\Api\Http\Resources\BargainActivityResource;
|
||
use App\Endpoint\Api\Http\Resources\BargainOrderResource;
|
||
use App\Endpoint\Api\Http\Resources\OrderResource;
|
||
use App\Events\OrderPaid;
|
||
use App\Exceptions\BizException;
|
||
use App\Models\BargainActivity;
|
||
use App\Models\BargainOrder;
|
||
use App\Models\BargainSku;
|
||
use App\Models\ProductSku;
|
||
use App\Services\BargainService;
|
||
use App\Services\OrderService;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Throwable;
|
||
|
||
class BargainController extends Controller
|
||
{
|
||
/**
|
||
*
|
||
*
|
||
* @param \Illuminate\Http\Request $request
|
||
* @return void
|
||
*/
|
||
public function activityDetail($id)
|
||
{
|
||
$activity = BargainActivity::with('skus')->findOrFail($id);
|
||
if (!$activity->is_enable) {
|
||
throw new BizException('砍价活动暂未开始');
|
||
}
|
||
if ($activity->start_at > now() || $activity->end_at < now()) {
|
||
throw new BizException('不在活动时间范围');
|
||
}
|
||
|
||
return BargainActivityResource::make($activity);
|
||
}
|
||
|
||
/**
|
||
* 通过砍价商品查看当前砍价记录
|
||
*
|
||
* @param [type] $id
|
||
* @param Request $request
|
||
* @return void
|
||
*/
|
||
public function bargainSku($id)
|
||
{
|
||
//判断商品是否参与砍价
|
||
$bargainSku = BargainSku::with(['activity'=>function ($query) {
|
||
return $query->where('is_enable', true)->where('start_at', '<=', now())->where('end_at', '>=', now());
|
||
}, 'sku'])->where('sku_id', $id)->first();
|
||
|
||
if (!($bargainSku && $bargainSku->activity)) {
|
||
throw new BizException('该商品未参与砍价活动,或者活动已结束');
|
||
}
|
||
|
||
return response()->json([
|
||
'sku' => [
|
||
'name' => (string) $bargainSku->sku->name,
|
||
'subtitle' => (string) $bargainSku->sku->subtitle,
|
||
'cover' => (string) $bargainSku->sku->cover,
|
||
'sell_price' => (string) $bargainSku->sku->sell_price_format,
|
||
'vip_price' => (string) $bargainSku->sku->vip_price_format,
|
||
],
|
||
'max_bargain_price'=> array_sum(json_decode($bargainSku->activity->rules)),
|
||
'activity_id' => $bargainSku->activity->id,
|
||
'activity_images' => $bargainSku->activity->images,
|
||
'activity_share_image'=> $bargainSku->activity->share_image,
|
||
'activity_share_title'=> $bargainSku->activity->share_title,
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 通过商品获取当前砍价进度(看自己的)
|
||
*
|
||
* @param Request $request
|
||
* @return void
|
||
*/
|
||
public function barginaOrderBySku($id, Request $request)
|
||
{
|
||
// $order = [];
|
||
$user = $request->user();
|
||
$userId = $user?->id ?? 0;
|
||
$order = BargainOrder::with(['logs', 'logs.userInfo'])
|
||
->where([
|
||
'sku_id' => $id,
|
||
'user_id'=> $userId,
|
||
])->where('status', '>', 0)
|
||
->where('expire_at', '>', now())
|
||
->whereNull('order_id')
|
||
->orderBy('created_at', 'desc')->first();
|
||
if ($order) {
|
||
return BargainOrderResource::make($order);
|
||
}
|
||
return response()->noContent();
|
||
}
|
||
|
||
/**
|
||
* 通过订单ID获取当前砍价进度(看别人的)
|
||
*
|
||
* @param Request $request
|
||
* @return void
|
||
*/
|
||
public function bargainOrderById($id, Request $request)
|
||
{
|
||
$order = BargainOrder::with(['logs', 'logs.userInfo'])
|
||
->where([
|
||
'id' => $id,
|
||
])->where('status', '>', 0)
|
||
->where('expire_at', '>', now())
|
||
->whereNull('order_id')
|
||
->orderBy('created_at', 'desc')->first();
|
||
if ($order) {
|
||
return BargainOrderResource::make($order);
|
||
}
|
||
return response()->noContent();
|
||
}
|
||
|
||
/**
|
||
* 发起砍价
|
||
*
|
||
* @param [type] $id
|
||
* @param Request $request
|
||
* @return void
|
||
*/
|
||
public function createBargainOrder(ProductSku $sku, Request $request)
|
||
{
|
||
if (!$sku->isBargaing()) {
|
||
throw new BizException('活动已结束');
|
||
}
|
||
//判断是否已经有正在进行的砍价单, 直接返回
|
||
$order = BargainOrder::where([
|
||
'sku_id' => $sku->id,
|
||
'user_id'=> $request->user()->id,
|
||
])->where('status', '>', 0)
|
||
->where('expire_at', '>', now())
|
||
->whereNull('order_id')
|
||
->orderBy('created_at', 'desc')->first();
|
||
|
||
if (!$order) {
|
||
//创建新的砍价单
|
||
$bargainService = new BargainService();
|
||
try {
|
||
DB::beginTransaction();
|
||
$order = $bargainService->createBargainOrder($request->user(), $sku);
|
||
DB::commit();
|
||
} catch (Throwable $th) {
|
||
DB::rollBack();
|
||
report($th);
|
||
throw new BizException($th->getMessage());
|
||
}
|
||
}
|
||
|
||
$order->load(['logs', 'logs.userInfo']);
|
||
return BargainOrderResource::make($order);
|
||
}
|
||
|
||
/**
|
||
* 帮人砍价
|
||
*
|
||
* @param BargainOrder $order
|
||
* @param Request $request
|
||
* @return void
|
||
*/
|
||
public function bargain(BargainOrder $order, Request $request)
|
||
{
|
||
$bargainService = new BargainService();
|
||
try {
|
||
DB::beginTransaction();
|
||
$bargainService->bargain($request->user(), $order);
|
||
DB::commit();
|
||
} catch (Throwable $th) {
|
||
DB::rollBack();
|
||
report($th);
|
||
throw new BizException($th->getMessage());
|
||
}
|
||
|
||
$order->load(['logs', 'logs.userInfo']);
|
||
return BargainOrderResource::make($order);
|
||
}
|
||
|
||
/**
|
||
* 通过砍价订单创建待支付商城订单
|
||
*
|
||
* @param BargainOrder $bargainOrder
|
||
* @param Request $request
|
||
* @return void
|
||
*/
|
||
public function createMallOrderByBargainOrder(BargainOrder $bargainOrder, Request $request)
|
||
{
|
||
$rules = [
|
||
'shipping_address_id' => ['bail', 'required', 'int'],
|
||
'note' => ['bail', 'nullable', 'string', 'max:255'],
|
||
];
|
||
$input = $request->validate($rules, [], [
|
||
'shipping_address_id' => '收货地址',
|
||
'note' => '订单备注',
|
||
]);
|
||
$user = $request->user();
|
||
if ($user->id != $bargainOrder->user_id) {
|
||
throw new BizException('您不能替别人下单');
|
||
}
|
||
|
||
$orderService = new OrderService();
|
||
try {
|
||
DB::beginTransaction();
|
||
$bargainOrder->lockForUpdate();//下单的时候不允许砍价了
|
||
|
||
//已下单
|
||
if ($bargainOrder->order_id > 0) {
|
||
throw new BizException('您已下单,无需重复下单');
|
||
}
|
||
|
||
//砍价超时
|
||
if ($bargainOrder->expire_at < now()) {
|
||
throw new BizException('当前砍价已结束,无法下单');
|
||
}
|
||
|
||
//如果砍价活动结束了,也不能砍了
|
||
$activity = $bargainOrder->sku->isBargaing();
|
||
if (!$activity) {
|
||
throw new BizException('当前砍价已结束,无法下单');
|
||
}
|
||
|
||
$order = $orderService->createQuickOrder(
|
||
$user,
|
||
$bargainOrder->sku_id,
|
||
1,
|
||
$input['shipping_address_id'],
|
||
null,
|
||
$input['note'] ?? null,
|
||
$bargainOrder
|
||
);
|
||
$bargainOrder->update([
|
||
'order_id' => $order->id,
|
||
]);
|
||
DB::commit();
|
||
} catch (Throwable $th) {
|
||
DB::rollBack();
|
||
report($th);
|
||
throw new BizException($th->getMessage());
|
||
}
|
||
|
||
OrderPaid::dispatchIf($order->isPaid(), $order);
|
||
|
||
return OrderResource::make($order);
|
||
}
|
||
}
|