/** * 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 = ['campaignId' => $params['campaignId'], 'accountId' => $accountId, 'createdAt' => ['$gte' => $params['startDate'], '$lt' => $params['endDate']]]; $results = []; foreach ($types as $type) { $series = $info = $productNameData = $statsProduct = []; $k = 0; $where['type'] = new \MongoInt32($type); $datas = PromotionCodeAnalysis::find()->where($where)->orderBy(['createdAt' => 'asc', 'productName' => 'asc'])->all(); if (!empty($datas)) { foreach ($datas as $data) { if (self::PROMOTION_CODE_ANALYSIS_TOTAL_PARTICIPATE == $type) { $data['productName'] = self::PROMOTION_CODE_ANALYSIS_TOTAL_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 productName 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; }