ExportReadListController.class.php 5.7 KB
<?php
/**
 * Created by PhpStorm.
 * User: liyifei2012it
 * Date: 18/7/5
 * Time: 11:22
 */
namespace Apicp\Controller\News;

use Com\PythonExcel;
use Common\Common\ExportDownload;
use Common\Common\NewsHelper;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\User;
use Common\Service\ArticleService;
use Common\Service\RightService;
use Common\Service\ReadService;

class ExportReadListController extends \Apicp\Controller\AbstractController
{
    /**
     * ExportReadList
     * @desc 导出阅读列表
     * @param Int article_id:true 新闻ID
     * @param Int read_type:true:1 阅读类型(1=未读,2=已读)
     * @param String username 姓名
     * @param Array dp_ids 部门ID
     * @param Array job_ids 职位ID
     * @param Array role_ids 角色ID
     * @return mixed
     */
    public function Index_post()
    {
        // 验证规则
        $rules = [
            'article_id' => 'require|integer',
            'read_type' => 'require|integer',
            'username' => 'max:64',
            'dp_ids' => 'array',
            'job_ids' => 'array',
            'role_ids' => 'array',
        ];

        // 验证数据
        $validate = new PackageValidate($rules, [], array_keys($rules));
        $postData = $validate->postData;

        // 取新闻信息
        $articleServ = new ArticleService();
        $article = $articleServ->get($postData['article_id']);
        if (empty($article)) {
            E('_ERR_ARTICLE_NOT_FOUND');
        }

        // UC筛选uid
        $uidCond = $this->formatConds($postData);

        // 获取人员ID
        $newsHelper = &NewsHelper::instance();
        list(, $uids_study, $uids_unstudy) = $newsHelper->getReadData($article['article_id'], $uidCond);

        // Excel列名
        $columns = ['姓名', '组织', '岗位', '角色', '手机号'];

        // 实例化对象
        $userServ = &User::instance();

        $rows = [];
        if ($postData['read_type'] == Constant::READ_STATUS_IS_YES) {
            $titleSuffix = '_已学人员';
            $columns[] = '阅读时间';

            // 已读人员列表
            $readConds = [
                'uid' => $uids_study,
                'article_id' => $postData['article_id'],
            ];
            $readServ = new ReadService();
            $readList = $readServ->list_by_conds($readConds);

            // 人员详情列表
            $userList = $userServ->listByUid(array_column($readList, 'uid'));
            $userList = array_combine_by_key($userList, 'memUid');
            foreach ($readList as $item) {
                $uid = $item['uid'];
                $user = $userList[$uid];

                // Excel行数据
                $rows[] = [
                    $user['memUsername'],
                    $newsHelper->getDpPath($user),
                    $user['memJob'],
                    $user['memRole'],
                    $user['memMobile'],
                    rgmdate($item['created'], 'Y/m/d H:i'),
                ];
            }

        } else {
            $titleSuffix = '_未学人员';

            if ($uids_unstudy) {
                $userServ = &User::instance();
                $userList = $userServ->listUsersAll(['memUids' => $uids_unstudy]);

                if ($userList) {
                    foreach ($userList as $user) {
                        $rows[] = [
                            $user['memUsername'],
                            $newsHelper->getDpPath($user),
                            $user['memJob'],
                            $user['memRole'],
                            $user['memMobile'],
                        ];
                    }
                }
            }
        }

        $title = $article['title'] . $titleSuffix . rgmdate(MILLI_TIME, '_ymdHis');
        $filename = ExportDownload::get_down_dir($this->_login->user['eaId']) . $title . '.xls';
        $result = PythonExcel::instance()->write($filename, $columns, $rows);

        if ($result === true) {
            // 写入数据到下载中心
            $conditon = [
                'title' => $title,
                'ea_id' => $this->_login->user['eaId'],
                'type' => ExportDownload::EXCEL_TYPE,
                'size' => filesize($filename),
                'username' => $this->_login->user['eaRealname'],
                'url' => $filename,
                'app_dir' => APP_DIR
            ];

            ExportDownload::insert_down_load($conditon);
        }

        return true;
    }

    /**
     * 将搜索条件转化为用户UID
     * @author tangxingguo
     * @param array $postData 用户提交的数据
     * @return array|bool
     */
    private function formatConds($postData)
    {
        $condUids = [];

        $userServ = &User::instance();
        if (isset($postData['username'])) {
            // UC
            $userConds = ['memUsername' => $postData['username']];
            $list = $userServ->listAll($userConds);
            $condUids = array_column($list, 'memUid');
        }

        // 部门、岗位、角色搜索交集
        $right = array_intersect_key_reserved($postData, ['dp_ids', 'job_ids', 'role_ids'], true);
        if (!empty($right)) {
            $rightServ = new RightService();
            $rights = $rightServ->formatPostData($right);
            $unitUids = $rightServ->getUidsByRight($rights);
            if (!empty($condUids)) {
                // 筛选已经有值,取交集
                $condUids = array_intersect($condUids, $unitUids);
            } else {
                $condUids = $unitUids;
            }
        }

        // 如果有查询 并且 结果为空
        if ((!empty($right) || !empty($postData['username'])) && empty($condUids)) {
            return false;
        }

        return $condUids;
    }
}