| xqd
@@ -16,83 +16,92 @@ class CouponBatchGrant extends BatchAction
|
|
|
public function handle(Collection $collection,Request $request)
|
|
|
{
|
|
|
$coupon_id = request('id');
|
|
|
+ //优惠券数量
|
|
|
+ $coupon_num = request('num');
|
|
|
$user_arr = $collection->toArray();
|
|
|
+ //用户数量
|
|
|
$user_num = count($user_arr);
|
|
|
$coupon = Coupon::where('id',$coupon_id)->first();
|
|
|
- if ($coupon->num < $user_num)
|
|
|
+ $all_num = $coupon_num*$user_num;
|
|
|
+// dd($all_num);
|
|
|
+ //判断优惠券是否充足 优惠券数量*用户数量
|
|
|
+ if ($coupon->num < $all_num)
|
|
|
{
|
|
|
- return $this->response()->error('优惠券数量不足')->refresh();
|
|
|
+ return $this->response()->error('优惠券'.Coupon::where('id',$coupon_id)->value('name').'数量不足')->refresh();
|
|
|
}
|
|
|
- //获取过期类型是指定几天过期
|
|
|
- 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,
|
|
|
+ for ($i = 0;$i<$all_num;$i++)
|
|
|
+ {
|
|
|
+ //获取过期类型是指定几天过期
|
|
|
+ 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,
|
|
|
+ ];
|
|
|
+ }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)
|
|
|
+ //拿到类型数组
|
|
|
+ $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)
|
|
|
{
|
|
|
- $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]);
|
|
|
}
|
|
|
- $coupon = Coupon::where('id',$coupon_id)->first();
|
|
|
- Coupon::where('id',$coupon_id)->update(['num'=>$coupon->num-1]);
|
|
|
}
|
|
|
return $this->response()->success('优惠券发放成功')->refresh();
|
|
|
}
|
| xqd
@@ -101,5 +110,6 @@ class CouponBatchGrant extends BatchAction
|
|
|
{
|
|
|
$name = Coupon::where('position_type',2)->pluck('name','id');
|
|
|
$this->select('id', '优惠券名称')->options($name);
|
|
|
+ $this->text('num', '优惠券数量');
|
|
|
}
|
|
|
}
|