IndexController.class.php 4.08 KB
<?php
/*/**
 * 【员圈直播】导出直播情况
 * @author: houyingcai
 * @email:  594609175@qq.com
 * @date :  2017-06-01 09:41:09
 * @version $Id$
 */

namespace Apicp\Controller\Export;


use Common\Common\Constant;
use Common\Common\ExportDownload;
use Common\Service\MainService;
use Common\Service\ParticipateService;
use Common\Service\RangeService;

use Com\PythonExcel;


class IndexController extends \Apicp\Controller\AbstractController
{


    public function before_action($action = '')
    {
        if (!parent::before_action($action)) {
            return false;
        }
        return true;
    }

    public function Index()
    {
        $lm_id = I('lm_id');
        $watch_type = I('watch_type');
        if (empty($lm_id)) {
            E('_EMPTY_DOWN__LIVE_ID');
        }
        if (!in_array($watch_type, array(Constant::LIVE_WATCH_TYPE_FALSE, Constant::LIVE_WATCH_TYPE_TRUE))) {
            E('_ERR_DOWN__WATCH_TYPE_INVALID');
        }
        $postData = array('lm_id' => $lm_id, 'watch_type' => $watch_type);
        // 获取直播信息
        $mainService = new MainService();
        $mainDetail = $mainService->get($lm_id);

        // 直播信息不存在
        if (empty($mainDetail)) {

            E('_ERR_DOWN_LIVE_IS_NOT_EXIST');
        }

        // 直播已观看、未观看、可观看人员UID
        $rangeServ = new RangeService();
        list($uids_watched, $uids_unwatch, $uids_all) = $rangeServ->getWatchDataUids($lm_id);
        // 初始化数据
        $list = [];
        $participateService = new ParticipateService();
        // 已观看人员列表
        if (Constant::LIVE_WATCH_TYPE_TRUE == $watch_type) {
            $list = $participateService->DownWatchedData($postData);
        } elseif (Constant::LIVE_WATCH_TYPE_FALSE == $postData['watch_type']) {
            $list = $participateService->DownUnwatchData($uids_unwatch);
        }
        // 执行导出
        $this->_download($list, $watch_type);

        return true;
    }


    /**
     * 导出模板
     *
     * @param array $list 列表数据
     * @param int $watch_type 导出类型
     */
    private function _download($list = [], $watch_type = 0)
    {
        $row_data = [];

        // 直播人员(已参与)
        if (Constant::LIVE_WATCH_TYPE_TRUE == $watch_type) {

            $file_name = '导出参与直播人员' . date('_YmdHi');
            $title = array(
                '姓名',
                '组织',
                '岗位',
                '角色',
                '手机',
                '最后参与时间',
            );
            foreach ($list as $k => $v) {
                $row_data[] = array(
                    $v['memUsername'],
                    $v['dpName'],
                    $v['jobName'],
                    $v['roleName'],
                    $v['telephone'],
                    rgmdate($v['last_watched_time'], "'Y-m-d H:i"),
                );
            }
        } else {

            $file_name = '导出未参与直播人员' . date('_YmdHi');
            $title = array(
                '姓名',
                '组织',
                '岗位',
                '角色',
                '手机'
            );
            foreach ($list as $k => $v) {
                $row_data[] = array(
                    $v['memUsername'],
                    $v['dpName'],
                    $v['jobName'],
                    $v['roleName'],
                    $v['telephone']
                );
            }
        }
        // Python导出excel
        $realpath = ExportDownload::get_down_dir($this->_login->user['eaId'].microtime(true)) . $file_name . ".xls";
        $ret = PythonExcel::instance()->write($realpath, $title, $row_data);
        if ($ret) {
            $conditon = [
                'title' => $file_name,
                'ea_id' => $this->_login->user['eaId'],
                'username' => $this->_login->user['eaRealname'],
                'type' => ExportDownload::EXCEL_TYPE,
                'url' => $realpath
            ];

            ExportDownload::insert_down_load($conditon);
        }
        
        return true;
    }
}