ListController.class.php 3.74 KB
<?php
/**
 * 基础版本邀请人员组件
 * @author: daijun
 * @date :  2018-05-18 10:18:44
 */

namespace Api\Controller\Mem;

use Common\Common\Department;
use Common\Common\User;
use Common\Service\TeacherService;

class ListController extends AbstractController
{
    /**
     * List
     * @author
     * @desc 邀请人员通用组件
     * @param String dp_id 部门ID
     * @param String memUsername 搜索用户名
     * @return array 邀请人员列表
                    array(
                    'dp_name' => 'A部门', // 当前部门名称
                    'dp_id' => 1, // 当前部门ID
                    'user_total'=>22, 1, // 当前部门人数
                    'user_list' => array(
                                    array(
                                    'memUsername'=>'张三', //用户名
                                    'memUid'=>'', // 用户名称
                                    'memFace'=>'', // 用户图像
                                    )
                    ),
                    'dp_list' => array(
                            array(
                            'dpId'=>'', // 部门ID
                            'dpName'=>'', // 部门名称
                            )
                    )
            )
     */
    public function Index_post()
    {
        $params = I('post.');

        $dp_id = $params['dp_id'];
        $memUsername = $params['memUsername'];

        $deaprtment =& Department::instance();

        // 如果上级部门ID为空
        if (empty($dp_id)) {
            // 获取顶级部门
            $deaprtment->get_top_dpId($top_dp_id);

            $dp_info = $deaprtment->getById($top_dp_id[0]);

        } else {
            $dp_info = $deaprtment->getById($dp_id);
        }

        $dp_result_list = [];

        // 查询当前部门的子部门列表(不递归)
        $dp_ids = $deaprtment->list_childrens_by_cdid([$dp_info['dpId']], false, false);

        sort($dp_ids);

        if (empty($memUsername)) {
            $dp_list = $deaprtment->listById($dp_ids);

            foreach ($dp_list as $v) {
                $dp_result_list[] = [
                    'dpId' => $v['dpId'],
                    'dpName' => $v['dpName'],
                ];
            }
        }

        $user_serv = &User::instance();

        $conds = [];
        if ($dp_id) {
            // 如果传入了dp_id
            $conds = [
                'dpIdList' => [$dp_id],
                'departmentChildrenFlag' => 1,
            ];

            if (!empty($memUsername)) {
                // 如果姓名不为空
                $conds['memUsername'] = $memUsername;
            }
        } elseif (!empty($dp_ids)) {
            // 如果是搜索的所有部门
            if(!empty($top_dp_id)){
                $dp_ids[]=$top_dp_id[0];
            }

            // 如果当前部门子部门存在
            $conds = [
                'dpIdList' => $dp_ids,
                'departmentChildrenFlag' => 1,
            ];

            if (!empty($memUsername)) {
                // 如果姓名不为空
                $conds['memUsername'] = $memUsername;
            }
        }

        $user_list_total = 0;
        $user_list = [];
        if ($conds) {

            // 查询当前部门子部门下的用户数量(递归)
            $user_list = $user_serv->listBasicByConds($conds, 1, 1);
            $user_list_total = $user_list['total'];
            $user_list = $user_serv->listAllBasic($conds);
        }

        // 组织返回数据
        $this->_result = [
            'dp_name' => $dp_info['dpName'],
            'dp_id' => $dp_info['dpId'],
            'user_list' => $user_list,
            'dp_list' => $dp_result_list,
            'user_total' => $user_list_total
        ];

    }
}