<?php 
namespace model\import;

use model\smart\preview;
use model\smart\smart;
/**
 * 
 */
class reportCustom  extends smart
{
	 /**
     * 标题 (用于消息提醒)
     */
	public $title = '自定义型号清单'; //固定
	public $import = null;    //固定
	public $rules = null;    //规则
	public $demoData = null;  //样例数据
	public $demoremark = null;  //样例说明
	private $dataUpdate='';
	private $dataTitle = '';
	private $mainDb = null;
	public function __construct()
	{
		parent::__construct(); //执行父类
		$this->import = new \lib\import();
		$this->mainDb = new \lib\db(\mvc::$cfg['db']['main']);
		$must = false;
		$hasUserIds=$this->getManageUsers();
		if($hasUserIds){
			$must = true;
		}

		$this->rules = [
			['key' => 'smt_report_pid', 'caption' => lg('GSP型号'), 'formatCell' => 'pc_id', 'must' => true],
			['key' => 'smt_report_cid', 'caption' => lg('客户名称'), 'formatCell' => 'getCid','must'=>$must],
			['key' => 'smt_report_area', 'caption' => lg('区域'), 'formatCell' => 'getArea','must' => true],
		];

		$this->demoData = [
			
		];

		$this->demoremark = '';
        $this->dataUpdate = strtotime(urldecode(\mvc::$req['time']));
		if(empty($this->dataUpdate) && !empty(\mvc::$req['params'])){
            $params =json_decode(\mvc::$req['params'],true);
            $this->dataUpdate =strtotime(urldecode($params['time']));
        }
		//标题
		$this->dataTitle = urldecode(\mvc::$req['title']);
		if (empty($this->dataTitle) && !empty(\mvc::$req['params'])) {
			$params = json_decode(\mvc::$req['params'], true);
			$this->dataTitle = urldecode($params['title']);
		}
	}
	


	/**处理导入
     * @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))
		{
			
		}
		//入库
		if($state && !empty($this->import->data))
		{
			$this->db->Begin();
			if(!$this->dataTitle){
			    return;
			}
			$sr_id = $this->db->get('smt_rules_custom','sr_id',['sr_title'=>$this->dataTitle,'opuser'=>getOpuser()]);
			$srdata =[
				'sr_title' => $this->dataTitle,    
				'opuser' => getOpUser(),
				'sr_cols'=>json_encode(["smt_report_id","pro_rule","NewSAPCode","AccountNumber","Name","smt_p_state_main","pro_bigcate","pro_cate","smt_p_outnum","smt_p_innum","smt_ctime","modelText","smt_year_from","pro_sales_qty","pro_vio","total_quantity_vehicles","opuser","update_time"])
			];
			if($this->dataUpdate){
				$srdata['update_time'] = $this->dataUpdate?date('Y-m-d H:i:s',$this->dataUpdate):getNowTime();
			}
			if(!$sr_id){
				$this->db->insert('smt_rules_custom',$srdata);
				$sr_id = $this->db->id();
			}else{
				$this->db->update('smt_rules_custom',$srdata,['sr_id'=>$sr_id]);
			}
			
            $preview = new preview();
			$ver = 	$preview->newVer();
			$tempArr = [];
			foreach($this->import->data as $k=> &$mv)
			{
				$where = ['pc_id'=>$mv['smt_report_pid']];
				$smt_report_outnum = $this->mainDb->get('p_code',"pc_outnum",$where);
				
				if($mv['smt_report_cid'] === false){
					$mv['excelInfo']['error'] = '[客户名称]系统未找到匹配项';
					$this->import->lineNum = $mv['excelInfo']['line'];
					$this->import->lastError = $mv['excelInfo']['error'];
					break;
				}
				
				if(empty($mv['smt_report_pid'])){
					$mv['excelInfo']['error'] = '[GSP型号]系统未找到匹配项';
					$this->import->lineNum = $mv['excelInfo']['line'];
					$this->import->lastError = $mv['excelInfo']['error'];
					break;
				}
				$set = [
					'smt_report_ver'=>$ver,
					'smt_report_pid'=>$mv['smt_report_pid'],
					'smt_report_outnum'=>$smt_report_outnum,
					'smt_report_time'=>$this->dataUpdate?date('Y-m-d H:i:s',$this->dataUpdate):getNowTime(),
					'smt_report_user'=>getOpUser(),
					'smt_report_sr_id'=>$sr_id
				];
				if (!empty($mv['smt_report_area']) && !empty($mv['smt_report_cid'])) {
					foreach ($mv['smt_report_area'] as $area) {
						foreach ($mv['smt_report_cid'] as $cid) {
							$set['smt_report_cid'] = $cid;
							$set['smt_report_area'] = $area;
							$tempArr[] = $set;
						}
					}
				}else{
					if (!empty($mv['smt_report_area'])) {
						foreach ($mv['smt_report_area'] as $area) {
							$set['smt_report_area'] = $area;
							$tempArr[] = $set;
						}
					}
					if (!empty($mv['smt_report_cid'])) {
						foreach ($mv['smt_report_cid'] as $cid) {
							$set['smt_report_cid'] = $cid;
							$tempArr[] = $set;
						}
					}
				}
			}
			if($tempArr){
				foreach ($tempArr as $val) {
					$this->db->insert('smt_report_custom', $val);
				}
			}
			
			$this->db->End();
		}
        return $this->import->output(); //生成处理结果
	}

}