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;
 }
 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;
 }
Esempio n. 3
0
 /**
  * @args {"description": "Direct: Analysis daily promotion code "}
  */
 public function perform()
 {
     $yesterday = ModelPromotionCodeAnalysis::getTime(-1);
     $type = new MongoInt32(ModelPromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_TOTAL);
     $where = ['createdAt' => $yesterday, 'type' => $type];
     $status = ModelPromotionCodeAnalysis::checkExistData($where);
     if ($status) {
         $yesterdayStamp = TimeUtil::today() - 24 * 3600;
         $yesterday = new MongoDate($yesterdayStamp);
         //get campaignlogs in yesterday
         $campaignLogs = ModelPromotionCodeAnalysis::getMemberCampaignLog();
         //create datas
         if (!empty($campaignLogs)) {
             $campaignData = [];
             foreach ($campaignLogs as $key => $campaignLog) {
                 //get total the day before yesterday
                 $beforeYesterday = new MongoDate(TimeUtil::today() - 2 * 24 * 3600);
                 $productId = $campaignLog['_id']['productId'];
                 $campaignId = $campaignLog['_id']['campaignId'];
                 $accountId = $campaignLog['_id']['accountId'];
                 $condition = ['productId' => $productId, 'campaignId' => $campaignId, 'accountId' => $accountId, 'createdAt' => $beforeYesterday, 'type' => $type];
                 $beforeYesterdayData = ModelPromotionCodeAnalysis::findOne($condition);
                 if (empty($beforeYesterdayData)) {
                     $beforeYesterdayData['total'] = 0;
                 }
                 $condition = ['campaignId' => $campaignId, 'accountId' => $accountId, 'productId' => $productId];
                 $number = ModelPromotionCodeAnalysis::checkMemberUnique($condition, TimeUtil::today());
                 //subtract the member who is recorded before
                 $total = $beforeYesterdayData['total'] + $number;
                 $campaignLogs[$key]['total'] = $total;
             }
             $campaignData = ModelPromotionCodeAnalysis::createAnalysisData($campaignLogs, $type, $yesterday);
             if (false === ModelPromotionCodeAnalysis::batchInsert($campaignData)) {
                 LogUtil::error(['message' => 'Faild to create daily data', 'date' => date('Y-m-d H:i:s'), 'data' => json_encode($campaignData)], 'resque');
             }
         }
         //set the default value when the value is not exists
         $yesterdayStamp -= 3600 * 24;
         ModelPromotionCodeAnalysis::setDefault($yesterdayStamp, $type);
     } else {
         LogUtil::info(['message' => 'Total analysis data is exists', 'date' => date('Y-m-d H:i:s')], 'resque');
     }
     return true;
 }
 /**
  * @args {"description": "Direct: Analysis participate promotion code "}
  */
 public function perform()
 {
     $yesterday = ModelPromotionCodeAnalysis::getTime(-1);
     $type = new MongoInt32(ModelPromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_PARTICIPATE);
     $where = ['createdAt' => $yesterday, 'type' => $type];
     $status = ModelPromotionCodeAnalysis::checkExistData($where);
     if ($status) {
         $yesterdayStamp = TimeUtil::today() - 24 * 3600;
         $yesterday = new MongoDate($yesterdayStamp);
         $createWhere = ModelPromotionCodeAnalysis::getCreateTime();
         $campaignIds = CampaignLog::distinct('campaignId', $createWhere);
         $campaignLogs = [];
         if (!empty($campaignIds)) {
             $where = array_merge($createWhere, ['campaignId' => ['$in' => $campaignIds]]);
             $campaignLogs = CampaignLog::getCollection()->aggregate([['$match' => $where], ['$group' => ['_id' => ['campaignId' => '$campaignId', 'memberId' => '$member.id', 'accountId' => '$accountId', 'productId' => '$productId']]]]);
         }
         if (!empty($campaignLogs)) {
             //get total for take part in a campaign
             $campaignData = [];
             foreach ($campaignLogs as $data) {
                 $campaignId = $data['_id']['campaignId'];
                 $key = (string) $campaignId . (string) $data['_id']['productId'];
                 if (isset($campaignData[$key])) {
                     //to sum the total in every product in same campaign
                     $campaignData[$key]['total'] += 1;
                 } else {
                     $product = Product::findByPk($data['_id']['productId']);
                     $productName = empty($product['name']) ? '' : $product['name'];
                     $result = ['productId' => $data['_id']['productId'], 'productName' => $productName, 'campaignId' => $campaignId, 'accountId' => $data['_id']['accountId'], 'createdAt' => $yesterday, 'total' => 1, 'type' => $type];
                     $campaignData[$key] = $result;
                 }
             }
             if (false === ModelPromotionCodeAnalysis::batchInsert($campaignData)) {
                 LogUtil::error(['message' => 'Faild to create daily data', 'date' => date('Y-m-d H:i:s'), 'data' => json_encode($campaignData)], 'resque');
             }
             unset($datas, $campaignIds, $campaignData);
         }
     } else {
         LogUtil::info(['message' => 'Participate analysis data is exists', 'date' => date('Y-m-d H:i:s')], 'resque');
     }
     return true;
 }
 /**
  * @args {"description": "Direct: Analysis total participate"}
  */
 public function perform()
 {
     $yesterday = ModelPromotionCodeAnalysis::getTime(-1);
     $type = new MongoInt32(ModelPromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_TOTAL_PARTICIPATE);
     $where = ['createdAt' => $yesterday, 'type' => $type];
     $status = ModelPromotionCodeAnalysis::checkExistData($where);
     if ($status) {
         $yesterdayStamp = TimeUtil::today() - 24 * 3600;
         $yesterday = new MongoDate($yesterdayStamp);
         $where = ModelPromotionCodeAnalysis::getCreateTime();
         $campaignData = ModelPromotionCodeAnalysis::getMemberAllTimes($where);
         $campaignData = ModelPromotionCodeAnalysis::createAnalysisData($campaignData, $type, $yesterday);
         if (false === ModelPromotionCodeAnalysis::batchInsert($campaignData)) {
             LogUtil::error(['message' => 'Faild to create daily data', 'date' => date('Y-m-d H:i:s'), 'data' => json_encode($campaignData)], 'resque');
         }
     } else {
         LogUtil::info(['message' => 'Total participate analysis data is exists', 'date' => date('Y-m-d H:i:s')], 'resque');
     }
     return true;
 }
 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;
 }
 public static function setData($type, $yesterdayStamp, $createWhere = [], $searchTime = '')
 {
     $yesterday = new MongoDate($yesterdayStamp);
     if (empty($createWhere)) {
         $createWhere = PromotionCodeAnalysis::getCreateTime();
     }
     if (empty($searchTime)) {
         $searchTime = TimeUtil::today();
     }
     switch ($type) {
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_PARTICIPATE:
             $productIds = CampaignLog::distinct('productId', $createWhere);
             $campaignLogs = self::_getCampaignLogs($productIds, $createWhere);
             if (!empty($campaignLogs)) {
                 self::_setStatsParticipate($campaignLogs, $yesterday);
             }
             break;
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_TOTAL:
             //get campaignlogs in yesterday
             $group = ['_id' => ['accountId' => '$accountId', 'productId' => '$productId', 'memberId' => '$member.id']];
             $secondGroup = ['_id' => ['accountId' => '$_id.accountId', 'productId' => '$_id.productId'], 'total' => ['$sum' => 1]];
             $campaignLogs = PromotionCodeAnalysis::getMemberCampaignLog(false, $searchTime, $group, $secondGroup);
             //create datas
             if (!empty($campaignLogs)) {
                 $campaignData = [];
                 foreach ($campaignLogs as $key => $campaignLog) {
                     //get total the day before yesterday
                     $beforeYesterday = new MongoDate($searchTime - 2 * 24 * 3600);
                     $productId = $campaignLog['_id']['productId'];
                     $accountId = $campaignLog['_id']['accountId'];
                     $condition = ['productId' => $productId, 'accountId' => $accountId, 'createdAt' => $beforeYesterday, 'type' => $type];
                     $beforeYesterdayData = ModelStatsPromotionCodeAnalysis::findOne($condition);
                     if (empty($beforeYesterdayData)) {
                         $beforeYesterdayData['total'] = 0;
                     }
                     $condition = ['accountId' => $accountId, 'productId' => $productId];
                     $number = PromotionCodeAnalysis::checkMemberUnique($condition, $searchTime);
                     //subtract the member who is recorded before
                     $total = $beforeYesterdayData['total'] + $number;
                     $campaignLogs[$key]['total'] = $total;
                 }
                 $campaignData = ModelStatsPromotionCodeAnalysis::createAnalysisData($campaignLogs, $type, $yesterday);
                 ModelStatsPromotionCodeAnalysis::batchInsert($campaignData);
             }
             //set the default value when the value is not exists
             $yesterdayStamp -= 3600 * 24;
             ModelStatsPromotionCodeAnalysis::setDefault($yesterdayStamp, $type);
             break;
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_EVERYDAY_PRIZE:
             $group = ['_id' => ['accountId' => '$accountId', 'productId' => '$productId'], 'total' => ['$sum' => 1]];
             $campaignLogs = PromotionCodeAnalysis::getCampaignLog(false, $searchTime, $group);
             if (!empty($campaignLogs)) {
                 $campaignData = ModelStatsPromotionCodeAnalysis::createAnalysisData($campaignLogs, $type, $yesterday);
                 ModelStatsPromotionCodeAnalysis::batchInsert($campaignData);
             }
             break;
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_TOTAL_PARTICIPATE:
             $where = PromotionCodeAnalysis::getCreateTime($searchTime);
             $group = ['_id' => ['accountId' => '$accountId', 'memberId' => '$member.id']];
             $secondGroup = ['_id' => ['accountId' => '$_id.accountId'], 'total' => ['$sum' => 1]];
             $campaignData = PromotionCodeAnalysis::getMemberAllTimes($where, $group, $secondGroup);
             $campaignData = ModelStatsPromotionCodeAnalysis::createAnalysisData($campaignData, $type, $yesterday);
             ModelStatsPromotionCodeAnalysis::batchInsert($campaignData);
             break;
     }
 }
 /**
  * create a default data
  * @param $accountIds,array
  * @param $yesterday,int
  * @param $type.mongoInt32
  */
 public static function setDefault($yesterday, $type)
 {
     $now = $yesterday + 3600 * 24;
     $today = new MongoDate($now);
     //get all compaign id
     $campaignLogs = self::getMemberCampaignLog(true, $now);
     $yesterday = new MongoDate($yesterday);
     //sum every campaign and product
     if (!empty($campaignLogs)) {
         $defaultData = [];
         foreach ($campaignLogs as &$campaignLog) {
             //check data not exist
             $where = ['campaignId' => $campaignLog['_id']['campaignId'], 'type' => $type, 'accountId' => $campaignLog['_id']['accountId'], 'productId' => $campaignLog['_id']['productId'], 'createdAt' => $today];
             $data = PromotionCodeAnalysis::findOne($where);
             if (empty($data)) {
                 //create today data,to get yesterday data
                 $where['createdAt'] = $yesterday;
                 $data = PromotionCodeAnalysis::findOne($where);
                 $total = 0;
                 if (!empty($data)) {
                     $total = $data['total'];
                 }
                 $campaignLog['total'] = $total;
                 $defaultData = self::createAnalysisData([$campaignLog], $type, $today);
                 PromotionCodeAnalysis::batchInsert($defaultData);
             }
         }
     }
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['beginTime']) || empty($args['endTime']) || empty($args['type'])) {
         ResqueUtil::log(['error' => 'missing params', 'args' => $args]);
         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 = new \MongoInt32($args['type']);
     switch ($args['type']) {
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_PARTICIPATE:
             //delete data and create data
             for ($t = $beginTime; $t <= $endTime; $t += 3600 * 24) {
                 $where = ['createdAt' => new \MongoDate($t - 3600 * 24), 'type' => $type];
                 PromotionCodeAnalysis::deleteAll($where);
                 //create data begin
                 $createWhere = PromotionCodeAnalysis::getCreateTime($t);
                 $campaignIds = CampaignLog::distinct('campaignId', $createWhere);
                 $campaignLogs = [];
                 if (!empty($campaignIds)) {
                     $where = array_merge($createWhere, ['campaignId' => ['$in' => $campaignIds]]);
                     $campaignLogs = CampaignLog::getCollection()->aggregate([['$match' => $where], ['$group' => ['_id' => ['campaignId' => '$campaignId', 'accountId' => '$accountId', 'productId' => '$productId', 'memberId' => '$member.id']]]]);
                 }
                 if (!empty($campaignLogs)) {
                     //get total for take part in a campaign
                     $campaignData = [];
                     foreach ($campaignLogs as $data) {
                         $campaignId = $data['_id']['campaignId'];
                         $key = (string) $campaignId . (string) $data['_id']['productId'];
                         if (isset($campaignData[$key])) {
                             //to sum the total in every product in same campaign
                             $campaignData[$key]['total'] += 1;
                         } else {
                             $product = Product::findByPk($data['_id']['productId']);
                             $productName = empty($product['name']) ? '' : $product['name'];
                             $result = ['productId' => $data['_id']['productId'], 'productName' => $productName, 'campaignId' => $campaignId, 'accountId' => $data['_id']['accountId'], 'createdAt' => new \MongoDate($t - 3600 * 24), 'total' => 1, 'type' => $type];
                             $campaignData[$key] = $result;
                         }
                     }
                     PromotionCodeAnalysis::batchInsert($campaignData);
                     unset($datas, $campaignIds, $campaignData);
                 }
             }
             break;
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_TOTAL:
             //delete data and create data
             for ($t = $beginTime; $t <= $endTime; $t += 3600 * 24) {
                 $where = ['createdAt' => new \MongoDate($t - 3600 * 24), 'type' => $type];
                 PromotionCodeAnalysis::deleteAll($where);
                 //get campaignlogs in yesterday
                 $campaignLogs = PromotionCodeAnalysis::getMemberCampaignLog(false, $t);
                 //create datas
                 $yesterday = new \MongoDate($t - 2 * 24 * 3600);
                 if (!empty($campaignLogs)) {
                     $campaignData = [];
                     foreach ($campaignLogs as $key => $campaignLog) {
                         //get total the day yesterday
                         $productId = $campaignLog['_id']['productId'];
                         $campaignId = $campaignLog['_id']['campaignId'];
                         $accountId = $campaignLog['_id']['accountId'];
                         $condition = ['productId' => $productId, 'campaignId' => $campaignId, 'accountId' => $accountId, 'createdAt' => $yesterday, 'type' => $type];
                         $yesterdayData = PromotionCodeAnalysis::findOne($condition);
                         if (empty($yesterdayData)) {
                             $yesterdayData['total'] = 0;
                         }
                         $condition = ['campaignId' => $campaignId, 'accountId' => $accountId, 'productId' => $productId];
                         $number = PromotionCodeAnalysis::checkMemberUnique($condition, $t);
                         //subtract the member who is recorded before
                         $total = $yesterdayData['total'] + $number;
                         $campaignLogs[$key]['total'] = $total;
                     }
                     $campaignData = PromotionCodeAnalysis::createAnalysisData($campaignLogs, $type, new \MongoDate($t - 24 * 3600));
                     PromotionCodeAnalysis::batchInsert($campaignData);
                 }
                 //set the default value when the value is not exists
                 PromotionCodeAnalysis::setDefault($t - 2 * 24 * 3600, $type);
             }
             break;
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_EVERYDAY_PRIZE:
             for ($t = $beginTime; $t <= $endTime; $t += 3600 * 24) {
                 //delete data
                 $where = ['createdAt' => new \MongoDate($t - 3600 * 24), 'type' => $type];
                 PromotionCodeAnalysis::deleteAll($where);
                 $campaignLogs = PromotionCodeAnalysis::getCampaignLog(false, $t);
                 if (!empty($campaignLogs)) {
                     $campaignData = PromotionCodeAnalysis::createAnalysisData($campaignLogs, $type, new \MongoDate($t - 3600 * 24));
                     PromotionCodeAnalysis::batchInsert($campaignData);
                 }
             }
             break;
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_TOTAL_PARTICIPATE:
             for ($t = $beginTime; $t <= $endTime; $t += 3600 * 24) {
                 //delete data
                 $where = ['createdAt' => new \MongoDate($t - 3600 * 24), 'type' => $type];
                 PromotionCodeAnalysis::deleteAll($where);
                 unset($where);
                 $where = PromotionCodeAnalysis::getCreateTime($t);
                 $campaignLogs = PromotionCodeAnalysis::getMemberAllTimes($where);
                 if (!empty($campaignLogs)) {
                     $campaignData = PromotionCodeAnalysis::createAnalysisData($campaignLogs, $type, new \MongoDate($t - 3600 * 24));
                     PromotionCodeAnalysis::batchInsert($campaignData);
                 }
             }
             break;
     }
     return true;
 }
Esempio n. 10
0
 public function update($productId, $name, $categoryId)
 {
     Goods::updateAll(['$set' => ['productName' => $name, 'categoryId' => $categoryId]], ['productId' => $productId]);
     StoreGoods::updateAll(['$set' => ['productName' => $name, 'categoryId' => $categoryId]], ['productId' => $productId]);
     PromotionCodeAnalysis::updateAll(['$set' => ['productName' => $name]], ['productId' => $productId]);
 }