ListController.class.php 2.84 KB
<?php
/**
 * 【后台】员工激励列表
 * ListController.class.php
 * @author:daijun
 * @date:2017-08-31
 */

namespace Apicp\Controller\Incentives;

use Common\Service\IncentivesService;
use Common\Service\EducationService;

class ListController extends \Apicp\Controller\AbstractController
{
    /**
     * 员工激励列表接口
     * @author daijun
     */
    public function Index_post()
    {
        // 获取参数
        $params = I('post.');

        if (empty($params['ed_id'])) {

            E('_EMPTY_ED_ID');
        }

        $education_server = new EducationService();
        $education = $education_server->get($params['ed_id']);
        // 培训不存在
        if (empty($education)) {

            E('_ERR_EDUCATION_NOT_EXIST');
        }
        // 获取培训状态
        $ed_status = $education_server->get_ed_status($education);

        $incentives_serv = new IncentivesService();

        // 默认值
        $page = !empty($params['page']) ? intval($params['page']) : self::PAGE_DEFAULT;
        $limit = !empty($params['limit']) ? intval($params['limit']) : self::PAGE_LIMIT_DEFAULT;

        list($start, $limit) = page_limit($page, $limit);

        // 分页参数
        $page_option = [$start, $limit];

        //排序参数
        $order_option = ['updated' => 'DESC', 'sr_id' => 'DESC'];

        $uids = [];
        // 如果部门、岗位、角色、用户姓名有一项不为空
        if (!empty($params['dpIDs']) || !empty($params['jobID']) || !empty($params['roleID']) || !empty($params['memUsername'])) {

            // 根据搜索条件获取用户ID集合(此处为各条件获取的用户uid的交集)
            $uids = $incentives_serv->list_uids_by_dp_job_role($params['dpIDs'], $params['roleID'], $params['jobID'],
                $params['memUsername']);

            if (empty($uids)) {
                // 表示搜索条件的交集为空,则意味着搜索结果为空,此处直接返回
                $this->_result = [
                    'page' => $page,
                    'limit' => $limit,
                    'total' => 0,
                    'list' => []
                ];
                return true;
            }
        }

        // 组装查询条件
        $conds['ed_id'] = $params['ed_id'];
        if (!empty($uids)) {
            $conds['sr_uid'] = $uids;
        }

        // 获取总数
        $total = $incentives_serv->count_by_conds($conds);

        $list = [];
        if ($total > 0) {
            // 查询列表
            $list = $incentives_serv->list_by_conds($conds, $page_option, $order_option);
        }

        $this->_result = [
            'page' => $page,
            'limit' => $limit,
            'total' => $total,
            'ed_status' => $ed_status,
            'list' => $incentives_serv->format_list_data($list)
        ];

        return true;
    }
}