146 lines
4.5 KiB
PHP
146 lines
4.5 KiB
PHP
<?php
|
|
namespace App\Admin\Controllers;
|
|
|
|
use Admin;
|
|
use App\Models\Seller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use RuLong\Coupon\Models\CouponInfo;
|
|
use Validator;
|
|
|
|
class CouponController extends Controller
|
|
{
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$title = $request->title;
|
|
$orderField = $request->orderField;
|
|
$orderDirection = $request->orderDirection;
|
|
$numPerPage = $request->numPerPage ?: 30;
|
|
|
|
$coupons = CouponInfo::where('seller_id', Admin::user()->seller_id)
|
|
->when($title, function ($query) use ($title) {
|
|
$query->where('title', 'like', "%{$title}%");
|
|
})
|
|
->when($orderField, function ($query) use ($orderField, $orderDirection) {
|
|
$query->orderBy($orderField, $orderDirection);
|
|
})
|
|
->orderBy('created_at', 'desc')
|
|
->paginate($numPerPage);
|
|
return view('Admin::coupon.index', compact('coupons'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$sellers = Seller::where('status', 1)->get();
|
|
return view('Admin::coupon.create', compact('sellers'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'title' => 'required|min:2|max:50',
|
|
'remark' => 'required|min:4',
|
|
'bouns' => 'required',
|
|
], [
|
|
'title.required' => '标题必须填写',
|
|
'title.min' => '标题最少为:min字符',
|
|
'title.max' => '标题最多为:max字符',
|
|
'remark.required' => '券码描述必须填写',
|
|
'remark.min' => '券码描述最少:min个字',
|
|
'bouns.required' => '享用额度必须填写',
|
|
'bouns.number' => '享用额度必须是数字',
|
|
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return $this->error($validator->errors()->first());
|
|
}
|
|
|
|
try {
|
|
DB::transaction(function () use ($request) {
|
|
$status = 0;
|
|
if (Admin::user()->seller_id == 1) {
|
|
$status = 1;
|
|
}
|
|
$info = CouponInfo::create($request->all());
|
|
});
|
|
return $this->success('操作成功', 'close');
|
|
} catch (\Exception $e) {
|
|
return $this->error('操作失败' . $e->getmessage());
|
|
}
|
|
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$info = CouponInfo::find($id);
|
|
return view('Admin::coupon.edit', compact('info'));
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$info = CouponInfo::find($id);
|
|
$validator = Validator::make($request->all(), [
|
|
'title' => 'required|min:2|max:50',
|
|
'remark' => 'required|min:4',
|
|
'bouns' => 'required',
|
|
], [
|
|
'title.required' => '标题必须填写',
|
|
'title.min' => '标题最少为:min字符',
|
|
'title.max' => '标题最多为:max字符',
|
|
'remark.required' => '券码描述必须填写',
|
|
'remark.min' => '券码描述最少:min个字',
|
|
'bouns.required' => '享用额度必须填写',
|
|
'bouns.number' => '享用额度必须是数字',
|
|
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return $this->error($validator->errors()->first());
|
|
}
|
|
|
|
try {
|
|
DB::transaction(function () use ($request, $info) {
|
|
$info = $info->update($request->all());
|
|
});
|
|
return $this->success('操作成功', 'close');
|
|
} catch (\Exception $e) {
|
|
return $this->error('操作失败' . $e->getmessage());
|
|
}
|
|
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$info = CouponInfo::find($id);
|
|
try {
|
|
$info->status = 0;
|
|
$info->save();
|
|
return $this->success('操作成功');
|
|
} catch (\Exception $e) {
|
|
return $this->error('操作失败' . $e->getmessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 彻底删除单条目
|
|
* @Author:<C.Jason>
|
|
* @Date:2018-11-02T13:43:36+0800
|
|
* @param [type] $user [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function delete(CouponInfo $info)
|
|
{
|
|
try {
|
|
DB::transaction(function () use ($info) {
|
|
$info->delete();
|
|
});
|
|
return $this->success('操作成功');
|
|
} catch (\Exception $e) {
|
|
return $this->error('操作失败' . $e->getmessage());
|
|
}
|
|
|
|
}
|
|
}
|