<?php
namespace model\pdflib;
use model\pro\param;
class pdfData extends \model
{
	public $paramObj;
	public $proCateParamSet = [];
	public $proCateParamValue = []; //缓存产品参数值
	public function __construct($proCates)
    {
		parent::__construct();
		$this->paramObj = new param();

		$proCateArr  = explode(',',$proCates);
		foreach($proCateArr as $cid){
			$this->proCateParamSet[$cid] = $this->getProParamSet($cid);
		}
	}

	/**
	  * @method 产品参数设定
	  * @param
	  */
	public function getProParamSet($proCid){
		//参数
        $this->paramObj = new param();
		$rows = $this->paramObj->getParamByCid($proCid);
		$paramSet = [];
		foreach($rows as $row){
			$arr = explode('<line>',$row['pdfcol']);
			foreach($arr as $v){
				$item = explode('=>',$v);
				if($item[1]!=''){
					if(in_array($row['ptpa_type'], ['int','list','radio'])){
						$field = 'ppv_val_int';
					}else if(in_array($row['ptpa_type'], ['double'])){
						$field = 'ppv_val_double';
					}else if(in_array($row['ptpa_type'], ['smalltxt','bigtxt'])){
						$field = 'ppv_val_smalltxt';
					}else{
						$field = 'ppv_val_string';
					}
					$paramSet[$item[0].'.'.$item[1]] = ['id'=>$row['ptpa_id'],'field'=>$field,'dec'=>$row['ptpa_decimal']];
				}
			}
		}
		return $paramSet;
	}

	/**
	  * @method 产品参数的值
	  * @param int $proCid 品名
	  * @param string $code 子板
	  * @param array $paramNames 参数名 如 ['ABS']
	  */
	public function getProParamValue($proCid,$pid,$code,$paramNames){
		/*if(isset($this->proCateParamValue[$pid])){
			return $this->proCateParamValue[$pid];
		}*/
		$idArr = [];
		foreach($paramNames as $name){
			$key = $code.'.'.$name;
			if(isset($this->proCateParamSet[$proCid][$key])){
				$t = $this->proCateParamSet[$proCid][$key];
				$idArr[$t['id']] = [$t['field'],$name,$t['dec']];
			}
		}
		if(empty($idArr)) $idArr = [0];
		$data = [];
		$rows = $this->db->select('gsp_pro_param_val','*',[
			'ppv_p_id'=>$pid,
			'ppv_ptpa_id'=> array_keys($idArr)
		]);
		if($rows){
			foreach($rows as $row){
				$t = $idArr[$row['ppv_ptpa_id']];
				$v = $row[ $t[0] ];
				if(is_numeric($v) && $t[2]>0) $v = sprintf("%.".$t[2]."f",$v);
				if($t[1]=='ABS') $v = formatNum($v);
				$data[ $t[1] ] = $v;
			}
		}
		//$this->proCateParamValue[$pid] = $data;
		return $data;
	}

	/**
	  * @method 保存导出版本数据
	  */
	public function savePdfVerData($catalogId, $cateVer,$cateArea,$newVerType,$newVer,$numbers){
	    if($newVerType=='NEW'){
	        //如果是新品
	        return;
	    }
		$dir = \mvc::$cfg['dir']['files'].'/pdfver';
		mkdirs($dir);
		$newfile = $dir. '/tpl-' . $catalogId.'-'.$cateVer.'-number-'.$cateArea.'-'.$newVer;
		$numbers = array_unique($numbers);
		file_put_contents($newfile,serialize($numbers));
		$verName = $this->db->get("object",'cn_name',['o_id'=>$newVer]);
		if($verName=='') $verName = $newVer;
		foreach($numbers as $number){
		    $row = $this->db->get("p_code",['pc_release_time','pc_release_vers'],['pc_outnum'=>$number]);
		    $vers = trim($row['pc_release_vers']) ? explode(',',trim($row['pc_release_vers'])) : [];
		    array_push($vers,$verName);
		    $vers = array_unique($vers);
		    if(empty($row['pc_release_time'])){
		        $data['pc_release_time'] = getNowDate();
		    }
	        $data['pc_release_vers'] = implode(',',$vers);
	        $this->db->update("p_code",$data,['pc_outnum'=>$number]);
		}
	}

	/**
	  * @method 计算新号码
	  */
	public function getPdfVerNewNumbers($catalogId, $cateVer,$cateArea,$newVerType,$newVer,$lastVer,$numbers){
	    if($newVerType!=null && $newVerType!='NEW'){
	        //如果不是新品，及指定了 对照版本
	        $lastVer = $newVerType;
	    }
		$oldNumbers = [];
		$newNumbers = [];
		$dir = \mvc::$cfg['dir']['files'].'/pdfver';
		$oldfile = $dir.'/tpl-'. $catalogId. '-'.$cateVer.'-number-'.$cateArea.'-'.$lastVer;
		if(file_exists($oldfile)){
			$oldNumbers = unserialize(file_get_contents($oldfile));
		}
		//dd($oldfile, $oldNumbers, $numbers);
		foreach($numbers as $outnum){
			if(!in_array($outnum, $oldNumbers)){
				$newNumbers[] = $outnum;
			}
		}
		sort($newNumbers);
		return $newNumbers;
	}
}