<?php
namespace model\import;

use lib\excel\excel;
use model\smart\smart;

/**
 * 导入投诉报告处理类
 * @package model
 */
class uploadImportMarkline extends smart
{
    public $cacheIds;
	public function __construct()
	{
		ini_set('memory_limit', \mvc::$cfg['upload']['memory_limit']);
		set_time_limit(0);
		ignore_user_abort(true);
		parent::__construct();
	    $this->cacheIds = [];
	}


    /**检测上传
     * @param 
     * @return
     */
	public function uploadCheck($filecount,$reqfilename,$reqfilesize,$uploadID,$extname,$category,$item,$params)
	{
        $cfg = \mvc::$cfg['upload']['category'][$category]['items'][$item];
		if(!in_array($extname,$cfg['type'])){
		    return lg('仅支持'). implode(',',$cfg['type']);
		}
		return true;
	}


    /**接收上传的图片处理
     * @param 
     * @return
     */
	public function uploadGet($fileid,$filename,$dir,$filebase,$extname,$category,$item,$params)
	{
        include(\mvc::$cfg['mvcDir'].'/lib/excel/excelInterface.php');
        include(\mvc::$cfg['mvcDir'].'/lib/excel/excel.php');
        include(\mvc::$cfg['mvcDir'].'/lib/excel/excelBase.php');
        include(\mvc::$cfg['mvcDir'].'/lib/excel/excelXlsx.php');
        $excel = new \lib\excel\excel('xlsx');
      
        $excel->setDirFile($filename);
        $data =   $excel->toArray();
        $header = $data[0];
        unset($data[0]);
        $yearsArr = []; 
        $this->db->Begin();
        $this->db->delete('smt_markline_base',[]);
        $areaArr = [];
        foreach ($data as $val) {
            $areaArr[] = $val[0];
        }
        $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 ($data as $key => $val) {
            $key =  implode('@',[$val[0],$val[2],$val[5]]);
            $mk_id =  $this->db->get('copy_pdc_m_make','mk_id',['name'=>$val[2]]);
            if($mk_id){
                $ms_id =  $this->db->get('copy_pdc_m_series','ms_id',['name'=>$val[5],'ms_mk_id'=>$mk_id]);
            }
            if (!$arr[$key]) {
                $arr[$key] = [
                    'area' => $val[0],
                    'area_id' => (int)$countryArr[$val[0]],
                    'brand' => $val[2],
                    'make_id' => $mk_id ?: 0,
                    'model' => $val[5],
                    'series_id' => $ms_id ?: 0,
                    'totalVIO' =>(int)$val[7],
                    'opuser' => getOpuser(),
                    'create_time' => getNowTime(),
                    'update_time' => getNowTime()
                ];
            } else {
                $arr[$key]['totalVIO'] += (int)$val[7];
            }
        }
        if($arr){
            foreach ($arr as $key => $val) {
                $this->db->insert('smt_markline_base',$val);
            }
        } 
        $this->db->End();
        $this->db->execute('CALL build_smt_markline_vio');
	}

}