0
0
Files
Babyclass/app/Api/Controllers/GoodsController.php
2020-08-04 10:09:42 +08:00

301 lines
13 KiB
PHP

<?php
namespace App\Api\Controllers;
use App\Api\Resources\ChannelIndexResource;
use App\Api\Resources\GoodsDetailResource;
use App\Api\Resources\GoodsGiftDetailResource;
use App\Api\Resources\GoodsGiftListResource;
use App\Api\Resources\GoodsListResource;
use App\Api\Resources\GoodsParamsDetailResource;
use App\Api\Resources\GoodsParamsListResource;
use App\Api\Resources\GoodsParamsScoreDetailResource;
use App\Models\Category;
use App\Models\Goods;
use App\Models\GoodsParams;
use App\Models\Seller;
use App\Models\VipPament;
use App\User;
use Illuminate\Http\Request;
use RuLong\Order\Models\Order;
use Illuminate\Support\Facades\DB;
class GoodsController extends Controller
{
public function __construct()
{
// $this->middleware('auth.api');
// $this->user = \Auth::guard('api')->user();
// $this->uid = \Auth::guard('api')->id();
$this->user = User::find(824);
$this->uid = 824;
}
public function index()
{
$addresses = User::find(824)->addresses()->orderBy('is_default', 'desc')->orderBy('id', 'desc')->get();
return $this->success($addresses);
}
/**
* 商品详情 小程序商品详情页请求该接口。
* @param \App\Models\Goods $good
* @return array
*/
public function show(Goods $good,Request $request)
{
$type = $request->get('type', 'cashScore');
if($type == 'score'){
$params_count = $good->params()->where('status', 1)->where('stock', '>', 0)->where('score', '>', 0)->whereRaw('price = score')->count();
if($params_count == 0){
return $this->failed('商品已下架');
}else{
return $this->success(new GoodsParamsScoreDetailResource($good));
}
}elseif($type == 'cashScore'){
$params_count = $good->params()->where('status', 1)->where('stock', '>', 0)->where('score', '>', 0)->whereRaw('price > score')->count();
if($params_count == 0){
return $this->failed('商品已下架');
}else{
return $this->success(new GoodsParamsDetailResource($good));
}
}
}
/**
* 相关商品 小程序商品详情页请求该接口。
* @param \App\Models\Goods $good
* @return array
*/
public function related(Goods $good)
{
$data = $good->seller->goods()->where('status', 1)->limit(6)->get();
return GoodsListResource::collection($data)->additional([
'status' => 'SUCCESS',
'status_code' => 200,
]);
}
/**
* 商品列表 小程序点击某一分类页面请求该接口。
* @param \Illuminate\Http\Request $request
* @return array
*/
public function lists(Request $request)
{
$category = $request->get('categoryId', '');
$title = $request->get('title', '');
$type = $request->get('type', 'cashScore');
$data = [];
switch ($type){
case 'cash':
$data = GoodsParams::whereHas('goods', function ($query) use($title,$category)
{
return $query->when($category, function ($query) use ($category) {
$category_ids = Category::where('id',$category)->orWhere('parent_id', $category)->pluck('id');
return $query->whereIn('goods.category_id', $category_ids);
})->when($title, function ($query) use ($title) {
return $query->where('goods.title', 'like', "%$title%");
})->where('status',1);
})->where('status', 1)->where('stock', '>', 0)->where('score', '=', 0)->where('price', '>', 0)->select('goods_id', DB::raw('any_value(stock) as stock'), DB::raw('any_value(original) as original'),DB::raw('any_value(status) as status'), DB::raw('any_value(price) as price'), DB::raw('any_value(score) as score'), DB::raw('any_value(id) as id'))
->groupBy('goods_id')
->orderBy('price','asc')
->with('goods')
->paginate(20);
break;
case 'score':
$data = GoodsParams::whereHas('goods', function ($query) use($title,$category)
{
return $query->when($category, function ($query) use ($category) {
$category_ids = Category::where('id',$category)->orWhere('parent_id', $category)->pluck('id');
return $query->whereIn('goods.category_id', $category_ids);
})->when($title, function ($query) use ($title) {
return $query->where('goods.title', 'like', "%$title%");
})->where('status',1);
})->where('status', 1)->where('stock', '>', 0)->where('score', '>', 0)->whereRaw('price = score')->select('goods_id', DB::raw('any_value(stock) as stock'), DB::raw('any_value(original) as original'),DB::raw('any_value(status) as status'), DB::raw('any_value(price) as price'), DB::raw('any_value(score) as score'), DB::raw('any_value(id) as id'))
->groupBy('goods_id')
->orderBy('price','asc')
->with('goods')
->paginate(20);
break;
case 'cashScore':
$data = GoodsParams::whereHas('goods', function ($query) use($title,$category)
{
return $query->when($category, function ($query) use ($category) {
$category_ids = Category::where('id',$category)->orWhere('parent_id', $category)->pluck('id');
return $query->whereIn('goods.category_id', $category_ids);
})->when($title, function ($query) use ($title) {
return $query->where('goods.title', 'like', "%$title%");
})->where('status',1);
})->where('status', 1)->where('stock', '>', 0)->where('score', '>', 0)->whereRaw('price > score')->select('goods_id', DB::raw('any_value(stock) as stock'), DB::raw('any_value(original) as original'),DB::raw('any_value(status) as status'), DB::raw('any_value(price) as price'), DB::raw('any_value(score) as score'), DB::raw('any_value(id) as id'))
->groupBy('goods_id')
->with('goods')
->orderBy('price','asc')
->paginate(20);
break;
}
return GoodsParamsListResource::collection($data)->additional([
'status' => 'SUCCESS',
'status_code' => 200,
]);
}
/**
* 商品分类 小程序点击某一分类页面请求该接口。
* @param \Illuminate\Http\Request $request
* @return array
*/
public function category(Request $request)
{
$categoryId = $request->get('id', '');
$categoryLists = Category::where('parent_id', 1)->orderBy('sort', 'desc')->orderBy('created_at', 'desc')->get();
$currentCategory = Category::find($categoryId);
return [
'data' => [
'currentCategory' => new ChannelIndexResource($currentCategory),
'categoryLists' => ChannelIndexResource::collection($categoryLists),
],
'status' => 'SUCCESS',
'status_code' => 200,
];
}
/**
* 商品满仓列表 小程序点击满仓赠品请求该接口。
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fullLists(Request $request)
{
$title = $request->get('title', '');
$fullGoodsLists = Goods::where('status', 1)
->when($title, function ($query) use ($title) {
return $query->where('title', 'like', "%$title%");
})
->whereHas('params', function ($query) {
return $query->where('stock', '>', 0)->where('status', 1);
})
->where('is_mall_gift', 1)->get();
$isfull = $this->user->account->act_a > 0 ? true : false;
$fullOrder = Order::where(['user_id' => $this->uid, 'item_type' => 'FULL_GIFT'])->whereRaw('substring(cast(status as char),1,1) = 1')->first();
$canCreateOrder = !$fullOrder && $isfull ? true : false;
return [
'data' => [
'isfull' => $isfull,
'fullGoodsLists' => GoodsGiftListResource::collection($fullGoodsLists),
'canCreateOrder' => $canCreateOrder,
],
'status' => 'SUCCESS',
'status_code' => 200,
];
}
/**
* vip赠品列表 小程序点击vip赠品请求该接口。
* @param \Illuminate\Http\Request $request
* @return array
*/
public function vipLists(Request $request)
{
$title = $request->get('title', '');
$vipGoodsLists = Goods::where('status', 1)
->when($title, function ($query) use ($title) {
return $query->where('title', 'like', "%$title%");
})
->whereHas('params', function ($query) {
return $query->where('stock', '>', 0)->where('status', 1);
})
->where('is_seller_gift', 1)->get();
//最近的上级总裁的商品。
$parentMallLists = [];
$parent_ids = array_reverse(array_filter(explode(',', $this->user->relation->bloodline)));
foreach ($parent_ids as $key => $parent_id) {
$parent_seller[$key] = Seller::where(['status' => 1, 'user_id' => $parent_id])->first();
if ($parent_seller[$key]) {
$parentMallLists = $parent_seller[$key]->goods()->where('is_seller_gift', 1)->get();
if ($parentMallLists && $parentMallLists->count() > 0) {
break;
}
}
}
$defaultMallLists = Goods::where('status', 1)->whereHas('params', function ($query) {
return $query->where('stock', '>', 0)->where('status', 1);
})
->where('is_seller_gift', 1)->where('seller_id', 1)->get();
$vipPay = VipPament::where(['user_id' => $this->uid, 'state' => 'SUCCESS'])->first();
if ($vipPay && $vipPay->type == 'CDKEY') {
$vipGoodsLists = !empty($parentMallLists) && $parentMallLists->count() > 0 ? [] : $defaultMallLists;
}
$isVipUser = $vipPay ? true : false;
$vipOrder = Order::where(['user_id' => $this->uid, 'item_type' => 'VIP_GIFT'])->whereRaw('substring(cast(status as char),1,1) = 1')->first();
$canCreateOrder = !$vipOrder && $isVipUser ? true : false;
return [
'data' => [
'parentMallLists' => !empty($parentMallLists) && $parentMallLists->count() > 0 ? GoodsGiftListResource::collection($parentMallLists) : [],//最近上级的店铺赠品
'vipGoodsLists' => !empty($vipGoodsLists) && $vipGoodsLists->count() > 0 ? GoodsGiftListResource::collection($vipGoodsLists) : [],//可选的全部赠品
'isVipUser' => $isVipUser,//是否是vip
'canCreateOrder' => $canCreateOrder//是否可以创建订单
],
'status' => 'SUCCESS',
'status_code' => 200,
];
}
/**
* 满仓赠品详情 小程序点击满仓赠品查看赠品详情请求该接口。
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fullGift(Goods $good)
{
$isfull = $this->user->account->act_a > 0 ? true : false;
$fullOrder = Order::where(['user_id' => $this->uid, 'item_type' => 'FULL_GIFT'])->whereRaw('substring(cast(status as char),1,1) = 1')->first();
$canCreateOrder = !$fullOrder && $isfull ? true : false;
return [
'data' => [
'isfull' => $isfull,
'canCreateOrder' => $canCreateOrder,
'goods' => new GoodsGiftDetailResource($good),
],
'status' => 'SUCCESS',
'status_code' => 200,
];
}
/**
* vip赠品详情 小程序点击vip赠品查看赠品详情请求该接口。
* @param \Illuminate\Http\Request $request
* @return array
*/
public function vipGift(Goods $good)
{
$vipPay = VipPament::where(['user_id' => $this->uid, 'state' => 'SUCCESS'])->first();
$isVipUser = $vipPay ? true : false;
$vipOrder = Order::where(['user_id' => $this->uid, 'item_type' => 'VIP_GIFT'])->whereRaw('substring(cast(status as char),1,1) = 1')->first();
$canCreateOrder = !$vipOrder && $isVipUser ? true : false;
return [
'data' => [
'goods' => new GoodsGiftDetailResource($good),
'isVipUser' => $isVipUser,//是否是vip
'canCreateOrder' => $canCreateOrder//是否可以创建订单
],
'status' => 'SUCCESS',
'status_code' => 200,
];
}
}