/**
  * Refined store location
  */
 public function actionIndex()
 {
     $stores = Store::findAll([]);
     if (!empty($stores)) {
         foreach ($stores as $store) {
             if (!empty($location = $store->location)) {
                 if (!empty($location['county'])) {
                     unset($location['county']);
                     $store->location = $location;
                     $store->save();
                 }
                 if (!empty($location['province']) && !empty($location['city']) && !empty($location['district'])) {
                     $accountId = $store->accountId;
                     $locationKeys = ['province', 'city', 'district'];
                     $parentName = null;
                     $storeLocation = StoreLocation::findByPk($store->_id);
                     if (empty($storeLocation)) {
                         $storeLocation = new StoreLocation();
                         $storeLocation->_id = $store->_id;
                         $storeLocation->name = $store->name;
                         $storeLocation->parentName = $location['district'];
                         $storeLocation->level = StoreLocation::LOCATION_LEVEL_STORE;
                         $storeLocation->accountId = $accountId;
                         $storeLocation->save();
                     }
                     foreach ($locationKeys as $key) {
                         $storeLocation = StoreLocation::findOne(['name' => $location[$key], 'accountId' => $accountId]);
                         if (empty($storeLocation)) {
                             // if store location doesn't exist, add it
                             $storeLocation = new StoreLocation();
                             $storeLocation->_id = new \MongoId();
                             $storeLocation->name = $location[$key];
                             $storeLocation->parentName = $parentName;
                             $storeLocation->level = $this->levelMap[$key];
                             $storeLocation->accountId = $accountId;
                             $storeLocation->save();
                         }
                         // current location name is the next's parent
                         $parentName = $storeLocation->name;
                     }
                 }
             }
         }
     }
     $storeLocations = StoreLocation::findAll(['level' => StoreLocation::LOCATION_LEVEL_STORE]);
     if (!empty($storeLocations)) {
         foreach ($storeLocations as $storeLocation) {
             $findStoreLocation = Store::findByPk($storeLocation->_id);
             if (empty($findStoreLocation)) {
                 $storeLocation->delete();
             }
         }
     }
     foreach (array_reverse($this->levelMap) as $level) {
         $storeLocations = StoreLocation::findAll(['level' => $level]);
         if (!empty($storeLocations)) {
             foreach ($storeLocations as $storeLocation) {
                 $accountId = $storeLocation->accountId;
                 $findStoreLocation = StoreLocation::findOne(['parentName' => $storeLocation->name, 'accountId' => $accountId]);
                 if (empty($findStoreLocation)) {
                     $storeLocation->delete();
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Get the store locations
  *
  * <b>Request Type</b>: GET<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/channel/offlinestore/store/location<br/><br/>
  * <b>Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for getting store locations
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     name: string, the store location name<br/>
  *     <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * <pre>
  * [
  *     {
  *         "id": "5518b4362736e7fa648b4567",
  *         "name": "北京"
  *     },
  *     {
  *         "id": "5518b4362736e7fa648b4568",
  *         "name": "上海"
  *     }
  * ]
  * </pre>
  */
 public function actionLocation()
 {
     $parentName = $this->getQuery('name', null);
     $accountId = Token::getAccountId();
     $locations = StoreLocation::find()->where(['parentName' => $parentName, 'accountId' => $accountId, 'isDeleted' => StoreLocation::NOT_DELETED])->all();
     return $locations;
 }
Exemplo n.º 3
0
 /**
  * Remove the old store locations
  * @param array  $location  contains 'province','city' and 'district'
  * @param string $accountId
  */
 private function _removeOldLocation($location, $accountId)
 {
     $locationKeys = ['province', 'city', 'district'];
     foreach ($locationKeys as $key) {
         if (empty($location[$key])) {
             return false;
         }
     }
     $result = true;
     $parentName = null;
     if (!empty($locationKeys)) {
         // remove the old store locations
         foreach (array_reverse($locationKeys) as $key) {
             if (!empty($location[$key])) {
                 $storeLocation = StoreLocation::findOne(['parentName' => $location[$key], 'accountId' => $accountId]);
                 if (empty($storeLocation)) {
                     $result &= StoreLocation::deleteAll(['name' => $location[$key], 'accountId' => $accountId]);
                 }
             }
         }
     }
     return (bool) $result;
 }