更新代码
This commit is contained in:
264
app/Admin/Controllers/LotteryController.php
Normal file
264
app/Admin/Controllers/LotteryController.php
Normal file
@@ -0,0 +1,264 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Controllers;
|
||||
|
||||
use App\Models\Activity;
|
||||
use App\Models\GoodsParams;
|
||||
use App\Models\Lottery;
|
||||
use App\Models\LotteryGift;
|
||||
use App\Models\LotteryLog;
|
||||
use Illuminate\Http\Request;
|
||||
use RuLong\Coupon\Models\CouponInfo;
|
||||
use Validator;
|
||||
|
||||
class LotteryController extends Controller
|
||||
{
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$orderField = $request->orderField;
|
||||
$orderDirection = $request->orderDirection;
|
||||
$numPerPage = $request->numPerPage ?: 30;
|
||||
|
||||
$lists = Lottery::withCount('logs')
|
||||
->withCount('gifts')
|
||||
->when($orderField, function ($query) use ($orderField, $orderDirection) {
|
||||
$query->orderBy($orderField, $orderDirection);
|
||||
})
|
||||
->paginate($numPerPage);
|
||||
|
||||
return view('Admin::lottery.index', compact('lists'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return view('Admin::lottery.create');
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'title' => 'required|min:2|max:50',
|
||||
'status' => 'required',
|
||||
'start_at' => 'required',
|
||||
'end_at' => 'required',
|
||||
], [
|
||||
'title.required' => '名称必须填写',
|
||||
'title.min' => '名称最少为:min字符',
|
||||
'title.max' => '名称最多为:max字符',
|
||||
'status.required' => '状态必须选择',
|
||||
'start_at.required' => '开始时间填写',
|
||||
'end_at.required' => '结束时间填写',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first());
|
||||
}
|
||||
if (Lottery::create($request->all())) {
|
||||
return $this->success('', 'close');
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
public function edit(Lottery $lottery)
|
||||
{
|
||||
return view('Admin::lottery.edit', compact('lottery'));
|
||||
}
|
||||
|
||||
public function update(Request $request, Lottery $lottery)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'title' => 'required|min:2|max:50',
|
||||
'status' => 'required',
|
||||
'start_at' => 'required',
|
||||
'end_at' => 'required',
|
||||
], [
|
||||
'title.required' => '名称必须填写',
|
||||
'title.min' => '名称最少为:min字符',
|
||||
'title.max' => '名称最多为:max字符',
|
||||
'status.required' => '状态必须选择',
|
||||
'start_at.required' => '开始时间填写',
|
||||
'end_at.required' => '结束时间填写',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first());
|
||||
}
|
||||
if ($lottery->update($request->all())) {
|
||||
return $this->success('', 'close');
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
public function logs(Lottery $lottery)
|
||||
{
|
||||
$logs = LotteryLog::where('lottery_id', $lottery->id)->paginate();
|
||||
return view('Admin::lottery.logs', compact('logs'));
|
||||
}
|
||||
|
||||
public function gifts(Request $request, Lottery $lottery)
|
||||
{
|
||||
$lists = LotteryGift::where('lottery_id', $lottery->id)->orderBy('level', 'asc')->paginate();
|
||||
return view('Admin::lottery.gifts', compact('lottery', 'lists'));
|
||||
}
|
||||
|
||||
//添加奖品
|
||||
public function addgifts(Request $request, Lottery $lottery)
|
||||
{
|
||||
$item_type = LotteryGift::$class[$request->type] ?? 'App\Models\GoodsParams';
|
||||
|
||||
if ($request->isMethod('POST')) {
|
||||
$validator = Validator::make($request->all(), [
|
||||
'number' => 'required',
|
||||
'level' => 'required',
|
||||
'chance' => 'required',
|
||||
], [
|
||||
'number.required' => '请输入商品数量',
|
||||
'level.required' => '请选择奖项',
|
||||
'chance.required' => '请填写概率',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first());
|
||||
}
|
||||
|
||||
if ($request->type && empty($request->item_id)) {
|
||||
return $this->error('请选择商品');
|
||||
}
|
||||
|
||||
if (!$request->type) {
|
||||
$request->item_id = '';
|
||||
$request->item_type = '';
|
||||
}
|
||||
|
||||
$data = [
|
||||
'level' => $request->level,
|
||||
'lottery_id' => $request->lottery_id,
|
||||
'chance' => $request->chance,
|
||||
'item_id' => $request->item_id,
|
||||
'item_type' => $request->item_type,
|
||||
'type' => $request->type,
|
||||
'number' => $request->number,
|
||||
'site' => LotteryGift::$chances[$request->level],
|
||||
];
|
||||
|
||||
if (LotteryGift::create($data)) {
|
||||
return $this->success('', 'close');
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
} else {
|
||||
$hasLevels = LotteryGift::where('lottery_id', $lottery->id)->pluck('level')->toArray();
|
||||
$params_id = LotteryGift::where('lottery_id', $lottery->id)
|
||||
->where('type', 1)
|
||||
->where('item_type', $item_type)
|
||||
->pluck('item_id')
|
||||
->toArray();
|
||||
|
||||
if ($request->type == 'activity') {
|
||||
$params = Activity::where('status', 1)
|
||||
->whereNotIn('id', $params_id)
|
||||
->get();
|
||||
} elseif ($request->type == 'goods') {
|
||||
$params = GoodsParams::with(['goods'])
|
||||
->whereHas('goods', function ($query) {
|
||||
$query->where('status', 1)->where('is_lottery_gift', 1);
|
||||
})
|
||||
->whereNotIn('id', $params_id)
|
||||
->get();
|
||||
} elseif ($request->type == 'coupon') {
|
||||
$params = CouponInfo::where('status', 1)->where('type', 'lottery')
|
||||
->whereNotIn('id', $params_id)
|
||||
->get();
|
||||
}
|
||||
|
||||
$levels = LotteryGift::$levels;
|
||||
$item_type = LotteryGift::$class[$request->type] ?? 'App\Models\GoodsParams';
|
||||
|
||||
return view('Admin::lottery.addgifts', compact('lottery', 'params', 'levels', 'hasLevels', 'item_type'));
|
||||
}
|
||||
}
|
||||
|
||||
//编辑奖品
|
||||
public function editgifts(Request $request, LotteryGift $gift)
|
||||
{
|
||||
if ($request->isMethod('PUT')) {
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'number' => 'required',
|
||||
'level' => 'required',
|
||||
'chance' => 'required',
|
||||
'item_id' => 'required',
|
||||
], [
|
||||
'number.required' => '请输入商品数量',
|
||||
'level.required' => '请选择奖项',
|
||||
'chance.required' => '请填写概率',
|
||||
'item_id.required' => '请选择商品',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->error($validator->errors()->first());
|
||||
}
|
||||
|
||||
$data = [
|
||||
'level' => $request->level,
|
||||
'chance' => $request->chance,
|
||||
'item_id' => $request->item_id,
|
||||
'type' => $request->type,
|
||||
'number' => $request->number,
|
||||
'site' => LotteryGift::$chances[$request->level],
|
||||
];
|
||||
|
||||
if ($gift->update($data)) {
|
||||
return $this->success('', 'close');
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
} else {
|
||||
$hasLevels = LotteryGift::where('lottery_id', $gift->lottery_id)->pluck('level')->toArray();
|
||||
|
||||
$params_id = LotteryGift::where('lottery_id', $gift->lottery_id)
|
||||
->where('type', 1)
|
||||
->where('item_type', $gift->item_type)
|
||||
->where('id', '<>', $gift->id)
|
||||
->pluck('item_id')
|
||||
->toArray();
|
||||
|
||||
if ($gift->item_type == 'App\Models\Activity') {
|
||||
$params = Activity::where('status', 1)
|
||||
->whereNotIn('id', $params_id)
|
||||
->get();
|
||||
} elseif ($gift->item_type == 'App\Models\GoodsParams') {
|
||||
$params = GoodsParams::with(['goods'])
|
||||
->whereHas('goods', function ($query) {
|
||||
$query->where('status', 1)->where('is_lottery_gift', 1);
|
||||
})
|
||||
->whereNotIn('id', $params_id)
|
||||
->get();
|
||||
} elseif ($gift->item_type == 'RuLong\Coupon\Models\CouponInfo') {
|
||||
$params = CouponInfo::where('status', 1)->where('type', 'lottery')
|
||||
->whereNotIn('id', $params_id)
|
||||
->get();
|
||||
} else {
|
||||
$params = collect();
|
||||
}
|
||||
|
||||
$levels = LotteryGift::$levels;
|
||||
|
||||
return view('Admin::lottery.editgifts', compact('gift', 'params', 'levels', 'hasLevels'));
|
||||
}
|
||||
}
|
||||
|
||||
//删除奖品
|
||||
public function delgifts(Request $request, LotteryGift $gift)
|
||||
{
|
||||
if ($gift->delete()) {
|
||||
return $this->success();
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user