示例#1
0
 public function actionCreate()
 {
     $goods = $this->getParams('goods');
     $storeId = $this->getParams('storeId');
     $accountId = $this->getAccountId();
     if (empty($goods) || empty($storeId)) {
         throw new BadRequestHttpException(Yii::t('common', 'parameters_missing'));
     }
     $storeId = new \MongoId($storeId);
     $store = Store::findByPk($storeId);
     if (empty($store)) {
         throw new InvalidParameterException(Yii::t('common', 'data_error'));
     }
     $productIds = [];
     foreach ($goods as $item) {
         if (!StoreGoods::validatePrice($item['price'])) {
             throw new InvalidParameterException(Yii::t('store', 'price_error'));
         }
         $productIds[] = new \MongoId($item['productId']);
     }
     $storeGoodsCount = StoreGoods::countByProductId($productIds, $storeId);
     if ($storeGoodsCount > 0) {
         throw new InvalidParameterException(Yii::t('store', 'goods_exists'));
     }
     $storeGoods = [];
     foreach ($goods as $item) {
         $product = Product::findByPk($item['productId']);
         if (empty($product)) {
             throw new InvalidParameterException(Yii::t('common', 'data_error'));
         }
         $category = $product->category;
         $pictures = ArrayHelper::getColumn($product->pictures, 'url', false);
         $pictures = array_slice($pictures, 0, 5);
         $storeGoods[] = ['storeId' => $storeId, 'categoryId' => $category['id'], 'productName' => $product->name, 'sku' => $product->sku, 'productId' => $product->_id, 'pictures' => $pictures, 'status' => StoreGoods::STATUS_OFF, 'offShelfTime' => new \Mongodate(), 'price' => !empty($item['price']) ? floatval($item['price']) : 0.0, 'accountId' => $accountId];
     }
     if (StoreGoods::batchInsert($storeGoods)) {
         return ['message' => 'OK', 'data' => null];
     } else {
         throw new ServerErrorHttpException(Yii::t('common', 'save_fail'));
     }
 }