ExportLikeListController.class.php 3.15 KB
<?php
/**
 * Created by PhpStorm.
 * User: liyifei2012it
 * Date: 18/7/6
 * Time: 10:53
 */
namespace Apicp\Controller\News;

use Com\PackageValidate;
use Com\PythonExcel;
use Common\Common\ExportDownload;
use Common\Common\NewsHelper;
use Common\Common\User;
use Common\Service\LikeService;
use Common\Service\ArticleService;

class ExportLikeListController extends \Apicp\Controller\AbstractController
{
    /**
     * LikeList
     * @desc 导出点赞列表
     * @param int article_id:true 新闻ID
     * @return mixed
     */
    public function Index_post()
    {
        // 验证规则
        $rules = [
            'article_id' => 'require|integer',
        ];

        // 验证数据
        $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');
        }

        // 点赞列表
        $likeServ = new LikeService();
        $likeList = $likeServ->list_by_conds(['article_id' => $postData['article_id']]);

        if ($likeList) {
            // UC人员信息
            $uids = array_column($likeList, 'uid');
            $userServ = &User::instance();
            $userList = $userServ->listAll(['memUids' => $uids]);
            $userList = array_combine_by_key($userList, 'memUid');

            // Excel文件名前缀
            $titleSuffix = '_点赞人员';

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

            // Excel行数据
            $rows = [];
            if ($userList) {
                $newsHelper = &NewsHelper::instance();

                foreach ($likeList as $k => $v) {
                    $uid = $v['uid'];
                    if (isset($userList[$uid])) {
                        $user = $userList[$uid];

                        $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;
    }
}