ExcelResolveController.class.php 4.34 KB
<?php
/**
 *【线下培训-后台】解析人员导入模板
 * @author: houyingcai
 * @email:     594609175@qq.com
 * @date :  2017-08-29 15:24:15
 * @version $Id$
 */

namespace Apicp\Controller\Paper;

use Com\PythonExcel;

class ExcelResolveController extends \Apicp\Controller\AbstractController
{
    // 表头最小长度
    const HEADER_MIN_LENGTH = 2;

    // 模版数据记录数
    const TPL_LEN = 3;

    // 标题长度
    const TITLE_LENGTH = 2;

    // 最大长度
    const HEADER_MAX_LENGTH = 3;

    // 初始值
    const LEN = 0;

    // 列表名称
    protected $__fieldName = [
        [
            'key' => 'username',
            'name' => '员工姓名',
        ],
        [
            'key' => 'mobile',
            'name' => '手机号码',
        ]
    ];

    public function Index_post()
    {
        // 获取上传文件详情
        $file = $_FILES['file'];

        // 验证数据
        if (!$this->xls_validation($_FILES)) {

            return false;
        }

        // xls文件名称
        $filename = $file['tmp_name'];
        $data = PythonExcel::instance()->read($filename, 0);

        // 获取列表
        list($data, $title, $total, $headTotal) = $this->get_excel_list($data);

        // 如果数据为空
        if (empty($data)) {

            E('_EMPTY_XLS_DATA');
        }

        //返回值
        $this->_result = [
            'total' => $total,
            'head_total' => $headTotal,
            'head' => $title,
            'list' => $data,
        ];

        return true;
    }

    /**
     * 验证数据
     *
     * @param array $file POST数据
     *
     * @return bool
     */
    protected function xls_validation($file = [])
    {
        // 请上传文件
        if (empty($file)) {

            E('_ERR_FILE_UNDEFINED');

        }

        // 文件上传失败
        if ($file['error'] > 0) {

            E('_ERR_UPLOAD_FILE');

        }

        // 文件大小判断(2M以内)
        if ($file['size'] > 1024 * 2 * 1000) {

            E('_ERR_FILE_SIZE');
        }

        return true;
    }

    /**
     * 获取head数据列表以及整合过的xls中数据列表
     *
     * @author houyingcai
     * @param array $list xls数据列表
     *
     * @return array
     */

    public function get_excel_list($list = [])
    {
        // 初始化返回值
        $data = [];

        // 初始化
        $i = 0;

        // 存储标题长度
        $storage = [];

        // 循环数据
        foreach ($list as $v) {

            // 截取前三条数据
            if ($i < self::TPL_LEN) {
                $i++;
                continue;
            }
            // 姓名和手机号都不能为空
            if (empty($v[0]) && empty($v[1])) {

                continue;
            }

            // 赋值
            $data[] = [
                'username' => $v[0],
                'mobile' => (string)$v[1],
            ];

            // 存储所有表格长度
            $storage[] = $this->countHead($v);
        }

        // 表格题目数组
        $title = [];

        // 初始化表头最小长度
        $min = self::HEADER_MIN_LENGTH;

        // 设置最大表头长度
        if (max($storage) < $min) {

            $max_title = $min;
        } else {

            $max_title = max($storage);
        }

        // 重组表格标题
        for ($i = 0; $i < $max_title; $i++) {

            // 标题表格列表
            $title[$i] = $this->__fieldName[$i];

            // 如果是最大值
            if ($max_title - self::LEN == $i) {

                // 新增是否导入成功
                $title[$i + self::LEN] = $this->__fieldName[self::HEADER_MAX_LENGTH];

            }
        }
        $data = array_filter($data);
        $title = array_filter($title);
        return [$data, $title, count($data), count($title) - self::LEN];
    }

    /**
     * 获取标题长度
     *
     * @author houyingcai
     * @param array $list 列表数据
     *
     * @return int|string
     */

    private function countHead($list)
    {
        // 实例化
        $data = 0;

        // 循环数据
        for ($i = self::TITLE_LENGTH; $i > 0; $i--) {

            // 判断非空
            if (!empty($list[$i])) {

                // 赋值
                $data = $i + 1;

                break;
            }

        }

        return $data;
    }
}