<?php
/**
  * @method 中国TCD表基本操作
  * @author soul
  * @copyright 2016/3/31
  */
class TcdcnItem extends TcdcnBase
{
	/**
	  * @method 格式化的厂商缓存
	  * @author soul
	  * @copyright 2017/6/5
	  * @return [["status"]=> bool(true) ["data"]=> ['part'=>'', 'partWord'] ["code"]=> int(0) ]
	  */
	public function getFormatSup($cache = true)
	{
		$arr = [];
		$file = mvc::$cfg['AUTOLOAD']['Tcdcn']['path'].'cache/formatSup.php';
		if(!file_exists($file) || !$cache)
		{
			$res = $this->get('data_brand', ['*']);
			foreach($res['data'] as $v)
			{
				$v['format'] = strtoupper(trim($v['bra_name']));
				$arr[$v['format']][] = $v['bra_id'];
			}
			LibFc::ArraySave($arr, $file);
		}
		else
		{
			$arr = include($file);
		}
		return LibFc::ReturnData(true, $arr);
	}

	/**
	  * @method 格式化的零件分类缓存
	  * @author soul
	  * @copyright 2017/6/5
	  * @return [["status"]=> bool(true) ["data"]=> ['part'=>'', 'partWord'] ["code"]=> int(0) ]
	  */
	public function getFormatPart($cache = true)
	{
		$arr = [];
		$file = mvc::$cfg['AUTOLOAD']['Tcdcn']['path'].'cache/formatPart.php';
		if(!file_exists($file) || !$cache)
		{
			$res = $this->get('data_group', ['*']);
			$partArr = array_column($res['data'], null, 'prt_id');
			foreach($partArr as $v)
			{
				if($v['prt_haschilds']) continue;
				$v['path'] = [];
				foreach(explode(',', $v['prt_path']) as $pv)
				{
					if(!empty($partArr[$pv]['prt_assemblygroupname']))
						$v['path'][] = strtoupper(trim($partArr[$pv]['prt_assemblygroupname']));
				}
				if(!empty($v['path']))
					$arr[implode('>', $v['path'])][] = $v['prt_id'];

				$v['format'] = strtoupper(trim($v['prt_assemblygroupname']));
				$arr[$v['format']][] = $v['prt_id'];
			}
			LibFc::ArraySave($arr, $file);
		}
		else
		{
			$arr = include($file);
		}
		return LibFc::ReturnData(true, $arr);
	}
}