<?php

/**
 * 号码操作类
 */
class EtkNum extends EtkBasic
{
    /**
     * 获取号码各自的产品ID
     * @param $aValue
     * @return array
     */
    public function GetPidByNum($aValue)
    {
        if (empty($aValue)) {
            return LibFc::ReturnData(false, 'realoem 搜索号码缺少参数');
        }

        $sWhere = sprintf('and src_format in ("%s")', implode('","', $aValue));
        $aQuery = $this->Get('data_product_search', ['src_format', 'src_pro_id'], $sWhere);

        $aReturn = [];
        if ($aQuery['status'] && !empty($aQuery['data']))
        {
            foreach ($aQuery['data'] as $sKey => $aVal)
            {
                $aReturn[$aVal['src_format']] = $aVal['src_pro_id'];
            }
        }
        return LibFc::ReturnData(true, $aReturn);
    }


	/**
	  * @method get  号码匹配车型
	  * @http  get
	  * @param  $number			   string [必填] 号码 11138616793,11 41 8 612 615 多个号码用英文逗号分隔
	  * @param  $modtype		   string [选填] 默认全车件 all:全车件模式  engine:发动机件模式  chassis:底盘模式
	  * @author soul
	  * @copyright 2023/7/12
	  * @return 
		{"status":true,"data":["4189","21007","6288"],"code":"","runtime":97}

		{"status":true,"data":["03f7f994a401d9e400c9a85bbbf22ea9","0d81f78820842364de0992ebb51a2b62","0e2319508534fcfba30263ec12e488e4","283b822d4b2102ccbe08eb400b961375","3c994c8f901c476ad04c4f3c570bd8bb","555ae17fbf12947ec80fe10bda474c51","65cd2e3bfcb2c03bdfdac617a92a651c","6720de725c1b9dd8e79f067f234021a7","683fb9d08d8630a3ce75bad0e4ab3a34","701dd1cb7853089fb571698c0dd7c97d","7eb56b10575d9f1788824d528dba992e","97375ced4f9a36123be358f99f57ef74","9889db65689220a14db5304f47d59b8d","c01f653c700613e4e190ebd721d4f1ac","c8810ee688ff4dc0d55a907a016d02bc","cb107f17d1ab7d068082a8585fad26c9","d20ab39b5ec34a38129f368365e1b7f2","dd337687b850ad2e66760f9a098bd228","eb9ed68b9eed6230c64091d24e34ea2a","f16996d8b76cfcb3f3da033ccd8c2554"],"code":"","runtime":125}
	  */
	public function getModelByNumber($number, $modtype = 'all')
	{
		$arr = [];
		$numArr = [];
		foreach(explode(',', $number) as $tnum){
			$formatNum = LibFc::FormatNum($tnum);
			$formatNumArr = LibFc::SearchNum($formatNum);
			foreach($formatNumArr as $num){
				if (strlen($num) > 2) $numArr[] = $num;
			}
		}
		$numArr = array_unique($numArr);
		if(empty($num)) return LibFc::ReturnData(true, $arr);
	
		//尝试从007替换号中查询
		$res = $this->Get('number_replace_list JOIN number_replace ON num_id = nrl_num_id', ['DISTINCT num_format'], sprintf(" and nrl_format in ('%s')", implode("','", $numArr)));
		if(!empty($res['data'])){
			foreach($res['data'] as $tv){
				$numArr[] = $tv['num_format'];
			}
			$numArr = array_unique($numArr);
		}
		//echo microtime(true), "\n";
        $res = $this->Get('data_product_search', ['src_format', 'src_etk_id'], sprintf(" and src_format in ('%s') group by src_etk_id, src_format", implode("','", $numArr)));
		//echo microtime(true), "\n";
		$etkArr = [];
		foreach($res['data'] as $v){
			$etkArr[$v['src_etk_id']][] = $v['src_format'];
		}
		foreach($etkArr as $etkid=>$nums){
			$EpcItem = new EpcItem($etkid);
			$res = $EpcItem->getNumberMod3Id($nums);
			$arr = array_merge($arr, $res['data']);
		}
		$arr = array_values(array_unique($arr));
		if(!empty($arr) && $modtype != 'all'){
			$Ypc2Base = new Ypc2Base();
			if($modtype == 'engine'){
				$res = $Ypc2Base->get('bc_modengine_mod3', ['distinct mem_men_code id_code'], sprintf('and mem_mod3_id in (%s)', implode(',', $arr)));
				$arr = array_column($res['data'], 'id_code');
			}else if($modtype == 'chassis'){
				$res = $Ypc2Base->get('bc_modchassis_mod3', ['distinct mcm_mch_code id_code'], sprintf('and mcm_mod3_id in (%s)', implode(',', $arr)));
				$arr = array_column($res['data'], 'id_code');
			}
		}
		return LibFc::ReturnData(true, $arr);
	}


	/**
	  * @method get  获取号码的替换号
	  * @http  get
	  * @param  $number			   string|array [必填] 号码 11138616793 ['11138616793',....]
	  * @author soul
	  * @copyright 2024/10/8
	  * @return 
	  */
	public function getReplaceNumber($number)
	{
		$arr = [];
		$formatNumArr = [];
		if(!is_array($number)) $number = [$number];
		foreach($number as $v){
			$v = LibFc::FormatNum($v);
			if(strlen($v) > 2){
				$formatNumArr = array_merge($formatNumArr, LibFc::SearchNum($v));
			}
		}
		if(!empty($formatNumArr)){
			$res = $this->get('number_replace_list', ['nrl_display, nrl_format'], sprintf(" and nrl_num_id IN (SELECT DISTINCT nrl_num_id FROM number_replace_list WHERE nrl_format IN ('%s')) group by nrl_format order by nrl_seq", implode("','", $formatNumArr)));
			if(count($res['data']) < 200){//避免源数据有问题
				foreach($res['data'] as $v){
					$arr[] = ['oe'=>1, 'factory'=>'OE', 'display'=>$v['nrl_display'], 'format'=>$v['nrl_format']];
				}
			}
		}
		return LibFc::ReturnData(true, $arr);
	}


	/**
	  * @method get  获取号码的替换号
	  * @http  get
	  * @param  $number			   string|array [必填] 号码 11138616793 ['11138616793',....]
	  * @author soul
	  * @copyright 2024/10/8
	  * @return 
	  */
	public function getReplaceNumberFull($number)
	{
		$arr = [];
		$formatNumArr = [];
		if(!is_array($number)) $number = [$number];
		foreach($number as $v){
			$v = LibFc::FormatNum($v);
			if(strlen($v) > 2){
				$formatNumArr = array_merge($formatNumArr, LibFc::SearchNum($v));
			}
		}
		if(!empty($formatNumArr)){
			$res = $this->get('number_replace_list', ['nrl_display display', 'nrl_brand_name oe_make', 'nrl_name pro_name'], sprintf(" and nrl_num_id IN (SELECT DISTINCT nrl_num_id FROM number_replace_list WHERE nrl_format IN ('%s')) group by nrl_format order by nrl_seq", implode("','", $formatNumArr)));
			if(!empty($res['data'])){
				if(count($res['data']) < 200){//避免源数据有问题
					$arr = $res['data'];
				}
			}else{
				$res = $this->get('number_replace', ['num_display display', 'num_brand_name oe_make', 'num_name pro_name'], sprintf(" and num_format IN ('%s') group by num_format", implode("','", $formatNumArr)));
				$arr = $res['data'];
			}
		}
		return LibFc::ReturnData(true, $arr);
	}
	


	/**
	  * @method 转换查询号码
	  * @http  get
	  * @param  $number			   string [必填] 号码 11138616793
	  * @author soul
	  * @copyright 2024/10/9
	  * @return 
	  */
	public function getSearchNum($number)
	{
		$arr = ['nums'=>[], 'hasreplace'=>false];
		$formatNum = LibFc::FormatNum($number);
		if(strlen($formatNum) > 2){
			$formatNumArr = LibFc::SearchNum($formatNum);
			$arr['nums'] = $formatNumArr;
			$bcout = count($arr['nums']);
			$res = $this->get('number_replace_list', ['DISTINCT nrl_format'], sprintf(" and nrl_num_id IN (SELECT DISTINCT nrl_num_id FROM number_replace_list WHERE nrl_format IN ('%s'))", implode("','", $formatNumArr)));
			foreach($res['data'] as $v){
				if(strlen($v['nrl_format']) > 2)
					$arr['nums'] = array_merge($arr['nums'], LibFc::SearchNum($v['nrl_format']));
			}
			$arr['nums'] = array_unique($arr['nums']);
			$endcount = count($arr['nums']);
			if($endcount > $bcout) $arr['hasreplace'] = true;
		}
		return LibFc::ReturnData(true, $arr);
	}
}