Import.class.php 12.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
<?php
/**
 * 数据导入
 *
 * @name Facade_Import
 * @package facade
 * @copyright yoho.inc
 * @version 5.0 (2014-3-14 13:47:57)
 * @author whb <huanbao.wang@yoho.cn>
 */
class Facade_Import
{
	private static $_service = null;
	
	public static function service()
	{
		if(self::$_service == null)
		{
			self::$_service = new Service_Import();
		}
		return self::$_service;
	}
	
	/**
	 * yohobuy全部导入
	 * 
	 * @param int $offset
	 * @param int $limit
	 * $total
	 */
	public static function importYohoBuyData($offset = 0 , $limit = 1000, $total = 10000)
	{
		$dataInc = $offset;
		while($offset<=$total)
		{
			$authService = new Service_Auth();
			$userProfiles = self::service()->getUserProfile(intval($offset), intval($limit));
			$offset += $limit;
			echo $offset."\n";
			foreach($userProfiles as $userProfile)
			{
				$dataInc++;
				$uid = 0;
				$ssoUid = 0;
				$importStatus = 0;
				$yohobuyUid = $userProfile['uid'];
				$yohobuyEmail = $userProfile['email'];
				$yohobuyMobile = $userProfile['mobile'];
				//获取第三方信息
				$partnerDatas = self::getYohobuyPartner($yohobuyUid);
				echo 'data: '.$dataInc;
				//第三方数据,已经存在了,只有邮箱
				if(is_array($partnerDatas) && empty($partnerDatas) && !empty($yohobuyEmail) && empty($yohobuyMobile))
				{
					echo sprintf("%s time :yohobuy partner data: %s --- %s ---%s over finish\n", time(), $yohobuyUid, $yohobuyEmail, $yohobuyMobile);
					continue;
				}
				$userBase = self::service()->getUserBase($yohobuyUid);
				$yohobuyEmailAuth = $yohobuyMobileAuth = array();
				if(!empty($yohobuyEmail))
				{
					$yohobuyEmailAuth = $authService->getAuthByTypeAndAid(Facade_Auth::AUTH_TYPE_EMAIL, $yohobuyEmail);
				}
				if(!empty($yohobuyMobile))
				{
					$yohobuyMobileAuth = $authService->getAuthByTypeAndAid(Facade_Auth::AUTH_TYPE_MOBILE, $yohobuyMobile);
				}
				//都不存在,直接注册,万里长征,第一步
				if(empty($yohobuyEmailAuth) && empty($yohobuyMobileAuth))
				{
					$params = array('nick'=> $userBase['nickname'], 'gender'=> max(array(1, intval($userBase['gender']))),
							'birthday'=>strtotime($userBase['birthday']), 'real_name'=> $userBase['username'],
							'head_pic'=> $userBase['head_ico'],'income'=> $userBase['income'], 'create_time'=> $userProfile['create_time'],
							'status'=> $userProfile['status']);
					$uid = $authService->setUserinfo($params);
					if(!empty($yohobuyEmail))//导入帐号了
					{
						$authService->setAuth($uid, Facade_Auth::AUTH_TYPE_EMAIL, $yohobuyEmail);
					}
					if(!empty($yohobuyMobile))
					{
						$authService->setAuth($uid, Facade_Auth::AUTH_TYPE_MOBILE, $yohobuyMobile);
					}
					//设置密码
					$authService->setPassword($uid, $userProfile['password'], '');
					$ssoUid = $uid;
					$importStatus = 1;
				}
				else if(!empty($yohobuyEmailAuth) && !empty($yohobuyMobile) && empty($yohobuyMobileAuth)) //邮箱存在
				{
					$ssoUid = $yohobuyEmailAuth['uid'];
					$authService->setAuth($uid, Facade_Auth::AUTH_TYPE_MOBILE, $yohobuyMobile);
					$importStatus = 1;
				}
				else if(!empty($yohobuyMobileAuth) && !empty($yohobuyEmail) && empty($yohobuyEmailAuth))
				{
					$ssoUid = $yohobuyMobileAuth['uid'];
					$authService->setAuth($uid, Facade_Auth::AUTH_TYPE_EMAIL, $yohobuyEmail);
					$importStatus = 1;
				}
				else if(!empty($yohobuyEmailAuth) || !empty($yohobuyMobileAuth))
				{
					if(!empty($yohobuyEmailAuth))
					{
						$ssoUid = $yohobuyEmailAuth['uid'];
					}
					else
					{
						$ssoUid = $yohobuyMobileAuth['uid'];
					}
					
					$importStatus = 2;
				}
				if(!empty($ssoUid) && !empty($partnerDatas) && is_array($partnerDatas))
				{
					//导入第三方
					self::setYohobuyPartnerData($partnerDatas, $yohobuyUid, $ssoUid);
				}
				self::service()->setYohobuyLog($yohobuyUid, $ssoUid, $importStatus);
				echo sprintf("%s time : %s yohobuy data: %s --- %s ---%s finish\n", time(), $dataInc, $yohobuyUid, $yohobuyEmail, $yohobuyMobile);
			}
			unset($userProfiles);
			unset($authService);
			usleep(10);
		}
	}
	
	/**
	 * 获取第三方用户
	 * 
	 * @param  array $yohobuyUid
	 * @return array|1
	 */
	public static function getYohobuyPartner($yohobuyUid)
	{
		$authService = new Service_Auth();
		$yohobuyPartnerDatas = self::service()->getYohobuyOpenIdByUid($yohobuyUid);
		if(!empty($yohobuyPartnerDatas))
		{
			$partnerDatas = array();
			foreach($yohobuyPartnerDatas as $yohobuyPartnerData)
			{
				$yohobuyAuth = $yohobuyPartnerData['open_id'];
				//yohobuy: 0,yoho_cn:1,iphone:3,andriod: 4, alipay : 10, douban : 11, sina : 12, qq : 13, cbqq : 14, linktech:15, renren : 16
				switch($yohobuyPartnerData['refer_id'])
				{
					case 10://alipay
						$authType = Facade_Auth::AUTH_TYPE_ALIPAY;
						break;
					case 11://douban
						$authType = Facade_Auth::AUTH_TYPE_DOUBAN;
						break;
					case 12: //sina
						$authType = Facade_Auth::AUTH_TYPE_WEIBO;
						break;
					case 13: //qq
						$authType = Facade_Auth::AUTH_TYPE_QQ;
						break;
					case 14: //cbqq
						break;
					case 15: //linktech
						break;
					case 16: //renren
						$authType = Facade_Auth::AUTH_TYPE_RENREN;
						break;
					case 18: //qq微博
						$authType = Facade_Auth::AUTH_TYPE_QQ_WB;
				}
				$authInfo = $authService->getAuthByTypeAndAid($authType, $yohobuyAuth);
				if(empty($authInfo))
				{
					$partnerDatas[] = array('authType'=> $authType, 'yohobuyAuth'=> $yohobuyAuth);
				}
			}
			return $partnerDatas;
		}
		else
		{
			return true;
		}
	}
	
	/**
	 * 导入yohobuy第三方数据
	 * 
	 * @param array $partnerDatas
	 * @param string $yohobuyUid
	 * @param string $ssoUid
	 */
	public static function setYohobuyPartnerData($partnerDatas, $yohobuyUid, $ssoUid)
	{
		$authService = new Service_Auth();
		$importStatus = 0;
		foreach($partnerDatas as $partnerData)
		{
			$authType = $partnerData['authType'];
			$yohobuyAuth = $partnerData['yohobuyAuth'];
			$authInfo = $authService->getAuthByTypeAndAid($authType, $yohobuyAuth);
			if(empty($authInfo))
			{
				$authService->setAuth($ssoUid, $authType, $yohobuyAuth);
				$importStatus = 1;
			}
			else if($authInfo['uid'] == $ssoUid)
			{
				$importStatus = 2;
			}
			echo sprintf("time : %s yohobuy partner data: %s --- %s---finish\n", time(), $yohobuyUid, $yohobuyAuth);
			self::service()->setYohobuyPartnerLog($yohobuyUid, $ssoUid, $yohobuyAuth, $importStatus);
		}
	}
	
	/**
	 * 导入show第三方数据
	 * 
	 * @param unknown $offset
	 * @param unknown $limit
	 * @param unknown $total
	 */
	public static function importShowPartnerData($offset = 0 , $limit = 1000, $total = 10000)
	{
		while($offset<=$total)
		{
			$showDatas = self::service()->getShowPartner($offset, $limit);
			$authService = new Service_Auth();
			$offset += $limit;
			$associateService = new Facade_Associate();
			foreach ($showDatas as $showData)
			{
				$showType = $showData['type'];
				$showAuth = $showData['openid'];
				$showUid = $showData['uid'];
				$showToken = json_decode($showData['token'], true);
				$token = $showToken['access_token'];
				$refreshToken = '';
				$expiresIn = '';
				if(isset($showToken['refresh_token']))
				{
					$refreshToken = $showToken['refresh_token'];
				}
				if(isset($showToken['expires_in']))
				{
					$expiresIn = $showToken['expires_in'];
				}
				$importStatus = 0;
				$ssoUid = 0;
				switch ($showType)
				{
					case 1: //新浪微博
						$authType = Facade_Auth::AUTH_TYPE_WEIBO;
						break;
					case 2: //腾讯微博
						$authType = Facade_Auth::AUTH_TYPE_QQ_WB;
						break;
					case 3: //腾讯互联
						$authType = Facade_Auth::AUTH_TYPE_QQ;
						break;
					case 4: //Facebook
						$authType = Facade_Auth::AUTH_TYPE_FACEBOOK;
						break;
				}
				$authInfo = $authService->getAuthByTypeAndAid($authType, $showAuth);
				if(empty($authInfo))
				{
					$userBase = self::service()->getShowMemberBasic($showUid);
					$params = array('nick'=> $userBase['nickname'], 'gender'=> max(array(1, intval($userBase['sex']))),'birthday'=>0,
							'head_pic'=> $userBase['headpic'],'income'=> 0, 'create_time'=> $userBase['create_time'],'status'=> 1);
					$uid = $authService->setUserinfo($params);
					$authService->setAuth($uid, $authType, $showAuth);
					$ssoUid = $uid;
					$importStatus = 1;
				}
				else
				{
					$ssoUid = $authInfo['uid'];
					$importStatus = 2;
				}
				$authPartner = $associateService->get($showAuth, $authType);
				if(empty($authPartner))
				{
					$associateService->set($showAuth, $authType, $ssoUid, $token, $refreshToken, $expiresIn);
				}
				echo sprintf("time : %s show partner data: %s --- %s---finish\n", time(), $showUid, $showAuth);
				self::service()->setShowPartnerLog($showUid, $ssoUid, $importStatus);
			}
		}
	}
	
	/**
	 * 导入yoho数据
	 * 
	 * @param int $offset
	 * @param int $limit
	 * @param int $total
	 */
	public static function importYohoData($offset, $limit = 1000, $total)
	{
		$dataInc = 0;
		while ($offset<=$total)
		{
			$yohoDatas = self::service()->getYohoMember($offset, $limit);
			//导数据,又开始了,LOL
			$authService = new Service_Auth();
			foreach($yohoDatas as $yohoData)
			{
				$yohoUid = $yohoData['id'];
				$yohoEmail = $yohoData['common_email'];
				$yohoMobile = $yohoData['mobile'];
				$importStatus = 0;
				$ssoUid = 0;
				$yohoEmailAuth = $yohoMobileAuth = $yohoAuth = '';
				if(!empty($yohoEmail))
				{
					$yohoEmailAuth = $authService->getAuthByTypeAndAid(Facade_Auth::AUTH_TYPE_EMAIL, $yohoEmail);
				}
				if(!empty($yohoMobile))
				{
					$yohoMobileAuth = $authService->getAuthByTypeAndAid(Facade_Auth::AUTH_TYPE_MOBILE, $yohoMobile);
				}
				//获取第三方信息
				$yohoPartnerData = self::service()->getYohoPartner($yohoUid);
				if(!empty($yohoPartnerData))
				{
					$yohoAuth = $yohoPartnerData['uinfo'];
					//1--新浪  2--腾讯微博  3--腾讯
					switch($yohoPartnerData['type'])
					{
						case 1:
							$authType = Facade_Auth::AUTH_TYPE_WEIBO;
							break;
						case 2:
							$authType = Facade_Auth::AUTH_TYPE_QQ_WB;
							break;
						case 3:
							$authType = Facade_Auth::AUTH_TYPE_QQ;
							break;
					}
					$authInfo = $authService->getAuthByTypeAndAid($authType, $yohoAuth);
					if(!empty($authInfo))
					{
						$ssoUid = $authInfo['uid'];
						if(empty($yohoEmailAuth) && !empty($yohoEmail))
						{
							$authService->setAuth($ssoUid, Facade_Auth::AUTH_TYPE_EMAIL, $yohoEmail);
						}
						else if(empty($yohoMobileAuth) && !empty($yohoMobile))
						{
							$authService->setAuth($ssoUid, Facade_Auth::AUTH_TYPE_MOBILE, $yohoMobile);
						}
						self::service()->setShowPartnerLog($yohoUid, $ssoUid, 2);
						echo sprintf("time : %s yoho data: %s --- %s---finish\n", time(), $yohoUid, $yohoEmail);
						continue;
					}
				}
				
				if(empty($yohoEmailAuth) && empty($yohoMobile))
				{
					$params = array('nick'=> $yohoData['nickname'], 'gender'=> max(array(1, intval($yohoData['sex']))),
							'birthday'=> 0, 'real_name'=> '','head_pic'=> $yohoData['headpic'], 'create_time'=> $yohoData['create_time'],
							'status'=> $yohoData['status']);
					$uid = $authService->setUserinfo($params);
					if(!empty($yohoEmail))//导入帐号了
					{
						$authService->setAuth($uid, Facade_Auth::AUTH_TYPE_EMAIL, $yohoEmail);
					}
					if(!empty($yohoMobile))
					{
						$authService->setAuth($uid, Facade_Auth::AUTH_TYPE_MOBILE, $yohoMobile);
					}
					//设置密码
					$authService->setPassword($uid, $yohoData['password'], '');
					$ssoUid = $uid;
					$importStatus = 1;
				}
				else if(!empty($yohoEmailAuth) && !empty($yohoMobile) && empty($yohoMobileAuth)) //邮箱存在
				{
					$ssoUid = $yohoEmailAuth['uid'];
					$authService->setAuth($uid, Facade_Auth::AUTH_TYPE_MOBILE, $yohoMobile);
					$importStatus = 1;
				}
				else if(!empty($yohoMobileAuth) && !empty($yohoEmail) && empty($yohoEmailAuth))
				{
					$ssoUid = $yohoMobileAuth['uid'];
					$authService->setAuth($uid, Facade_Auth::AUTH_TYPE_EMAIL, $yohoEmail);
					$importStatus = 1;
				}
				else if(!empty($yohoEmailAuth) || !empty($yohoMobileAuth))
				{
					if(!empty($yohoEmailAuth))
					{
						$ssoUid = $yohoEmailAuth['uid'];
					}
					else
					{
						$ssoUid = $yohoMobileAuth['uid'];
					}
					$importStatus = 2;
				}
				if(!empty($ssoUid) && !empty($yohoPartnerData) && is_array($yohoPartnerData))
				{
					//导入第三方
					$authService->setAuth($ssoUid, $authType, $yohoAuth);
				}
				echo sprintf("time : %s yoho data: %s --- %s ---finish\n", time(), $yohoUid, $yohoEmail);
				self::service()->setYohoLog($yohoUid, $ssoUid, $importStatus);
			}
			$offset +=$limit;
		}
	}
	
	public static function getUserProfileTotal()
	{
		return self::service()->getUserProfileTotal();
	}
	
	public static function getYohoBuyLogTotal()
	{
		return self::service()->getYohoBuyLogTotal();
	}
}