/** * @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 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 the condition to describe when to create the data * @param $now,timestamp */ public static function getCreateTime($now = '') { if (empty($now)) { $now = TimeUtil::today(); } $yesterday = $now - 3600 * 24; $now = new MongoDate($now); $yesterday = new MongoDate($yesterday); $createWhere = ['createdAt' => ['$gte' => $yesterday, '$lt' => $now]]; return $createWhere; }
/** * Conversion format for membershipDiscount. * @param $coupon Object * @param $member Object */ public static function transformMembershipDiscount($coupon, $member, $receiveType = null) { if ($coupon->time['type'] == self::ABSOLUTE) { $startTime = $coupon->time['beginTime']; $endTime = $coupon->time['endTime']; } else { $startDate = TimeUtil::today() + 60 * 60 * $coupon->time['beginTime'] * 24; $startTime = new MongoDate($startDate); $endTime = new MongoDate($startDate + $coupon->time['endTime'] * 60 * 60 * 24); } $couponItem = ['id' => $coupon->_id, 'title' => $coupon->title, 'picUrl' => $coupon->picUrl, 'startTime' => $startTime, 'endTime' => $endTime, 'status' => self::UNUSED, 'type' => $coupon->type, 'receiveType' => empty($receiveType) ? self::RECEIVE_COUPON : $receiveType, 'discountAmount' => $coupon->discountAmount, 'discountCondition' => $coupon->discountCondition, 'reductionAmount' => $coupon->reductionAmount]; $code = self::getCouponCode($coupon->accountId); $hostInfo = self::getHostInfo(); $qrcode = Yii::$app->qrcode->create($hostInfo, Coupon::COUPON_QRCODE_RECEIVED, $code, $coupon->accountId); $qrcodeItem = ['_id' => $qrcode->_id, 'qiniuKey' => $qrcode->qiniuKey]; //get member name $memberName = ''; if (!empty($member->properties)) { foreach ($member->properties as $properties) { if ($properties['name'] == Member::DEFAULT_PROPERTIES_NAME) { $memberName = $properties['value']; } } } $membershipDiscount = new MembershipDiscount(); $membershipDiscount->qrcode = $qrcodeItem; $membershipDiscount->coupon = $couponItem; $membershipDiscount->member = ['id' => $member->_id, 'name' => $memberName]; $membershipDiscount->code = $code; $membershipDiscount->createdAt = new MongoDate(); $membershipDiscount->accountId = $coupon->accountId; return $membershipDiscount; }
public static function getTotalScoreYesterday($accountId) { $todayTimeStamp = TimeUtil::today(); $today = new MongoDate($todayTimeStamp); $yesterday = new MongoDate($todayTimeStamp - 24 * 3600); $raw = self::getCollection()->aggregate([['$match' => ['createdAt' => ['$lte' => $today, '$gt' => $yesterday], 'accountId' => $accountId]], ['$group' => ['_id' => null, 'totalScore' => ['$sum' => '$increment']]]]); if (empty($raw)) { return 0; } return $raw[0]['totalScore']; }