public static function rewardByScoreRule($ruleName, $memberId, $accountId) { //get score rule $rule = ScoreRule::getByName($ruleName, $accountId); if (!empty($rule) && $rule->isEnabled) { //if check the property, the property is not filled,not need to give reward if (!self::_checkRulePropertiesFilled($rule, $memberId)) { return true; } //get reward type if ($rule->rewardType == ScoreRule::REWARD_SCORE_TYPE) { // member rewarded $rewardHistory = ScoreHistory::getByRuleName($ruleName, $memberId, $accountId); if (!empty($rewardHistory)) { return true; } //reward score $memberList = Member::giveScoreByIds($rule->score, [$memberId]); //update history $scoreHistory = new ScoreHistory(); $scoreHistory->assigner = ScoreHistory::ASSIGNER_RULE; $scoreHistory->increment = $rule->score + 0; $scoreHistory->memberId = $memberId; $scoreHistory->brief = ScoreHistory::ASSIGNER_RULE; $scoreHistory->description = $ruleName; $scoreHistory->channel = ['origin' => ScoreHistory::PORTAL]; $scoreHistory->accountId = $accountId; $scoreHistory->save(); } else { if ($rule->rewardType == ScoreRule::REWARD_COUPON_TYPE && !empty($rule->couponId)) { $rewardCouponLog = CouponLog::getLogByRuleName($ruleName, $memberId, $accountId); if (!empty($rewardCouponLog)) { return true; } $coupon = Coupon::findByPk($rule->couponId); if (empty($coupon)) { LogUtil::error(['message' => 'can not find the coupon when give member birthday reward', 'couponId' => (string) $couponId], 'member'); return true; } if (Coupon::updateAll(['$inc' => ['total' => -1]], ['total' => ['$gt' => 0], '_id' => $rule->couponId])) { //issue membershipdiscount $member = Member::findByPk($memberId); $membershipDiscount = MembershipDiscount::transformMembershipDiscount($coupon, $member, $ruleName); if (false == $membershipDiscount->save()) { Coupon::updateAll(['$inc' => ['total' => 1]], ['_id' => $rule->couponId]); LogUtil::error(['message' => 'add membershipdiscount fail', 'memberId' => (string) $member->_id, 'couponId' => (string) $coupon->_id], 'member'); } } else { LogUtil::error(['message' => 'coupon is not enough when give member birthday reward', 'memberId' => (string) $member->_id, 'couponId' => (string) $coupon->_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'); } } } }