UnJoinListController.class.php 3.11 KB
<?php
/**
 * 导出未参与人员列表
 * User: daijun
 * Date: 18/3/24
 * Time: 下午3:44
 */

namespace Apicp\Controller\Export;

use Common\Common\ExportDownload;
use Common\Common\UserCache;
use Com\PythonExcel;
use Common\Service\ActivityService;
use Common\Service\RecordService;
use Common\Service\RightService;

class UnJoinListController extends \Apicp\Controller\AbstractController
{

    public function Index()
    {
        // 获取参数
        $ac_id = I('post.ac_id');

        if (empty($ac_id)) {
            // 活动ID不能为空
            E('_EMPTY_ACTIVITY_ID');
        }

        $activity_serv = new ActivityService();

        $detail = $activity_serv->get($ac_id);

        if (empty($detail)) {
            // 活动信息不存在
            E('_ERR_ACTIVITY_DATA');
        }

        $record_serv = new RecordService();

        $record_list = $record_serv->list_by_conds(['ac_id' => $ac_id], null, [], 'uid');

        // 已参与的人员uid集合
        $uids = [];
        if (!empty($record_list)) {
            $uids = array_filter(array_unique(array_column($record_list, 'uid')));
        }

        // 查询权限人员集合
        $right = new RightService();
        $all_right_users = $right->get_uids_by_right($detail);

        // 获取未参与人员uid集合
        $un_join_uids = array_diff($all_right_users, $uids);

        // 从缓存获取用户列表
        $list = UserCache::get_all_user_by_cache_list($un_join_uids);

        $rand_str = substr(md5(QY_DOMAIN), 0, 1) . substr(md5(QY_DOMAIN), -1);

        $file_name = $detail['title'] . '未参与记录_' . rgmdate(strval(MILLI_TIME), 'YmdHis') . $rand_str; // 导出文件名称

        // 生成下载Excel
        $this->_download($list, $file_name);

        return true;
    }


    /**
     * 导出模板
     *
     * @param array $list 导出数据
     * @param string $file_name 文件名称
     *
     * @return bool
     */
    protected function _download($list = [], $file_name)
    {

        // Excel 表头字段
        $title = [
            '姓名',
            '组织',
            '岗位',
            '角色',
            '手机号',
        ];

        $rows = [];
        // 数据处理
        foreach ($list as $v) {

            $rows[] = [
                $v['memUsername'],
                implode(',', array_column($v['dpName'], 'dpName')),
                $v['memJob'],
                $v['memRole'],
                " " . $v['memMobile'],
            ];
        }

        unset($list);

        $dir = DATA_PATH;

        // 生成Excel 下载
        $real_path = $dir . D_S . $file_name . '.xls';
        $ret = PythonExcel::instance()->write($real_path, $title, $rows);

        if ($ret) {

            $data = [
                'title' => $file_name,
                'ea_id' => $this->_login->user['eaId'],
                'type' => ExportDownload::EXCEL_TYPE,
                'username' => $this->_login->user['eaRealname'],
                'url' => $real_path,
                'app_dir' => APP_DIR
            ];

            ExportDownload::insert_down_load($data);
        }

        return true;
    }

}