ExportDataController.class.php 4.67 KB
<?php
/**
 * Created by PhpStorm.
 * User: liyifei2012it
 * Date: 17/9/13
 * Time: 10:18
 */
namespace Frontend\Controller\Index;

use Com\PythonExcel;
use Common\Common\User;
use Common\Common\Integral;
use Common\Service\UserAwardService;

class ExportDataController extends AbstractController
{
    protected $_require_login = false;

    /**
     * 获得勋章的人员数据
     * @author liyifei
     */
    public function Index()
    {
        // 返回数据
        $rows = [];

        // 时间段
        $startTime = 0;
        $endTime = rstrtotime('2017-09-13 12:00', 1);

        // 勋章及获得人员列表
        $awardServ = new UserAwardService();
        $awardList = $awardServ->getMedalList($startTime, $endTime);

        if (!empty($awardList)) {

            $uids = array_unique(array_column($awardList, 'uid'));
            $medalIds = array_unique(array_column($awardList, 'medal_id'));

            // 人员列表
            $userServ = &User::instance();
            $userList = $userServ->listByUid($uids);

            // 勋章列表
            $medalServ = &Integral::instance();
            $medalList = $medalServ->listMedal($medalIds);
            if (!empty($medalList)) {

                $medalList = array_combine_by_key($medalList, 'im_id');
            }

            // 组合人员、勋章数据
            foreach ($awardList as $award) {

                $uid = $award['uid'];
                $medalId = $award['medal_id'];

                // 组织(部门)信息
                $dpName = '';
                $dpList = $userList[$uid]['dpName'];
                if (!empty($dpList)) {

                    $dpNames = array_column($dpList, 'dpName');
                    $dpName = implode(',', $dpNames);
                }

                $rows[] = [
                    $uid,
                    $userList[$uid]['memUsername'] ? $userList[$uid]['memUsername'] : $award['username'],
                    $userList[$uid]['custom1'] ? $userList[$uid]['custom1'] : '人员已删除',
                    $userList[$uid]['memMobile'] ? $userList[$uid]['memMobile'] : '人员已删除',
                    $dpName ? $dpName : '人员已删除',
                    $medalList[$medalId]['im_id'],
                    $medalList[$medalId]['name'],
                ];
            }
        }

        if (empty($rows)) {

            exit('暂无数据');
        }

        // Excel文件名
        $fileName = '获得勋章列表_' . date('YmdHi') . '.xls';
        // Excel首行标题
        $titles = ['人员UID', '姓名', '工号', '手机', '组织', '勋章ID', '勋章'];

        // $this->_download($fileName, $titles, $rows);
        $this->_download_by_python($fileName, $titles, $rows);
    }

    /**
     * 导出到Excel
     * @author liyifei
     * @param string $fileName 文件名
     * @param array $titles 标题
     * @param array $rows 数据列表
     */
    private function _download($fileName, $titles, $rows)
    {
        $widths = array(35, 10, 15, 15, 50, 10, 15);
        $objExcelPhp = \Com\Excel::instance();
        $objExcelPhp->make_excel_download($fileName, $titles, $widths, $rows);
        exit;
    }


    /**
     * 通过python导出数据
     * @param string $fileName 文件名
     * @param array $titles 标题
     * @param array $rows 数据列表
     */
    private function _download_by_python($fileName, $titles, $rows)
    {
        // 生成 Excel 并输出
        PythonExcel::instance()->write(get_sitedir() . $fileName, $titles, $rows);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");

        // 根据浏览器类型来判断是否需要特殊处理中文字符
        $encoded_filename = urlencode($fileName);
        $encoded_filename = str_replace("+", "%20", $encoded_filename);
        $ua = $_SERVER["HTTP_USER_AGENT"];
        if (preg_match("/MSIE/", $ua) || preg_match("/rv:11.0/", $ua) || preg_match("/Edge/", $ua)) {

            header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
        } else if (preg_match("/Firefox/", $ua)) {

            header('Content-Disposition: attachment; filename*="utf8\'\'' . $fileName . '"');
        } else {

            header('Content-Disposition: attachment; filename="' . $fileName . '"');
        }

        header("Content-Transfer-Encoding:binary");
        echo file_get_contents(get_sitedir() . $fileName);
        exit;
    }
}