<?php 
namespace model\import;

use model\smart\preview;
use model\smart\smart;
/**
 * 
 */
class markline  extends smart
{
	 /**
     * 标题 (用于消息提醒)
     */
	public $title = 'markline'; //固定
	public $import = null;    //固定
	public $rules = null;    //规则
	public $demoData = null;  //样例数据
	public $demoremark = null;  //样例说明
	private $dataUpdate='';
	private $years = null;
	public function __construct()
	{
		parent::__construct(); //执行父类
		$this->import = new \lib\import();
		$this->rules = [
			['key' => 'area', 'caption' => lg('国家/地区')],
            ['key'=>'group','caption'=>lg('集团')],
			['key' => 'brand', 'caption' => lg('整车厂/品牌')],
            ['key'=>'style','caption'=>lg('车种')],
            ['key'=>'level','caption'=>lg('级别')],
			['key' => 'model', 'caption' => lg('车型')],
            ['key' => 'totalVIO', 'caption' => lg('totalVIO')],
            // ['key'=>'power','caption'=>lg('动力总成')]
		];
        $this->years = range(2004,2025);
        foreach ($this->years as $val) {
            $this->rules[] = ['key' => ''.$val, 'caption' => ''.$val];
        }
		$this->demoData = [
			
		];
	}
	


	/**处理导入
     * @param array|string $src 数组或者文件名
     * @param isInit 是否是初始化
     * @return 可返回任意值(string|array|bool)，都会到前端
     */
    public function import($src,$isInit=false) //固定
    {
		$state = $this->import->execute($src, $this->rules);
	
		//入库
		if($state && !empty($this->import->data))
		{
			$this->db->Begin();
            $this->db->delete('smt_markline_base',[]);
            $areaArr =  array_column($this->import->data,'area');
            $countryArr = $this->db->select('copy_pdc_crm_country',['cc_id',LG_NAME],[LG_NAME=>$areaArr]);
            $countryArr = array_column($countryArr,'cc_id',LG_NAME);
            $arr = [];
			foreach ($this->import->data as $key => $val) {
                $key =  implode('@',[$val['area'],$val['brand'],$val['model']]);
                $mk_id =  $this->db->get('copy_pdc_m_make','mk_id',['name'=>$val['brand']]);
                if($mk_id){
                    $ms_id =  $this->db->get('copy_pdc_m_series','ms_id',['name'=>$val['model'],'ms_mk_id'=>$mk_id]);
                }
                if (!$arr[$key]) {
                    $arr[$key] = [
                        'area' => $val['area'],
                        'area_id' => (int)$countryArr[$val['area']],
                        'brand' => $val['brand'],
                        'make_id' => $mk_id ?: 0,
                        'model' => $val['model'],
                        'series_id' => $ms_id ?: 0,
                        'totalVIO' =>(int)$val['totalVIO'],
                        'opuser' => getOpuser(),
                        'create_time' => getNowTime(),
                        'update_time' => getNowTime(),
                        'group'=>$val['group']?[$val['group']]:[],
                        'style'=>$val['style']?[$val['style']]:[],
                        'level'=>$val['level']?[$val['level']]:[],
                        // 'power'=>$val['power']?[$val['power']]:[],
                        'year'=>[],
                    ];
                    
                } else {
                    $arr[$key]['totalVIO'] += (int)$val['totalVIO'];
                    $arr[$key]['group'][] = $val['group']??'';
                    $arr[$key]['style'][] = $val['style']??'';
                    $arr[$key]['level'][] = $val['level']??'';
                    // $arr[$key]['power'][] = $val['power']??'';
                }
                foreach ($this->years as $yval) {
                    if(!isset($arr[$key]['year'][$yval])){
                        $arr[$key]['year'][$yval] = 0;
                    }
                    $arr[$key]['year'][$yval] +=  (int)$val[$yval];
                } 
            }
            if($arr){
                foreach ($arr as $key => $val) {
                    $val['group'] = implode(',',array_filter(array_unique($val['group'])));
                    $val['style'] = implode(',',array_filter(array_unique($val['style'])));
                    $val['level'] = implode(',',array_filter(array_unique($val['level'])));
                    // $val['power'] = implode(',',array_filter(array_unique($val['power'])));
                    $val['year'] = json_encode($val['year']);
                    $this->db->insert('smt_markline_base',$val);
                }
            } 
			$this->db->End();
            $this->db->execute('CALL build_smt_markline_vio');
		}
        return $this->import->output(); //生成处理结果
	}

}