'卡券', 'user_type' => '发放用户', 'user_list' => '选择用户', ]; } public function edit() { if(!$this->validate()){ return $this->getModelError(); } $coupon = Coupon::findOne(['is_delete' => 0,'id' => $this->coupon_id]); if($coupon == null){ return ['code' => 1,'msg' => '卡券不存在或已被清理']; } if($coupon->end_time <= time()){ return ['code' => 1,'msg' => '卡券已过有效时间']; } if($this->user_type == '1' && empty($this->user_list)){ return ['code' => 1,'msg' =>'您已选择指定用户,请添加发放用户']; } if($this->user_type == '0'){ $user_ids = User::find()->where(['is_delete' => 0])->andWhere(['!=','id',1])->select('id')->column(); }else{ if(empty($this->user_list)){ return ['code' => 1,'msg' =>'您已选择指定用户,请添加发放用户']; } $user_ids = array_column($this->user_list,'id'); } if(empty($user_ids)){ return ['code' => 1,'msg' =>'请检查系统内是否存在用户或是否添加发放用户']; } $array = []; foreach ($user_ids as $item){ array_push($array,[$item,$this->coupon_id,0,time(),time(),0,0,1]); } $key = ['user_id','coupon_id','status','created_at','updated_at','is_delete','deleted_at','is_admin_send']; $chunk = array_chunk($array,300); $success_count = 0; $error_count = 0; foreach ($chunk as $index => $value){ $r = \Yii::$app->db->createCommand()->batchInsert(UserCoupon::tableName(),$key, $value)->execute(); if(!$r){ $error_count += count($value); }else{ $success_count += count($value); } } return ['code' => 0,'msg' =>"成功发放{$success_count}条,发放失败{$error_count}条"]; } }