<?php
namespace model\import;
use \model;
use \model\check;

/**
 * 
 */
class series  extends model
{
	 /**
     * 标题 (用于消息提醒)
     */
	public $title = '小车型'; //固定
	public $import = null;    //固定
	public $rules = null;    //规则
	public $demoData = null;  //样例数据
	public $demoremark = null;  //样例说明
	public $areaArr = null;  
	public $carmodel = null;  
	
	
	public function __construct()
	{
		parent::__construct(); //执行父类
		$this->import = new \lib\import();
		$this->carmodel = new \model\carmodel();
		$this->areaArr = $this->carmodel->getArea();

		$this->rulesUpdate = [
		    ['key'=>'ms_id',          'caption'=>lg('ID'),'must'=>true],
			['key'=>'name',       'caption'=>lg('Modelrange'),'must'=>true,'update'=>true],
		    ['key'=>'region_c','caption'=>lg('Region C'),'formatCell'=>'coll25','update'=>true],
		];
		foreach($this->areaArr as $v)
		{
			$this->rulesUpdate[] = ['key'=>'sal_name_'.$v['ma_id'], 'caption'=>$v['name'].lg('别名'),'update'=>true];
		}
	}
	

	/**批量更新
     * @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))
		{
			$this->db->Begin();
			foreach($this->import->data as $k=>$mv)
			{
				$set = [];
				foreach($this->rulesUpdate as $fv)
				{
					if($fv['update'])
					{
						if(in_array($fv['key'], ['name', 'region_c']))
						{
							$set['base'][$fv['key']] = $mv[$fv['key']];
						}
						else if(stripos($fv['key'], 'sal_name_') == 0)
						{
							if(empty($set['areas'])) $set['areas'] = [];
							$maid = str_replace('sal_name_', '', $fv['key']);
							if(!empty($mv[$fv['key']]))
							{
								$set['areas'][] = [
									 "sal_ma_id" =>$maid,
									 "sal_ms_id" =>$mv['ms_id'],
									 "sal_name" => $mv[$fv['key']]
								];
							}
						}
					}
				}
				if(!empty($set))
					$this->carmodel->saveSeriesDetail($mv['ms_id'], $set);
			}
			$this->db->End();
		}
        return $this->import->output(); //生成处理结果
	}
}