<?php
namespace model\import;
use \model;
use \model\check;
use model\pro\cate as ProCate;

class cate  extends model
{
	 /**
     * 标题 (用于消息提醒)
     */
	public $title = '小类'; //固定
	public $import = null;    //固定
	public $rules = null;    //规则
	public $demoData = null;  //样例数据
	public $demoremark = null;  //样例说明
	
	public function __construct()
	{
		parent::__construct(); //执行父类
		$this->import = new \lib\import();
        
        $this->rulesUpdate = [
			['key'=>'id',       'caption'=>lg('小类ID'),'must'=>true],
		];
		//更新
        foreach(\mvc::$cfg['LANGS'] as $lk=>$lv){
            $must = false;
            if(in_array($lk, ['cn','en'])){
                $must = true;
            }
            $this->rulesUpdate[] = ['key'=>$lv['field'],       'caption'=>lg($lv['name'].'名称'),'update'=>true];
        }
        $this->rulesUpdate[] =  ['key'=>'cat_gpc',       'caption'=>lg('GPC分类代码'),'update'=>true];
        $this->rulesUpdate[] =  ['key'=>'prl_code_a',       'caption'=>lg('编码规则'),'update'=>true];
        $this->rulesUpdate[] =  ['key'=>'is_engine',       'caption'=>lg('发动机相关'),'update'=>true];
        $this->rulesUpdate[] =  ['key'=>'tcd_name',       'caption'=>lg('TECDOC名称'),'update'=>true];
        $this->rulesUpdate[] =  ['key'=>'cat_rate_number',       'caption'=>lg('号码确认率'),'update'=>true];
        $this->rulesUpdate[] =  ['key'=>'cat_rate_model',       'caption'=>lg('车型确认率'),'update'=>true];
        $this->rulesUpdate[] =  ['key'=>'cat_pmanager',       'caption'=>lg('产品经理'),'update'=>true];
        $this->rulesUpdate[] =  ['key'=>'cat_procurement',       'caption'=>lg('采购项目经理'),'update'=>true];

		$this->demoData = [
			
		];

		$this->demoremark = '';
	}
	
	

	/**批量更新
     * @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))
		{   
            $unikey = ['id','cat_gpc','prl_code_a','is_engine','tcd_name','cat_rate_number','cat_rate_model'];
            foreach(\mvc::$cfg['LANGS'] as $lk=>$lv){
                $unikey[] = $lk;
            }
		
			$state = !$this->import->checkRepeat([
			    $unikey
		    ]);

            foreach ($this->import->data as $key => &$item) {
                if(isset($item['is_engine']))
				{
					if(in_array($item['is_engine'], ['是', 'Y','1'])){
                        $item['is_engine'] = 1;
                    }else{
                        $item['is_engine'] = 0; 
                    }
				}
                if(isset($item['cat_pmanager']))
				{
					$cat_pmanager  = $this->db->get(
					    'adm_role',
					    'ar_id',
					    ['ar_name'=>$item['cat_pmanager'],'ar_ispmanager'=>1]
					 );
                     $item['cat_pmanager'] = $cat_pmanager??0;
				}
                if(isset($item['cat_procurement']))
				{
					$cat_procurement = $this->db->get(
					    'adm_role',
					    'ar_id',
					    ['ar_name'=>$item['cat_procurement'],'ar_isprocurement'=>1]
					 );
                     $item['cat_procurement'] = $cat_procurement??0;
				}
            }
            unset($item);
		}


		//入库
		if($state && !empty($this->import->data))
		{   
            $opuser = getOpuser();
            $time = getNowTime();
			$this->db->Begin();
			foreach($this->import->data as $k=>$mv)
			{   
                $findId = $this->db->get(
			        'pro_cate',
			        'id',
			        ['id'=>$mv['id']]
			     );
                if($findId){
                    $set = [];
                    foreach($this->rulesUpdate as $rule){
                        if(isset($mv[$rule['key']])&&$mv[$rule['key']]!=='!' && $mv[$rule['key']]!=='！' && isset($rule['update']) && $rule['update']){
                            $set[$rule['key']] = $mv[$rule['key']];
                        }
                    }
                    $set['cat_opuser'] = $opuser;
                    $set['update_time'] = $time;
                    $prl_code_a = $set['prl_code_a'];
                    unset($set['prl_code_a']);
                    $this->db->update('pro_cate', $set, ['id'=>$findId]);
                    //更新编码
                    $updata = [
                        'prl_code_a' => $prl_code_a,
                        'prl_opuser'=>$opuser,
                        'prl_update_time'=>$time
                    ];
                    $this->db->update('pro_rule_link', $updata, ['prl_type' => 'cate', 'prl_val_id' => $findId, 'prl_pb_id' => 1]);
                }
                
			}
			$this->db->End();
		}
        return $this->import->output(); //生成处理结果
	}
}