ReadListController.class.php 9.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: tangxingguo
 * Date: 2017/4/13
 * Time: 11:48
 */
namespace Apicp\Controller\News;

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

class ReadListController extends \Apicp\Controller\AbstractController
{

    /**
     * ReadList
     * @desc 阅读列表
     * @param Int article_id:true 新闻ID
     * @param Int read_type:true 阅读类型(1=未读,2=已读)
     * @param Int page 页码
     * @param Int limit 每页数据条数
     * @return array 阅读列表
     *               array(
                        'article_id' => 123, // 新闻ID
                        'read_type' => 1, // 阅读类型(1=未读,2=已读)
                        'title' => '', // 新闻标题
                        'send_time' => '', // 更新时间
                        'page' => '', // 页码
                        'limit' => '', // 每页数据条数
                        'read_total' => '', // 已读总数
                        'unread_total' => '', // 未读总数
                        'list' => array(
                            'username' => '张三', // 姓名
                            'dp_name' => array('技术部'), // 所属部门
                            'job' => 'PHP', // 职位
                            'mobile' => '15821392414', // 手机号码
                            'created' => '1234566898988', // 阅读时间(毫秒级时间戳)
                        ),
                    );
     */
    public function Index_post()
    {
        // 验证规则
        $rules = [
            'article_id' => 'require|integer',
            'read_type' => 'require|integer',
            'page' => 'integer',
            'limit' => 'integer',
            'dp_ids' => 'array',
            'job_ids' => 'array',
            'role_ids' => 'array',
            'username' => 'max:64',
        ];

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

        // 默认值
        $postData['page'] = isset($postData['page']) ? $postData['page'] : Constant::PAGING_DEFAULT_PAGE;
        $postData['limit'] = isset($postData['limit']) ? $postData['limit'] : Constant::PAGING_DEFAULT_LIMIT;
        // 查询条件
        $uidCond = $this->formatConds($postData);

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

        $list = [];
        $total = 0;
        $readServ = new ReadService();

        $read_total = 0;
        $unread_total = 0;
        if ($uidCond !== false) {
            // 已读
            $readConds = ['article_id' => $postData['article_id']];
            $read_total = $readServ->count_by_conds($readConds);

            if (!empty($uidCond)) {
                $readConds['uid'] = $uidCond;
            }
            if ($postData['read_type'] == Constant::READ_STATUS_IS_YES) {
                list($start, $perpage) = page_limit($postData['page'], $postData['limit']);
                $readList = $readServ->list_by_conds($readConds, [$start, $perpage]);
                $userServ = &User::instance();
                $userlist = $userServ->listByUid(array_column($readList, 'uid'));
                $userlist = array_combine_by_key($userlist, 'memUid');
                foreach ($readList as &$item) {
                    $uid = $item['uid'];
                    $list[] = [
                        'username' => $userlist[$uid]['memUsername'],
                        'dp_name' => empty($userlist[$uid]['dpName']) ?
                            [] : array_column($userlist[$uid]['dpName'], 'dpName'),
                        'job' => $userlist[$uid]['memJob'],
                        'role' => $userlist[$uid]['memRole'],
                        'mobile' => $userlist[$uid]['memMobile'],
                        'created' => $item['created']
                    ];
                }

                $total = $read_total;
            }

            // 未读
            list($uids_all, $uids_read, $uids_unread, $unread_total) =
                $this->getReadData($postData['article_id'], $uidCond);

            if ($postData['read_type'] == Constant::READ_STATUS_IS_NO) {
                $total = count($uids_unread);
                // 取未读人员信息
                if ($uids_unread) {
                    $userServ = &User::instance();
                    $userList = $userServ->listByConds(['memUids' => $uids_unread], $postData['page'], $postData['limit']);
                    if ($userList) {
                        foreach ($userList['list'] as $v) {
                            $list[] = [
                                'uid' => $v['memUid'],
                                'username' => $v['memUsername'],
                                'dp_name' => empty($v['dpName']) ? [] : array_column($v['dpName'], 'dpName'),
                                'job' => $v['memJob'],
                                'role' => $v['memRole'],
                                'mobile' => $v['memMobile'],
                            ];
                        }
                    }

                }
            }
        }

        $this->_result = [
            'article_id' => $postData['article_id'],
            'read_type' => $postData['read_type'],
            'limit' => $postData['limit'],
            'page' => $postData['page'],
            'title' => $newsInfo['title'],
            'send_time' => $newsInfo['send_time'],
            'read_total' => $read_total,
            'unread_total' => $unread_total,
            'total' => $total,
            'list' => $list,
        ];
    }

    /**
     * 将搜索条件转化为用户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) && isset($postData['username'])) {
            $rightServ = new RightService();
            $rights = $rightServ->formatPostData($right);
            $unitUids = $rightServ->getUidsByRight($rights);
            if (!empty($condUids)) {
                // 筛选已经有值,取交集
                $condUids = array_intersect($condUids, $unitUids);
            } else {
                //$condUids = $unitUids;
                return false;
            }
        }

        // 部门/岗位存在  名字存在  人名不存在
        if (!empty($right) && !isset($postData['username'])) {
            $rightServ = new RightService();
            $rights = $rightServ->formatPostData($right);
            $unitUids = $rightServ->getUidsByRight($rights);
            $condUids = $unitUids;
        }

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

        return $condUids;
    }

    /**
     * 获取新闻可读、已读、未读人员ID数组
     * @author zhonglei
     * @param int $article_id 新闻ID
     * @return array [0 => 可读人员ID, 1 => 已读人员ID, 2 => 未读人员ID]
     */
    public function getReadData($article_id, $uidCond)
    {
        $uids_all = [];
        $uids_read = [];
        $uids_unread = [];

        // 查询条件
        $conds = ['article_id' => $article_id];
        // 获取权限数据
        $rightServ = new RightService();
        $right_list = $rightServ->list_by_conds($conds);
        $rights = $rightServ->formatDBData($right_list);

        if (empty($rights)) {
            return [$uids_all, $uids_read, $uids_unread];
        }

        // 获取可读数据
        $uids_all = $rightServ->getUidsByRight($rights);
        // 获取已读数据
        $readServ = new ReadService();
        if (!empty($uidCond)) {
            $conds['uid'] = $uidCond;
        }
        $study_list = $readServ->list_by_conds($conds);
        $uids_read = array_column($study_list, 'uid');
        $uids_unread = array_values(
            array_diff(
            // 如果外部传了条件
                !empty($uidCond) ? array_intersect($uids_all, $uidCond) : $uids_all,
                $uids_read
            )
        );
        // 已经学习人数 (跟传入条件无关)
        $studyListWithOutConds = $readServ->list_by_conds(['article_id' => $article_id]);
        $studyListWithOutConds = array_column($studyListWithOutConds, 'uid');

        // 未读数据
        return [$uids_all, $uids_read, $uids_unread, count(array_diff($uids_all, $studyListWithOutConds))];
    }
}