更新代码
This commit is contained in:
589
app/Admin/Controllers/GoodsController.php
Normal file
589
app/Admin/Controllers/GoodsController.php
Normal file
@@ -0,0 +1,589 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Controllers;
|
||||
|
||||
use Admin;
|
||||
use App\Models\Cart;
|
||||
use App\Models\Category;
|
||||
use App\Models\Goods;
|
||||
use App\Models\GoodsParams;
|
||||
use App\Models\Seller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Validator;
|
||||
|
||||
class GoodsController extends Controller
|
||||
{
|
||||
|
||||
public function all(Request $request)
|
||||
{
|
||||
$title = $request->title;
|
||||
$seller = $request->seller;
|
||||
$position = $request->position;
|
||||
$is_gift = $request->is_gift;
|
||||
$category_id = $request->category_id;
|
||||
$orderField = $request->orderField;
|
||||
$orderDirection = $request->orderDirection;
|
||||
$numPerPage = $request->numPerPage ?: 30;
|
||||
|
||||
$goods = Goods::where('status', '>=', 0)
|
||||
->when($title, function ($query) use ($title) {
|
||||
$query->where('title', 'like', "%{$title}%");
|
||||
})
|
||||
->when($seller, function ($query) use ($seller) {
|
||||
$query->whereHas('seller', function ($query) use ($seller) {
|
||||
$query->where('name', 'like', "%{$seller}%");
|
||||
});
|
||||
})
|
||||
->when($category_id, function ($query) use ($category_id) {
|
||||
$category_ids = Category::where('id', $category_id)->orWhere('parent_id', $category_id)->pluck('id');
|
||||
return $query->whereIn('category_id', $category_ids);
|
||||
})
|
||||
->when($orderField, function ($query) use ($orderField, $orderDirection) {
|
||||
$query->orderBy($orderField, $orderDirection);
|
||||
})
|
||||
->when($is_gift, function ($query) use ($is_gift) {
|
||||
switch ($is_gift) {
|
||||
case 'is_seller_gift':
|
||||
$query->where('is_seller_gift', 1);
|
||||
break;
|
||||
case 'is_mall_gift':
|
||||
$query->where('is_mall_gift', 1);
|
||||
break;
|
||||
}
|
||||
})
|
||||
->when($position, function ($query) use ($position) {
|
||||
switch ($position) {
|
||||
case 'is_recommend':
|
||||
$query->where('is_recommend', 1);
|
||||
break;
|
||||
case 'is_hot_sell':
|
||||
$query->where('is_hot_sell', 1);
|
||||
break;
|
||||
case 'is_hot_changed':
|
||||
$query->where('is_hot_changed', 1);
|
||||
break;
|
||||
case 'is_seller_package':
|
||||
$query->where('is_seller_package', 1);
|
||||
break;
|
||||
}
|
||||
})
|
||||
->with(['category', 'seller'])
|
||||
->orderBy('seller_id', 'desc')
|
||||
->orderBy('status', 'desc')
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate($numPerPage);
|
||||
$categoies = Category::treeSelect();
|
||||
|
||||
return view('Admin::goods.all', compact('goods', 'categoies'));
|
||||
}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$title = $request->title;
|
||||
$orderField = $request->orderField;
|
||||
$orderDirection = $request->orderDirection;
|
||||
$numPerPage = $request->numPerPage ?: 30;
|
||||
$admin = Admin::user();
|
||||
|
||||
$goods = Goods::when(Admin::user()->seller_id > 1, function ($query) use ($admin) {
|
||||
$query->where('seller_id', $admin->seller_id);
|
||||
})
|
||||
->when($title, function ($query) use ($title) {
|
||||
$query->where('title', 'like', "%{$title}%");
|
||||
})
|
||||
->when($orderField, function ($query) use ($orderField, $orderDirection) {
|
||||
$query->orderBy($orderField, $orderDirection);
|
||||
})
|
||||
->with(['category', 'seller'])
|
||||
->orderBy('status', 'desc')
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate($numPerPage);
|
||||
|
||||
return view('Admin::goods.index', compact('goods'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$sellers = Seller::where('status', 1)->get();
|
||||
$categoies = Category::treeSelect(91);
|
||||
|
||||
return view('Admin::goods.create', compact('categoies', 'sellers'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'title' => 'required|min:2|max:50',
|
||||
'category_id' => 'required',
|
||||
'description' => 'required|min:4',
|
||||
'content' => 'required|min:4',
|
||||
'storage_id' => 'required',
|
||||
], [
|
||||
'title.required' => '标题必须填写',
|
||||
'title.min' => '标题最少为:min字符',
|
||||
'title.max' => '标题最多为:max字符',
|
||||
'category_id.required' => '分类必须选择',
|
||||
'description.required' => '简介必须填写',
|
||||
'description.min' => '简介最少:min个字',
|
||||
'content.required' => '内容必须填写',
|
||||
'content.min' => '内容最少:min个字',
|
||||
'storage_id.required' => '标题图必须上传',
|
||||
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first());
|
||||
}
|
||||
|
||||
if (!isset($request->params)) {
|
||||
return $this->error('商品规格必须填写');
|
||||
}
|
||||
|
||||
foreach ($request->params as $p) {
|
||||
$validator = Validator::make($p, [
|
||||
'original' => 'required|numeric|min:0.001',
|
||||
'price' => 'required|numeric|min:0.001',
|
||||
'stock' => 'required|integer|min:1',
|
||||
'heavy' => 'required|numeric|min:0.001',
|
||||
], [
|
||||
'original.required' => '原价必须填写',
|
||||
'original.numeric' => '原价必须为数字',
|
||||
'original.min' => '原价最小值:min',
|
||||
'price.required' => '售价必须填写',
|
||||
'price.numeric' => '售价必须为数字',
|
||||
'price.min' => '售价最小值:min',
|
||||
'stock.required' => '库存必须填写',
|
||||
'stock.integer' => '库存必须为整数',
|
||||
'stock.min' => '库存最小值:min',
|
||||
'heavy.required' => '重量必须填写',
|
||||
'heavy.numeric' => '重量必须为数字',
|
||||
'heavy.min' => '重量最小值:min',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first());
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($request) {
|
||||
$status = 1;
|
||||
if (Admin::user()->seller_id == 1) {
|
||||
$status = 1;
|
||||
}
|
||||
|
||||
$seller_id = Admin::id() == 1 ? $request->seller_id : Admin::user()->seller_id;
|
||||
$goods = Goods::create([
|
||||
'title' => $request->title,
|
||||
'category_id' => $request->category_id,
|
||||
'description' => $request->description,
|
||||
'content' => $request->content,
|
||||
'storage_id' => $request->storage_id,
|
||||
'seller_id' => $seller_id,
|
||||
'status' => $status,
|
||||
]);
|
||||
|
||||
$goods->params()->createMany($request->params);
|
||||
|
||||
});
|
||||
return $this->success('操作成功', 'close');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('操作失败' . $e->getmessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function edit(Goods $good)
|
||||
{
|
||||
$categoies = Category::treeSelect(91);
|
||||
$sellers = Seller::where('status', 1)->get();
|
||||
return view('Admin::goods.edit', compact('categoies', 'good', 'sellers'));
|
||||
}
|
||||
|
||||
public function magageedit(Goods $good)
|
||||
{
|
||||
$categoies = Category::treeSelect();
|
||||
return view('Admin::goods.magageedit', compact('categoies', 'good'));
|
||||
}
|
||||
|
||||
public function update(Request $request, Goods $good)
|
||||
{
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'title' => 'required|min:2|max:50',
|
||||
'category_id' => 'required',
|
||||
'description' => 'required|min:4',
|
||||
'content' => 'required|min:4',
|
||||
'storage_id' => 'required',
|
||||
], [
|
||||
'title.required' => '标题必须填写',
|
||||
'title.min' => '标题最少为:min字符',
|
||||
'title.max' => '标题最多为:max字符',
|
||||
'category_id.required' => '分类必须选择',
|
||||
'description.required' => '简介必须填写',
|
||||
'description.min' => '简介最少:min个字',
|
||||
'content.required' => '内容必须填写',
|
||||
'content.min' => '内容最少:min个字',
|
||||
'storage_id.required' => '标题图必须上传',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first());
|
||||
}
|
||||
|
||||
if (!isset($request->params)) {
|
||||
return $this->error('商品规格必须填写');
|
||||
}
|
||||
foreach ($request->params as $p) {
|
||||
$validator = Validator::make($p, [
|
||||
'original' => 'required|numeric|min:0.001',
|
||||
'price' => 'required|numeric|min:0.001',
|
||||
// 'score' => 'required|numeric|min:0',
|
||||
'stock' => 'required|integer|min:1',
|
||||
'heavy' => 'required|numeric|min:0.001',
|
||||
], [
|
||||
'original.required' => '原价必须填写',
|
||||
'original.numeric' => '原价必须为数字',
|
||||
'original.min' => '原价最小值:min',
|
||||
'price.required' => '售价必须填写',
|
||||
'price.numeric' => '售价必须为数字',
|
||||
'price.min' => '售价最小值:min',
|
||||
'stock.required' => '库存必须填写',
|
||||
'stock.integer' => '库存必须为整数',
|
||||
'stock.min' => '库存最小值:min',
|
||||
'heavy.required' => '重量必须填写',
|
||||
'heavy.numeric' => '重量必须为数字',
|
||||
'heavy.min' => '重量最小值:min',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first());
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($good, $request) {
|
||||
|
||||
$seller_id = Admin::id() == 1 ? $request->seller_id : Admin::user()->seller_id;
|
||||
|
||||
$good->update([
|
||||
'title' => $request->title,
|
||||
'category_id' => $request->category_id,
|
||||
'description' => $request->description,
|
||||
'content' => $request->content,
|
||||
'seller_id' => $seller_id,
|
||||
'storage_id' => $request->storage_id,
|
||||
]);
|
||||
|
||||
$ids = [];
|
||||
|
||||
foreach ($request->params as $param) {
|
||||
if (isset($param['id'])) {
|
||||
$params = GoodsParams::find($param['id']);
|
||||
$params->update($param);
|
||||
array_push($ids, $param['id']);
|
||||
} else {
|
||||
$params = $good->params()->create($param);
|
||||
array_push($ids, $params['id']);
|
||||
}
|
||||
}
|
||||
$good->params()->where('status', 1)->whereNotIn('id', $ids)->update(['status' => 0, 'stock' => 0]);
|
||||
});
|
||||
return $this->success('操作成功');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('操作失败' . $e->getmessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function destroy(Goods $good)
|
||||
{
|
||||
// if ($good->seller_id != Admin::user()->seller_id) {
|
||||
// return $this->error('非法操作');
|
||||
// }
|
||||
try {
|
||||
DB::transaction(function () use ($good) {
|
||||
Cart::where('goods_id', $good->id)->delete();
|
||||
// $good->params()->update(['status' => 0, 'stock' => 0]);
|
||||
$good->status = 0;
|
||||
$good->save();
|
||||
});
|
||||
return $this->success('操作成功');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('操作失败' . $e->getmessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function recycle(Request $request)
|
||||
{
|
||||
$title = $request->title;
|
||||
$orderField = $request->orderField;
|
||||
$orderDirection = $request->orderDirection;
|
||||
$numPerPage = $request->numPerPage ?: 30;
|
||||
|
||||
$goods = Goods::where('seller_id', Admin::user()->seller_id)->onlyTrashed()
|
||||
->when($title, function ($query) use ($title) {
|
||||
$query->where('title', 'like', "%{$title}%");
|
||||
})
|
||||
->when($orderField, function ($query) use ($orderField, $orderDirection) {
|
||||
$query->orderBy($orderField, $orderDirection);
|
||||
})
|
||||
->with(['category', 'seller'])
|
||||
->paginate($numPerPage);
|
||||
return view('Admin::goods.recycle', compact('goods'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空回收站
|
||||
* 未考虑删除关联数据的问题
|
||||
* @Author:<C.Jason>
|
||||
* @Date:2018-11-02T13:41:40+0800
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
if (Goods::where('seller_id', Admin::user()->seller_id)->onlyTrashed()->forceDelete()) {
|
||||
return $this->success();
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 还原
|
||||
* @Author:<C.Jason>
|
||||
* @Date:2018-11-02T13:43:47+0800
|
||||
* @param [type] $user [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function resume($good)
|
||||
{
|
||||
if (Goods::where('seller_id', Admin::user()->seller_id)->withTrashed()->where('id', $good)->restore()) {
|
||||
return $this->success();
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 彻底删除单条目
|
||||
* @Author:<C.Jason>
|
||||
* @Date:2018-11-02T13:43:36+0800
|
||||
* @param [type] $user [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function delete(Goods $good)
|
||||
{
|
||||
try {
|
||||
DB::transaction(function () use ($good) {
|
||||
$good->params()->delete();
|
||||
$good->delete();
|
||||
});
|
||||
return $this->success('操作成功');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('操作失败' . $e->getmessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function putOn(Request $request, Goods $good)
|
||||
{
|
||||
if ($request->isMethod('PUT')) {
|
||||
$validator = Validator::make($request->all(), [
|
||||
'title' => 'required|min:2|max:50',
|
||||
'category_id' => 'required',
|
||||
'description' => 'required|min:4',
|
||||
'content' => 'required|min:4',
|
||||
'storage_id' => 'required',
|
||||
], [
|
||||
'title.required' => '标题必须填写',
|
||||
'title.min' => '标题最少为:min字符',
|
||||
'title.max' => '标题最多为:max字符',
|
||||
'category_id.required' => '分类必须选择',
|
||||
'description.required' => '简介必须填写',
|
||||
'description.min' => '简介最少:min个字',
|
||||
'content.required' => '内容必须填写',
|
||||
'content.min' => '内容最少:min个字',
|
||||
'storage_id.required' => '标题图必须上传',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first());
|
||||
}
|
||||
|
||||
if (!isset($request->params)) {
|
||||
return $this->error('商品规格必须填写');
|
||||
}
|
||||
foreach ($request->params as $p) {
|
||||
$validator = Validator::make($p, [
|
||||
'original' => 'required|numeric|min:0.001',
|
||||
'price' => 'required|numeric|min:0.001',
|
||||
'stock' => 'required|integer|min:1',
|
||||
'heavy' => 'required|numeric|min:0.001',
|
||||
], [
|
||||
'original.required' => '原价必须填写',
|
||||
'original.numeric' => '原价必须为数字',
|
||||
'original.min' => '原价最小值:min',
|
||||
'price.required' => '售价必须填写',
|
||||
'price.numeric' => '售价必须为数字',
|
||||
'price.min' => '售价最小值:min',
|
||||
'stock.required' => '库存必须填写',
|
||||
'stock.integer' => '库存必须为整数',
|
||||
'stock.min' => '库存最小值:min',
|
||||
'heavy.required' => '重量必须填写',
|
||||
'heavy.numeric' => '重量必须为数字',
|
||||
'heavy.min' => '重量最小值:min',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first());
|
||||
}
|
||||
if ($p['score'] > $p['price']) {
|
||||
return $this->error('可使用积分数量不能大于商品价格');
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($good, $request) {
|
||||
$good->update([
|
||||
'title' => $request->title,
|
||||
'category_id' => $request->category_id,
|
||||
'description' => $request->description,
|
||||
'content' => $request->content,
|
||||
'storage_id' => $request->storage_id,
|
||||
'status' => 1,
|
||||
]);
|
||||
|
||||
$ids = [];
|
||||
|
||||
foreach ($request->params as $param) {
|
||||
if (isset($param['id'])) {
|
||||
$params = GoodsParams::find($param['id']);
|
||||
$params->update($param);
|
||||
array_push($ids, $param['id']);
|
||||
} else {
|
||||
$params = $good->params()->create($param);
|
||||
array_push($ids, $params['id']);
|
||||
}
|
||||
}
|
||||
});
|
||||
return $this->success('操作成功');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('操作失败' . $e->getmessage());
|
||||
}
|
||||
} else {
|
||||
$categoies = Category::treeSelect();
|
||||
return view('Admin::goods.putOn', compact('categoies', 'good'));
|
||||
}
|
||||
}
|
||||
|
||||
public function setting(Request $request, Goods $good)
|
||||
{
|
||||
if ($request->isMethod('PUT')) {
|
||||
try {
|
||||
$good->update([
|
||||
'is_recommend' => $request->is_recommend,
|
||||
'is_hot_sell' => $request->is_hot_sell,
|
||||
'is_hot_changed' => $request->is_hot_changed,
|
||||
'is_seller_package' => $request->is_seller_package,
|
||||
'banner_id' => $request->is_seller_package == 1 && $request->banner_id ? $request->banner_id : 0,
|
||||
]);
|
||||
return $this->success('设置成功', 'close');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('设置失败' . $e->getmessage());
|
||||
}
|
||||
} else {
|
||||
return view('Admin::goods.setting', compact('good'));
|
||||
}
|
||||
}
|
||||
|
||||
public function setgift(Goods $good)
|
||||
{
|
||||
try {
|
||||
DB::transaction(function () use ($good) {
|
||||
$good->is_seller_gift = 1;
|
||||
$good->save();
|
||||
});
|
||||
return $this->success('设置成功');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('设置失败' . $e->getmessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function cancelgift(Goods $good)
|
||||
{
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($good) {
|
||||
$good->is_seller_gift = 0;
|
||||
$good->save();
|
||||
});
|
||||
return $this->success('设置成功');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('设置失败' . $e->getmessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function setlottery(Goods $good)
|
||||
{
|
||||
try {
|
||||
DB::transaction(function () use ($good) {
|
||||
$good->is_lottery_gift = 1;
|
||||
$good->save();
|
||||
});
|
||||
return $this->success('设置成功');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('设置失败' . $e->getmessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function cancellottery(Goods $good)
|
||||
{
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($good) {
|
||||
$good->is_lottery_gift = 0;
|
||||
$good->save();
|
||||
});
|
||||
return $this->success('设置成功');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('设置失败' . $e->getmessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function setFull(Goods $good)
|
||||
{
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($good) {
|
||||
$good->is_mall_gift = 1;
|
||||
$good->save();
|
||||
});
|
||||
return $this->success('设置成功');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('设置失败' . $e->getmessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function cancelFull(Goods $good)
|
||||
{
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($good) {
|
||||
$good->is_mall_gift = 0;
|
||||
$good->save();
|
||||
});
|
||||
return $this->success('设置成功');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('设置失败' . $e->getmessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user