/** * delete muti redemption template and promotion template(delete-product-template) */ public function actionDeleteProductTemplate() { $accountInfos = Account::findAll(['enabledMods' => ['$all' => ['product']]]); if ($accountInfos) { $redemptionTemplate = $promotionTemplate = []; foreach ($accountInfos as $accountInfo) { if (empty($redemptionTemplate) || empty($promotionTemplate)) { $datas = MessageTemplate::getDefaultTemplateData($accountInfo['_id']); foreach ($datas as $data) { if ($data['name'] == MessageTemplate::REDEMPTION_TITLE) { $redemptionTemplate = ['name' => $data['name'], 'weChat' => $data['weChat'], 'email' => $data['email'], 'mobile' => $data['mobile']]; } if ($data['name'] == MessageTemplate::PROMOTIONCODE_TITLE) { $promotionTemplate = ['name' => $data['name'], 'weChat' => $data['weChat'], 'email' => $data['email'], 'mobile' => $data['mobile']]; } } } $redemptionTemplate['accountId'] = $accountInfo['_id']; $promotionTemplate['accountId'] = $accountInfo['_id']; $redemptionDatas = MessageTemplate::findAll($redemptionTemplate); if (count($redemptionDatas) >= 2) { MessageTemplate::deleteAll(['_id' => $redemptionDatas[0]->_id]); echo 'delete redemptionTemplate id:' . $redemptionDatas[0]->_id . PHP_EOL; } $promotionDatas = MessageTemplate::findAll($promotionTemplate); if (count($promotionDatas) >= 2) { MessageTemplate::deleteAll(['_id' => $promotionDatas[0]->_id]); echo 'delete promotionTemplate id:' . $promotionDatas[0]->_id . PHP_EOL; } } } echo 'over' . PHP_EOL; }
/** * @args {"description": "Delay: Stats of StatsMemberGrowthMonthly every day"} * @author Rex Chen */ public function perform() { $args = $this->args; $date = empty($args['date']) ? '' : $args['date']; $datetime = TimeUtil::getDatetime($date); $dateStr = date('Y-m', $datetime); $startTime = new \MongoDate(strtotime($dateStr . '-01')); $endTime = new \MongoDate(strtotime($dateStr . '-01 +1 month')); $accounts = Account::findAll(['enabledMods' => 'member']); foreach ($accounts as $account) { $accountId = $account->_id; $totalMember = Member::countByAccount($accountId, null, $endTime); $totalActive = MemberLogs::getTotalActiveByAccount($accountId, $startTime, $endTime); $totalNew = Member::countByAccount($accountId, $startTime, $endTime); $statsGrowth = ModelStatsMemberGrowthMonthly::getByMonthAndAccount($accountId, $dateStr); if (empty($statsGrowth)) { $statsGrowth = new ModelStatsMemberGrowthMonthly(); $statsGrowth->accountId = $accountId; $statsGrowth->month = $dateStr; } $statsGrowth->totalNew = $totalNew; $statsGrowth->totalActive = $totalActive; $statsGrowth->totalInactive = $totalMember - $totalActive; try { $statsGrowth->save(); } catch (Exception $e) { ResqueUtil::log(['Update StatsMemberGrowthMonthly error' => $e->getMessage(), 'StatsMemberGrowthMonthly' => $statsGrowth]); continue; } } return true; }
/** * @args {"description": "Delay: Clean offline client and helpdesk every minute"} */ public function perform() { $accounts = Account::findAll([]); foreach ($accounts as $account) { $accountId = $account->_id; $setting = HelpDeskSetting::findOne(['accountId' => $accountId]); if (!empty($setting)) { $maxWaitTime = $setting->maxWaitTime; // Close timeout conversation $chatConversations = ChatConversation::findAll(['accountId' => $accountId, 'status' => ChatConversation::STATUS_OPEN, 'lastChatTime' => ['$lt' => TimeUtil::msTime(time() - $maxWaitTime * 60)]]); foreach ($chatConversations as $chatConversation) { HelpDesk::disconnect($chatConversation->_id, ['type' => 'brake']); } // Delete timeout pending client $pendingClients = PendingClient::findAll(['accountId' => $accountId, 'lastPingTime' => ['$lt' => TimeUtil::msTime(time() - PendingClient::PING_THRESHOLD)]]); foreach ($pendingClients as $pendingClient) { $pendingClient->delete(); } // Clean offline helpdesk $cache = Yii::$app->cache; $onlineHelpDesks = $cache->get(HelpDesk::CACHE_PREFIX_HELPDESK_PING . $accountId); if (!empty($onlineHelpDesks)) { foreach ($onlineHelpDesks as $deskId => $lastPingTime) { if ($lastPingTime < TimeUtil::msTime(time() - HelpDesk::PING_THRESHOLD)) { HelpDesk::leave($deskId, $accountId, ['type' => 'droping']); } } } } } }
public function actionIndex() { $accounts = Account::findAll([]); if (!empty($accounts)) { foreach ($accounts as $account) { $options = Yii::$app->params['sensitive_options']; foreach ($options as $name => $options) { SensitiveOperation::initOptions($name, $options, $account->_id); } } } echo "Fininsh init the sensitive operation.\n"; }
/** * @args {"description": "Delay: Issue birthday score every day"} */ public function perform() { $args = $this->args; //Delay issue birthday score every day if (empty($args['memberId']) || empty($args['accountId'])) { $accounts = Account::findAll(['enabledMods' => 'member']); foreach ($accounts as $account) { $this->getBirthDayReward($account->_id); } } else { //issue birthday score when update member profile $this->getBirthDayReward(new MongoId($args['accountId']), new MongoId($args['memberId'])); } return true; }
public function actionFixData($startData, $endData) { $accounts = Account::findAll(['enabledMods' => 'product']); foreach ($accounts as $account) { $accountId = $account->_id; $condition = ['accountId' => $accountId, 'createdAt' => ['$gte' => new MongoDate(strtotime($startData)), '$lt' => new Mongodate(strtotime($endData))]]; $pipeline = [['$match' => $condition], ['$group' => ['_id' => ['campaignId' => '$campaignId', 'code' => '$code'], 'count' => ['$sum' => 1]]], ['$match' => ['count' => ['$gt' => 1]]]]; $stats = CampaignLog::getCollection()->aggregate($pipeline); if (!empty($stats)) { foreach ($stats as $stat) { $code = $stat['_id']['code']; $logCondition = array_merge($condition, $stat['_id']); $logs = CampaignLog::find()->where($logCondition)->orderBy(['createdAt' => 1])->all(); $memberId = $logs[0]['member']['id']; $productName = $logs[0]['productName']; $description = $productName . ' ' . $code; $scoreHistoryCondition = ['memberId' => $memberId, 'brief' => ScoreHistory::ASSIGNER_EXCHANGE_PROMOTION_CODE, 'description' => $description]; $scoreHistorys = ScoreHistory::find()->where($scoreHistoryCondition)->orderBy(['createdAt' => 1])->all(); $keepScoreHistory = $scoreHistorys[0]; unset($scoreHistorys[0]); $removeScoreHistoryIds = []; $deduct = 0; foreach ($scoreHistorys as $scoreHistory) { $removeScoreHistoryIds[] = $scoreHistory->_id; $deduct += $scoreHistory->increment; } $member = Member::findByPk($memberId); if ($member->score <= $deduct || $member->totalScore <= $deduct || $member->totalScoreAfterZeroed <= $deduct) { echo 'Failed : Member' . $memberId . ' score not enough ' . 'score: ' . $member->score; echo ' totalScore: ' . $member->totalScore; echo ' totalScoreAfterZeroed: ' . $member->totalScoreAfterZeroed . PHP_EOL; continue; } $deductScore = 0 - $deduct; Member::updateAll(['$inc' => ['score' => $deductScore, 'totalScore' => $deductScore, 'totalScoreAfterZeroed' => $deductScore]], ['_id' => $memberId]); ScoreHistory::deleteAll(['_id' => ['$in' => $removeScoreHistoryIds]]); $logIds = ArrayHelper::getColumn($logs, '_id'); $keepLogId = $logIds[0]; unset($logIds[0]); CampaignLog::deleteAll(['_id' => ['$in' => array_values($logIds)]]); echo 'Success: ' . $productName . ' ' . $code . ' ' . $stat['count']; echo ' Deduct member ' . $memberId . ' score ' . $deduct . PHP_EOL; } } } echo 'Success' . PHP_EOL; }
/** * @args {"description": "Delay: Statistics of location info and scores issued by rule every day"} */ public function perform() { $args = $this->args; $accounts = Account::findAll(['enabledMods' => 'member']); foreach ($accounts as $account) { $accountId = $account->_id; //score rule statistics $scoreRules = ScoreRule::getByAccount($accountId); foreach ($scoreRules as $scoreRule) { $this->updateScoreRuleStatistics($scoreRule->name, $accountId); } //location statistics $result = $this->updateLocationStatistics($accountId); if (!$result) { LogUtil::error(['message' => 'statistics location error', 'date' => date('Y-m-d H:i:s'), 'args' => $args], 'resque'); } } return true; }
/** * @args {"description": "Delay: stats of member orders", "runNextJob": true} * @author Rex Chen */ public function perform() { $dateStr = date('Y-m-d'); $operateTimeTo = new MongoDate(strtotime($dateStr)); $accounts = Account::findAll([]); foreach ($accounts as $account) { $modelStatsOrder = StatsOrder::getLatestByAccount($account->_id); if (empty($modelStatsOrder)) { $operateTimeFrom = null; } else { //ensure Y-m-d mongodate $operateDate = MongodbUtil::MongoDate2String($modelStatsOrder->createdAt, 'Y-m-d'); if ($dateStr === $operateDate) { return true; } $operateTimeFrom = new MongoDate(strtotime($operateDate)); } $this->statsMemberOrder($account->_id, $operateTimeFrom, $operateTimeTo); $this->updateMemberRecentTransactionCount($account->_id); } }
/** * create default category base on the account id(create-default-category) * @param $accountId, string; if this value is all,it will support all accounts,otherwise it only support this account */ public function actionCreateDefaultCategory($accountId) { $where = ['enabledMods' => ['$all' => ['helpdesk']]]; if (empty($accountId)) { echo 'AccountId can not be empty' . PHP_EOL; exit; } elseif ($accountId == 'all') { $accounts = Account::findAll($where); if (!empty($accounts)) { foreach ($accounts as $account) { $this->_createDefaultCategory($account->_id); } } } else { $accountId = new MongoId($accountId); $account = Account::findOne(array_merge(['_id' => $accountId], $where)); if (empty($account)) { echo 'Can not find the account by ' . $accountId . PHP_EOL; exit; } $this->_createDefaultCategory($accountId); } echo 'Create default value successfully' . PHP_EOL; }
/** * delete member log when the member is deleted(delete-member-log) */ public function actionDeleteMemberLog() { $accounts = Account::findAll(['enabledMods' => 'member']); foreach ($accounts as $account) { $deletedMembers = Member::find()->where(['isDeleted' => Member::DELETED, 'accountId' => $account->_id])->all(); $deletedMemberIds = ArrayHelper::getColumn($deletedMembers, '_id'); MemberLogs::deleteAll(['memberId' => ['$in' => $deletedMemberIds]]); } }
/** * Get all accountId * @return array */ public static function getAllAccount() { $accounts = Account::findAll([]); $ids = []; foreach ($accounts as $account) { $ids[] = $account->_id; } return $ids; }
/** * create default page cover base on account id and host info(域名)(create-default-page-cover) * @param $accountId, string; if this value is all,it will support all accounts,otherwise it only support this account * @param $hostinfo, string, domain name example:http://wm.com */ public function actionCreateDefaultPageCover($accountId, $hostinfo) { $where = ['enabledMods' => ['$all' => ['microsite']]]; if (empty($accountId) || empty($hostinfo)) { echo 'accountId and hostinfo can not be empty' . PHP_EOL; exit; } elseif ($accountId == 'all') { $accounts = Account::findAll($where); if (!empty($accounts)) { foreach ($accounts as $account) { $this->_createDefaultPageCover($account->_id, $hostinfo); } } } else { $accountId = new MongoId($accountId); $account = Account::findOne(array_merge(['_id' => $accountId], $where)); if (empty($account)) { echo 'Can not find the account by ' . $accountId . PHP_EOL; exit; } $this->_createDefaultPageCover($accountId, $hostinfo); } echo 'Create default value successfully' . PHP_EOL; }