/** * Give points to member * * <b>Request Type: </b>POST<br/> * <b>Request Endpoint: </b>http://{server-domain}/api/member/score/give<br/> * <b>Content-type: </b>application/json<br/> * <b>Summary: </b>This api is for give scores to specific members or conditions to filter member.<br/> * * <b>Request Parameters: </b> * scores: int, the value of the scores to give, required<br/> * filterType: string, the type of the filter, "name" or "number" or "tag", required<br/> * names: array<string>, the array of the names for filter, required only if the filterType is "name"<br/> * numbers: array<string>, the array of the numbers to filter members, required only if the filterType is "number"<br/> * tags: array<string>, the array of the tags fo filter members, required only if the filterType is "tag"<br/> * description: string, the description. optional. <br/> * * <b>Request Example</b><br/> * <pre> * {"score":100, "filterType":"name", "names":["Zhang San", "Li Si"]} * </pre> * */ public function actionGive() { $filterType = $this->getParams('filterType'); $score = $this->getParams('score'); $description = $this->getParams('description'); if (empty($filterType) || empty($score)) { throw new BadRequestHttpException('Missing required parameters'); } $userId = $this->getUserId(); $user = User::findByPk($userId); $user = ['id' => $userId, 'name' => $user->name]; $filterKey = $filterType . 's'; $filterValue = $this->getParams($filterKey); if (empty($filterValue)) { throw new BadRequestHttpException("Missing required parameters"); } $function = "giveScoreBy" . $filterKey; $memberList = Member::$function($score, $filterValue); if (empty($memberList)) { throw new InvalidParameterException(['member-' . $filterType => \Yii::t('member', 'no_member_find')]); } if ($memberList) { foreach ($memberList as $member) { $scoreHistory = new ScoreHistory(); $scoreHistory->assigner = ScoreHistory::ASSIGNER_ADMIN; $scoreHistory->increment = $score; $scoreHistory->memberId = $member; $scoreHistory->brief = $score >= 0 ? ScoreHistory::ASSIGNER_ADMIN_ISSUE_SCORE : ScoreHistory::ASSIGNER_ADMIN_DEDUCT_SCORE; $scoreHistory->description = $description; $scoreHistory->channel = ['origin' => ScoreHistory::PORTAL]; $scoreHistory->user = $user; $scoreHistory->accountId = $this->getAccountId(); if (!$scoreHistory->save()) { LogUtil::error(['message' => 'save scoreHistory failed', 'data' => $scoreHistory->toArray()], 'member'); } } return ['status' => 'ok']; } }
/** * 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'); } }
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'); } } } }
/** * 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'); } }
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; } }
/** * 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; } }