Browse Source

优惠券管理

ChenWuJie 4 năm trước cách đây
mục cha
commit
2ec707e41f

+ 58 - 0
app/Admin/Actions/backstage/Coupon/CouponBatchGrant.php

xqd
@@ -0,0 +1,58 @@
+<?php
+
+namespace App\Admin\Actions\backstage\Coupon;
+
+use App\Models\Coupon;
+use App\Models\User;
+use App\Models\UserCoupon;
+use Encore\Admin\Actions\BatchAction;
+use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Http\Request;
+
+class CouponBatchGrant extends BatchAction
+{
+    public $name = '优惠券批量发放';
+    public function handle(Collection $collection,Request $request)
+    {
+        $coupon_id = request('id');
+        $user_arr = $collection->toArray();
+        $user_num = count($user_arr);
+        $coupon = Coupon::where('id',$coupon_id)->first();
+        if ($coupon->num < $user_num)
+        {
+            return $this->response()->error('优惠券数量不足')->refresh();
+        }
+        $arr = [
+            'user_id' => 0,
+            'coupon_id'=>$coupon_id,
+            'name' => $coupon->name,
+            'title' => $coupon->title,
+            'desc' => $coupon->desc,
+            'rules' => $coupon->rules,
+            'icon' => $coupon->icon,
+            'type' => $coupon->type,
+            'usable_type' => $coupon->usable_type,
+            'money' => $coupon->money,
+            'discount' => $coupon->discount,
+            'min_consume_amount' => $coupon->min_consume_amount,
+            'max_reduce_amount' => $coupon->max_reduce_amount,
+            'expire_type' => $coupon->expire_type,
+            'effective_days' => $coupon->effective_days,
+            'start_time' => $coupon->start_time,
+            'end_time' => $coupon->end_time,
+            'coupons_num'=>0
+        ];
+        foreach ($collection as $model) {
+            $arr['user_id'] = $model->id;
+            UserCoupon::insert($arr);
+            $coupon = Coupon::where('id',$coupon_id)->first();
+            Coupon::where('id',$coupon_id)->update(['num'=>$coupon->num-1]);
+        }
+        return $this->response()->success('优惠券发放成功')->refresh();
+    }
+    public function form()
+    {
+        $name = Coupon::pluck('name','id');
+        $this->select('id', '优惠券名称')->options($name);
+    }
+}

+ 19 - 0
app/Admin/Actions/backstage/Coupon/CouponDetails.php

xqd
@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Admin\Actions\backstage\Coupon;
+
+use Encore\Admin\Actions\RowAction;
+use Illuminate\Database\Eloquent\Model;
+
+class CouponDetails extends RowAction
+{
+    public $name = '优惠券详情';
+
+    public function handle(Model $model)
+    {
+        // $model ...
+
+        return $this->response()->success('跳转订单详情成功')->redirect('/admin/coupons_details?id='.$this->row->id.'');
+    }
+
+}

+ 19 - 0
app/Admin/Actions/backstage/Coupon/CouponDistribution.php

xqd
@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Admin\Actions\backstage\Coupon;
+
+use Encore\Admin\Actions\RowAction;
+use Illuminate\Database\Eloquent\Model;
+
+class CouponDistribution extends RowAction
+{
+    public $name = '优惠券发放';
+
+    public function handle(Model $model)
+    {
+        // $model ...
+
+        return $this->response()->success('跳转订单详情成功')->redirect('/admin/coupon_distribution_user_list?coupon_id='.$this->row->id.'');
+    }
+
+}

+ 103 - 0
app/Admin/Controllers/CouponManagement/CouponDetailsController.php

xqd
@@ -0,0 +1,103 @@
+<?php
+
+namespace App\Admin\Controllers\CouponManagement;
+
+use App\Models\Coupon;
+use Encore\Admin\Controllers\AdminController;
+use Encore\Admin\Form;
+use Encore\Admin\Grid;
+use Encore\Admin\Show;
+
+class CouponDetailsController extends AdminController
+{
+    /**
+     * Title for current resource.
+     *
+     * @var string
+     */
+    protected $title = '优惠券详情';
+
+    /**
+     * Make a grid builder.
+     *
+     * @return Grid
+     */
+    protected function grid()
+    {
+        $grid = new Grid(new Coupon());
+
+        $grid->column('id', __('Id'));
+        $grid->column('name', __('券名称'));
+        $grid->column('title', __('券标题'));
+        $grid->column('desc', __('使用说明'))->limit(20,'...');
+        $grid->column('rules', __('使用规则'))->limit(20,'...');
+        $grid->column('min_consume_amount', __('最低消费金额'))->display(function ($money){
+            return $money/100;
+        });
+        $grid->column('max_reduce_amount', __('折扣券的最大抵扣金额'))->display(function ($money){
+            return $money/100;
+        });
+
+        return $grid;
+    }
+
+    /**
+     * Make a show builder.
+     *
+     * @param mixed $id
+     * @return Show
+     */
+    protected function detail($id)
+    {
+        $show = new Show(Coupon::findOrFail($id));
+
+        $show->field('id', __('Id'));
+        $show->field('name', __('Name'));
+        $show->field('title', __('Title'));
+        $show->field('desc', __('Desc'));
+        $show->field('rules', __('Rules'));
+        $show->field('icon', __('Icon'));
+        $show->field('type', __('Type'));
+        $show->field('usable_type', __('Usable type'));
+        $show->field('money', __('Money'));
+        $show->field('discount', __('Discount'));
+        $show->field('min_consume_amount', __('Min consume amount'));
+        $show->field('max_reduce_amount', __('Max reduce amount'));
+        $show->field('expire_type', __('Expire type'));
+        $show->field('effective_days', __('Effective days'));
+        $show->field('start_time', __('Start time'));
+        $show->field('end_time', __('End time'));
+        $show->field('created_at', __('Created at'));
+        $show->field('updated_at', __('Updated at'));
+
+        return $show;
+    }
+
+    /**
+     * Make a form builder.
+     *
+     * @return Form
+     */
+    protected function form()
+    {
+        $form = new Form(new Coupon());
+
+        $form->text('name', __('Name'));
+        $form->text('title', __('Title'));
+        $form->text('desc', __('Desc'));
+        $form->text('rules', __('Rules'));
+        $form->text('icon', __('Icon'));
+        $form->switch('type', __('Type'));
+        $form->switch('usable_type', __('Usable type'));
+        $form->number('money', __('Money'));
+        $form->decimal('discount', __('Discount'))->default(0.00);
+        $form->number('min_consume_amount', __('Min consume amount'));
+        $form->number('max_reduce_amount', __('Max reduce amount'));
+        $form->switch('expire_type', __('Expire type'));
+        $form->number('effective_days', __('Effective days'));
+        $form->number('start_time', __('Start time'));
+        $form->number('end_time', __('End time'));
+
+        return $form;
+    }
+}

+ 140 - 0
app/Admin/Controllers/CouponManagement/CouponDistributionUserList.php

xqd
@@ -0,0 +1,140 @@
+<?php
+
+namespace App\Admin\Controllers\CouponManagement;
+
+use App\Admin\Actions\backstage\Coupon\CouponBatchGrant;
+use App\Models\User;
+use Encore\Admin\Controllers\AdminController;
+use Encore\Admin\Form;
+use Encore\Admin\Grid;
+use Encore\Admin\Show;
+
+class CouponDistributionUserList extends AdminController
+{
+    /**
+     * Title for current resource.
+     *
+     * @var string
+     */
+    protected $title = '优惠券发放用户列表';
+
+    /**
+     * Make a grid builder.
+     *
+     * @return Grid
+     */
+    protected function grid()
+    {
+        $grid = new Grid(new User());
+        $coupon_id = request('coupon_id');
+        $grid->filter(function($filter){
+            // 去掉默认的id过滤器
+            $filter->disableIdFilter();
+            $filter->equal('phone','手机')->mobile()->placeholder("请输入手机");
+            $filter->like('patient.name','患者姓名')->placeholder("请输入患者姓名");
+            $filter->like('nickname','昵称');
+            $filter->equal('status','用户状态')->radio([
+                ''   => '不限',
+                0    => '黑名单',
+                1    => '正常',
+            ]);
+            $filter->equal('is_pack','用户状态')->radio([
+                ''   => '全部用户',
+                0    => '普通用户',
+                1    => '服务包用户',
+            ]);
+        });
+        $grid->batchActions(function ($batch) {
+            $batch->add(new CouponBatchGrant($_GET['coupon_id']));
+        });
+        $grid->actions(function ($actions) {
+            // 去掉删除
+            $actions->disableDelete();
+            // 去掉编辑
+            $actions->disableEdit();
+            // 去掉查看
+            $actions->disableView();
+        });
+        $grid ->model()->where('status','>=','0');
+        $grid->column('id', __('用户id'))->sortable();
+        $grid->column('nickname', __('用户名'));
+        $grid->column('sex', __('性别'))->using([0=>'未知',1=>'男',2=>'女']);
+        $grid->column('age', __('年龄'));
+        $grid->column('status', __('状态'))->using([0=>'黑名单',1=>'正常',]);
+        $grid->column('is_pack', __('用户身份'))->using([0=>'普通用户',1=>'付费用户']);
+        $grid->column('balance', __('当前余额'))->display(function ($price){
+            return $price/100;
+        });
+        $grid->column('created_at', __('注册时间'));
+        $grid->column('last_login_time', __('最后登录时间'))->display(function ($time){
+            return date("Y-m-d H:i",$time);
+        });
+//            $grid->column('patient', __('患者姓名'))->pluck('name')->map('ucwords')->implode(',');
+        $grid->column('patient', __('患者姓名'))->pluck('name')->label('default');
+        return $grid;
+    }
+
+    /**
+     * Make a show builder.
+     *
+     * @param mixed $id
+     * @return Show
+     */
+    protected function detail($id)
+    {
+        $show = new Show(User::findOrFail($id));
+
+        $show->field('id', __('Id'));
+        $show->field('nickname', __('Nickname'));
+        $show->field('phone', __('Phone'));
+        $show->field('sex', __('Sex'));
+        $show->field('avatar', __('Avatar'));
+        $show->field('openid', __('Openid'));
+        $show->field('remark', __('Remark'));
+        $show->field('birthday', __('Birthday'));
+        $show->field('status', __('Status'));
+        $show->field('balance', __('Balance'));
+        $show->field('giving_balance', __('Giving balance'));
+        $show->field('topup_balance', __('Topup balance'));
+        $show->field('session_key', __('Session key'));
+        $show->field('pay_password', __('Pay password'));
+        $show->field('latitude', __('Latitude'));
+        $show->field('longitude', __('Longitude'));
+        $show->field('is_pack', __('Is pack'));
+        $show->field('last_login_time', __('Last login time'));
+        $show->field('created_at', __('Created at'));
+        $show->field('updated_at', __('Updated at'));
+
+        return $show;
+    }
+
+    /**
+     * Make a form builder.
+     *
+     * @return Form
+     */
+    protected function form()
+    {
+        $form = new Form(new User());
+
+        $form->text('nickname', __('Nickname'));
+        $form->mobile('phone', __('Phone'));
+        $form->switch('sex', __('Sex'));
+        $form->image('avatar', __('Avatar'));
+        $form->text('openid', __('Openid'));
+        $form->text('remark', __('Remark'));
+        $form->text('birthday', __('Birthday'));
+        $form->switch('status', __('Status'))->default(1);
+        $form->number('balance', __('Balance'));
+        $form->number('giving_balance', __('Giving balance'));
+        $form->number('topup_balance', __('Topup balance'));
+        $form->text('session_key', __('Session key'));
+        $form->text('pay_password', __('Pay password'));
+        $form->decimal('latitude', __('Latitude'))->default(0.0000000);
+        $form->decimal('longitude', __('Longitude'))->default(0.0000000);
+        $form->switch('is_pack', __('Is pack'));
+        $form->number('last_login_time', __('Last login time'));
+
+        return $form;
+    }
+}

+ 128 - 0
app/Admin/Controllers/CouponManagement/CouponManagementController.php

xqd
@@ -0,0 +1,128 @@
+<?php
+
+namespace App\Admin\Controllers\CouponManagement;
+
+use App\Admin\Actions\backstage\Coupon\CouponDetails;
+use App\Admin\Actions\backstage\Coupon\CouponDistribution;
+use App\Models\Coupon;
+use Encore\Admin\Controllers\AdminController;
+use Encore\Admin\Form;
+use Encore\Admin\Grid;
+use Encore\Admin\Show;
+
+class CouponManagementController extends AdminController
+{
+    /**
+     * Title for current resource.
+     *
+     * @var string
+     */
+    protected $title = '优惠券管理';
+
+    /**
+     * Make a grid builder.
+     *
+     * @return Grid
+     */
+    protected function grid()
+    {
+        $grid = new Grid(new Coupon());
+
+        $grid->actions(function ($actions) {
+            $actions->disableView();
+            $actions->add(new CouponDetails());
+            if($actions->row->position_type == 2 )
+            {
+                $actions->add(new CouponDistribution());
+            }
+        });
+
+        $grid->column('id', __('Id'));
+        $grid->column('name', __('劵名称'));
+        $grid->column('title', __('劵标题'));
+        $grid->column('icon', __('券图标'))->image();
+        $grid->column('type', __('类型'))->using([1=>'满减券',2=>'折扣券']);
+        $grid->column('usable_type', __('可用类型'))->using([1=>'全部产品通用',2=>'部分产品可用']);
+        $grid->column('money', __('满减券的优惠钱数'))->display(function ($money){
+            return $money/100;
+        });
+        $grid->column('discount', __('折扣'));
+        $grid->column('position_type', __('发放平台'))->using([1=>'领券中心',2=>'后台发放']);
+        $grid->column('num', __('数量'));
+        $grid->column('effective_days', __('有效天数'));
+        $grid->column('start_time', __('有效期开始时间'))->display(function ($time){
+            return date('Y-m-d H:i:s',$time);
+        });
+        $grid->column('end_time', __('有效期结束时间'))->display(function ($time){
+            return date('Y-m-d H:i:s',$time);
+        });
+        return $grid;
+    }
+
+    /**
+     * Make a show builder.
+     *
+     * @param mixed $id
+     * @return Show
+     */
+    protected function detail($id)
+    {
+        $show = new Show(Coupon::findOrFail($id));
+
+        $show->field('id', __('Id'));
+        $show->field('name', __('Name'));
+        $show->field('title', __('Title'));
+        $show->field('desc', __('Desc'));
+        $show->field('rules', __('Rules'));
+        $show->field('icon', __('Icon'));
+        $show->field('type', __('Type'));
+        $show->field('usable_type', __('Usable type'));
+        $show->field('money', __('Money'));
+        $show->field('discount', __('Discount'));
+        $show->field('min_consume_amount', __('Min consume amount'));
+        $show->field('max_reduce_amount', __('Max reduce amount'));
+        $show->field('expire_type', __('Expire type'));
+        $show->field('effective_days', __('Effective days'));
+        $show->field('start_time', __('Start time'));
+        $show->field('end_time', __('End time'));
+        $show->field('created_at', __('Created at'));
+        $show->field('updated_at', __('Updated at'));
+
+        return $show;
+    }
+
+    /**
+     * Make a form builder.
+     *
+     * @return Form
+     */
+    protected function form()
+    {
+        $form = new Form(new Coupon());
+
+        $form->text('name', __('优惠券名称'));
+        $form->text('title', __('券标题'));
+        $form->text('desc', __('使用说明'));
+        $form->text('rules', __('使用规则'));
+        $form->image('icon', __('券图标'));
+        $form->select('type', '类型')
+            ->options([
+                1 => '满减券',
+                2 => '折扣券',
+            ])->when(1, function (Form $form) {
+                $form->text('money', '满减券的优惠钱数');
+                $form->text('min_consume_amount', '最低消费金额');
+            })->when(2, function (Form $form) {
+                $form->text('discount', '折扣');
+                $form->text('max_reduce_amount', '折扣券的最大抵扣金额');
+            });
+        $form->select('position_type',__('发放平台'))->options([1=>'领券中心',2=>'后台发放']);
+        $form->number('num', __('数量'));
+        $form->select('usable_type',__('可用类型'))->options([1=>'全部产品通用',2=>'部分产品可用']);
+        $form->select('expire_type', __('过期类型'))->options([1=>'领取N天过期',2=>'设置固定过期时间']);
+        $form->text('effective_days', __('有效天数'));
+        $form->date('start_time', __('有效期开始时间'));
+        $form->date('end_time', __('有效期结束时间'));
+        return $form;
+    }
+}

+ 0 - 1
app/Admin/Controllers/UserCouponsController.php

xqd
@@ -65,7 +65,6 @@ class UserCouponsController extends AdminController
             return date("Y-m-d H:i",$time);
         });
         $grid->column('use_time', __('使用时间'));
-        $grid->column('coupons_num', __('优惠券数'));
         $grid->column('created_at', __('创建时间'));
         $grid->column('updated_at', __('更新时间'));