Procházet zdrojové kódy

Merge branch 'master' of http://git.9026.com/swdz-WangHaijun/BaoMa

Swdz-WangHaiJun před 4 roky
rodič
revize
ee07632f05

+ 114 - 0
app/Admin/Actions/backstage/Coupon/AllCouponBatchGrant.php

xqd
@@ -0,0 +1,114 @@
+<?php
+
+namespace App\Admin\Actions\backstage\Coupon;
+
+use App\Models\Coupon;
+use App\Models\CouponType;
+use App\Models\UserCoupon;
+use Encore\Admin\Actions\BatchAction;
+use Illuminate\Database\Eloquent\Collection;
+
+class AllCouponBatchGrant extends BatchAction
+{
+    public $name = '后台所有优惠券批量发放';
+
+    public function handle(Collection $collection)
+    {
+        //计算总共要发放多少个用户
+        $user_arr = $collection->toArray();
+        $user_num = count($user_arr);
+        //获取所有类型为后台发放的优惠券
+        $allcoupon = Coupon::where('position_type',2)->get();
+        $coupon_arr = $allcoupon->toArray();
+        //判断优惠券数量是否充足
+        foreach ($coupon_arr as $value)
+        {
+            if ($value['num'] < $user_num)
+            {
+                return $this->response()->error('优惠券'.$value['name'].'的数量不足')->refresh();
+            }
+        }
+
+        foreach ($coupon_arr as $coupon)
+        {
+            //拿到基础数据
+            //获取过期类型是指定几天过期
+            if ($coupon['expire_type'] == 1){
+                $effective_days = $coupon['effective_days'];
+                $str_time = '+'.$effective_days.'day';
+                $expire_time_num = strtotime($str_time);
+                $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'],
+                    'expire_time' => $expire_time_num,
+//                'coupons_num'=>0
+                ];
+            }else if ($coupon->expire_type == 2){
+                //设置有效期的基础数据
+                $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'],
+                    'start_time' => $coupon['start_time'],
+                    'end_time' => $coupon['end_time'],
+//                'coupons_num'=>0
+                ];
+            }
+
+            //如果是部分产品可用,才有类型数组
+            if ($coupon['usable_type'] == 2)
+            //获取类型数组
+            {
+                $type = CouponType::where('coupon_id',$coupon['id'])->where('user_coupon_id',0)->pluck('product_type');
+            }
+            foreach ($collection as $model) {
+                //插入到用户优惠券中
+                $arr['user_id'] = $model->id;
+                //获取到插入之后的id
+                $insert_id = UserCoupon::create($arr)->id;
+                //如果是部分产品可用,则要插入可用类型到优惠券可用类型表
+                if ($coupon['usable_type'] == 2)
+                {
+                    foreach ($type as $value)
+                    {
+                        $coupon_type_arr = [
+                            'coupon_id' => 0,
+                            'user_coupon_id' =>$insert_id,
+                            'product_type' =>$value
+                        ];
+                        CouponType::create($coupon_type_arr);
+                    }
+                }
+                $coupon = Coupon::where('id',$coupon['id'])->first();
+                Coupon::where('id',$coupon['id'])->update(['num'=>$coupon['num']-1]);
+            }
+        }
+        return $this->response()->success('优惠券发放成功')->refresh();
+
+    }
+
+}

+ 16 - 9
app/Admin/Actions/backstage/Coupon/CouponBatchGrant.php

xqd
@@ -68,28 +68,35 @@ class CouponBatchGrant extends BatchAction
 //                'coupons_num'=>0
             ];
         }
-        //拿到类型数组
-        $type = CouponType::where('coupon_id',$coupon_id)->where('user_coupon_id',0)->pluck('product_type');
+        if ($coupon->usable_type == 2)
+        {
+            //拿到类型数组
+            $type = CouponType::where('coupon_id',$coupon_id)->where('user_coupon_id',0)->pluck('product_type');
+        }
 
         foreach ($collection as $model) {
             //插入到用户优惠券中
             $arr['user_id'] = $model->id;
             //获取到插入之后的id
             $insert_id = UserCoupon::create($arr)->id;
-            foreach ($type as $value)
+            if ($coupon->usable_type == 2)
             {
-                $coupon_type_arr = [
-                    'coupon_id' => 0,
-                    'user_coupon_id' =>$insert_id,
-                    'product_type' =>$value
-                ];
-                CouponType::create($coupon_type_arr);
+                foreach ($type as $value)
+                {
+                    $coupon_type_arr = [
+                        'coupon_id' => 0,
+                        'user_coupon_id' =>$insert_id,
+                        'product_type' =>$value
+                    ];
+                    CouponType::create($coupon_type_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::where('position_type',2)->pluck('name','id');

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

xqd xqd
@@ -2,6 +2,7 @@
 
 namespace App\Admin\Controllers\CouponManagement;
 
+use App\Admin\Actions\backstage\Coupon\AllCouponBatchGrant;
 use App\Admin\Actions\backstage\Coupon\CouponBatchGrant;
 use App\Models\User;
 use Encore\Admin\Controllers\AdminController;
@@ -44,6 +45,7 @@ class CouponDistributionUserList extends AdminController
         });
         $grid->batchActions(function ($batch) {
             $batch->add(new CouponBatchGrant());
+            $batch->add(new AllCouponBatchGrant());
         });
         $grid->disableActions();
         $grid ->model()->where('status','>=','0');

+ 30 - 0
app/Admin/Controllers/UserManagement/BmUser/UserCouponsController.php

xqd
@@ -40,6 +40,36 @@ class UserCouponsController extends AdminController
         $grid->column('title', __('券标题'));
         $grid->column('type', __('类型'))->using([1=>'满减券',2=>'折扣券']);
         $grid->column('usable_type', __('可用类型'))->using([1=>'全部产品通用',2=>'部分产品可用']);
+        $grid->column('CouponType', __('可用产品'))->pluck('product_type','id')->display(function ($i){
+            $name = [];
+            foreach ($i as $value)
+            {
+                switch ($value){
+                    case 1:
+                        array_push($name,'电话咨询');
+                        break;
+                    case 2:
+                        array_push($name,'图文咨询');
+                        break;
+                    case 3:
+                        array_push($name,'门诊咨询');
+                        break;
+                    case 4:
+                        array_push($name,'疫苗接种');
+                        break;
+                    case 5:
+                        array_push($name,'儿保预约');
+                        break;
+                    case 6:
+                        array_push($name,'服务包');
+                        break;
+                    case 7:
+                        array_push($name,'充值');
+                        break;
+                }
+            }
+            return $name;
+        })->label('info');
         $grid->column('money', __('满减券的优惠钱数'))->display(function ($money){
             return $money/100;
         });

+ 3 - 0
app/Models/CouponType.php

xqd
@@ -13,4 +13,7 @@ class CouponType extends BaseModel
     public function coupon (){
         return $this->belongsTo(Coupon::class);
     }
+    public function userCoupon (){
+        return $this->belongsTo(UserCoupon::class);
+    }
 }

+ 4 - 0
app/Models/UserCoupon.php

xqd
@@ -47,4 +47,8 @@ class UserCoupon extends BaseModel
 
         return $discountAmount;
     }
+
+    public function couponType (){
+        return $this->hasMany(CouponType::class);
+    }
 }