<?php
namespace model\import;
use \model;
use \model\check;
use model\pro\pro;

/**
 * 
 */
class oeLink  extends model
{
	public $title = '零件关联号码'; //固定
	public $import = null;    //固定
	public $rules = null;    //规则
	public $demoData = null;  //样例数据
	public $demoremark = null;  //样例说明
	public $oe_id = null;
	public $ol_type = null;
	
	public function __construct()
	{
		parent::__construct(); //执行父类
		$this->import = new \lib\import();
	
		$this->oe_id = \mvc::$req['oe_id'];
		if(empty($this->oe_id) && !empty(\mvc::$req['params'])){
			$params =json_decode(\mvc::$req['params'],true);
			$this->oe_id =$params['oe_id'];
		}

        $this->ol_type = \mvc::$req['ol_type'];
		if(empty($this->ol_type) && !empty(\mvc::$req['params'])){
			$params =json_decode(\mvc::$req['params'],true);
			$this->ol_type =$params['ol_type'];
		}

        //没有oe_id
        if(!$this->oe_id){
			$this->rules = [
				['key'=>'ol_pf_id1','caption'=>lg('厂商1'),'formatCell'=>'pf_id'],
				['key'=>'ol_number1','caption'=>lg('号码1'),'must'=>true],
				['key'=>'ol_pf_id2','caption'=>lg('厂商2'),'formatCell'=>'pf_id'],
				['key'=>'ol_number2','caption'=>lg('号码2'),'must'=>true],
				['key'=>'ol_from','caption'=>lg('数据来源'),'formatCell'=>[\model\collocation::class,[],'getOidByName',['@', 13],null]],
			];
        }else{
			$this->rules = [
				['key'=>'ol_pf_id2','caption'=>lg('厂商'),'formatCell'=>'pf_id'],
				['key'=>'ol_number2','caption'=>lg('号码'),'must'=>true],
				['key'=>'ol_from','caption'=>lg('数据来源'),'formatCell'=>[\model\collocation::class,[],'getOidByName',['@', 13],null]],
			];
		}
	
		$this->demoData = [
		];

		$this->demoremark = '';
	}
	
	 /**处理导入
     * @param array|string $src 数组或者文件名
     * @param isInit 是否是初始化
     * @return 可返回任意值(string|array|bool)，都会到前端
     */
    public function import($src,$isInit=false) //固定
    {
		$state = $this->import->execute($src, $this->rules);
        $data = [];

        $state = !$this->import->checkRepeat([
            ['ol_pf_id1','ol_number1','ol_pf_id2','ol_number2']
        ]);

		//数据重复验证 - 自己
		if($state && !empty($this->import->data))
		{           
    
            foreach ($this->import->data as &$mv) 
            {   
                //源号码
                $mv['ol_pf_id1'] = $mv['ol_pf_id1']?:1; 
				//如果存在oe_id	
				if($this->oe_id){
                    $arr = $this->db->get('oe_item',['oe_pf_id','oe_oe'],['oe_id'=>$this->oe_id]);
					$mv['ol_pf_id1'] = $arr['oe_pf_id'];
					$mv['ol_number1'] = $arr['oe_oe'];
                    $mv['ol_oe_id1'] = $this->oe_id;
					$mv['ol_format1'] =formatNum($mv['ol_number1']);
				}else{
					$mv['ol_format1'] =formatNum($mv['ol_number1']);
					$snums = $mv['ol_pf_id1'] == 1? reSearchNum($mv['ol_format1']): $mv['ol_format1'];
					$mv['ol_oe_id1'] =$this->db->get('oe_item',['[><]oe_numidx'=>['oe_id'=>'on_oe_id']],'oe_id',['on_pf_id'=>$mv['ol_pf_id1'],'on_formart'=>$snums]);
				}


				if(!$mv['ol_oe_id1']){
					$state = false;
					$mv['excelInfo']['error'] = '不存在与【厂商1、号码1】匹配的零件';
					$this->import->lineNum = $mv['excelInfo']['line'];
					$this->import->lastError = $mv['excelInfo']['error'];
					continue;
				}

                //关联号码
                $mv['ol_pf_id2'] = $mv['ol_pf_id2']?:1; 
				$mv['ol_format2'] = formatNum($mv['ol_number2']);
				$snums = $mv['ol_pf_id2'] == 1? reSearchNum($mv['ol_format2']): $mv['ol_format2'];

				$mv['ol_oe_id2'] = $this->db->get('oe_item',['[><]oe_numidx'=>['oe_id'=>'on_oe_id']],'oe_id',['on_pf_id'=>$mv['ol_pf_id2'],'on_formart'=>$snums]);
               
				if(!$mv['ol_oe_id2'] && $this->ol_type=='pair'){
					$state = false;
					$mv['excelInfo']['error'] = '不存在与【厂商2、号码2】匹配的零件';
					$this->import->lineNum = $mv['excelInfo']['line'];
					$this->import->lastError = $mv['excelInfo']['error'];
					continue;
				}
				
                $data[$mv['ol_oe_id1'].'_'.$mv['ol_oe_id2']] = $mv;
				if($this->ol_type=='pair'){
					//配对的数据强制存两条
					$mv1 = [
						'ol_oe_id1'=>$mv['ol_oe_id2'],
						'ol_pf_id1'=>$mv['ol_pf_id2'],
						'ol_number1'=>$mv['ol_number2'],
						'ol_format1'=>$mv['ol_format2'],
					
						'ol_oe_id2'=>$mv['ol_oe_id1'],
						'ol_pf_id2'=>$mv['ol_pf_id1'],
						'ol_number2'=>$mv['ol_number1'],
						'ol_format2'=>$mv['ol_format1'],
						'ol_from'=>$mv['ol_from'],
					];
					$data[$mv1['ol_oe_id1'].'_'.$mv1['ol_oe_id2']] = $mv1;
				}
            }
            unset($mv);
		}
		//入库
		if($state && !empty($data))
		{
            $check = new check();
            $opuser = getOpuser();
			$datetime = getNowTime();

            $addAnyArr = [];
			$perAdd = 1000;
			$total = count($data);
			$this->db->Begin();

			$data = array_values($data);
			foreach($data as $k=>$mv)
			{
				if($k > 0 && $k%1000 === 0) errorLog(__CLASS__.$this->title.':'.$k, 'import');

				$set = [
                    'ol_type'=>$this->ol_type,
                    'ol_opuser'=>$opuser,
					'ol_update_time'=>$datetime
                ];
				foreach($mv as $mtk=>$mtv)
				{ 
					if(stripos($mtk, 'ol_') === 0) 
					{
						$set[$mtk] = $mtv;
					}
				}

                $exist = $check->checkExist('oe_link', $mv);
                if(!empty($exist))
                {
                    $this->db->update('oe_link', $set, ['ol_id'=>$exist]);
                }
                else
                {
                    $set['ol_create_time'] = $datetime;
                    $addAnyArr[] = $set;
                }
				if(count($addAnyArr) == $perAdd || ($k+1) == $total)
				{
					if(!empty($addAnyArr)) $this->db->insert('oe_link', $addAnyArr);
					$addAnyArr = [];
					$logArr = [
						'register'=>$k,
						'class'=>__CLASS__,
						'title'=>$this->title,
						'option'=>'importing'
					];
					errorLog($logArr, 'import');
				}
			}
			$this->db->End();
		}
        return $this->import->output(); //生成处理结果
	}
}