private function _repairData() { $fileName = 'deleted-' . time(); $result = $this->_getRepairData(); foreach ($result as $item) { $condition = ['properties' => ['$elemMatch' => ['name' => Member::DEFAULT_PROPERTIES_MOBILE, 'value' => $item['_id']['tel']]], 'accountId' => $item['_id']['accountId']]; $members = Member::findAll($condition); $score = []; $keepMember = []; $deleteMember = []; foreach ($members as $member) { if (!in_array($member->score, $score)) { $score[] = $member->score; $keepMember[] = $member->_id; } else { $msg = 'deleted member ' . $member->_id . PHP_EOL; $deleteMember[] = $member->_id; echo $msg; $this->_saveFile($msg, $fileName); } } $deleteCondition = ['_id' => ['$in' => $deleteMember]]; Member::deleteAll($deleteCondition); } }
public function actionMerge() { $params = $this->getParams(); if (empty($params['main']) || empty($params['others'])) { throw new BadRequestHttpException(Yii::t('common', 'parameters_missing')); } if (!is_array($params['others']) || in_array($params['main'], $params['others'])) { throw new BadRequestHttpException(Yii::t('common', 'data_error')); } $accountId = $this->getAccountId(); $mainMember = Member::findByPk(new \MongoId($params['main']), ['accountId' => $accountId]); if (empty($mainMember)) { throw new InvalidParameterException(Yii::t('member', 'no_member_find')); } //get mongoIds $otherMemberIds = []; foreach ($params['others'] as $otherId) { $otherMemberIds[] = new \MongoId($otherId); } $otherMembers = Member::findAll(['_id' => ['$in' => $otherMemberIds], 'accountId' => $accountId]); if (empty($otherMembers) || count($otherMembers) != count($params['others'])) { throw new InvalidParameterException(Yii::t('member', 'no_member_find')); } $mainProperties = []; $mainPropertyIds = []; foreach ($mainMember->properties as $mainProperty) { if (!empty($mainProperty['value'])) { $mainPropertyIds[] = $mainProperty['id']; $mainProperties[] = $mainProperty; } } $mainMemberLocation = $mainMember->location; $otherMemberLocation = []; $newMemberTag = empty($mainMember->tags) ? [] : $mainMember->tags; foreach ($otherMembers as $otherMember) { $mainMember->score += $otherMember->score; $mainMember->totalScore += $otherMember->totalScore; $mainMember->totalScoreAfterZeroed += $otherMember->totalScoreAfterZeroed; $newMemberTag = array_merge($newMemberTag, empty($otherMember->tags) ? [] : $otherMember->tags); $location = $otherMember->location; if (empty($otherMemberLocation['country']) && !empty($location['country'])) { $otherMemberLocation = $location; } foreach ($otherMember->properties as $otherProperty) { if (!in_array($otherProperty['id'], $mainPropertyIds) && !empty($otherProperty['value'])) { $mainPropertyIds[] = $otherProperty['id']; $mainProperties[] = $otherProperty; } } } $mainMember->properties = $mainProperties; $mainMember->tags = array_values(array_unique($newMemberTag)); if (empty($mainMemberLocation['country']) && !empty($otherMemberLocation)) { $mainMember->location = $otherMemberLocation; } $updateMember = $mainMember->save(false); $deleteMember = Member::deleteAll(['_id' => ['$in' => $otherMemberIds]]); if ($updateMember && $deleteMember) { $mainMember->upgradeCard(); $jobArgs = ['mainMember' => serialize($mainMember), 'otherMemberIds' => serialize($otherMemberIds)]; Yii::$app->job->create('backend\\modules\\member\\job\\MergeMember', $jobArgs); return ['message' => 'OK', 'data' => '']; } else { throw new ServerErrorHttpException('Merge member fail'); } }