public static function generateData($memberProperty, $property, $year, $quarter, $accountId)
 {
     $propertyKey = 'memProperty.' . $memberProperty->_id;
     $keys = ['productId' => true, $propertyKey => true];
     $condition = ['year' => $year, 'quarter' => $quarter, 'accountId' => $accountId];
     $initial = ['avg' => 0, 'codes' => ['count' => 0], 'members' => ['count' => 0]];
     $reduce = 'function(doc, prev) {
         if (!prev.members[doc.memberId]) {
             prev.members[doc.memberId] = true;
             prev.members["count"]++;
         }
         if (!prev.codes[doc.code]) {
             prev.codes[doc.code] = true;
             prev.codes["count"]++;
         }
     }';
     $finalize = 'function(prev) {
         prev.avg = prev.codes["count"] / prev.members["count"];
         delete prev.codes;
         delete prev.members;
     }';
     $statsItems = ModelStatsMemberCampaignLogDaily::getCollection()->group($keys, $initial, $reduce, ['condition' => $condition, 'finalize' => $finalize]);
     foreach ($statsItems as $statsItem) {
         $condition['propId'] = $property;
         $condition['propValue'] = $statsItem[$propertyKey];
         $productId = $statsItem['productId'];
         $condition['productId'] = $productId;
         $stats = ModelStatsMemberPropTradeCodeAvgQuarterly::findOne($condition);
         if (empty($stats)) {
             $stats = new ModelStatsMemberPropTradeCodeAvgQuarterly();
             $product = Product::findByPk($productId);
             $productName = '';
             if (!empty($product)) {
                 $productName = $product->name;
             }
             $stats->propId = $property;
             $stats->propValue = $statsItem[$propertyKey];
             $stats->productId = $productId;
             $stats->productName = $productName;
             $stats->year = $year;
             $stats->quarter = $quarter;
             $stats->accountId = $accountId;
         }
         $stats->avg = $statsItem['avg'];
         $stats->save();
     }
 }
 public function actionIndex($accountId)
 {
     if (empty($accountId)) {
         echo 'accountId is invaild' . PHP_EOL;
         exit;
     }
     $where['accountId'] = new \MongoId($accountId);
     // delete member info
     Member::getCollection()->remove($where);
     //delete MemberLogs
     MemberLogs::getCollection()->remove($where);
     //delete scoreHistory
     ScoreHistory::getCollection()->remove($where);
     //delete CampaignLog
     CampaignLog::getCollection()->remove($where);
     //delete GoodsExchangeLog
     GoodsExchangeLog::getCollection()->remove($where);
     //delete MemberStatistics
     MemberStatistics::getCollection()->remove($where);
     //delete ReMemberCampaign
     ReMemberCampaign::getCollection()->remove($where);
     //delete StatsCampaignProductCodeQuarterly
     StatsCampaignProductCodeQuarterly::getCollection()->remove($where);
     //delete StatsMemberCampaignLogDaily
     StatsMemberCampaignLogDaily::getCollection()->remove($where);
     //delete StatsMemberDaily
     StatsMemberDaily::getCollection()->remove($where);
     //delete StatsMemberGrowthMonthly
     StatsMemberGrowthMonthly::getCollection()->remove($where);
     //delete StatsMemberGrowthQuarterly
     StatsMemberGrowthQuarterly::getCollection()->remove($where);
     //delete StatsMemberMonthly
     StatsMemberMonthly::getCollection()->remove($where);
     //delete StatsMemberPropAvgTradeQuarterly
     StatsMemberPropAvgTradeQuarterly::getCollection()->remove($where);
     //delete StatsMemberPropMonthly
     StatsMemberPropMonthly::getCollection()->remove($where);
     //delete StatsMemberPropQuaterly
     StatsMemberPropQuaterly::getCollection()->remove($where);
     //delete StatsMemberPropTradeCodeAvgQuarterly
     StatsMemberPropTradeCodeAvgQuarterly::getCollection()->remove($where);
     //delete StatsMemberPropTradeCodeQuarterly
     StatsMemberPropTradeCodeQuarterly::getCollection()->remove($where);
     //delete StatsMemberPropTradeQuarterly
     StatsMemberPropTradeQuarterly::getCollection()->remove($where);
     //delete PromotionCodeAnalysis
     PromotionCodeAnalysis::getCollection()->remove($where);
     //delete order
     Order::getCollection()->remove($where);
     //delete stats member order
     StatsMemberOrder::getCollection()->remove($where);
     //delete stats order
     StatsOrder::getCollection()->remove($where);
     //delete MembershipDiscount
     MembershipDiscount::getCollection()->remove($where);
     //delete couponLog
     CouponLog::getCollection()->remove($where);
     echo 'delete data successful.' . PHP_EOL;
 }
 public static function generateStatsMemberPropertyTradeCodeAvgQuarterlyData($memberProperty, $property, $condition)
 {
     $propertyKey = 'memProperty.' . $memberProperty->_id;
     $keys = ['productId' => true, $propertyKey => true];
     $initial = ['avg' => 0, 'codes' => ['count' => 0], 'members' => ['count' => 0]];
     $reduce = 'function(doc, prev) {
         if (!prev.members[doc.memberId]) {
             prev.members[doc.memberId] = true;
             prev.members["count"]++;
         }
         if (!prev.codes[doc.code]) {
             prev.codes[doc.code] = true;
             prev.codes["count"]++;
         }
     }';
     $finalize = 'function(prev) {
         prev.avg = prev.codes["count"] / prev.members["count"];
         delete prev.codes;
         delete prev.members;
     }';
     $statsItems = StatsMemberCampaignLogDaily::getCollection()->group($keys, $initial, $reduce, ['condition' => $condition, 'finalize' => $finalize]);
     $productNames = self::getProductNames($condition);
     $rows = [];
     foreach ($statsItems as $statsItem) {
         $condition['propId'] = $property;
         $condition['propValue'] = $statsItem[$propertyKey];
         $productId = $statsItem['productId'];
         $condition['productId'] = $productId;
         $productName = isset($productNames[(string) $productId]) ? $productNames[(string) $productId] : '';
         $rows[] = ['propId' => $property, 'propValue' => $statsItem[$propertyKey], 'productId' => $productId, 'productName' => $productName, 'year' => $condition['year'], 'quarter' => $condition['quarter'], 'accountId' => $condition['accountId'], 'avg' => $statsItem['avg']];
     }
     StatsMemberPropTradeCodeAvgQuarterly::batchInsert($rows);
     unset($rows);
 }
 public function actionCodeAvgQuarterly()
 {
     $year = $this->getQuery('year');
     $quarter = $this->getQuery('quarter');
     if (empty($year) || empty($quarter)) {
         throw new BadRequestHttpException(Yii::t('common', 'parameters_missing'));
     }
     $accountId = $this->getAccountId();
     $condition = ['accountId' => $accountId, 'year' => $year, 'quarter' => (int) $quarter];
     $data = [];
     $stats = StatsMemberPropTradeCodeAvgQuarterly::findAll($condition);
     $result = $this->_formatStatsWithCategory($stats, 'productId', 'productName', 'avg');
     return $result;
 }