<?php
namespace model\sample;
use \model\pro\factory;
use \model\collocation;
class numidx extends \model
{
    public $table = 'sample_numidx';
    public $key = 'sn_id';
    public $order = 'sn_number';
    public $factory = null;
    public $collocation = null;
    public $findCaches = []; //查询临时缓存

	public function __construct()
    {
		parent::__construct();
        $this->factory = new factory();
        $this->collocation = new collocation();
	}

	function list($key, $where='',$page=0,$pagesize=0,$sidx='',$sord='asc',$orderby=''){
        $fields = '*';
        $where = $this->filter2medoo($where);
		$where = $this->listWhere($where,$page,$pagesize,$sidx,$sord,$orderby);
        $where['sn_s_id'] = $key;
		if($page>0 && $pagesize>0){
			$pages = $this->paging($this->table,$where,$page,$pagesize);
			$where["LIMIT"] = [$pages['limit'],$pagesize];
		}
        $join = [
            '[~]pro_numidx(num)' => ['sn_numfomart' => 'pn_formart', 'sn_pro_factory' => 'pn_pf_id'],            
        ];
        $where['GROUP'] = 'sn_id';
		$rows = $this->db->select($this->table, $join ,$fields,$where);
        //dd($this->db->last());
        if(!empty($rows)) foreach($rows as $k=>&$row){
            $row = $this->getRowInfo($row);
            $row['sn_has'] = ($row['pn_id'] < 1) ? '' : 'Y';
        }
		$data = [
			'page'=> $page,
			'total'=> $pages['total'],
			'records' => $pages['records'],
			'rows' => $rows
		];
		return $data;
    }

    /**
     * 得到记录全量信息
     * @param id|row $var id或者行记录
     * @return array
     */
	function getRowInfo($var){
        $row = $var;
		if(!is_array($var)){
            $row = $this->getRowByKey($var);
        }
        //品牌
        if(!empty($row['sn_pro_factory'])){
            if(isset($this->findCaches['factory'][$row['sn_pro_factory']])){
                $facRow = $this->findCaches['factory'][$row['sn_pro_factory']];
            }else{
                $facRow = $this->factory->getRowByKey($row['sn_pro_factory']);
                $this->findCaches['factory'][$row['sn_pro_factory']] = $facRow ;
            }
           $row['captions']['sn_pro_factory'] = $facRow['pf_name'];
        }
        //来源
        if(!empty($row['sn_src'])){
           $row['captions']['sn_src'] = $this->getCollocationText($row['sn_src']);
        }
        return $row;
    }

    function getCollocationText($value,&$code=null){
        if(isset($this->findCaches['collocation'][$value])){
            $collRow = $this->findCaches['collocation'][$value];
        }else{
            $collRow = $this->collocation->getNode($value);
            $this->findCaches['collocation'][$value] = $collRow;
        }
        if($code!==null) $code = $collRow['code'];
        return $collRow[LG_NAME];
    }

    public function addRow($key)
    {
        $data = [
            'sn_s_id'=>$key,
            'sn_pro_factory'=>0,
            'sn_src'=>0,
            'opuser'=>$this->getOpuser(),
            'create_time'=>$this->getDateTime(),
            'update_time'=>$this->getOpuser(),
        ];
		return $this->db->insert($this->table,$data);
    }

    public function deleteRows($keys)
    {
        if(!is_array($keys)) $keys = explode(',',$keys);
		return $this->db->delete($this->table,[$this->key=>$keys]);
    }
}
