Esempio n. 1
0
 public function actionImport()
 {
     $full_dir = \Yii::getAlias('@webroot') . "/persontarget";
     $dir = opendir($full_dir);
     while (($file = readdir($dir)) !== false) {
         if ($file !== "." && $file !== "..") {
             $p = pathinfo($file);
             $ftxt = $p['filename'];
             $ftxt = strtolower($ftxt);
             $ext = $p['extension'];
             if (strtolower($ext) == 'txt') {
                 $info = explode("_", $file);
                 $rep = isset($info[1]) ? $info[1] : '';
                 $hos = isset($info[2]) ? $info[2] : '';
                 $model = new SysUploadPersonTarget();
                 $model->file_name = $file;
                 $transaction = \Yii::$app->db->beginTransaction();
                 try {
                     $sql = "LOAD DATA LOCAL INFILE '{$full_dir}/{$file}'";
                     $sql .= " REPLACE INTO TABLE person_target";
                     $sql .= " FIELDS TERMINATED BY '|'  LINES TERMINATED BY '\r\n' IGNORE 1 LINES";
                     $sql .= " SET rep_year='{$rep}'";
                     $count = \Yii::$app->db->createCommand($sql)->execute();
                     $transaction->commit();
                     $model->note1 = substr($hos, 0, 5);
                     $model->note2 = strval($count);
                     $model->note3 = substr($rep, 0, 4);
                     $model->upload_date = date('Ymd');
                     $model->upload_time = date('His');
                     $model->save();
                     echo "Import {$file} Success!\r\n<br>";
                 } catch (Exception $e) {
                     $transaction->rollBack();
                     echo $e->message();
                 }
             }
         }
     }
     closedir($dir);
     $dir = opendir($full_dir);
     while (($file = readdir($dir)) !== false) {
         if ($file !== "." && $file !== "..") {
             if ($file !== "." && $file !== "..") {
                 unlink("{$full_dir}/{$file}");
             }
         }
     }
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = SysUploadPersonTarget::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 15]]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id]);
     $query->andFilterWhere(['like', 'file_name', $this->file_name])->andFilterWhere(['like', 'file_size', $this->file_size])->andFilterWhere(['like', 'upload_date', $this->upload_date])->andFilterWhere(['like', 'upload_time', $this->upload_time])->andFilterWhere(['like', 'note1', $this->note1])->andFilterWhere(['like', 'note2', $this->note2])->andFilterWhere(['like', 'note3', $this->note3])->andFilterWhere(['like', 'note4', $this->note4])->andFilterWhere(['like', 'note5', $this->note5]);
     return $dataProvider;
 }