Exemplo n.º 1
0
 /**
  * This command import office supervision data.
  * @param string $filename the file to be imported to DB.
  */
 public function actionSupervisor($filename = 'supervisor.csv')
 {
     $file = Yii::$app->getRuntimePath() . DIRECTORY_SEPARATOR . 'imported_data' . DIRECTORY_SEPARATOR . $filename;
     $fh = fopen($file, "r");
     $i = 0;
     while (!feof($fh)) {
         $line = fgets($fh);
         $i++;
         if (empty($line) || trim($line) == '') {
             continue;
         }
         $fields = explode(",", $line);
         $office_name = trim($fields[0]);
         $office_name_utf8 = iconv('GBK', 'UTF-8//IGNORE', $office_name);
         $msc_name = trim(trim($fields[1]), "0..9");
         $msc_name_utf8 = iconv('GBK', 'UTF-8//IGNORE', $msc_name);
         $region_name = trim(trim($fields[2]), "0..9");
         $region_name_utf8 = iconv('GBK', 'UTF-8//IGNORE', $region_name);
         $supervisor_name = trim($fields[3]);
         $supervisor_name_utf8 = iconv('GBK', 'UTF-8//IGNORE', $supervisor_name);
         $supervisor_mobile = trim(isset($fields[4]) ? $fields[4] : '');
         $comment = trim(isset($fields[5]) ? $fields[5] : '');
         $comment_utf8 = iconv('GBK', 'UTF-8//IGNORE', $comment);
         //            echo "comment = ".$comment.", comment_utf8 = ".$comment_utf8.PHP_EOL;
         $need2delete = false;
         if (mb_strpos($comment_utf8, '删除') === false) {
         } else {
             $need2delete = true;
         }
         if ($need2delete) {
             //                echo "deleting ......".PHP_EOL;
             if (!empty($office_name_utf8) && $office_name_utf8 != '') {
                 $office = MOffice::findOne(['title' => $office_name_utf8, 'gh_id' => \app\models\MGh::GH_XIANGYANGUNICOM]);
                 if (!empty($office)) {
                     // 删除督导关系
                     //                       echo "deleting supervision relation ...".$office->title.PHP_EOL;
                     yii::$app->db->createCommand()->delete('wx_rel_supervision_staff_office', ['office_id' => $office->office_id])->execute();
                     // 删除营服所属关系
                     if (!empty($office->msc)) {
                         //                            echo "deleting MSC/MR relation ...".$office->title.PHP_EOL;
                         $office->msc->updateCounters(['office_total_count' => -1]);
                         $office->msc->marketingRegion->updateCounters(['office_total_count' => -1]);
                         if ($office->msc->marketingRegion->office_total_count == 0) {
                             $office->msc->marketingRegion->delete();
                         }
                         if ($office->msc->office_total_count == 0) {
                             $office->msc->delete();
                         }
                         yii::$app->db->createCommand()->delete('wx_rel_office_msc', ['office_id' => $office->office_id])->execute();
                     }
                 }
             }
             continue;
         }
         if (empty($region_name_utf8) || $region_name_utf8 == '') {
             continue;
         }
         $region = MMarketingRegion::findOne(['name' => $region_name_utf8]);
         if (empty($region)) {
             //                $region = new MMarketingRegion;
             //                $region->name = $region_name_utf8;
             //                $region->save(false);
             echo "{$region_name_utf8} 不存在。" . PHP_EOL;
             continue;
         }
         if (empty($msc_name_utf8) || $msc_name_utf8 == '') {
             continue;
         }
         $msc = MMarketingServiceCenter::findOne(['name' => $msc_name_utf8]);
         if (empty($msc)) {
             //                $msc = new MMarketingServiceCenter;
             //                $msc->name = $msc_name_utf8;
             //                $msc->region_id = $region->id;
             //                $msc->save(false);
             echo "{$msc_name_utf8} 不存在。" . PHP_EOL;
             continue;
         }
         if (empty($office_name_utf8) || $office_name_utf8 == '') {
             continue;
         }
         $office = MOffice::findOne(['title' => $office_name_utf8]);
         if (empty($office)) {
             //                $office = new MOffice;
             //                $office->gh_id = \app\models\MGh::GH_XIANGYANGUNICOM; // 襄阳联通公共ID
             //                $office->title = $office_name_utf8;
             //                $office->is_jingxiaoshang = 1;
             //                $office->save(false);
             echo "{$office_name_utf8} 不存在。" . PHP_EOL;
             continue;
         }
         if (empty($office->msc)) {
             yii::$app->db->createCommand()->insert('wx_rel_office_msc', ['office_id' => $office->office_id, 'msc_id' => $msc->id])->execute();
             $msc->updateCounters(['office_total_count' => 1]);
             $region->updateCounters(['office_total_count' => 1]);
         }
         if (empty($supervisor_name_utf8) || $supervisor_name_utf8 == '') {
             continue;
         }
         $staff = MStaff::findOne(['name' => $supervisor_name_utf8, 'gh_id' => \app\models\MGh::GH_XIANGYANGUNICOM]);
         //            if (empty($staff)) {
         //                $staff = new MStaff;
         //                $staff->office_id = 25;
         //                $staff->name = $supervisor_name_utf8;
         //                $staff->gh_id = \app\models\MGh::GH_XIANGYANGUNICOM; // 襄阳联通公共ID
         //                $staff->mobile = $supervisor_mobile;
         //                $staff->cat = 0;
         //                $staff->save(false);
         //            } else if ($staff->mobile != $supervisor_mobile) {
         //                $staff->updateAttributes(['mobile' => $supervisor_mobile]); // 修改员工电话
         //            }
         if (empty($staff) && empty($supervisor_mobile)) {
             echo "{$supervisor_name_utf8} 不在数据库中。" . PHP_EOL;
             continue;
         } else {
             if (empty($staff)) {
                 $staff = new MStaff();
                 $staff->office_id = 25;
                 $staff->name = $supervisor_name_utf8;
                 $staff->gh_id = \app\models\MGh::GH_XIANGYANGUNICOM;
                 // 襄阳联通公共ID
                 $staff->mobile = $supervisor_mobile;
                 $staff->cat = 0;
                 $staff->save(false);
             }
         }
         if (empty($staff->supervisedOffices) || empty($office->supervisor)) {
             yii::$app->db->createCommand()->insert('wx_rel_supervision_staff_office', ['office_id' => $office->office_id, 'staff_id' => $staff->staff_id])->execute();
         } else {
             if ($office->supervisor->staff_id != $staff->staff_id) {
                 // 如果旧有的督导关系需要修改,先要删除原督导关系,再重新建立新的督导关系
                 yii::$app->db->createCommand()->delete('wx_rel_supervision_staff_office', ['office_id' => $office->office_id, 'staff_id' => $office->supervisor->staff_id])->execute();
                 yii::$app->db->createCommand()->insert('wx_rel_supervision_staff_office', ['office_id' => $office->office_id, 'staff_id' => $staff->staff_id])->execute();
             }
         }
     }
     fclose($fh);
 }
Exemplo n.º 2
0
 public function actionStaffcreate()
 {
     $model = new MStaff();
     if (Yii::$app->request->isPost) {
         $model->load(Yii::$app->request->post());
         $model->gh_id = Yii::$app->user->getGhid();
         if ($model->save()) {
             return $this->redirect(['stafflist']);
         } else {
             U::W($model->getErrors());
         }
     }
     return $this->render('staffcreate', ['model' => $model]);
 }
Exemplo n.º 3
0
 public function actionStaffbind($gh_id, $openid)
 {
     if (Yii::$app->request->isAjax) {
         U::W('is ajax....');
     }
     $this->layout = 'wapx';
     $mobile = $_GET['mobile'];
     $model = MStaff::findOne(['gh_id' => $gh_id, 'openid' => $openid]);
     if ($model === null) {
         $model = MStaff::findOne(['mobile' => $mobile]);
         if ($model === null) {
             $model = new MStaff();
         }
         $model->gh_id = $gh_id;
         $model->openid = $openid;
         $model->mobile = $mobile;
     }
     if ($model->load(Yii::$app->request->post())) {
         if ($model->save()) {
             return $this->redirect(['staffhome', 'gh_id' => $gh_id, 'openid' => $openid]);
         } else {
             U::W($model->getErrors());
         }
     }
     return $this->render('staffbind', ['model' => $model]);
 }
Exemplo n.º 4
0
 public function actionR2()
 {
     $lock = 'LOCK#1';
     $gh_id = 'G';
     $office_id = rand(1, 100000);
     for ($i = 0; $i < 10000; $i++) {
         if (yii::$app->mutex->acquire($lock, 10)) {
             $staff = new MStaff();
             $staff->gh_id = $gh_id;
             $staff->office_id = $office_id;
             $staff->scene_id = MStaff::newSceneId($gh_id);
             $staff->name = 'office-' . $office_id;
             $staff->cat = MStaff::SCENE_CAT_OFFICE;
             if (!$staff->save(false)) {
                 U::W(['error', __METHOD__, $staff]);
             }
             yii::$app->mutex->release($lock);
         } else {
             yii::error('acquire lock error in R2');
         }
     }
 }
Exemplo n.º 5
0
 public function newSceneIdForOpenid()
 {
     $staff = new MStaff();
     $staff->gh_id = $this->gh_id;
     $staff->openid = $this->openid;
     $staff->scene_id = MStaff::newSceneId($this->gh_id);
     $staff->name = $this->nickname;
     $staff->cat = MStaff::SCENE_CAT_FAN;
     if (!$staff->save(false)) {
         U::W(['error', __METHOD__, $staff]);
     }
     return $staff;
 }
Exemplo n.º 6
0
 public function getQrImageUrl2()
 {
     $officeStaff = $this->officeStaff;
     if (empty($officeStaff)) {
         $officeStaff = new MStaff();
         $officeStaff->gh_id = $this->gh_id;
         $officeStaff->office_id = $this->office_id;
         $officeStaff->cat = MStaff::SCENE_CAT_OFFICE;
         $officeStaff->name = $this->title;
         $officeStaff->save(false);
         //            return false;
     }
     return $officeStaff->getQrImageUrl2();
 }