public static function getWechatInfo($memberId) { $where = ['_id' => $memberId, 'origin' => self::WECHAT]; $memberInfo = Member::findOne($where); $channel = $openId = ''; if (!empty($memberInfo) && !empty($memberInfo->socialAccountId)) { $channel = $memberInfo->socialAccountId; $openId = $memberInfo->openId; } return ['channel' => $channel, 'openId' => $openId]; }
public function actionChannels() { $memberId = $this->getQuery("memberId"); if (empty($memberId)) { throw new BadRequestHttpException(Yii::t('common', 'parameters_missing')); } // Get member. $member = Member::findOne(['_id' => new MongoId($memberId)]); $channels = $member->getChannels($memberId); return $channels; }
/** * get member info by condition * @return array * @param $condition, array * @param $one,boolean,if this value is true,it will return a data,otherwise it will return all datas */ public function getByCondition($condition, $one = true) { if ($one) { return ModelMember::findOne($condition); } else { return ModelMember::findAll($condition); } }
/** * exchange the campaign code * 1.check the code whether is vaild * 2.if the code is vaild,get the productId from the promotioncode, * then to find which campaign is fit with this product and add score for member and record log * 3.if code is invalid,throw a exception * @param $params, array, user to commit data to server * @param $accountId, MongoId, account id * @param $type, string, whitch chnanel that user to exchange */ public static function exchangeCampaignCode($params, $accountId, $type = 'mobile') { $member = Member::findOne(['_id' => $params['memberId'], 'accountId' => $accountId]); if (empty($member) || $member->isDisabled) { throw new InvalidParameterException('无效的会员!'); } //get productid $where = ['code' => $params['code'], 'accountId' => $member->accountId, 'isUsed' => false]; $promotionCode = PromotionCode::findOne($where); if (empty($promotionCode)) { if ('mobile' == $type) { throw new InvalidParameterException(['exchange-tip' => '促销码无效!']); } else { return ['result' => 'error', 'message' => '促销码无效!']; } } $params['productId'] = $promotionCode->productId; //add a operation to clear cache when member exchange code in offline if ($type != 'mobile') { self::clearExchangeRecord((string) $member->_id, ''); } //add score for member and log log $isTip = $type == 'mobile' ? true : false; //whether have campaign to redeem this code list($result, $data) = self::getCode2Campaign($params, $member); LogUtil::info(['message' => 'whether have campaign to redeem this code', 'result' => $result, 'number' => count($data), 'params' => $params], 'exchangeCode'); //update promotion code if ($result) { $row = self::updatePromotionCode($params, $member, $promotionCode->_id); LogUtil::info(['message' => 'update promotion code', 'affectRow' => $row, 'promotionCode' => Json::encode($promotionCode->toArray()), 'member' => Json::encode($member->toArray()), 'params' => $params], 'exchangeCode'); } if ($result && $row == 1) { //redeem code in every camoaign, if the result is true and row is 1 return self::redeemCampaign($params, $data, $member, true, $isTip); } else { if ('mobile' == $type) { throw new InvalidParameterException(['exchange-tip' => $data]); } else { LogUtil::error(['message' => 'Faild to exchange code', 'code' => $code, 'result' => $result, 'row' => $row], 'product'); return ['result' => 'error', 'message' => $data]; } } }
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; } }
/** * Check the connect client whether has the VIP helpdesk * @param array $client * @param MongoId $accountId * @return mixed */ public static function hasVIPDesk($client, $accountId) { // Check the client from 'website' or 'WeConnect' if (!empty($client['originId'])) { $member = Member::findOne(['openId' => $client['originId']]); if (!empty($member) && !empty($member->tags)) { $client['tags'] = $member->tags; $helpdesks = self::getByAccountAndTags($member->tags, $accountId); if (!empty($helpdesks)) { $maxClient = HelpDeskSetting::getMaxClientCount($accountId); $availableDeskIds = []; foreach ($helpdesks as $helpdesk) { $helpdesk = $helpdesk->toArray(); if ($helpdesk['isOnline'] && $helpdesk['clientCount'] < $maxClient) { $availableDeskIds[] = new \MongoId($helpdesk['id']); } } if (!empty($availableDeskIds)) { $lastChatConversation = ChatConversation::find()->where(['desk.id' => ['$in' => $availableDeskIds]])->orderBy(['lastChatTime' => SORT_DESC])->one(); $targetDeskId = null; if (!empty($lastChatConversation)) { $targetDeskId = (string) $lastChatConversation->desk['id']; } else { $targetDeskId = (string) $availableDeskIds[0]; } return $targetDeskId; } } } } return false; }
/** * @param begin=2015-10-01 * @param end=2015-10-02 */ public function actionExportKlpMember() { $params = $this->getQuery(); $accountId = $this->getAccountId(); unset($params['accesstoken']); $condition = ['accountId' => $accountId]; $key = ''; if (!empty($params['begin'])) { $begin = TimeUtil::ms2sTime($params['begin']); $key = date('Y-m-d', $begin); $condition['updatedAt']['$gte'] = new \MongoDate($begin); } if (!empty($params['end'])) { $end = TimeUtil::ms2sTime($params['end']) + 24 * 3600; $key = !empty($key) ? $key . '_' . date('Y-m-d', $end) : date('Y-m-d', $end); $condition['updatedAt']['$lt'] = new \MongoDate($end); } if (empty($condition['updatedAt'])) { $key = date('Y-m-d'); $condition['updatedAt']['$lt'] = new \MongoDate(); } //only export enable member $condition['isDisabled'] = ['$ne' => true]; $result = Member::findOne($condition); if (!empty($result)) { $condition = serialize($condition); $header = ['gender' => 'Title/ Salutation', 'firstName' => 'First name', 'lastName' => 'Surname', '工作職稱' => 'Job title', 'email' => 'Email address', 'tel' => 'Username (prefilled)', '密碼' => 'Password', 'tel_1' => 'Mobile phone number', '餐廳電話' => 'phone number', '經營型態' => 'Type of business', '每日供餐量' => 'Number of covers per day', '訂閱' => 'Opt-in / emailable (y/n)', '餐廳名稱' => 'Business name', '餐廳地址' => 'Business address', 'letter_unknow' => 'Street number / Letter', '地址' => 'Street name', '縣市' => 'City', 'county' => 'County', '郵遞區號' => 'Postcode', 'country' => 'Country', 'locations_unkonw' => 'Number of locations', '首選經銷商' => 'Wholesaler/ Supplier', '廚房廚師人數' => 'Number of kitchen staff', 'cuisine_unknow' => 'Type of cuisine', '感興趣產品' => 'UFS brands']; $fields = 'cardId,location,tags,properties,cardNumber,score,remarks,birth,socials'; $exportArgs = ['collection' => 'member', 'fields' => $fields, 'key' => $key, 'header' => $header, 'accountId' => (string) $accountId, 'condition' => $condition, 'description' => 'Direct: export KLP member']; $jobId = Yii::$app->job->create('backend\\modules\\member\\job\\ExportKlpMember', $exportArgs); return ['result' => 'success', 'message' => 'exporting file', 'data' => ['jobId' => $jobId, 'key' => $key]]; } else { return ['result' => 'error', 'message' => 'no datas', 'data' => []]; } }
public function perform() { # Run task $args = $this->args; if (empty($args['locationPath']) || empty($args['accountId']) || empty($args['filePath']) || empty($args['qiniuBucket']) || empty($args['fileName'])) { ResqueUtil::log(['error' => 'missing params in MemberCheckCode', 'args' => $args]); } //get qiniu file $qiniuFile = Yii::$app->curl->get($args['filePath'], [], false); if (false === file_put_contents($args['locationPath'], $qiniuFile)) { ResqueUtil::log(['message' => 'Fail to get file from qiniu in MemberCheckCode class', 'args' => $args]); return false; } $phpreader = new \PHPExcel_Reader_Excel2007(); $filePath = $args['locationPath']; if (!$phpreader->canRead($filePath)) { $phpreader = new \PHPExcel_Reader_Excel5(); if (!$phpreader->canRead($filePath)) { $phpreader = \PHPExcel_IOFactory::createReader('CSV')->setDelimiter(',')->setInputEncoding('GBK')->setEnclosure('"')->setLineEnding("\r\n")->setSheetIndex(0); if (!$phpreader->canRead($filePath)) { ResqueUtil::log(['error' => 'file can not read in MemberCheckCode', 'args' => $args]); return false; } } } $phpexcel = $phpreader->load($filePath); //read the first of table with excel. $currentSheet = $phpexcel->getSheet(0); //get the largest column $allColumn = $currentSheet->getHighestColumn(); //get total row num $allRow = $currentSheet->getHighestRow(); //make a key to be called a name for redis set $cacheSetInsert = self::SET_HEAD_INSERT . md5($args['accountId'] . "_" . $args['fileName']); $cacheSetUpdate = self::SET_HEAD_UPDATE . md5($args['accountId'] . "_" . $args['fileName']); //make a key to be called a name for redis hash $cacheHash = md5($args['accountId'] . "_" . $args['fileName']); $ignorePropertyKey = self::MEMBER_FILE_IGNORE . $cacheHash; $missPropertyKey = self::MEMBER_FILE_MISS . $cacheHash; $repeatTitleKey = self::MEMBER_REPEAT_TITLE . $cacheHash; //the key for wrong number for store in redis $wrongKey = self::MEMBER_WRONG; //the key for right number for store in redis $rightKey = self::MEMBER_RIGHT; $total = $wrong = $right = $rightInsert = $rightUpdate = 0; $redis = Yii::$app->cache->redis; $redis->expire($cacheHash, self::EXPIRE); $title = []; // verify the member of phone. $re = "/^0?1[0-9]{10}\$/"; $reTel = "/^09[0-9]{8}\$/"; // verify the member of birthday (YYYY-MM-DD) $reDateOne = "/^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)\$/"; // define the default of property. $defaultProperties = ['name' => 'name', 'mobile' => 'tel', 'gender' => 'gender', 'birthday' => 'birthday', 'email' => 'email']; $dbExistProperties = ['name' => 'Name', 'tel' => 'Mobile', 'gender' => 'Gender', 'birthday' => 'Birthday', 'email' => 'Email']; $ignoreProperties = []; $columnElements = []; // all elements of a column $property = []; $titles = []; $realTitles = []; $rowIndex = 1; $defaulyProperties = []; $properties = []; $requiredProperty = []; $existsTitle = []; if ($allRow <= 1) { ResqueUtil::log(['error' => 'the file of excel hasn`t data']); $array = ['value' => null, 'rows' => null, 'cols' => null, 'property' => null, 'wrongNum' => self::MEMBER_NO_DATA]; $this->storeError($redis, $array, $args); return false; } // get title of excel. $mapTitlePropertyId = []; $titleIndexClo = -1; for ($columnIndex = 'A'; $columnIndex <= $allColumn; $columnIndex++) { $titleIndexClo++; $titleName = strtolower(trim((string) $currentSheet->getCell($columnIndex . $rowIndex)->getValue())); if (empty($titleName)) { continue; } if (array_key_exists((string) $titleName, $defaultProperties)) { $findValues = $defaultProperties[(string) $titleName]; $propertyValueOne = MemberProperty::findOne(['name' => new \MongoRegex("/^{$findValues}\$/i"), 'accountId' => new MongoId($args['accountId']), 'isDeleted' => false]); $property[] = empty($propertyValueOne) ? [] : $propertyValueOne; $titles[] = empty($defaultProperties[(string) $titleName]) ? "" : $defaultProperties[(string) $titleName]; $realTitles[] = empty($propertyValueOne) ? '' : (string) $propertyValueOne['name']; $mapTitlePropertyId[$titleName] = $propertyValueOne['_id']; } else { $findValue = (string) $titleName; $propertyValueOne = MemberProperty::findOne(['propertyId' => new \MongoRegex("/^{$findValue}\$/i"), 'accountId' => new MongoId($args['accountId']), 'isDeleted' => false]); $property[] = empty($propertyValueOne) ? [] : $propertyValueOne; $titles[] = empty((string) $titleName) ? '' : (string) $titleName; $realTitles[] = empty($propertyValueOne) ? '' : (string) $propertyValueOne['propertyId']; $mapTitlePropertyId[$titleName] = $propertyValueOne['_id']; } if (!in_array((string) $titleName, $title)) { $title[] = (string) $titleName; } else { $existsTitle[] = (string) $titleName; } } // Get index of mobile in titles. $mobileIndex = array_search(Member::DEFAULT_PROPERTIES_MOBILE, $titles); $mobileColumn = \PHPExcel_Cell::stringFromColumnIndex($mobileIndex); $mobiles = []; // Verify if the title is repeated. if (count($existsTitle) > 0) { ResqueUtil::log(['error' => 'the title is repeated', 'args' => ['existsTitle' => $existsTitle]]); $array = ['value' => null, 'rows' => null, 'cols' => null, 'property' => null, 'wrongNum' => self::MEMBER_TITLE_REPEATED]; $this->storeError($redis, $array, $args); $redis->sadd($repeatTitleKey, serialize($existsTitle)); unset($existsTitle); return false; } // get all property. $defaulyProperty = MemberProperty::find()->select(['_id', 'name'])->from('memberProperty')->where(['isRequired' => true, 'isVisible' => true, 'isDefault' => true, 'isDeleted' => false, 'accountId' => new MongoId($args['accountId'])])->all(); foreach ($defaulyProperty as $data) { $defaulyProperties[] = $data['name']; } $propertyValue = MemberProperty::find()->select(['_id', 'propertyId'])->from('memberProperty')->where(['isRequired' => true, 'isVisible' => true, 'isDefault' => false, 'isDeleted' => false, 'accountId' => new MongoId($args['accountId'])])->all(); foreach ($propertyValue as $data) { $properties[] = $data['propertyId']; } $propertyItems = array_merge($defaulyProperties, $properties); // get required properties in db. foreach ($propertyItems as $item) { if (array_key_exists($item, $dbExistProperties)) { $itemValue = $dbExistProperties[$item]; } else { $itemValue = $item; } if (!in_array(strtolower($itemValue), $title) && !in_array($itemValue, $requiredProperty)) { $requiredProperty[] = $itemValue; } } if (count($requiredProperty) > 0) { ResqueUtil::log(['error' => 'Resqued property is miss', 'args' => ['title' => $requiredProperty]]); $array = ['value' => null, 'rows' => null, 'cols' => null, 'property' => null, 'wrongNum' => self::MEMBER_NO_EXIST]; $this->storeError($redis, $array, $args); $redis->sadd($missPropertyKey, serialize($requiredProperty)); unset($requiredProperty); return false; } // check content for excel. $batchIndex = 1; $batchIndexColumn = []; $batch = 1; $batchs = ceil(($allRow - 1) / self::BATCH_COUNT); $decimal = $allRow % self::BATCH_COUNT; $result = []; $resultTitle = []; // Mapping between name and properties. $nameAndProperties = $defaulyProperty = MemberProperty::find()->select(['_id', 'name', 'propertyId'])->from('memberProperty')->where(['isDeleted' => false, 'accountId' => new MongoId($args['accountId'])])->all(); $mapProperties = []; if (!empty($nameAndProperties)) { foreach ($nameAndProperties as $item) { $mapProperties[] = $item->toArray(); } $result = ArrayHelper::map($mapProperties, 'propertyId', 'name'); $resultTitle = ArrayHelper::map($mapProperties, 'name', 'propertyId'); } // Get all tag $tags = Account::getAllTags(new MongoId($args['accountId'])); for ($rowIndex = 2; $rowIndex <= $allRow; $rowIndex++) { if ($batchIndex == self::BATCH_COUNT) { $batchIndex = 1; $batchIndexColumn = []; ++$batch; } $columnValue = []; $telName = ''; $telId = ''; $telValue = ''; $updateMember = []; $insertMember = []; $titleIndex = -1; for ($columnIndex = 'A'; $columnIndex <= $allColumn; $columnIndex++) { $titleIndex++; // get member propery by propertyId(the title of excel). //++$titleIndex; $columnNum = \PHPExcel_Cell::columnIndexFromString($columnIndex) - 1; $value = trim((string) $currentSheet->getCell($columnIndex . $rowIndex)->getValue()); $mobile = trim((string) $currentSheet->getCell($mobileColumn . $rowIndex)->getValue()); if ($titleIndex < count($title) && $columnNum == $titleIndex) { if (empty((string) $title[$titleIndex])) { continue; } // Verify if the mobile is right. if (empty($mobile)) { $array = ['value' => $mobile, 'rows' => $rowIndex, 'cols' => $mobileIndex + 1, 'property' => $title[$mobileIndex], 'wrongNum' => self::MEMBER_PROPERTY_REQUIRED]; $this->storeError($redis, $array, $args); return false; } else { if (strstr($mobile, self::MEMBER_ENGLISH_COLON)) { // check mobile.(T:1312797391); $str = explode(self::MEMBER_ENGLISH_COLON, $mobile); // T:1312797391 if ($str[0] != 'T') { $array = ['value' => $mobile, 'rows' => $rowIndex, 'cols' => $mobileIndex + 1, 'property' => $title[$mobileIndex], 'wrongNum' => self::MEMBER_PROPERTY_ERROR]; $this->storeError($redis, $array, $args); return false; } $telValue = $str[1]; } else { if (strstr($mobile, self::MEMBER_CHINESE_COLON)) { $str = explode(self::MEMBER_CHINESE_COLON, $mobile); if ($str[0] != 'T') { $array = ['value' => $mobile, 'rows' => $rowIndex, 'cols' => $mobileIndex + 1, 'property' => $title[$mobileIndex], 'wrongNum' => self::MEMBER_PROPERTY_ERROR]; $this->storeError($redis, $array, $args); return false; } $telValue = $str[1]; } else { $array = ['value' => $mobile, 'rows' => $rowIndex, 'cols' => $mobileIndex + 1, 'property' => $title[$mobileIndex], 'wrongNum' => self::MEMBER_PROPERTY_ERROR]; $this->storeError($redis, $array, $args); return false; } } if ((string) $title[$titleIndex] == self::MEMBER_MOBILE) { $value = (string) $telValue; } // get mobile in excel. if (!in_array((string) $telValue, $mobiles)) { $mobiles[] = (string) $telValue; } } // validate tag. if ($titles[$titleIndex] == self::MEMBER_TAGS) { if (!empty($value) && !in_array($value, $tags)) { $array = ['value' => $value, 'rows' => $rowIndex, 'cols' => $titleIndex + 1, 'property' => $title[$titleIndex], 'wrongNum' => self::MEMBER_PROPERTY_ILLEGAL]; $this->storeError($redis, $array, $args); return false; } $columnValue[$rowIndex][] = ['tags' => $value]; } else { // get all the ignore properties. if (empty($property[$titleIndex])) { if (!in_array((string) $title[$titleIndex], $ignoreProperties)) { $ignoreProperties[] = (string) $title[$titleIndex]; } } else { if ($property[$titleIndex]['isVisible'] == false) { if (!in_array((string) $title[$titleIndex], $ignoreProperties)) { $ignoreProperties[] = (string) $title[$titleIndex]; } continue; } // Varitify if it`s required. if (strcasecmp($property[$titleIndex]['type'], self::MEMBER_RADIO) != 0 && (string) $title[$titleIndex] != self::MEMBER_MOBILE) { if ($property[$titleIndex]['isRequired'] == true && empty($value)) { $array = ['value' => $value, 'rows' => $rowIndex, 'cols' => $titleIndex + 1, 'property' => $title[$titleIndex], 'wrongNum' => self::MEMBER_PROPERTY_REQUIRED]; $this->storeError($redis, $array, $args); return false; } } // Varitify if it`s isUnique in excel. if (!empty($value) && $property[$titleIndex]['isUnique'] == true) { if ((string) $title[$titleIndex] != self::MEMBER_MOBILE) { // check db. $batchIndexColumn[$columnNum][] = $value; if ($decimal == 0 || $rowIndex == $allRow || $rowIndex == self::BATCH_COUNT) { // check update or insert, if the data will be insert, I will validate unique, not do it. $members = Member::findMobiles($mobiles, new MongoId($args['accountId'])); if (!empty($members)) { $memberId = []; foreach ($members as $member) { $mobile = ''; $uniqueFileValue = ''; $uniqueFileId = ''; foreach ($member['properties'] as $propertyItem) { if ($propertyItem['name'] == Member::DEFAULT_PROPERTIES_MOBILE) { $mobile = $propertyItem['value']; $index = array_search($mobile, $mobiles); unset($batchIndexColumn[$columnNum][$index]); } if ($propertyItem['id'] == $mapTitlePropertyId[$title[$titleIndex]]) { $uniqueFileValue = $propertyItem['value']; $uniqueFileId = $propertyItem['id']; } } $rindex = ($batch - 1) * self::BATCH_COUNT + $index + 2; $uniqueFileValue = trim((string) $currentSheet->getCell(\PHPExcel_Cell::stringFromColumnIndex($titleIndex) . $rindex)->getValue()); $isExisUpdatetProperties = Member::findOne(['properties' => ['$elemMatch' => ['id' => $uniqueFileId, 'value' => $uniqueFileValue]], '_id' => ['$ne' => $member['_id']], 'isDeleted' => false, 'accountId' => new MongoId($args['accountId'])]); if (!empty($isExisUpdatetProperties)) { if ($property[$titleIndex]['isDefault'] == true) { $array = ['value' => $value, 'rows' => $rindex, 'cols' => $titleIndex + 1, 'property' => $property[$titleIndex]['name'], 'wrongNum' => self::MEMBER_PROPERTY_UNIQUE]; $this->storeError($redis, $array, $args); return false; } else { $array = ['value' => $value, 'rows' => $rindex, 'cols' => $titleIndex + 1, 'property' => $property[$titleIndex]['propertyId'], 'wrongNum' => self::MEMBER_PROPERTY_UNIQUE]; $this->storeError($redis, $array, $args); return false; } } } } $isExistProperties = Member::findOne(['properties' => ['$elemMatch' => ['value' => ['$in' => $batchIndexColumn[$columnNum]], 'id' => $property[$titleIndex]['_id']]], 'isDeleted' => false, 'accountId' => new MongoId($args['accountId'])]); if (!empty($isExistProperties)) { foreach ($isExistProperties['properties'] as $propertyItem) { if ($property[$titleIndex]['_id'] == $propertyItem['id']) { if ($property[$titleIndex]['isDefault'] == true) { $colIndexNum = array_search(strtolower((string) $propertyItem['name']), $title) + 1; } else { $colIndexNum = array_search(strtolower($resultTitle[(string) $propertyItem['name']]), $title) + 1; } if ($rowIndex == 2) { $columnElements[$columnNum][] = ''; } if (in_array((string) $propertyItem['value'], $columnElements[$columnNum])) { $rowIndexNum = array_search((string) $propertyItem['value'], $columnElements[$columnNum]) + 2; } else { if ($rowIndex <= self::BATCH_COUNT) { $rowIndexNum = ($batch - 1) * self::BATCH_COUNT + count($batchIndexColumn[$columnNum]) + 1; } else { $rowIndexNum = ($batch - 1) * self::BATCH_COUNT + count($batchIndexColumn[$columnNum]); } } if ($rowIndex == 2) { unset($columnElements[$columnNum][0]); } } continue; } if ($property[$titleIndex]['isDefault'] == true) { $array = ['value' => $value, 'rows' => $rowIndexNum, 'cols' => $colIndexNum, 'property' => $property[$titleIndex]['name'], 'wrongNum' => self::MEMBER_PROPERTY_UNIQUE]; $this->storeError($redis, $array, $args); return false; } else { $array = ['value' => $value, 'rows' => $rowIndexNum, 'cols' => $colIndexNum, 'property' => $property[$titleIndex]['propertyId'], 'wrongNum' => self::MEMBER_PROPERTY_UNIQUE]; $this->storeError($redis, $array, $args); return false; } } } // check excel. $isInsert = $redis->Hset($cacheHash . $columnNum, $value, $columnNum); if ($isInsert == 0) { $array = ['value' => $value, 'rows' => $rowIndex, 'cols' => $titleIndex + 1, 'property' => $title[$titleIndex], 'wrongNum' => self::MEMBER_PROPERTY_UNIQUE]; $this->storeError($redis, $array, $args); return false; } } $columnElements[$columnNum][] = $value; } switch ($property[$titleIndex]['type']) { case Member::DEFAULT_PROPERTIES_EMAIL: $EmailTitle = $title[$titleIndex]; $EmailTitles = $titles[$titleIndex]; $EmailParams = ['redis' => $redis, 'args' => $args, 'titleIndex' => $titleIndex, 'EmailTitles' => $EmailTitles]; $EmailTitleIndex = $titleIndex + 1; $propertyError = self::MEMBER_PROPERTY_ERROR; $Emailarray = ['value' => $value, 'rows' => $rowIndex, 'cols' => $EmailTitleIndex, 'property' => $EmailTitle, 'wrongNum' => $propertyError]; $columnValue[$rowIndex][] = $this->validateEmail($Emailarray, $EmailParams, $property, $result, $realTitles, $titles); break; case self::MEMBER_DATE: if (!empty($value)) { if (strstr($value, '-')) { $str = explode('-', $value); if (count($str) != 3) { $array = ['value' => $value, 'rows' => $rowIndex, 'cols' => $titleIndex + 1, 'property' => $title[$titleIndex], 'wrongNum' => self::MEMBER_PROPERTY_ERROR]; $this->storeError($redis, $array, $args); return false; } } if (strstr($value, self::MEMBER_LINE)) { $str = explode(self::MEMBER_LINE, $value); if (count($str) == 3) { $time = $str[2] . '-' . $str[0] . '-' . $str[1]; } else { $array = ['value' => $value, 'rows' => $rowIndex, 'cols' => $titleIndex + 1, 'property' => $title[$titleIndex], 'wrongNum' => self::MEMBER_PROPERTY_ERROR]; $this->storeError($redis, $array, $args); return false; } } else { $time = $value; } } if (!empty($value) && !preg_match($reDateOne, $time)) { $array = ['value' => $value, 'rows' => $rowIndex, 'cols' => $titleIndex + 1, 'property' => $title[$titleIndex], 'wrongNum' => self::MEMBER_PROPERTY_ILLEGAL]; $this->storeError($redis, $array, $args); return false; } if (!empty($value)) { if (strstr($value, self::MEMBER_LINE)) { $str = explode(self::MEMBER_LINE, $value); $dayStr = $str[2] . '-' . $str[0] . '-' . $str[1]; $date = (int) strtotime($dayStr) * 1000; } else { $date = (int) strtotime($value) * 1000; } } else { $date = null; } if (!empty($value) && $date >= time() * 1000) { $array = ['value' => $value, 'rows' => $rowIndex, 'cols' => $titleIndex + 1, 'property' => $title[$titleIndex], 'wrongNum' => self::MEMBER_PROPERTY_ILLEGAL]; $this->storeError($redis, $array, $args); return false; } $columnValue[$rowIndex][] = ['id' => $property[$titleIndex]['_id'], 'name' => $property[$titleIndex]['isDefault'] == true ? $titles[$titleIndex] : $result[$realTitles[$titleIndex]], 'value' => $date]; break; case self::MEMBER_CHECKBOX: $checkboxValue = []; if (!empty($value) && strstr($value, self::MEMBER_ENGLISH_COMMA) && !strstr($value, self::MEMBER_CHINESE_COMMA)) { //english $checkboxValue = explode(self::MEMBER_ENGLISH_COMMA, $value); $checkboxValue = array_values(array_unique($checkboxValue)); } if (!empty($value) && strstr($value, self::MEMBER_CHINESE_COMMA) && !strstr($value, self::MEMBER_ENGLISH_COMMA)) { // chinese $checkboxValue = explode(self::MEMBER_CHINESE_COMMA, $value); $checkboxValue = array_values(array_unique($checkboxValue)); } if (!empty($value) && strstr($value, self::MEMBER_ENGLISH_COMMA) && strstr($value, self::MEMBER_CHINESE_COMMA)) { $array = ['value' => $value, 'rows' => $rowIndex, 'cols' => $titleIndex + 1, 'property' => $title[$titleIndex], 'wrongNum' => self::MEMBER_PROPERTY_ERROR]; $this->storeError($redis, $array, $args); return false; } // one value if (!empty($value) && !strstr($value, self::MEMBER_ENGLISH_COMMA) && !strstr($value, self::MEMBER_CHINESE_COMMA)) { $checkboxValue[] = $value; } if (count($checkboxValue) > 0) { foreach ($checkboxValue as $item) { if (!in_array($item, $property[$titleIndex]['options'])) { $array = ['value' => $value, 'rows' => $rowIndex, 'cols' => $titleIndex + 1, 'property' => $title[$titleIndex], 'wrongNum' => self::MEMBER_PROPERTY_ILLEGAL]; $this->storeError($redis, $array, $args); return false; } continue; } } $columnValue[$rowIndex][] = ['id' => $property[$titleIndex]['_id'], 'name' => $property[$titleIndex]['isDefault'] == true ? $titles[$titleIndex] : $result[$realTitles[$titleIndex]], 'value' => $checkboxValue]; break; case self::MEMBER_RADIO: if ((string) $title[$titleIndex] != Member::DEFAULT_PROPERTIES_GENDER) { if (!empty($value) && !in_array($value, $property[$titleIndex]['options'])) { $array = ['value' => $value, 'rows' => $rowIndex, 'cols' => $titleIndex + 1, 'property' => $title[$titleIndex], 'wrongNum' => self::MEMBER_PROPERTY_ILLEGAL]; $this->storeError($redis, $array, $args); return false; } $columnValue[$rowIndex][] = ['id' => $property[$titleIndex]['_id'], 'name' => $property[$titleIndex]['isDefault'] == true ? $titles[$titleIndex] : $result[$realTitles[$titleIndex]], 'value' => empty($value) ? $property[$titleIndex]['options'][0] : $value]; } else { if (!empty($value) && $value == self::MEMBER_SC_GENDER || $value == self::MEMBER_TC_GENDER || strcasecmp($value, self::MEMBER_MALE) == 0) { $genderValue = self::MEMBER_MALE; } else { if (!empty($value) && $value == self::MEMBER_C_GENDER || strcasecmp($value, self::MEMBER_FEMALE) == 0) { $genderValue = self::MEMBER_FEMALE; } else { if (!empty($value) && $value == self::MEMBER_WEIZHI || $value == self::MEMBER_UNKNOW || strcasecmp($value, self::MEMBER_UNKNOWN) == 0) { $genderValue = self::MEMBER_UNKNOWN; } else { if (empty($value)) { $genderValue = self::MEMBER_MALE; } else { $array = ['value' => $value, 'rows' => $rowIndex, 'cols' => $titleIndex + 1, 'property' => $title[$titleIndex], 'wrongNum' => self::MEMBER_PROPERTY_ILLEGAL]; $this->storeError($redis, $array, $args); return false; } } } } $columnValue[$rowIndex][] = ['id' => $property[$titleIndex]['_id'], 'name' => $property[$titleIndex]['isDefault'] == true ? $titles[$titleIndex] : $result[$realTitles[$titleIndex]], 'value' => $genderValue]; } break; case MemberProperty::TYPE_TEXTAREA: $columnValue[$rowIndex][] = ['id' => $property[$titleIndex]['_id'], 'name' => $property[$titleIndex]['isDefault'] == true ? $titles[$titleIndex] : $result[$realTitles[$titleIndex]], 'value' => str_replace("\\n", PHP_EOL, $value)]; break; default: if ((string) $title[$titleIndex] == Member::DEFAULT_PROPERTIES_NAME) { $valueLen = mb_strlen($value, 'utf-8'); if (!empty($value)) { if ($valueLen < 2 || $valueLen > 30) { $array = ['value' => $value, 'rows' => $rowIndex, 'cols' => $titleIndex + 1, 'property' => $title[$titleIndex], 'wrongNum' => self::MEMBER_PROPERTY_ILLEGAL]; $this->storeError($redis, $array, $args); return false; } } $columnValue[$rowIndex][] = ['id' => $property[$titleIndex]['_id'], 'name' => $property[$titleIndex]['isDefault'] == true ? $titles[$titleIndex] : $result[$realTitles[$titleIndex]], 'value' => $value]; } else { switch ((string) $title[$titleIndex]) { case self::MEMBER_MOBILE: if (!empty($value) && !preg_match($re, $value) && !preg_match($reTel, $value)) { $array = ['value' => $value, 'rows' => $rowIndex, 'cols' => $titleIndex + 1, 'property' => $title[$titleIndex], 'wrongNum' => self::MEMBER_PROPERTY_ILLEGAL]; $this->storeError($redis, $array, $args); return false; } $columnValue[$rowIndex][] = ['id' => $property[$titleIndex]['_id'], 'name' => $property[$titleIndex]['isDefault'] == true ? $titles[$titleIndex] : $result[$realTitles[$titleIndex]], 'value' => $value]; $telName = $defaultProperties[(string) $title[$titleIndex]]; $telId = (string) $property[$titleIndex]['_id']; $telValue = $value; break; default: $columnValue[$rowIndex][] = ['id' => $property[$titleIndex]['_id'], 'name' => $property[$titleIndex]['isDefault'] == true ? $titles[$titleIndex] : $result[$realTitles[$titleIndex]], 'value' => $value]; break; } } break; } } } } } // verify if the column value has already exist. if it is exist, will store $updateMember, // if it isn`s exist, will store $insertMember. $total++; $memberOne = null; if (!empty($telId)) { $memberOne = Member::findOne(['properties' => ['$elemMatch' => ['id' => new MongoId($telId), 'name' => $telName, 'value' => $telValue]], 'accountId' => new MongoId($args['accountId'])]); } if (!empty($memberOne)) { $updateMember[] = $columnValue[$rowIndex]; $redis->sadd($cacheSetUpdate, serialize($updateMember)); } else { if (!empty($columnValue[$rowIndex])) { $insertMember[] = $columnValue[$rowIndex]; $redis->sadd($cacheSetInsert, serialize($insertMember)); } } ++$batchIndex; } unset($columnElements); $redis->del($cacheHash . $columnNum); //store the number of the wrong code and the number of the right code $rightInsert = $redis->scard($cacheSetInsert); $rightUpdate = $redis->scard($cacheSetUpdate); $right = $rightInsert + $rightUpdate; $wrong = $total - $right; $redis->Hset($cacheHash, $wrongKey, $wrong); $redis->Hset($cacheHash, $rightKey, $right); // store the ignore of properties. if ($wrong == 0 && count($ignoreProperties) > 0) { $redis->sadd($ignorePropertyKey, serialize($ignoreProperties)); } else { $redis->sadd($ignorePropertyKey, null); } return true; }