Example #1
0
 public function actionStafftopbyrange()
 {
     $cur_date = Yii::$app->request->get('cur_date');
     //        $cur_date = '2014-10-31';
     if (empty($cur_date)) {
         $date_start = Yii::$app->request->get('date_start', date("Y-m-d", strtotime("-1 day")));
         $date_end = Yii::$app->request->get('date_end', date("Y-m-d", strtotime("-1 day")));
     } else {
         $date_start = $cur_date;
         $date_end = $cur_date;
     }
     //        $searchModel = new MSceneDetailSearch;
     //        $dataProvider = $searchModel->search(Yii::$app->request->get());
     $query = MSceneDay::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['sum_score' => SORT_DESC]], 'pagination' => ['pageSize' => 400]]);
     $dataProvider->setSort(false);
     /*
             $query->select(['sum(score) as sum_score']);
             $query->andWhere(['wx_scene_day.gh_id' => Yii::$app->user->getGhid()]);
             $query->andWhere('create_date >= :date_start', [':date_start'=>$date_start]);
             $query->andWhere('create_date <= :date_end', [':date_end'=>$date_end]);
             
             $query->andWhere('wx_scene_day.scene_id > 0');
             $query->groupBy(['wx_scene_day.scene_id']);
             $query->orderBy(['sum_score' => SORT_DESC]);        
     */
     $query->select(['*', 'sum(score) as sum_score']);
     $query->andWhere(['gh_id' => Yii::$app->user->getGhid()]);
     $query->andWhere('create_date >= :date_start', [':date_start' => $date_start]);
     $query->andWhere('create_date <= :date_end', [':date_end' => $date_end]);
     $query->andWhere('scene_id > 0');
     $query->groupBy(['scene_id']);
     $query->orderBy(['sum_score' => SORT_DESC]);
     if (!Yii::$app->user->getIsAdmin()) {
         //$query->andWhere(['office_id' => Yii::$app->user->identity->office_id]);
     }
     //      $query->andWhere(['cat' => 0]);
     if (isset($_GET['download'])) {
         $data = $dataProvider->getModels();
         \yii\helpers\ArrayHelper::multisort($data, 'sum_score', SORT_DESC);
         $date = date('Y-m-d-His');
         $filename = Yii::$app->getRuntimePath() . "/Stafftopbyrange-{$date}.csv";
         $csv = new \app\models\ECSVExport($data);
         $attributes = ['staff.name', 'staff.mobile', 'sum_score'];
         $csv->setInclude($attributes);
         $csv->setHeaders(['staff.name' => '推广者', 'staff.mobile' => '手机号码', 'Sum Score' => '推广成绩']);
         $csv->toCSV($filename);
         Yii::$app->response->sendFile($filename);
         return;
     }
     return $this->render('stafftopbyrange', ['dataProvider' => $dataProvider, 'cur_date' => $cur_date, 'date_start' => $date_start, 'date_end' => $date_end]);
 }
Example #2
0
 public function getScoreByMonth($month)
 {
     if ($this->scene_id == 0) {
         $sum_score = 0;
     } else {
         $sum_score = MSceneDay::find()->where(['gh_id' => $this->gh_id, 'scene_id' => $this->scene_id, 'month(create_date)' => $month])->sum('score');
     }
     return empty($sum_score) ? 0 : $sum_score;
 }
Example #3
0
 public static function statSceneDay($date)
 {
     U::W(__METHOD__ . " BEGIN");
     $tableName = MSceneDay::tableName();
     $ghs = MGh::find()->all();
     foreach ($ghs as $gh) {
         foreach ($gh->staffs as $staff) {
             if ($staff->scene_id != 0) {
                 $score = MAccessLog::getScoreByRange($gh->gh_id, $staff->scene_id, $date, $date);
                 if ($score != 0) {
                     Yii::$app->db->createCommand("INSERT INTO {$tableName} (gh_id,create_date,scene_id,score) VALUES (:gh_id,:create_date,:scene_id,:score)", [':gh_id' => $gh->gh_id, ':create_date' => $date, ':scene_id' => $staff->scene_id, ':score' => $score])->execute();
                 }
             }
         }
         // for the fan without anyboby's recommend
         $staff = new MStaff();
         $staff->scene_id = 0;
         $score = MAccessLog::getScoreByRange($gh->gh_id, $staff->scene_id, $date, $date);
         if ($score != 0) {
             Yii::$app->db->createCommand("INSERT INTO {$tableName} (gh_id,create_date,scene_id,score) VALUES (:gh_id,:create_date,:scene_id,:score)", [':gh_id' => $gh->gh_id, ':create_date' => $date, ':scene_id' => $staff->scene_id, ':score' => $score])->execute();
         }
     }
     U::W(__METHOD__ . " END");
     return;
     /*
      $tableName = MAccessLog::tableName();
      $n = Yii::$app->db->createCommand("DELETE FROM $tableName WHERE create_time < DATE_SUB(NOW(), INTERVAL 90 day)")->execute();
      U::W("DELETE $tableName, $n");
     
      $tableName = MAccessLogAll::tableName();
      $n = Yii::$app->db->createCommand("DELETE FROM $tableName WHERE create_time < DATE_SUB(NOW(), INTERVAL 180 day)")->execute();
      U::W("DELETE $tableName, $n");
     */
 }