<?php 
namespace model\import;
use \model;
use \model\check;

class compare extends model
{
	 /**
     * 标题 (用于消息提醒)
     */
	public $title = '数据比对'; //固定
	public $import = null;    //固定
	public $rules = null;    //规则
	public $demoData = null;  //样例数据
	public $demoremark = null;  //样例说明
	public $rulesUpdate = null;    //更新规则
	
	public function __construct()
	{
		parent::__construct(); //执行父类
		$this->import = new \lib\import();

		$this->rules = [
            ['key' => 'artno',          'caption' => 'ArtNo'],
            ['key' => 'brandno',         'caption' => 'BrandNo'],
            ['key' => 'manno',        'caption' => 'ManNo'],
            ['key' => 'refno',         'caption' => 'RefNo'],
            ['key' => 'lnktargettype',             'caption' => 'LnkTargetType'],
            ['key' => 'lnktargetno',           'caption' => 'LnkTargetNo'],
	
		];

		// $this->rulesUpdate = [
		//      ['key'=>'pn_id',          'caption'=>lg('ID'),'must'=>true],
		//      ['key'=>'pn_p_id',          'caption'=>lg('GSP型号'),'must'=>true,'formatCell'=>'p_idBygspncv'],
		// 	 ['key'=>'pn_pf_id',         'caption'=>lg('号码品牌'),'formatCell'=>'pf_id'],
		//      ['key'=>'pn_number',        'caption'=>lg('号码'),'minlen'=>3,'maxlen'=>50],
		//      ['key'=>'pn_mk_id',         'caption'=>lg('OE品牌'),'formatCell'=>'mk_ids','update'=>true],
		//      ['key'=>'pn_src',           'caption'=>lg('数据来源'),'formatCell'=>'coll8','update'=>true],
		//      ['key'=>'pn_cn_remark',     'caption'=>lg('中文描述'),'maxlen'=>512,'update'=>true],
		//      ['key'=>'pn_en_remark',     'caption'=>lg('英文描述'),'maxlen'=>512,'update'=>true],
		//      ['key'=>'pn_ref',           'caption'=>lg('参考来源'),'maxlen'=>512,'update'=>true],
		//      ['key'=>'ckresult2',        'caption'=>lg('复审结论'),'must'=>true,'formatCell'=>'coll24','update'=>true]
		// ];

		$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);
        $com_type = \mvc::$req['com_type'];
		if(empty($com_type) && !empty(\mvc::$req['params'])){
			$params =json_decode(\mvc::$req['params'],true);
			$com_type = $params['com_type'];
		}
		//数据重复验证 - 自己
		if($state && !empty($this->import->data))
		{
			$state = !$this->import->checkRepeat([
			    ['artno', 'brandno','manno','refno','lnktargettype','lnktargetno']
		    ]);
		}
		//数据重复验证
		if($state && !empty($this->import->data))
		{
			$check = new check();
			$this->db->Begin();
			foreach($this->import->data as $k =>$dv)
			{	
				if($k > 0 && $k%1000 === 0) errorLog(__CLASS__.$this->title.':'.$k, 'import');
				$arr = [];
				$updateData = [];
				$add  = [];
                $dv['com_type'] = $com_type;
				foreach($dv as $dvk=>$dvv)
				{
					if(in_array($dvk, ['artno', 'brandno','manno','refno','lnktargettype','lnktargetno','com_type'])) 
					{
						$arr[$dvk] = $dvv;
					}
				}
                $exist =   $this->db->get('compare','com_id',$arr);
				$arr['opuser'] = getOpuser();
				//重复更新
				if(!empty($exist))
				{	
					$where = ['com_id'=>$exist];
					$updateData = $arr;
					$updateData['update_time'] = getNowTime();
					$this->db->update('compare',$updateData,$where);
				}
				else
				{	
					$add = $arr;
					$add['create_time'] = getNowTime();
					$add['update_time'] = getNowTime();
					$this->db->insert('compare', $add);
				}
			}
			$this->db->End();
		}
        return $this->import->output(); //生成处理结果
	}
}

?>