Ejemplo n.º 1
0
 /**
  * get qrcodeId name:struct array(qrcodeId=>value)
  * @param $qrcodeIds,array
  */
 public static function getQrcodeName($qrcodeIds)
 {
     $data = [];
     //promotion qrcode only check in store and staff
     if (!empty($qrcodeIds)) {
         $where = Store::createStoreChannelCondition($qrcodeIds);
         $storeInfos = Store::findAll($where);
         $storeIds = [];
         if (!empty($storeInfos)) {
             $model = new Store();
             $channels = $model->storeChannels;
             foreach ($storeInfos as $storeInfo) {
                 foreach ($channels as $channel) {
                     if (!empty($channel = $storeInfo->{$channel})) {
                         $storeIds[] = $channel['qrcodeId'];
                         $data[$channel['qrcodeId']] = $storeInfo->name;
                     }
                 }
             }
         }
         //get staff qrcode
         $staffIds = array_values(array_diff($qrcodeIds, $storeIds));
         $where = ['qrcodeId' => ['$in' => $staffIds]];
         $staffInfos = Staff::findAll($where);
         if (!empty($staffIds)) {
             foreach ($staffInfos as $staffInfo) {
                 $name = '';
                 if (!empty($staffInfo->name)) {
                     $name = $staffInfo->name;
                 }
                 $data[$staffInfo->qrcodeId] = '店员' . $name . '二维码';
             }
         }
     }
     return $data;
 }
 /**
  * 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();
                 }
             }
         }
     }
 }