public static function generateStatsCampaignProductCodeQuarterlyData($condition) { $campaignLogDailys = StatsMemberCampaignLogDaily::getCollection()->aggregate([['$match' => $condition], ['$group' => ['_id' => ['productId' => '$productId'], 'total' => ['$sum' => 1]]]]); if (!empty($campaignLogDailys)) { $productIds = StatsMemberCampaignLogDaily::distinct('productId', $condition); $products = Product::findAll(['_id' => ['$in' => $productIds]]); $productNames = []; foreach ($products as $product) { $productNames[(string) $product->_id] = $product->name; } foreach ($campaignLogDailys as $campaignLogDaily) { $productId = $campaignLogDaily['_id']['productId']; $rows[] = ['productId' => $productId, 'productName' => isset($productNames[(string) $productId]) ? $productNames[(string) $productId] : '', 'total' => $campaignLogDaily['total'], 'year' => $condition['year'], 'quarter' => $condition['quarter'], 'accountId' => $condition['accountId']]; } StatsCampaignProductCodeQuarterly::batchInsert($rows); } }
/** * check the goods info * @param $goods array */ public static function checkGoodsInfo($goods) { //get the productId $productIdList = []; foreach ($goods as $key => $info) { if (empty($info['productId'])) { throw new ServerErrorHttpException(Yii::t('product', 'invalide_params')); } if (!isset($info['score'])) { throw new ServerErrorHttpException(Yii::t('product', 'score_not_empty')); } if (!isset($info['total'])) { throw new ServerErrorHttpException(Yii::t('product', 'total_not_empty')); } $productIdList[] = new MongoId($info['productId']); } //check all product whether can be found in the product table $products = Product::findAll(['_id' => ['$in' => $productIdList]]); if (count($products) != count($productIdList) || count($products) <= 0) { throw new ServerErrorHttpException(Yii::t('product', 'invalide_params')); } //check the goods if (Goods::findOne(['productId' => ['$in' => $productIdList]])) { throw new ServerErrorHttpException(Yii::t('product', 'not_add_again')); } $data = []; foreach ($products as $product) { $key = $product['_id'] . ''; $data[$key]['productName'] = $product['name']; $data[$key]['sku'] = $product['sku']; } unset($key, $productIdList); foreach ($goods as $key => $value) { $productId = $value['productId'] . ''; $goods[$key]['productName'] = $data[$productId]['productName']; $goods[$key]['sku'] = $data[$productId]['sku']; } unset($productId, $data, $products); return $goods; }
public static function getProductsName($params, $accountId) { $where = ['accountId' => $accountId, '_id' => ['$in' => $params['id']]]; return Product::findAll($where); }