public function actionIndex()
 {
     $params = $this->getQuery();
     if (empty($params['startDate']) || empty($params['endDate']) || empty($params['campaignId'])) {
         throw new InvalidParameterException('missing params');
     }
     $params['startDate'] = new MongoDate(TimeUtil::ms2sTime($params['startDate']));
     $params['endDate'] = new MongoDate(TimeUtil::ms2sTime($params['endDate']) + 3600 * 24);
     $accountId = $this->getAccountId();
     $data = [];
     if (!empty($params['type'])) {
         if (!is_array($params['type'])) {
             $types = [$params['type']];
         } else {
             $types = $params['type'];
         }
     } else {
         $types = self::$types;
     }
     //to suport to get all campaign data
     if (MongoId::isValid($params['campaignId'])) {
         $params['campaignId'] = new MongoId($params['campaignId']);
         $data = PromotionCodeAnalysis::getAnalysisData($types, $accountId, $params);
     } else {
         $data = StatsPromotionCodeAnalysis::getAnalysisData($types, $accountId, $params);
     }
     return $data;
 }
 /**
  * get the analysis base on the type
  * 1.to get date in categories
  * 2.to get productName in series.name
  * 3.to get total in series.data
  * @param $type.int or array,the type of analysis
  * @param $accountId,mongoId
  * @param $params,array
  */
 public static function getAnalysisData($types, $accountId, $params)
 {
     $where = ['accountId' => $accountId, 'createdAt' => ['$gte' => $params['startDate'], '$lt' => $params['endDate']]];
     $results = [];
     foreach ($types as $type) {
         $series = $info = $productNameData = $statsProduct = [];
         $k = 0;
         $where['type'] = intval($type);
         $datas = StatsPromotionCodeAnalysis::find()->where($where)->orderBy(['createdAt' => 'asc', 'productName' => 'asc'])->all();
         if (!empty($datas)) {
             foreach ($datas as $data) {
                 if (self::PARTICIPATE == $type) {
                     $data['productName'] = self::PARTICIPATE_TITLE;
                 }
                 $analysisDate = MongodbUtil::MongoDate2String($data['createdAt'], 'Y-m-d');
                 //record the date
                 if (empty($info['categories'])) {
                     $info['categories'] = [$analysisDate];
                 } else {
                     if (!in_array($analysisDate, $info['categories'])) {
                         $info['categories'][] = $analysisDate;
                     }
                 }
                 //record campaignName
                 if (empty($productNameData)) {
                     $productNameData[] = $data['productName'];
                 } else {
                     if (!in_array($data['productName'], $productNameData)) {
                         $productNameData[] = $data['productName'];
                     }
                 }
                 //record productName => [$date => $total]
                 $statsProduct[$data['productName']][$analysisDate] = $data['total'];
             }
             foreach ($info['categories'] as $categories) {
                 foreach ($productNameData as $key => $productName) {
                     $series[$key]['name'] = $productName;
                     $total = 0;
                     if (isset($statsProduct[$productName][$categories])) {
                         $total = $statsProduct[$productName][$categories];
                     }
                     $series[$key]['data'][] = $total;
                 }
             }
             $info['series'] = $series;
         }
         $results[$type] = $info;
         unset($info);
     }
     unset($series, $data, $datas);
     //merge type value 1 and 4,the type is 4 that is the total for 1,so it is only one data
     if (!empty($results[1]['series']) && !empty($results[4]['series'][0])) {
         array_push($results[1]['series'], $results[4]['series'][0]);
         unset($results[4]);
     }
     return $results;
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['beginTime']) || empty($args['endTime']) || empty($args['type'])) {
         LogUtil::error(['message' => 'missing params when update stats promotion code analysis', 'args' => $args], 'resque');
         return false;
     }
     $beginTime = strtotime($args['beginTime']) + 3600 * 24;
     $endTime = strtotime($args['endTime']);
     if ($endTime > time()) {
         $endTime = strtotime(date('Y-m-d', time()));
     }
     $endTime += 3600 * 24;
     $type = intval($args['type']);
     for ($t = $beginTime; $t <= $endTime; $t += 3600 * 24) {
         $where = ['createdAt' => new MongoDate($t - 3600 * 24), 'type' => $type];
         ModelStatsPromotionCodeAnalysis::deleteAll($where);
         $createWhere = PromotionCodeAnalysis::getCreateTime($t);
         $yesterdayStamp = $t - 3600 * 24;
         StatsPromotionCodeAnalysis::setData($type, $yesterdayStamp, $createWhere, $t);
     }
     return true;
 }
 private static function _setStatsParticipate($campaignLogs, $yesterday)
 {
     //get total for take part in a campaign
     $campaignData = [];
     foreach ($campaignLogs as $data) {
         $key = (string) $data['_id']['productId'];
         if (isset($campaignData[$key])) {
             //to sum the total in every product in same campaign
             $campaignData[$key]['total'] += 1;
         } else {
             $productName = empty($data['_id']['productName']) ? '' : $data['_id']['productName'];
             $result = ['productId' => $data['_id']['productId'], 'productName' => $productName, 'accountId' => $data['_id']['accountId'], 'createdAt' => $yesterday, 'total' => 1, 'type' => PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_PARTICIPATE];
             $campaignData[$key] = $result;
         }
     }
     ModelStatsPromotionCodeAnalysis::batchInsert($campaignData);
     unset($datas, $campaignIds, $campaignData);
 }