<?php
class OthNumber
{
	/**
	  * @method get  号码搜索
	  * @param  $Params array 
	  * @param  word			   string [必填] 号码
	  * @param  type               string [选填] 数据源  默认所有 多个逗号分隔  etk,pjd,super,yp,tcd,aaia
	  * @param  dtype              string [选填] para...
	  * @param  count              int    [选填] TCD最大数量 默认10
	  * @param  uid                int [选填] 用户ID
	  * @author soul
	  * @copyright 2017/9/30
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function search($Params)
	{
		$proArr = [];
		$formatNum = LibFc::FormatNum($Params['word']);
		$formatNumArr = LibFc::SearchNum($formatNum);
		if(!isset($Params['uid'])) $Params['uid'] = 0;
		if(strlen($formatNum) > 2)
		{
			$allType = ['etk', 'pjd', 'super', 'yp', 'tcd', 'aaia'];
			$typeArr =  empty($Params['type']) ? $allType: explode(',', $Params['type']);
			$dtypeArr =  empty($Params['dtype']) ? []: explode(',', $Params['dtype']);
			$islike =  isset($Params['islike']) ? $Params['islike']: true;
			$count =  empty($Params['count']) || $Params['count'] < 1 || $Params['count'] > 200 ? 10: (int) $Params['count'];
			$proArr = [];
			$epcReNum = null;
			foreach($typeArr as $type)
			{
				$btime = microtime(true);
				switch($type)
				{
					case 'etk':
						$tnums = $formatNumArr;
						$resfrom = '';
						$EtkBasic = new EtkBasic();
						$nres = $EtkBasic->Get('data_product_search', ['distinct src_etk_id'], sprintf(" and src_format in ('%s')", implode("','", $tnums)));
						//epc或者宜配数据加追EPC替换号
						if(empty($nres['data'])){
							if($epcReNum === null){
								$EtkNum = new EtkNum();
								$res = $EtkNum->getSearchNum($formatNum);
								$epcReNum = $res['data'];
								if($epcReNum['hasreplace']){
									$res = $EtkNum->getReplaceNumber($formatNum);
									$epcReNum['replace'] = $res['data'];
								}
							}
							if(!empty($epcReNum['nums']) && $epcReNum['hasreplace']){
								$tnums = $epcReNum['nums'];
								$resfrom = 'reepcnum';
								$nres = $EtkBasic->Get('data_product_search', ['distinct src_etk_id'], sprintf(" and src_format in ('%s')", implode("','", $tnums)));
							}
						}
						foreach($nres['data'] as $v)
						{
							$etkId = $v['src_etk_id'];
							if(empty(mvc::$cfg['DB']['EPC'][$etkId])) continue;
							$EpcItem = new EpcItem($etkId);
							$pres = $EpcItem->getSearchNumber($tnums, $islike);
							if($Params['onlycheck']){
								$proArr[$type] = count($pres['data']);
							}else{
								foreach($pres['data'] as $vv){
									//替换号中找出搜索号码
									if($resfrom == 'reepcnum' && !empty($epcReNum['replace'])){
										$hasnums = [];
										foreach($vv['num'] as $tnum){
											$hasnums[] = LibFc::FormatNum($tnum['display']);
										}
										foreach($epcReNum['replace'] as $enum){
											if($formatNum == $enum['format'] && !in_array($enum['format'], $hasnums)){
												$vv['num'][] = ['factory'=>$enum['factory'], 'display'=>$enum['display']];
											}
										}
									}
									$vv['pro_name'] = strip_tags($vv['pro_name']);
									$proArr[$type][$etkId.$vv['pro_id']] = $vv;
								}
							}
						}
					break;
					case 'pjd':
						$B2cBase = new B2cBase();
						$where = sprintf(" and pro_status=1 and shop_show_pjl =1 and shop_show_b2c= 1 and num_format in ('%s')", implode("','", $formatNumArr));
						$table = 'bc_number join bc_product on pro_id = num_pro_id join sh_shop on pro_shop_id=shop_id and shop_status = 1 and shop_istest != 1';
						$res = $B2cBase->get($table, ['distinct num_pro_id'], $where);
						if(!empty($res['data']))
						{
							if($Params['onlycheck']){
								$proArr[$type] = count($res['data']);
							}else{
								$B2cProduct = new B2cProduct();
								$res = $B2cProduct->getAnyProductInfo(array_column($res['data'], 'num_pro_id'), $Params['uid'], array_merge($dtypeArr, ['numMain', 'picMain']));
								$proArr[$type] = $res['data'];
							}
						}
					break;
					case 'super':
						$B2cBase = new B2cBase();
						$where = sprintf(" and pro_status=1 and shop_show_pjl =1 and num_format in ('%s')", implode("','", $formatNumArr));
						$table = sprintf("bc_number 
						join bc_product on pro_id = num_pro_id
						join sh_shop on pro_shop_id=shop_id and shop_status = 1 and shop_istest != 1
						join sh_super on ssu_shop_id = shop_id and ssu_style = 'B2C' and ssu_etime >= %d and ssu_pjl_show = 1
						", time());
						$res = $B2cBase->get($table, ['distinct num_pro_id'], $where);
						if(!empty($res['data']))
						{
							if($Params['onlycheck']){
								$proArr[$type] = count($res['data']);
							}else{
								$B2cProduct = new B2cProduct();
								$res = $B2cProduct->getAnyProductInfo(array_column($res['data'], 'num_pro_id'), $Params['uid'], array_merge($dtypeArr, ['numMain', 'picMain', 'noPrice']));
								$proArr[$type] = $res['data'];
							}
						}
					break;
					case 'yp':
						$tnums = $formatNumArr;
						$resfrom = '';
						$YpsBase = new YpsBase();
						$table = 'yp_number AS n JOIN yp_factory AS fac ON fac.fid = n.fid';
						$filed = ['n.pid, n.display, factory'];

						$where = sprintf(" and n.format in ('%s') group by n.pid", implode("','", $tnums));
						$nres = $YpsBase->get($table, $filed, $where);
						//epc或者宜配数据加追EPC替换号
						if(empty($nres['data'])){
							if($epcReNum === null){
								$EtkNum = new EtkNum();
								$res = $EtkNum->getSearchNum($formatNum);
								$epcReNum = $res['data'];
								if($epcReNum['hasreplace']){
									$res = $EtkNum->getReplaceNumber($formatNum);
									$epcReNum['replace'] = $res['data'];
								}
							}
							if(!empty($epcReNum['nums']) && $epcReNum['hasreplace']){
								$tnums = $epcReNum['nums'];
								$resfrom = 'reepcnum';
								$where = sprintf(" and n.format in ('%s') group by n.pid", implode("','", $tnums));
								$nres = $YpsBase->get($table, $filed, $where);
							}
						}
						if(!empty($nres['data']))
						{
							if($Params['onlycheck']){
								$proArr[$type] = count($nres['data']);
							}else{
								$YpsProduct = new YpsProduct();
								$tres = $YpsProduct->getAnyProductInfo(array_column($nres['data'], 'pid'), $dtypeArr);
								$proArr[$type] = $tres['data'];
								foreach($nres['data'] as $v)
								{
									$proArr[$type][$v['pid']]['num'] = [['factory'=>$v['factory'], 'display'=>$v['display']]];
								}

								//替换号中找出搜索号码
								if($resfrom == 'reepcnum' && !empty($epcReNum['replace'])){
									foreach($proArr[$type] as $pid=>$vv){
										$hasnums = [];
										foreach($vv['num'] as $tnum){
											$hasnums[] = LibFc::FormatNum($tnum['display']);
										}
										foreach($epcReNum['replace'] as $enum){
											if($formatNum == $enum['format'] && !in_array($enum['format'], $hasnums)){
												$proArr[$type][$pid]['num'][] = ['factory'=>$enum['factory'], 'display'=>$enum['display']];
											}
										}
									}
								}

								$where = sprintf(" and n.pid in (%s) and n.ismain=1 and n.format not in ('%s') group by n.pid", implode(',', array_keys($proArr[$type])), implode("','", $tnums));
								$res = $YpsBase->get('yp_number AS n JOIN yp_factory AS fac ON fac.fid = n.fid', ['n.pid, n.display, factory'], $where);
								foreach($res['data'] as $v)
								{
									if(count($proArr[$type][$v['pid']]['num']) == 1)
										$proArr[$type][$v['pid']]['num'][] = ['factory'=>$v['factory'], 'display'=>$v['display']];
								}
							}
						}
					break;
					case 'tcd':
						$TcdmArt = new TcdmArt();
						$res = $TcdmArt->searchNumber($formatNum, $count);
						if(!empty($res['data'])){
							if($Params['onlycheck']){
								$proArr[$type] = count($res['data']);
							}else{
								$proArr[$type] = array_column($res['data'], null, 'pro_id');
							}
						}
					break;
					case 'aaia':
						$AaiaBase = new AaiaBase();
						$where = sprintf(" and n.format in ('%s') group by n.pid", implode("','", $formatNumArr));
						$table = 'yp_number AS n JOIN yp_factory AS fac ON fac.fid = n.fid';
						$filed = ['n.pid, n.display, factory'];
						$res = $AaiaBase->get($table, $filed, $where);
						if(!empty($res['data']))
						{
							if($Params['onlycheck']){
								$proArr[$type] = count($res['data']);
							}else{
								$AaiaProduct = new AaiaProduct();
								$tres = $AaiaProduct->getAnyProductInfo(array_column($res['data'], 'pid'), $dtypeArr);
								$proArr[$type] = $tres['data'];
								foreach($res['data'] as $v)
								{
									$proArr[$type][$v['pid']]['num'] = [['factory'=>$v['factory'], 'display'=>$v['display']]];
								}
								$where = sprintf(" and n.pid in (%s) and n.ismain=1 and n.format not in ('%s') group by n.pid", implode(',', array_keys($proArr[$type])), implode("','", $formatNumArr));
								$res = $AaiaProduct->get('yp_number AS n JOIN yp_factory AS fac ON fac.fid = n.fid', ['n.pid, n.display, factory'], $where);
								foreach($res['data'] as $v)
								{
									$proArr[$type][$v['pid']]['num'][] = ['factory'=>$v['factory'], 'display'=>$v['display']];
								}
							}
						}
					break;
				}
				//$proArr['time'][$type] = microtime(true)-$btime;
			}
			if($Params['onlycheck']) return LibFc::ReturnData(true, $proArr);

			foreach($proArr as $type=>$v)
			{
				//if($type == 'time') continue;
				foreach($v as $kk=>$vv)
				{
					if(empty($vv['pro_price'])) 
					{
						$vv['pro_price'] = ['title'=>'销售价', 'price'=>0.00];
						$v[$kk] = $vv;
					}
					if(!in_array('para', $dtypeArr))
						$v[$kk]['para'] = [];
				}
				$proArr[$type] = array_values($v); 
			}
			if(empty($proArr) && count($allType) == count($typeArr)){
				$this->addNumNullLog($Params['word'], $Params['from']);
			}
		}
		return LibFc::ReturnData(true, $proArr);
	}

	public function addNumNullLog($word, $from = '')
	{
		$formatNum = LibFc::FormatNum($word);
		$id = 0;
		if(strlen($formatNum) > 2){
			$YpwBase = new YpwBase();
			$res = $YpwBase->get('yp_search_numnull', ['ysn_id'], sprintf(" and ysn_format='%s'", $formatNum), true);
			if(!empty($res['data'])){
				$id = $res['data']['ysn_id'];
				$YpwBase->set('yp_search_numnull', ['ysn_count'=>'#@ysn_count+1'], sprintf(" and ysn_id=%d", $id));
			}else{
				$add = [
					'ysn_number'=>$word,
					'ysn_format'=>$formatNum,
					'ysn_count'=>1,
					'ysn_chkstatus'=>0,
					'ysn_chktime'=>time(),
					'ysn_atime'=>time(),
					'ysn_from'=>$from ??'',
				];
				$res = $YpwBase->add('yp_search_numnull', $add);
				$id = $res['data'];
			}
		}
		return LibFc::ReturnData(true, $id);
	}
}