<?php 
namespace model\import;
use \model;

use model\oe as oeOeModel;
/**
 * 
 */
class oe  extends model
{
	 /**
     * 标题 (用于消息提醒)
     */
	public $title = '零件信息'; //固定
	public $import = null;    //固定
	public $rules = null;    //规则
	public $demoData = null;  //样例数据
	public $demoremark = null;  //样例说明
	
	public function __construct()
	{
		parent::__construct(); //执行父类
		$this->import = new \lib\import();
		
		$this->rulesUpdate = [
            ['key'=>'oe_id',        'caption'=>lg('零件ID'),  'must'=>true],
            ['key'=>'oe_cate_id','caption'=>lg('小类'),'formatCell'=>'cate','update'=>true],
           

		];
        foreach(\mvc::$cfg['LANGS'] as $lk=>$lv){
            if($lk=='cn'){
                $this->rulesUpdate[]  = ['key'=>'oe_'.$lv['field'],        'caption'=>lg('产品名称'),'update'=>true];
            }else{
                $this->rulesUpdate[]  = ['key'=>'oe_'.$lv['field'],        'caption'=>lg($lv['name'].'名称'),'update'=>true];
            }
        }
        $this->rulesUpdate[]  = ['key'=>'oe_amount','caption'=>lg('装配数量'),'update'=>true];
        foreach(\mvc::$cfg['LANGS'] as $lk=>$lv){
            $this->rulesUpdate[]  = ['key'=>'oe_model_'.$lv['field'],        'caption'=>lg($lv['name'].'车型'), 'update'=>true];
        }
        
        $this->rulesUpdate[]  = ['key'=>'oe_chassis','caption'=>lg('底盘'),'update'=>true];
        $this->rulesUpdate[]  = ['key'=>'oe_model_year','caption'=>lg('年限'),'update'=>true];
		
        foreach(\mvc::$cfg['LANGS'] as $lk=>$lv){
            $this->rulesUpdate[]  = ['key'=>'oe_des_'.$lv['field'],        'caption'=>lg($lv['name'].'描述'), 'update'=>true];
        }

        $this->rulesUpdate[]  = ['key'=>'oe_develop_status','caption'=>lg('开发状态'),'formatCell'=>[\model\collocation::class,[],'getOidByName',['@', 17],null],'update'=>true];
        $this->rulesUpdate[]  = ['key'=>'devNoList','caption'=>lg('开发号'),'update'=>true];
        $this->demoData = [
			
		];

		$this->demoremark = '';
	}
	
	
	/**批量更新
     * @param array|string $src 数组或者文件名
     * @return 可返回任意值(string|array|bool)，都会到前端
     */
    public function importUpdate($src) //固定
    {
		$state = $this->import->execute($src, $this->rulesUpdate);
		//数据重复验证
		if($state && !empty($this->import->data))
		{
            $supplierRes = $this->db->select('supplier',['sup_id','sup_name']);
            $supplierArr = array_column($supplierRes,'sup_id','sup_name');

            $prostatus = array_flip(\mvc::$cfg['PROSTATUS']);
			foreach($this->import->data as &$dv)
			{
				$oe_id = $this->db->get('oe_item',"oe_id",['oe_id'=>$dv['oe_id']]);
                if(!$oe_id)
				{
					$state = false;
					$dv['excelInfo']['error'] = '记录不存在';
					$this->import->lineNum = $dv['excelInfo']['line'];
					$this->import->lastError = $dv['excelInfo']['error'];
					continue;
				}
                $dv['oe_id'] = $oe_id;
               
                //研发号
                $dv['devno'] = [];
                if($dv['devNoList']){
                    $devNum = explode("\n",$dv['devNoList']);
                    $i =0;
                    foreach ($devNum as $dval) {
                       
                        $pattern = '/^(.*)\((.*)\)$/';
                        if(preg_match($pattern, $dval, $matches))
                        {
                            $temp = $matches;
                            $oid_sup_id = 0;
                            if($temp[2]){
                                $oid_sup_id = $supplierArr[$temp[2]];
                            }
                            $exist = $this->db->get('oe_item_developno','oid_id',['oid_oe_id'=>$oe_id,'oid_format'=>formatNum($temp[1]),'oid_sup_id'=>$oid_sup_id]);
                            $dv['devno'][] = [
                                'oid_id'=>$exist?$exist:--$i,
                                'oid_sup_id'=>$oid_sup_id,
                                'oid_number'=>$temp[1],
                                'oid_format'=>formatNum($temp[1]),
                                'oid_oe_id'=>$oe_id
                            ];
                        }
                        
                    }
                }
                unset($dv['devNoList']);
			}
		}

		//入库
		if($state && !empty($this->import->data))
		{
			$opuser = getOpuser();
			$datetime = getNowTime();
			$this->db->Begin();
            $oeOeModel = new oeOeModel();
			foreach($this->import->data as $k=>$mv)
			{
				$set = [
					'oe_opuser'=>$opuser,	
					'oe_update_time'=>$datetime,
				];
				foreach($mv as $kk=>$vv){
					if(stripos($kk, 'oe_') === 0){
						$set[$kk] = $vv;
					}
				}
                $oe_id = $set['oe_id'];
                unset($set['oe_id']);
                $this->db->update('oe_item', $set, ['oe_id'=>$oe_id]);
                $oeOeModel->saveDevelopNo($oe_id,$mv['devno']);
			}
			$this->db->End();
		}
        return $this->import->output(); //生成处理结果
	}
	
	
}