Пример #1
0
 public static function record($memberId, $accountId, $operation, $operationAt = null)
 {
     $memberLogs = new MemberLogs();
     $memberLogs->memberId = $memberId;
     $memberLogs->accountId = $accountId;
     $memberLogs->operation = $operation;
     $memberLogs->operationAt = $operationAt == null ? new \MongoDate() : $operationAt;
     return $memberLogs->save();
 }
 /**
  * @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;
 }
Пример #3
0
 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;
 }
Пример #5
0
 /**
  * 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]]);
     }
 }
Пример #6
0
 private function renderPage($page, $useRiot = false, $injectWechatJS = true, $socialWebviewOnly = true)
 {
     $actionName = $this->action->id;
     $params = Yii::$app->request->get();
     $this->registerCommonResource($page, $useRiot);
     if ($injectWechatJS) {
         $sdk = Yii::$app->wechatSdk;
         $sdk->refererUrl = $sdk->refererDomain . substr(Yii::$app->request->getUrl(), 1);
         $signPackage = json_encode($sdk->getSignPackage());
         $logObj = json_encode(['url' => Yii::$app->params['frontendTrackUrl'], 'env' => Yii::$app->params['currentEnv']]);
         $js = "var options={$signPackage}, page='{$page}';window.trackerLog={$logObj};";
         $this->view->registerJs($js, View::POS_HEAD);
         if (empty($params['debug'])) {
             $this->registerBodyJsFile(self::BUILD_PATH . 'handlewechat.js?v=' . Yii::$app->params['buildVersion']);
         }
     }
     $socialWebviewOnly = $socialWebviewOnly && empty($params['debug']);
     if ($socialWebviewOnly && !BrowserUtil::isWeixinBrowser() && !BrowserUtil::isWeiboBrower() && !BrowserUtil::isAliBrower()) {
         $this->view->js = null;
         $this->view->jsFiles = null;
         $this->view->cssFiles = null;
         $this->view->registerCssFile(self::BUILD_PATH . 'common/app.css');
         return $this->render('common/error');
     } else {
         //Member active record
         $accessToken = $this->getAccessToken();
         if (!empty($params['memberId']) && !empty($accessToken)) {
             $memberId = new \MongoId($params['memberId']);
             $accountId = Token::getAccountId($accessToken);
             MemberLogs::record($memberId, $accountId, MemberLogs::OPERATION_VIEWED);
         }
         return $this->render($actionName . '/' . $page);
     }
 }
Пример #7
0
 /**
  * record log for exchange promotion code in a campaign
  * @param $accountid objectId
  * @param $params arrray
  * @param $member object
  * @param $campaign object
  * @param $scoreAdded int
  */
 public static function recordSelectedCampaign($params, $member, $campaign, $scoreAdded)
 {
     //add score for member
     self::addMemberScore($scoreAdded, $params['memberId']);
     $productInfo = Product::findByPk($params['productId']);
     $scoreHistoryDescription = $productInfo->name . ' ' . $params['code'];
     $channelInfo = self::getChannelInfo($params);
     //record campaign log
     $campaignLog = self::setCampaignLog($scoreAdded, $params, $member, $campaign, $productInfo, $channelInfo);
     //recode the scoreHistory
     if ($scoreAdded > 0) {
         self::setScoreHistory($scoreAdded, $member, $scoreHistoryDescription, $channelInfo['scoreHistoryChannel'], $params);
     }
     //record member log
     MemberLogs::record($member->_id, $member->accountId, MemberLogs::OPERATION_REDEEM, $campaignLog->redeemTime);
     unset($params, $productInfo, $member, $campaignLog);
 }
Пример #8
0
 public function perform()
 {
     # Run task
     $args = $this->args;
     //the key for wrong number for store in redis
     $wrongKey = self::MEMBER_WRONG;
     switch ($args['type']) {
         case self::JOB_DELETE_REDIS_CODE:
             if (!isset($args['filename']) || !isset($args['accountId'])) {
                 ResqueUtil::log(['error' => 'missing param', 'param' => $args]);
                 return false;
             }
             $redis = \Yii::$app->cache->redis;
             $cacheSetInsert = self::SET_HEAD_INSERT . md5($args['accountId'] . "_" . $args['fileName']);
             $cacheSetUpdate = self::SET_HEAD_UPDATE . md5($args['accountId'] . "_" . $args['fileName']);
             $redis->del($cacheSetInsert);
             $redis->del($cacheSetUpdate);
             unset($commonKey, $cacheTotalKey, $count);
             break;
         case self::JOB_INSERT:
             if (!isset($args['accountId']) || empty($args['filename']) || empty($args['hostInfo'])) {
                 ResqueUtil::log(['error' => 'missing params', 'param' => $args]);
             }
             $redis = Yii::$app->cache->redis;
             $cacheKey = $args['accountId'] . "_" . $args['filename'];
             $createdAt = new \MongoDate(time());
             $redis->expire($cacheKey, self::EXPIRE);
             $cacheSetInsert = self::SET_HEAD_INSERT . md5($cacheKey);
             $cacheSetUpdate = self::SET_HEAD_UPDATE . md5($cacheKey);
             // status
             $cacheSetLackProperties = self::MEMBER_IMPORT_NO_PROPERTY . md5($cacheKey);
             // get total of excel.
             $totalInsert = $redis->scard($cacheSetInsert);
             $totalUpdate = $redis->scard($cacheSetUpdate);
             // batch insert member.
             $memberInsert = [];
             $keyInsert = $redis->smembers($cacheSetInsert);
             // update member.
             $keyUpdate = $redis->smembers($cacheSetUpdate);
             // unserialize.
             if (count($keyInsert) > 0) {
                 $defaultCard = MemberShipCard::getDefault(new MongoId($args['accountId']));
                 if (empty($defaultCard)) {
                     ResqueUtil::log('Lack of default MemberShip Card');
                     // 0
                     $redis->Hset($cacheSetLackProperties, $wrongKey, self::MEMBER_NO_DEFAULT_PROPERTIES);
                     return false;
                 }
                 $cardId = $defaultCard->_id;
                 $origin = Member::PORTAL;
                 $tels = [];
                 for ($rowIndex = 0; $rowIndex < $totalInsert; $rowIndex++) {
                     $memberKey = unserialize($keyInsert[$rowIndex]);
                     $cardNumber = Member::generateCardNumber();
                     $properties = [];
                     $memberTag = [];
                     foreach ($memberKey[0] as $item) {
                         if (!empty($item['name'])) {
                             if ($item['name'] == Member::DEFAULT_PROPERTIES_MOBILE) {
                                 $telItem = $item;
                             }
                             $properties[] = $item;
                         } else {
                             $memberTag[] = $item['tags'];
                         }
                     }
                     $tels[] = $telItem;
                     $memberMessage = ["properties" => $properties, "tags" => $memberTag, "cardId" => $cardId, "origin" => $origin, "cardNumber" => empty($cardNumber) ? 0 : $cardNumber, "location" => ["country" => "", "province" => "", "city" => "", "detail" => ""], "avatar" => Yii::$app->params['defaultAvatar'], "score" => 0, "totalScore" => 0, "socials" => [], "qrcodeViewed" => false, "isDisabled" => false, "totalScoreAfterZeroed" => 0, "accountId" => new MongoId($args['accountId']), "cardProvideTime" => new \MongoDate()];
                     $memberInsert[] = $memberMessage;
                 }
                 // batch insert member.
                 $insertMemberResult = Member::batchInsert($memberInsert);
                 if ($insertMemberResult) {
                     ResqueUtil::log(['ok' => 'Batch insert member is success', 'data' => $memberInsert]);
                 } else {
                     ResqueUtil::log(['error' => 'Batch insert member is fail', 'data' => $memberInsert]);
                     $redis->Hset($cacheSetLackProperties, $wrongKey, self::MEMBER_NO_DEFAULT_PROPERTIES);
                 }
                 //create qrcode.
                 foreach ($tels as $param) {
                     $member = Member::findOne(['properties' => ['$elemMatch' => ['name' => $param['name'], 'value' => $param['value']]], 'accountId' => new MongoId($args['accountId'])]);
                     Member::webhookEvent($member);
                     MemberLogs::record($member->_id, new MongoId($args['accountId']), MemberLogs::OPERATION_VIEWED);
                     if (!defined('KLP') || !KLP) {
                         $a = Yii::$app->qrcode->create($args['hostInfo'], Qrcode::TYPE_MEMBER, $member->_id, new MongoId($args['accountId']));
                     }
                     Member::birthdayScore($member);
                 }
             }
             // update member.
             if (count($keyUpdate) > 0) {
                 for ($rowIndex = 0; $rowIndex < $totalUpdate; $rowIndex++) {
                     $memberKey = unserialize($keyUpdate[$rowIndex]);
                     $memberProperty = $memberKey[0];
                     $condition = [];
                     $updateData = [];
                     $memberUpdate = [];
                     $memberTag = [];
                     foreach ($memberProperty as $member) {
                         if (!empty($member['name'])) {
                             if ($member['name'] == Member::DEFAULT_PROPERTIES_MOBILE) {
                                 $condition = ["properties.value" => $member['value']];
                             }
                             $memberUpdate[] = $member;
                         } else {
                             $memberTag[] = $member['tags'];
                         }
                     }
                     $updateData[] = ["properties" => $memberUpdate, "tags" => $memberTag];
                     $updateMemberResult = Member::updateAll(['$set' => $updateData[0]], $condition);
                     if ($updateMemberResult) {
                         ResqueUtil::log(['ok' => 'Update member is success', 'data' => $memberInsert]);
                     } else {
                         ResqueUtil::log(['error' => 'Update member is fail', 'data' => $memberInsert]);
                         $redis->Hset($cacheSetLackProperties, $wrongKey, self::MEMBER_NO_DEFAULT_PROPERTIES);
                     }
                 }
             }
             break;
         default:
             break;
     }
 }
Пример #9
0
 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;
     }
 }
Пример #10
0
 public function actionCreate()
 {
     $params = $this->getParams();
     $accountId = $this->getAccountId();
     $member = new Member();
     $member->avatar = empty($params['avatar']) ? '' : $params['avatar'];
     $member->location = empty($params['location']) ? null : $params['location'];
     $member->tags = empty($params['tags']) ? [] : $params['tags'];
     $member->properties = empty($params['properties']) ? [] : $params['properties'];
     $member->accountId = $accountId;
     $this->_validateProperty($member);
     $defaultCard = MemberShipCard::getDefault($accountId);
     $member->cardId = $defaultCard->_id;
     $member->cardNumber = Member::generateCardNumber();
     $member->origin = Member::PORTAL;
     if ($member->save()) {
         Member::webhookEvent($member);
         MemberLogs::record($member->_id, $accountId, MemberLogs::OPERATION_VIEWED);
         if (!defined('KLP') || !KLP) {
             Yii::$app->qrcode->create(Yii::$app->request->hostInfo, Qrcode::TYPE_MEMBER, $member->_id, $accountId);
         }
         $this->attachBehavior('MemberBehavior', new MemberBehavior());
         $this->updateItemByScoreRule($member);
         return $member;
     } else {
         throw new ServerErrorHttpException('Fail to create member');
     }
 }