/** * Create store and store locations in mongoDB */ public function actionCreate() { $params = $this->getParams(); $store = new Store(); $store->attributes = $params; $store->_id = new MongoId(); $token = Token::getToken(); $store->accountId = $token->accountId; if ($store->validate()) { // all inputs are valid if ($store->save()) { $location = $store->location; unset($location['deatail']); $args = ['location' => $location, 'storeId' => $store->_id . '', 'accountId' => $store->accountId . '', 'description' => 'Direct: Create store locations in storeLocation collection']; // create a to create store locations Yii::$app->job->create('backend\\modules\\member\\job\\Location', $args); return $store; } else { throw new ServerErrorHttpException(Yii::t('common', 'save_fail')); } } else { // valid fail, return errors return $store->errors; } }
public function perform() { $args = $this->args; $accountId = new \MongoId($args['accountId']); $channels = $args['channels']; if (!empty($args['storeIds'])) { // Sync store data to wechat $userId = $args['userId']; $storeIds = $args['storeIds']; $storeData = []; foreach ($storeIds as $key => $storeId) { $storeId = new \MongoId($storeId); $store = Store::findByPk($storeId); $storeData[] = $store->toData(); } $failResult = []; foreach ($channels as $channelId) { try { $result = \Yii::$app->weConnect->addStore($channelId, $storeData); if (!empty($result['location_id_list']) && in_array(-1, $result['location_id_list'])) { foreach ($result['location_id_list'] as $index => $locationId) { if ($locationId === -1) { $failResult[] = ['channelId' => $channelId, 'storeId' => $storeIds[$index], 'storeName' => $storeData[$index]['business_name']]; } } } } catch (\Exception $e) { ResqueUtil::log($e); foreach ($storeData as $index => $store) { $failResult[] = ['channelId' => $channelId, 'storeId' => $storeIds[$index], 'storeName' => $store['business_name']]; } } } if (!empty($failResult)) { \Yii::$app->cache->set(Store::CACHE_PREFIX . $userId . Store::SYNC_TO_WECHAT, $failResult, Store::CACHE_EXPIRE_TIME); throw new \Exception('Sync store data to wechat failed.'); } } else { // Sync store data from wechat $account = Account::findByPk($accountId); $syncWechat = []; if (!empty($account->syncWechat)) { $syncWechat = $account->syncWechat; } $sizeCondition = ['offset' => static::STORE_START_NUM, 'count' => static::STORE_END_NUM]; foreach ($channels as $channelId) { $stores = \Yii::$app->weConnect->getStores($channelId, $sizeCondition); ResqueUtil::log($stores); if (!empty($stores['location_list'])) { foreach ($stores['location_list'] as $storeData) { $store = new Store(); $store->loadData($storeData); $store->accountId = $accountId; $store->save(); } } array_push($syncWechat, $channelId); } $account->syncWechat = $syncWechat; $account->save(); } }