<?php
/*
 [
    'key'=>'rule1',                             //键名
    'caption'=>'编码规则',                      //字段名
    'must'=> true,                              //是否必须,默认false
    'mustCols'=> ['ruleA','ruleB']              //当此列存在时，必须有的其他列 ，默认空
    'minlen'=>0,                                //最小长度 默认0不判断
    'maxlen'=>50,                               //最大长度 默认0不判断
    'type'=>'number',                           //类型 number,string,date,datetime 默认 string
    //自定义检测
    'checkfunction'=>function($arr, $k, $v, $obj){
        $obj->lastError = '[line:'.$obj->lineNum.'] ['.$obj->rules[$k]['caption'].'] 自定义检测, 当前值为[ '.$v.' ]';
        return false;
    }
     //格式化单元格的值 [
        类, 
        类的创建参数, 默认[]
        方法, 
        参数,  ['@innum','@outnum',123,true] @表示使用导入数据中的列中的值 ,'@'表示自己 
        如果结果是数组指定返回哪个键 默认null
     ]
    'formatCell'=> [\model\collocation::class,'getRowByKey',['@'],null],   或者 config/import.php 中的配置
],
 */
 
namespace model\import;
use \model;
use \model\tags;
use \model\check;

/**
 * 
 */
class code2  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'=>'pc_id',        'caption'=>lg('GSP型号'),  'must'=>true,'formatCell'=>'pc_id'],
			 ['key'=>'sale_coun',         'caption'=>lg('适用国家-自定义'),'must'=>true,'formatCell'=>'sale_coun','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))
		{
			$check = new \model\check();
			$proCateRule = $check->checkProRuleCate(array_column($this->import->data, 'pc_id'), 'B100', 'import');
			foreach($this->import->data as &$dv)
			{
				if(in_array($dv['pn_p_id'], $proCateRule['no']))
				{
					$state = false;
					$dv['excelInfo']['error'] = '无此品名权限';
					$this->import->lineNum = $dv['excelInfo']['line'];
					$this->import->lastError = $dv['excelInfo']['error'];
					continue;
				}
			}
		}

		//入库
		if($state && !empty($this->import->data))
		{
			$country = new \model\country();
			$opuser = getOpuser();
			$datetime = getNowTime();
			$this->db->Begin();
			foreach($this->import->data as $k=>$mv)
			{
				$country->changeLink('code', 'replace', [$mv['pc_id']], $mv['sale_coun']? : []);
			}
			$this->db->End();
		}
        return $this->import->output(); //生成处理结果
	}
	
	
}