<?php
/**
  * @method 宜配网产品库表基本操作
  * @author soul
  * @copyright 2016/3/31
  */
class YpProduct extends YpBase
{
	/**
	  * @method 获取宜配厂商的缓存
	  * @param  $cache  true|false   false:将重置缓存
	  * @author soul
	  * @copyright 2017/6/5
	  * @return 
			成功 ["status"=>true, "data"=> 一维数组] 
			失败 ["status"=>false, "data"=> 错误信息]  
	  */
	public function getFactoryByCache($cache = true)
	{
		$file = mvc::$cfg['AUTOLOAD']['Yp']['path'].'cache/ypFactory.php';
		$resArr = [];
		if(!file_exists($file) || !$cache)
		{
			$Res = $this->get('yp_factory', ['factory'], ' order by factory');
			foreach($Res['data'] as $V)
			{
				$resArr[] = $V['factory'];
			}
			LibFc::ArraySave($resArr, $file);
		}
		else
		{//括起来为了防止读取文件失败
			$resArr = include($file);
		}
		return LibFc::ReturnData(true, $resArr);
	}

	/**
	  * @method 获取某配件的参数
	  * @param  $pid  int  配件ID
	  * @param  $partId  int  配件所属零件ID
	  * @author soul
	  * @copyright 2017/6/5
	  * @return 
			成功 ["status"=>true, "data"=> 二维数组] 
			失败 ["status"=>false, "data"=> 错误信息]  
	  */
	public function getProductParas($pid, $partId)
	{
		$partId = (int) $partId;
		$pid = (int) $pid;
		$paras = array();
		if($partId > 0 && $pid > 0)
		{
			$table = 'yp_para_part_label_link as ppll
			join `yp_para_label` as pl on ppll.`labelid`=pl.`labelid`
			left join yp_lng_des as ld on pl.desid = ld.desid and lngid=1
			left join yp_lng_text as lt on ld.txtid = lt.txtid
			left join `yp_para_unit` as pu on ppll.`unitid`=pu.`unitid`';
			$res = $this->get($table, ['ppll.`labelid`, txt as label_name, pu.name as unit_name'], sprintf(" and partid = %d order by seq", $partId));
			if(!empty($res['data']))
			{
				$paraArr = [];
				foreach($res['data'] as $v)
				{
					$paraArr[$v['labelid']] = $v;
				}

				$res = $this->get('yp_para_float_link', ['labelid, value'], sprintf(" and pid = %d", $pid));
				foreach($res['data'] as $v)
				{
					$paraArr[$v['labelid']]['value'] = $v['value'];
				}

				$res = $this->get('yp_para_string_link', ['labelid, cn_value as value'], sprintf(" and pid = %d", $pid));
				foreach($res['data'] as $v)
				{
					$paraArr[$v['labelid']]['value'] = $v['value'];
				}
				foreach($paraArr as $v)
				{
					if(!empty($v['value']))
						$paras[] = $v;
				}
			}
		}
		return LibFc::ReturnData(true, $paras);
	}
}