<?php

/**
 * +----------------------------------------------------------------------
 * | Created By PhpStorm
 * +----------------------------------------------------------------------
 * | DATE: 2021/7/13
 * +----------------------------------------------------------------------
 * | TIME: 16:06
 * +----------------------------------------------------------------------
 * | Author: YangXing(925032253@qq.com)
 * +----------------------------------------------------------------------
 * | Class: Read
 * +----------------------------------------------------------------------
 */

namespace servers\excel;

use servers\excel\excel;
use Vtiful\Kernel\Excel as xlsx;

/**
 * Class read
 * @package servers\Excel
 */
class read extends excel
{
    /**
     * @var array
     */
    private $config = [];

    /**
     * 文件名称
     * @var string
     */
    protected $fileName = '';

    /**
     * excel 操作类
     * @var object
     */
    protected $excel = null;

    /**
     * read constructor.
     * @param $fileName
     * @param $maxCount
     * @param callable $callback
     */
    public function __construct($fileName,$maxCount,Callable $callback)
    {
        $this->fileName = $fileName;

        $this->config['path'] = $this->writePath();

        $this->excel = new xlsx($this->config);

        $firstLine = [];

        $count = 0;

        $sheetList = $this->excel->openFile($this->fileName)->sheetList();

        foreach ($sheetList as $sheetName)
        {
            // 通过工作表名称获取工作表数据
            $this->excel->openSheet($sheetName);

            while (($row = $this->excel->nextRow()) !== NULL)
            {
                $count++;

                if ($count == 1)
                {
                    $firstLine = $row;
                    continue;
                }
                $data[] = $row;

                if ($count % $maxCount == 0)
                {
                    call_user_func($callback,$data,$firstLine,$count);
                    $data = [];
                }
            }
        }
        call_user_func($callback,$data,$firstLine,$count);
        $data = [];
        return true;
    }

    public function getErrorList()
    {
        return $this->errorList;
    }


}
