Explorar el Código

优惠券管理选择部分产品可用,但不能选可用类型(多选)(12.18陈武杰)

ChenWuJie hace 4 años
padre
commit
eb1b645ca8

+ 18 - 3
app/Admin/Actions/backstage/Coupon/CouponBatchGrant.php

xqd xqd xqd
@@ -3,6 +3,7 @@
 namespace App\Admin\Actions\backstage\Coupon;
 
 use App\Models\Coupon;
+use App\Models\CouponType;
 use App\Models\User;
 use App\Models\UserCoupon;
 use Encore\Admin\Actions\BatchAction;
@@ -44,7 +45,7 @@ class CouponBatchGrant extends BatchAction
                 'expire_type' => $coupon->expire_type,
                 'effective_days' => $coupon->effective_days,
                 'expire_time' => $expire_time_num,
-                'coupons_num'=>0
+//                'coupons_num'=>0
             ];
         }else if ($coupon->expire_type == 2){
             $arr = [
@@ -64,12 +65,26 @@ class CouponBatchGrant extends BatchAction
                 'expire_type' => $coupon->expire_type,
                 'start_time' => $coupon->start_time,
                 'end_time' => $coupon->end_time,
-                'coupons_num'=>0
+//                'coupons_num'=>0
             ];
         }
+        //拿到类型数组
+        $type = CouponType::where('coupon_id',$coupon_id)->where('user_coupon_id',0)->pluck('product_type');
+
         foreach ($collection as $model) {
+            //插入到用户优惠券中
             $arr['user_id'] = $model->id;
-            UserCoupon::insert($arr);
+            //获取到插入之后的id
+            $insert_id = UserCoupon::create($arr)->id;
+            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]);
         }

+ 17 - 1
app/Admin/Controllers/CouponManagement/CouponManagementController.php

xqd
@@ -127,7 +127,23 @@ class CouponManagementController extends AdminController
             });
         $form->select('position_type',__('发放平台'))->options([1=>'领券中心',2=>'后台发放']);
         $form->number('num', __('数量'));
-        $form->select('usable_type',__('可用类型'))->options([1=>'全部产品通用',2=>'部分产品可用']);
+        $form->select('usable_type',__('可用类型'))
+            ->options([
+                1=>'全部产品通用',
+                2=>'部分产品可用'
+            ])->when(2,function (Form $form){
+                $form->hasMany('CouponType','可用类型',function (Form\NestedForm $form){
+                    $form->select('product_type','可用类型')->options([
+                        1=>'电话咨询',
+                        2=>'图文咨询',
+                        3=>'门诊咨询',
+                        4=>'疫苗接种',
+                        5=>'儿保预约',
+                        6=>'服务包',
+                        7=>'充值',
+                    ]);
+                });
+            });
         $form->select('expire_type', '过期类型')
             ->options([
                 1 => '领取N天过期',

+ 3 - 0
app/Models/Coupon.php

xqd
@@ -23,4 +23,7 @@ class Coupon extends BaseModel
 
         return 0;
     }
+    public function couponType (){
+        return $this->hasMany(CouponType::class);
+    }
 }

+ 3 - 1
app/Models/CouponType.php

xqd
@@ -10,5 +10,7 @@ namespace App\Models;
 
 class CouponType extends BaseModel
 {
-
+    public function coupon (){
+        return $this->belongsTo(Coupon::class);
+    }
 }