ImportController.class.php 1.31 KB
<?php
/**
 * 【后台】解析员工激励导入模板接口
 * ImportController.class.php
 * @author:daijun
 * @date:2017-08-31
 */

namespace Apicp\Controller\Incentives;

use Com\PythonExcel;
use Common\Service\IncentivesService;

class ImportController extends \Apicp\Controller\AbstractController
{
    /**
     * 解析员工激励导入模板接口
     * @author daijun
     */
    public function Index_post()
    {
        // 获取上传文件详情
        $file = $_FILES['file'];

        // 获取培训ID
        $ed_id = I('post.ed_id');

        $incentives_serv = new IncentivesService();

        // 验证数据
        if (!$incentives_serv->xls_validation($ed_id, $file)) {

            return false;
        }

        // xls文件名称
        $filename = $file['tmp_name'];

        $data = PythonExcel::instance()->read($filename, 0);

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

            E('_EMPTY_XLS_DATA');
        }

        // 循环组装返回数据
        $res_data = $incentives_serv->format_excel_list($data);

        if (!$res_data) {
            return false;
        }

        // 返回给前端的数据结构
        $this->_result = [
            'head' => $res_data['heads'],
            'list' => array_values($res_data['list']),
        ];

        return true;
    }
}