<?php
/**
  * @method 产品管理
  * @author soul
  * @copyright 2016/3/31
  */
class AaiaProduct extends AaiaBase
{
	/**
	  * @method 获取配件基本信息
	  * @param  $proIdArr array [11,2222....]
	  * @param  $typeArr ['picAll', 'model'...]
	  * @author soul
	  * @copyright 2017/7/7
	  * @return 
			成功 ["status"=>true, "data"=> [11=>[], ...]] 
			失败 ["status"=>false, "data"=> 错误信息] 
	  */
	public function getAnyProductInfo($proIdArr, $typeArr = [])
	{
		$proArr = [];
		foreach($proIdArr as $k=>$v)
		{
			$proIdArr[$k] = (int) $v;
		}
		$proIdArr = array_unique($proIdArr);
		if(!empty($proIdArr))
		{
			$table = 'yp_product AS p 
			join yp_number AS n on p.pid = n.pid AND n.ismain=1 
			JOIN yp_factory AS fac ON fac.fid = n.fid 
			join yp_part as pa on p.partid = pa.partid
			LEFT JOIN yp_files_pro_pic AS f ON p.pid=f.dataid AND p.pro_pic_id=f.fileid';
			$filed = ['p.pid, p.url, n.display, fac.factory, p.partid, pa.name as pa_name, f.path, f.name AS fname, p.remark, f.ext'];
			$res = $this->get($table, $filed, sprintf(" and p.pid in (%s)", implode(',', $proIdArr) ));
			foreach($res['data'] as $v)
			{
				$pro = [
					'pro_id'=>$v['pid'],
					'pro_name'=>$v['pa_name'],
					'pic'=>[],
					'num'=>[['factory'=>$v['factory'], 'display'=>$v['display']]],
					'pro_price'=>['title'=>'销售价', 'price'=>'0.00']
				];

				if(in_array('para', $typeArr))
				{
					$paraRes = $this->getProductPara($v['pid'], $v['partid']);
					$pro['para'] = !empty($paraRes['data'])? $paraRes['data']:[];
					$pro['parastr'] = !empty($paraRes['data'])? '':$v['remark'];
				}

				if(in_array('nums', $typeArr))
				{
					$table = 'yp_number AS n JOIN yp_factory AS f ON n.fid=f.fid LEFT JOIN yp_oe_make AS om ON om.oe_make_id=n.oe_make_id';
					$where = sprintf(" and n.pid = %d ORDER BY om.name, f.factory, n.format", $v['pid']);
					$res = $this->get($table, ['case when n.fid = 770 then 1 else 0 end as oe, case when om.name is not null then om.name else f.factory end as factory, n.display'], $where);
					$pro['nums'] = $res['data'];
				}

				if(!empty($v['fname']))
					$pro['pic'] = [mvc::$cfg['urls']['aaiaPic'].$v['path'].$v['fname'].'.'.$v['ext']];
				$proArr[$v['pid']] = $pro;
			}
			$proIdArr = array_keys($proArr);
			if(empty($proIdArr)) return LibFc::ReturnData(true, $proArr);
			
			if(in_array('picAll', $typeArr))
			{
				foreach($proArr as $k=>$v)
				{
					$proArr[$k]['pic'] = [];
				}
				$res = $this->get('yp_files_pro_pic', ['dataid', 'path', 'name', 'ext'], sprintf(" and dataid in(%s)", implode(',', $proIdArr)));
				foreach($res['data'] as $v)
				{
					$proArr[$v['dataid']]['pic'][] = mvc::$cfg['urls']['aaiaPic'].$v['path'].$v['name'].'.'.$v['ext'];
				}
			}
		}
		return LibFc::ReturnData(true, $proArr);
	}


	/**
	  * @method 获取配件的号码
	  * @param  $proId int 配件ID
	  * @param  $type string  oe|ref|all
	  * @param  $page int 第几页
	  * @param  $per int 每页数量
	  * @author soul
	  * @copyright 2017/6/5
	  * @return  ["status"=>true, "data"=>[{"factory":"FERODO","display":"2114023"},...] ]
	  */
	public function getItemNumber($proId, $type, $page, $per)
	{
		$proId = (int) $proId;
		$arr = [];
		$per = empty($per) || $per < 1 ? 20: (int) $per;
		$page = empty($page) || $page < 1 ? 1: (int) $page;
		$filed = ['case when n.fid = 770 then 1 else 0 end as oe, case when om.name is not null then om.name else f.factory end as factory, n.display'];
		$table = 'yp_number AS n JOIN yp_factory AS f ON n.fid=f.fid LEFT JOIN yp_oe_make AS om ON om.oe_make_id=n.oe_make_id';
		switch($type)
		{
			case 'oe':
				$res = $this->get($table, $filed, sprintf(" and n.fid=770 and n.pid = %d ORDER BY om.name, f.factory, n.format limit %d, %d", $proId, ($page-1)*$per, $per));
				$arr = $res['data'];
			break;
			case 'ref':
				$res = $this->get($table, $filed, sprintf(" and n.fid !=770 and n.pid = %d ORDER BY f.factory, n.format limit %d, %d", $proId, ($page-1)*$per, $per));
				$arr = $res['data'];
			break;
			default:
				$res = $this->get($table, $filed, sprintf(" and n.pid = %d ORDER BY om.name, f.factory, n.format limit %d, %d", $proId, ($page-1)*$per, $per));
				$arr = $res['data'];
			break;
		}
		return LibFc::ReturnData(true, $arr);
	}
	
	
	/**
	  * @method 获取产品参数
	  * @param  
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=>["code"]=> int(0) } 
	  */
	public function getProductPara($pid, $partId, $lang = 1)
	{
		$pid = (int) $pid;
		$partId = (int) $partId;
		$lang = (int) $lang;
		$para = [];
		if($pid > 0 && $partId > 0)
		{
			$ParaArr = array();
			$ParaArr2 = array();

			$FiledArr = ['labelid, value'];
			$Res = $this->get('yp_para_string_link', $FiledArr, sprintf(" AND pid=%d", $pid));
			foreach($Res['data'] as $V)
			{
				$ParaVal2[$V['labelid']] = $V;
			}

			$Table = 'yp_para_part_label_link AS ppll
			JOIN yp_para_label AS pl ON ppll.labelid = pl.labelid
			LEFT JOIN yp_para_unit AS pu ON ppll.unitid = pu.unitid';
			$Res = $this->get($Table, ['ppll.labelid, pl.name, pu.name as uname'], sprintf(" AND ppll.partid=%d ORDER BY ppll.seq", $partId));
			foreach($Res['data'] as $V)
			{
				if(!empty($ParaVal2[$V['labelid']]))
				{
					$V['uname'] = empty($V['uname']) ? '': $V['uname'];
					if($V['uname'] != 'mmLocation')
						$para[] = ['name'=>$V['name'], 'val'=>$ParaVal2[$V['labelid']]['value'], 'unit'=>$V['uname']];
				}
			}

			$Res = $this->get('yp_para_float_link', ['labelid, value'], sprintf(" AND pid=%d", $pid));
			foreach($Res['data'] as $V)
			{
				$ParaVal[$V['labelid']] = $V;
			}

			$Table = 'yp_para_part_label_link AS ppll
			JOIN yp_para_label AS pl ON ppll.labelid = pl.labelid
			LEFT JOIN yp_para_unit AS pu ON ppll.unitid = pu.unitid';
			$Res = $this->get($Table, ['ppll.labelid, pl.name, pu.name as uname'], sprintf(" AND ppll.partid=%d ORDER BY ppll.seq", $partId));
			foreach($Res['data'] as $V)
			{
				if(!empty($ParaVal[$V['labelid']]))
				{
					$V['uname'] = empty($V['uname']) ? '': $V['uname'];
					$para[] = ['name'=>$V['name'], 'val'=>$ParaVal[$V['labelid']]['value'], 'unit'=>$V['uname']];
				}
			}
		}
		return LibFc::ReturnData(true, $para);
	}

	/**
	  * @method 获取产品车型
	  * @param  
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=>["code"]=> int(0) } 
	  */
	public function getItemModel($proId)
	{
		$arr = [];
		$proId = (int) $proId;
		$table = 'yp_aaia_model3_pro_link AS m3p
			JOIN yp_aaia_model3 AS m3 ON m3p.model3_id = m3.model3_id
			JOIN yp_aaia_model2 AS m2 ON m2.model2_id= m3.model2_id
			JOIN yp_aaia_model1 AS m1 ON m1.model1_id= m3.model1_id
			JOIN yp_make AS m ON m.make_id=m3.make_id';
		return $this->get($table, ['m.make_id, m.name AS make_name,m1.name AS mod2name, m2.name AS mod3_name, MIN(m3.start_year) AS mod3_start_year, MAX(m3.end_year) AS mod3_end_year'], sprintf(" and m3p.pid= %d GROUP BY m2.model2_id", $proId));
	}
}