UserListController.class.php 4.91 KB
<?php
/**
 * Created by IntelliJ IDEA.
 * 勋章列表
 * User: zhoutao
 * Reader: zhoutao 2017-05-31 10:07:35
 * Date: 2017-05-24 15:43:49
 */

namespace Apicp\Controller\Medal;

use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\Integral;
use Common\Common\User;
use Common\Service\MedalService;
use Common\Service\MemberMedalService;

class UserListController extends AbstractController
{
    /**
     * UserList
     * @author tangxingguo
     * @desc 列表接口
     * @param Array dp_ids 组织ID
     * @param Array job_ids 岗位ID
     * @param Array role_ids 角色ID
     * @param String username 用户名
     * @return Array
            array(
                'page' => 1, // 当前页
                'limit' => 20, // 当前页条数
                'total' => 100, // 总条数
                'list' => array(
                    'mem_uid' => '3E31DE477F00000120C5EBDB2D6696BE', // 用户ID
                    'mem_username' => '老王', // 用户名
                    'dp_name' => array('组织1'), // 组织
                    'job_name' => '岗位', // 岗位
                    'role_name' => '角色', // 角色
                    'madel' => array(
                        array(
                            'name' => '学习楷模',
                            'num' => 2
                        )
                    )
                )
            );
     */
    public function index()
    {
        $rules = [
            'dp_ids' => 'array',
            'job_ids' => 'array',
            'role_ids' => 'array',
            'username' => 'max:64',
            'page' => 'integer',
            'limit' => 'integer',
        ];

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

        // 分页
        $page = $postData['page'] ?? Constant::PAGING_DEFAULT_PAGE;
        $limit = $postData['limit'] ?? Constant::PAGING_DEFAULT_LIMIT;
        list($start, $perpage) = page_limit($page, $limit);

        // 条件
        $conds = [];
        if (isset($postData['username'])) {
            $conds['mem_username like ?'] = '%' . $postData['username'] . '%';
        }

        // 组织、岗位、角色转UID
        $condUids = &Integral::instance()->getUids($postData);
        if (!empty($postData['dp_ids']) || !empty($postData['job_ids']) || !empty($postData['role_ids'])) {
            if (empty($condUids)) {
                $this->_result = [
                    'page' => $page,
                    'limit' => $limit,
                    'total' => 0,
                    'list' => []
                ];
                return true;
            }
        }

        $conds['mem_uid'] = $condUids;

        $medalServ = new MemberMedalService();
        $list = $medalServ->list_by_conds($conds,null);
        $all_uids = array_unique(array_filter(array_column($list, 'mem_uid')));
        $page_uids=array_slice($all_uids,$start,$perpage);

        // 去重列表
        $unList = [];
        // 按人员列表排序后的列表
        $orderList = [];

        if (!empty($list)) {
            // 用户信息
            $users = &User::instance()->listAll(['memUids' => $all_uids]);
            $users = array_combine_by_key($users, 'memUid');

            // 所有勋章
            $medalModel = new MedalService();
            $medalList = $medalModel->list_all();
            $medalList = array_combine_by_key($medalList, 'im_id');

            // 组织、岗位、角色、勋章
            foreach ($list as $v) {
                // 勋章
                if (isset($medalList[$v['im_id']])) {
                    if (isset($unList[$v['mem_uid']])) {
                        $unList[$v['mem_uid']]['madel'][] = [
                            'name' => $medalList[$v['im_id']]['name'],
                            'num' => $v['im_total']
                        ];

                    } else {
                        // 组织、岗位、角色
                        if (isset($users[$v['mem_uid']])) {
                            $dpInfo = $users[$v['mem_uid']]['dpName'];
                            $v['dp_name'] = array_column($dpInfo, 'dpName');
                            $v['job_name'] = $users[$v['mem_uid']]['memJob'];
                            $v['role_name'] = $users[$v['mem_uid']]['memRole'];
                        }

                        $v['madel'][] = [
                            'name' => $medalList[$v['im_id']]['name'],
                            'num' => $v['im_total']
                        ];
                        $unList[$v['mem_uid']] = $v;
                    }
                }
            }

            foreach ($page_uids as $uid) {
                $orderList[] = $unList[$uid];
            }
        }

        $this->_result = [
            'page' => $page,
            'limit' => $limit,
            'total' => count($all_uids),
            'list' => $orderList
        ];

        return true;
    }
}