<?php
/**
  * @method 词库
  * @author soul
  * @copyright 2018/5/5
  */
class MainDist extends MainBase
{
	/**
	  * @method 获取最新的私有词库[1小时更新一次]
	  * @param  $cache  true|false 默认true
	  * @author soul
	  * @copyright 2018/5/5
	  * @return
			成功：['status'=>true, 'data'=>['ext_track'=>xxxx, 'ext_track_etime'=>'', 'ext_status'=>'', 'ext_update_api'=>'', 'ext_update_end'=>'']]
			失败：['status'=>false, 'data'=>'xxx', 'code'=>'xxx']
	  */
	public function getLastDist($cache = true)
	{
		$file = mvc::$cfg['AUTOLOAD']['Main']['path'].'cache/dist.txt';
		if(!file_exists($file) || !$cache || (time() - filemtime($file)) > mvc::$cfg['CACHE_TIME']['dist'])
		{
			$this->resetAutoWord();
			$distStr = '# version:'.date('Y-m-d H:i:s')."\n# WORD	TF	IDF	ATTR\n";
			$res = $this->get('fc_dict', ['*'], sprintf(" and di_status=1 order by di_seq asc, length(di_word) desc, di_word asc"));
			foreach($res['data'] as $v)
			{
				$distStr .= sprintf("%s	%d	%d	%s\n", $v['di_word'], $v['di_if'], $v['di_idf'], $v['di_attr']);
			}
			file_put_contents($file, $distStr);
		}
		else
		{
			$distStr = file_get_contents($file);
		}
		return LibFc::ReturnData(true, $distStr);
	}


	/**
	  * @method 重置自动关键字  B2C、车型库、配件分类....
	  * @author soul
	  * @copyright 2018/5/5
	  * @return
			成功：['status'=>true, 'data'=>true]
			失败：['status'=>false, 'data'=>'xxx', 'code'=>'xxx']
	  */
	public function resetAutoWord()
	{
		$this->MainDb->Begin();
		$YpsBase = new YpsBase();
		$tableArr = [
			['t'=>'yp_make', 'f'=>['keyword'], 'w'=>' and keyword is not null'],
			['t'=>'yp_part', 'f'=>['keyword'], 'w'=>' and keyword is not null'],
			['t'=>'yp_tcd_model1', 'f'=>['keyword2'], 'w'=>' and keyword2 is not null'],
			/*['t'=>'yp_tcd_model2', 'f'=>['keyword2'], 'w'=>' and keyword2 is not null'],
			['t'=>'yp_tcd_model3', 'f'=>['keyword2'], 'w'=>' and keyword2 is not null'],*/
		];
		foreach($tableArr as $table)
		{
			$wordStr = '';
			$res = $YpsBase->get($table['t'], $table['f'], $table['w']);
			foreach($res['data'] as $v)
			{
				foreach($table['f'] as $f)
				{
					$wordStr .= $v[$f].',';
				}
			}
			$this->addWords(explode(',', LibFc::fromatKeyword($wordStr)));
		}

		//商城
		$B2cBase = new B2cBase();
		$res = $B2cBase->get('bc_part', ['part_keyword']);
		foreach($res['data'] as $v)
		{
			$wordStr .= $v['part_keyword'].',';
		}
		$this->addWords(explode(',', LibFc::fromatKeyword($wordStr)));
		
		$YpcBase = new YpcBase();
		$tableArr = [
			['t'=>'yp_brand', 'f'=>['b_keyword'], 'w'=>' and b_keyword is not null'],
			['t'=>'yp_make', 'f'=>['m_keyword'], 'w'=>' and m_keyword is not null'],
			['t'=>'yp_model1', 'f'=>['m1_keyword'], 'w'=>' and m1_keyword is not null'],
			['t'=>'yp_model2', 'f'=>['m2_keyword'], 'w'=>' and m2_keyword is not null'],
			['t'=>'yp_model3', 'f'=>['m3_keyword'], 'w'=>' and m3_keyword is not null']
		];
		foreach($tableArr as $table)
		{
			$wordStr = '';
			$res = $YpcBase->Get($table['t'], $table['f'], $table['w']);
			foreach($res['data'] as $v)
			{
				foreach($table['f'] as $f)
				{
					$wordStr .= $v[$f].',';
				}
			}
			$this->addWords(explode(',', LibFc::fromatKeyword($wordStr)));
		}
		$this->MainDb->End();
		return LibFc::ReturnData(true, true);
	}


	
	/**
	  * @method 新增词库
	  * @param  $wordArr  array  ['刹车片', '刹车盘']
	  * @author soul
	  * @copyright 2018/5/5
	  * @return
			成功：['status'=>true, 'data'=>true]
			失败：['status'=>false, 'data'=>'xxx', 'code'=>'xxx']
	  */
	public function addWords($wordArr)
	{
		$wordArr = array_unique($wordArr);
		$tempWordArr = [];
		foreach($wordArr as $k=>$v)
		{
			$v = trim(strtoupper($v));
			if(mb_strlen($v,'utf-8') > 1)
			{
				$wordArr[$k] = $v;
				$tempWordArr[] = sprintf("'%s'", LibFc::Escape($v));
			}
			else
			{
				unset($wordArr[$k]);
			}
		}
		if(!empty($tempWordArr))
		{
			$res = $this->get('fc_dict', ['di_word'], sprintf(" and di_word in (%s)", implode(',', $tempWordArr)));
			$hasWord = array_column($res['data'], 'di_word');
			$addWordArr = array_diff($wordArr, $hasWord);
			if(!empty($addWordArr))
			{
				$valArr = [];
				foreach($addWordArr as $v)
				{
					$valArr[] = ['di_word'=>$v];
				}
				$this->AddAny('fc_dict', ['di_word'], $valArr);
			}
		}
		return LibFc::ReturnData(true, true);
	}


	/**
	  * @method 编辑词库
	  * @param  $action  string  del | status1 || status-1
	  * @param  $dataArr  array  ['刹车片', '刹车盘']
	  * @param  $setBy  string  id || word
	  * @author soul
	  * @copyright 2018/5/5
	  * @return
			成功：['status'=>true, 'data'=>true]
			失败：['status'=>false, 'data'=>'xxx', 'code'=>'xxx']
	  */
	public function setWords($action, $dataArr, $setBy = 'id')
	{
		$dataArr = array_unique($dataArr);
		if(!empty($dataArr))
		{
			$where = '';
			switch($setBy)
			{
				case 'id':
					foreach($dataArr as $k=>$v)
					{
						$dataArr[$k] = (int) $v;
					}
					$where = sprintf(" and di_id in (%s)", implode(',', $dataArr));
				break;
				case 'word':
					foreach($dataArr as $k=>$v)
					{
						$dataArr[$k] = sprintf("'%s'", $v);
					}
					$where = sprintf(" and di_word in (%s)", implode(',', $dataArr));
				break;
			}
			if($where != '')
			{
				switch($action)
				{
					case 'del': 
						$this->Del('fc_dict', $where);
					break;
					case 'status1': 
						$this->Set('fc_dict', ['di_status'=>1], $where);
					break;
					case 'status-1': 
						$this->Set('fc_dict', ['di_status'=>-1], $where);
					break;
				}
			}
		}
		return LibFc::ReturnData(true, true);
	}
}
