public function perform() { $args = $this->args; if (empty($args['mainMember']) || empty($args['otherMemberIds'])) { ResqueUtil::log(['Merge member args error' => $args]); return; } $mainMember = unserialize($args['mainMember']); $otherMemberIds = unserialize($args['otherMemberIds']); //Get Name and phone $name = $phone = ''; foreach ($mainMember->properties as $mainProperty) { if ($mainProperty['name'] === Member::DEFAULT_PROPERTIES_NAME) { $name = $mainProperty['value']; } if ($mainProperty['name'] === Member::DEFAULT_PROPERTIES_MOBILE) { $phone = $mainProperty['value']; } } ScoreHistory::updateAll(['$set' => ['memberId' => $mainMember->_id]], ['memberId' => ['$in' => $otherMemberIds]]); MemberLogs::deleteAll(['memberId' => ['$in' => $otherMemberIds]]); CampaignLog::updateAll(['$set' => ['member.id' => $mainMember->_id, 'member.cardNumber' => $mainMember->cardNumber, 'member.name' => $name, 'member.phone' => $phone]], ['member.id' => ['$in' => $otherMemberIds]]); PromotionCode::updateAll(['$set' => ['usedBy.memberId' => $mainMember->_id, 'usedBy.memberNumber' => $mainMember->cardNumber]], ['usedBy.memberId' => ['$in' => $otherMemberIds]]); GoodsExchangeLog::updateAll(['$set' => ['memberId' => $mainMember->_id, 'memberName' => $name, 'telephone' => $phone]], ['memberId' => ['$in' => $otherMemberIds]]); $otherMemberStrIds = []; foreach ($otherMemberIds as $otherMemberId) { $otherMemberStrIds[] = (string) $otherMemberId; } Order::updateAll(['$set' => ['consumer.id' => (string) $mainMember->_id, 'consumer.name' => $name, 'consumer.phone' => $phone]], ['consumer.id' => ['$in' => $otherMemberStrIds]]); Qrcode::deleteAll(['type' => Qrcode::TYPE_MEMBER, 'associatedId' => ['$in' => $otherMemberIds]]); }
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; }
/** * Reward shake score * @param MongoId $memberId * @param int $score */ public function shakeScore($memberId, $score, $channelInfo) { ModelMember::updateAll(['$inc' => ['score' => $score, 'totalScore' => $score, 'totalScoreAfterZeroed' => $score]], ['_id' => $memberId]); $scoreHistory = new ScoreHistory(); $scoreHistory->assigner = ScoreHistory::ASSIGNER_SHAKE_SCORE; $scoreHistory->increment = $score; $scoreHistory->memberId = $memberId; $scoreHistory->brief = ScoreHistory::ASSIGNER_SHAKE_SCORE; $scoreHistory->description = ''; $scoreHistory->channel = $channelInfo; $scoreHistory->user = null; $scoreHistory->accountId = $this->accountId; if (!$scoreHistory->save()) { LogUtil::error(['message' => 'Failed to save the history for unknown problem', 'scoreHistory' => $scoreHistory->toArray()], 'resque'); } }
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; }
/** * Query the score history of a specific member * * <b>Request Type: </b>GET<br/> * <b>Request Endpoint: </b>http://{server-domain}/api/scores?memberId={memberId}&per-page={pageSize}&page={pageNum} * <b>Summary: </b>This api is for query the score history for a specific member * * <b>Response Example: </b> * <pre> * { * "items": [ * { * "id": "54aa2e1fdb4c0ec6048b4570", * "assigner": "admin", * "increment": 100, * "description": "abc123", * "createdAt": "2015-01-05 14:24:31" * }, * { * "id": "54aa2df3db4c0ec5048b4570", * "assigner": "admin", * "increment": 100, * "description": "", * "createdAt": "2015-01-05 14:23:47" * } * ], * "_links": { * "self": { * "href": "http://dev.cp.augmarketing.cn/api/member/scores?memberId=54a8f557ff64ee5203bede18&page=1" * } * }, * "_meta": { * "totalCount": 12, * "pageCount": 1, * "currentPage": 1, * "perPage": 20 * } *} * </pre> */ public function actionIndex() { $params = $this->getQuery(); if (!empty($params['memberId'])) { $params['memberId'] = new MongoId($params['memberId']); } $accountId = $this->getAccountId(); //build the query return ScoreHistory::search($params, $accountId); }
/** * Auto zeroed member score */ public static function resetScore($memberId, $accountId) { $member = self::findByPk($memberId); $memberScore = $member->score; $member->score = 0; $member->totalScoreAfterZeroed = 0; $updateResult = $member->update(); if ($updateResult) { $scoreHistory = new ScoreHistory(); $scoreHistory->assigner = ScoreHistory::ASSIGNER_AUTO_ZEROED; $scoreHistory->increment = -$memberScore; $scoreHistory->memberId = $memberId; $scoreHistory->brief = ScoreHistory::ASSIGNER_AUTO_ZEROED; $scoreHistory->channel = ['origin' => ScoreHistory::PORTAL]; $scoreHistory->accountId = $accountId; $saveResult = $scoreHistory->save(); if (!$saveResult) { LogUtil::error(['message' => 'Save score history fail', 'errors' => $scoreHistory->getErrors()], 'member'); } } else { LogUtil::error(['message' => 'Reset score fail', 'errors' => $member->getErrors(), 'memberId' => $member->_id], 'member'); } }
/** * This function is just for fix error promotionCode redeem data * @param MongoId $accountId * @param MongoId $memberId * @param Array $codes * @return boolean, true, if there is no error data */ private function fixData($accountId, $memberId, $codes) { $condition = ['accountId' => $accountId, 'member.id' => $memberId, 'code' => ['$in' => $codes]]; $pipeline = [['$match' => $condition], ['$group' => ['_id' => ['campaignId' => '$campaignId', 'code' => '$code'], 'count' => ['$sum' => 1]]], ['$match' => ['count' => ['$gt' => 1]]]]; $stats = CampaignLog::getCollection()->aggregate($pipeline); if (empty($stats)) { return true; } $logCondition = ['accountId' => $accountId, 'member.id' => $memberId]; $failedMessages = []; $successMessages = []; foreach ($stats as $stat) { $code = $stat['_id']['code']; //get campaign log $logCondition = array_merge($logCondition, $stat['_id']); $logs = CampaignLog::find()->where($logCondition)->orderBy(['createdAt' => SORT_ASC])->all(); $memberId = $logs[0]['member']['id']; $productName = $logs[0]['productName']; //get score history $description = $productName . ' ' . $code; $scoreHistoryCondition = ['memberId' => $memberId, 'brief' => ScoreHistory::ASSIGNER_EXCHANGE_PROMOTION_CODE, 'description' => $description]; $scoreHistorys = ScoreHistory::find()->where($scoreHistoryCondition)->orderBy(['createdAt' => SORT_ASC])->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 not enough, log continue if ($member->score <= $deduct || $member->totalScore <= $deduct || $member->totalScoreAfterZeroed <= $deduct) { $failedMessages[] = ['Failed' => 'Member score not enough', 'member' => $member->toArray(), 'deduct' => $deduct]; continue; } //fix member score $deductScore = 0 - $deduct; Member::updateAll(['$inc' => ['score' => $deductScore, 'totalScore' => $deductScore, 'totalScoreAfterZeroed' => $deductScore]], ['_id' => $memberId]); //remove scorehistory ScoreHistory::deleteAll(['_id' => ['$in' => $removeScoreHistoryIds]]); //remove campaignlog $logIds = ArrayHelper::getColumn($logs, '_id'); $keepLogId = $logIds[0]; unset($logIds[0]); CampaignLog::deleteAll(['_id' => ['$in' => array_values($logIds)]]); $successMessages[] = ['Success' => $productName . ' ' . $code . ' ' . $stat['count'], 'memberId' => $memberId, 'deduct' => $deduct]; } LogUtil::error(['Failed' => $failedMessages, 'Success' => $successMessages], 'fix-campaign-data'); }
private function issueBirthdayScore($accountId, $birthdayScore, $memberId = null) { if (!empty($birthdayScore) && $birthdayScore->isEnabled && $birthdayScore->rewardType == ScoreRule::REWARD_SCORE_TYPE) { //reward score $suitMemberIds = $this->getRewardMemberIds($accountId, $memberId, $birthdayScore); $memberList = Member::giveScoreByIds($birthdayScore->score, $suitMemberIds); //update history foreach ($memberList as $id) { $scoreHistory = new ScoreHistory(); $scoreHistory->assigner = ScoreHistory::ASSIGNER_RULE; $scoreHistory->increment = $birthdayScore->score + 0; $scoreHistory->memberId = $id; $scoreHistory->brief = ScoreHistory::ASSIGNER_RULE; $scoreHistory->description = $birthdayScore->name; $scoreHistory->channel = ['origin' => ScoreHistory::PORTAL]; $scoreHistory->accountId = $accountId; if (!$scoreHistory->save()) { LogUtil::error(['message' => 'birthday score member', 'member' => $memberList], 'member'); } } } }
/** * @param $fromDate string (eg:'2015-09-30 23:59:59') * @param $memberId MongoId * @param $accountId MongoId * @return int * * createdAt: a < createdAt <= b */ public static function getScoreOffset($fromDate, $memberId, $accountId) { $scoreOffset = 0; $time = strtotime($fromDate); $fromDate = new \MongoDate($time); $now = new \MongoDate(); $condition = ['accountId' => $accountId, 'memberId' => $memberId, 'createdAt' => ['$gt' => $fromDate, '$lte' => $now]]; $histories = ScoreHistory::find()->where($condition)->all(); if (!empty($histories) && count($histories) > 0) { foreach ($histories as $history) { $scoreOffset += $history->increment; } } unset($time, $fromDate, $now, $condition, $histories, $accountId); return $scoreOffset; }
public static function setScoreHistory($scoreAdded, $member, $scoreHistoryDescription, $channel, $params) { $history = ['assigner' => ScoreHistory::ASSIGNER_EXCHANGE_PROMOTION_CODE, 'increment' => intval($scoreAdded), 'memberId' => $member->_id, 'accountId' => $member->accountId, 'description' => $scoreHistoryDescription, 'brief' => ScoreHistory::ASSIGNER_EXCHANGE_PROMOTION_CODE, 'channel' => $channel, 'user' => $params['userInfo']]; ScoreHistory::recordScore($history); }
private function getTimeAndmemberCountWithScore($ruleName, $accountId) { $times = ScoreHistory::getTotalRule($ruleName, $accountId); $memberCount = ScoreHistory::getTotalRuleMember($ruleName, $accountId); return [$times, $memberCount]; }
private function _saveLog(Member $member, $exchanges, $params, $user = null) { $goodsExchangeLog = new GoodsExchangeLog(); $allGoods = []; $totalCount = 0; $scoreHistoryDescription = ''; foreach ($exchanges as $exchange) { $goods = $exchange['goods']; $count = $exchange['count']; $pictures = $goods->pictures; $allGoods[] = ['id' => $goods->_id, 'productId' => $goods->productId, 'sku' => $goods->sku, 'picture' => empty($pictures[0]) ? '' : $pictures[0], 'productName' => $goods->productName, 'count' => $count]; $totalCount += $count; $scoreHistoryDescription .= $goods->productName . "({$count}); "; } $scoreHistoryDescription = trim($scoreHistoryDescription, '; '); $goodsExchangeLog->goods = $allGoods; $goodsExchangeLog->memberId = $member->_id; $properties = $member->properties; $name = ''; foreach ($properties as $property) { if ($property['name'] == Member::DEFAULT_PROPERTIES_NAME) { $name = $property['value']; } if ($property['name'] == Member::DEFAULT_PROPERTIES_MOBILE) { $mobile = $property['value']; } } $goodsExchangeLog->memberName = $name; $goodsExchangeLog->telephone = empty($params['phone']) ? $mobile : $params['phone']; $goodsExchangeLog->usedScore = $params['usedScore']; $goodsExchangeLog->expectedScore = $params['expectedScore']; $goodsExchangeLog->count = $totalCount; $goodsExchangeLog->address = $params['address']; $goodsExchangeLog->receiveMode = $params['receiveMode']; $goodsExchangeLog->postcode = empty($params['postcode']) ? '' : $params['postcode']; $goodsExchangeLog->isDelivered = false; $scoreHistoryChannel = []; if (!empty($params['channelId'])) { $channelInfo = Channel::getByChannelId($params['channelId'], $member->accountId); $scoreHistoryChannel = ['id' => $channelInfo->channelId, 'name' => $channelInfo->name, 'origin' => $channelInfo->origin]; $goodsExchangeLog->usedFrom = ['id' => $params['channelId'], 'type' => $channelInfo->origin, 'name' => $channelInfo->name]; } else { $scoreHistoryChannel = ['origin' => GoodsExchangeLog::PORTAL]; $goodsExchangeLog->usedFrom = ['id' => '', 'type' => GoodsExchangeLog::PORTAL, 'name' => GoodsExchangeLog::OFFLINE_EXCHANGE]; } $goodsExchangeLog->accountId = $member->accountId; $scoreHistory = new ScoreHistory(); $scoreHistory->assigner = ScoreHistory::ASSIGNER_EXCHAGE_GOODS; $scoreHistory->increment = 0 - $params['usedScore']; $scoreHistory->memberId = $member->_id; $scoreHistory->brief = ScoreHistory::ASSIGNER_EXCHAGE_GOODS; $scoreHistory->description = $scoreHistoryDescription; $scoreHistory->accountId = $member->accountId; $scoreHistory->channel = $scoreHistoryChannel; $scoreHistory->user = $user; if ($goodsExchangeLog->save(true) && $scoreHistory->save(true)) { MemberLogs::record($member->_id, $member->accountId, MemberLogs::OPERATION_REDEEM); return true; } else { LogUtil::error(['exchange fail' => [$scoreHistory->getErrors(), $goodsExchangeLog->getErrors()]]); return false; } }
public function actionView($id) { $member = Member::findByPk(new \MongoId($id)); $accountId = $this->getAccountId(); if (empty($member)) { throw new BadRequestHttpException(Yii::t('member', 'invalid_member_id')); } $scoreHistory = ScoreHistory::getLastByMemberId($member->_id); $qrcode = Qrcode::getByTypeAndAssociated(Qrcode::TYPE_MEMBER, $member->_id, $accountId); $member = $member->toArray(); $properties = ArrayHelper::toArray(MemberProperty::getAllByAccount($accountId)); $mapPropertyDefault = ArrayHelper::map($properties, 'id', 'isDefault'); foreach ($member['properties'] as &$property) { $property['isDefault'] = empty($mapPropertyDefault[$property['id']]) ? false : $mapPropertyDefault[$property['id']]; } $member['qrcodeUrl'] = empty($qrcode->qiniuKey) ? '' : \Yii::$app->qrcode->getUrl($qrcode->qiniuKey); $member['scoreProvideTime'] = empty($scoreHistory->createdAt) ? '' : MongodbUtil::MongoDate2String($scoreHistory->createdAt); return $member; }
/** * record the score of member where the score come from */ public static function recordScore($data) { $scoreHistory = new ScoreHistory(); $scoreHistory->load($data, ''); if (false === $scoreHistory->save()) { return false; } else { return $scoreHistory; } }