<?php 
namespace model\import;
/*
 [
    '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 中的配置
],
 */
 
class dt_data 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->rules = [
			['key' => 'dcid', 'caption' => lg('项目'), 'formatCell' => 'ccate', 'must' => true],
			['key' => 'duid', 'caption' => lg('姓名'), 'formatCell' => 'auser', 'must' => true],
			['key' => 'ddate', 'caption' => lg('日期'), 'type'=>'date', 'must'=>true],
			['key' => 'dhour', 'caption' => lg('小时'), 'type'=>'number','minlen'=>1,'maxlen'=>2],
			['key' => 'dvalue', 'caption' => lg('数值'), 'type'=>'number', 'must'=>true],
			['key' => 'dremark', 'caption' => lg('备注'), 'maxlen'=>255],
		];

		$this->demoData = [
			
		];

		$this->demoremark = '';
	}
	


	/**处理导入
     * @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))
		{
            $blood = new \model\doctor\blood();
			$this->db->Begin();
			foreach($this->import->data as $k=>$row)
			{
                $newRow = $row;
                unset($newRow['excelInfo']);
				$this->db->insert('dt_data', $newRow);
			}
			$this->db->End();
		}
        return $this->import->output(); //生成处理结果
	}
}