ListController.class.php 5.73 KB
<?php
/**
 * 【考试中心-后台】 获取试卷管理列表
 * @author: houyingcai
 * @email:  594609175@qq.com
 * @date :  2017-05-23 16:28:26
 * @version $Id$
 */

namespace Apicp\Controller\Paper;

use Common\Common\Constant;
use Common\Common\TaskCenter;
use Common\Common\Train;
use Common\Service\CategoryService;
use Common\Service\PaperService;

class ListController extends AbstractController
{
    /** @var  PaperService  实例化试卷表对象 */
    protected $paper_service;
    /** @var  CategoryService 实例化试卷分类表对象 */
    protected $cate_service;

    // 试卷被使用
    const PAPER_USED = 2;
    // 试卷未被使用
    const PAPER_NO_USE = 1;

    public function before_action($action = '')
    {
        if (!parent::before_action($action)) {
            return false;
        }

        $this->paper_service = new PaperService();
        $this->cate_service = new CategoryService();

        return true;
    }

    public function Index_post()
    {
        $params = I('post.');

        // 默认值
        $page = !empty($params['page']) ? intval($params['page']) : 1;
        $limit = !empty($params['limit']) ? intval($params['limit']) : PaperService::DEFAULT_LIMIT_ADMIN;

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

        $page_option = [$start, $limit];

        // 最后更新时间倒叙
        $order_option = ['last_time' => 'desc'];

        // 列表总数
        $total = $this->paper_service->count_search_where($params);

        if ($total > 0) {

            $fields = 'ep_id,exam_type,ep_type,paper_type,ec_id,ep_name,begin_time,end_time,exam_status,cate_status,is_open_makeup,topic_count,join_count,unjoin_count,total_score,last_time,marking_type';
            $paper_list = $this->paper_service->list_search_where($params, $page_option, $order_option, $fields);
        }

        // 组装返回数据
        $this->format_paper_list($paper_list);

        $this->_result = [
            'total' => (int)$total,
            'limit' => (int)$limit,
            'page' => (int)$page,
            'list' => !empty($paper_list) ? $paper_list : []
        ];

        return true;
    }

    /**
     * 组装返回数据
     * @param array $list 试卷列表
     * @return bool
     */
    protected function format_paper_list(&$list)
    {

        // 【任务对接】 0907新增任务中心使用判断
        $app_data_ids = [];
        if (!empty($list) && is_array($list)) {

            $app_data_ids = array_column($list, 'ep_id');
        }

        $taskCenter = &TaskCenter::instance();
        // 查询任务模块使用情况
        $task_used_ids = $taskCenter->checkAppDataUsed($app_data_ids);

        $train = &Train::instance();
        // 查询培训模块使用情况
        $train_used_ids = $train->checkUsing($app_data_ids);

        // 获取被占用的试卷ID的集合
        if (!empty($task_used_ids)) {
            $used_ids = array_merge($task_used_ids, $train_used_ids);
        } else {
            $used_ids = $train_used_ids;
        }

        foreach ($list as &$val) {

            switch ($val['exam_status']) {

                // 草稿
                case PaperService::PAPER_DRAFT:

                    $val['exam_status'] = PaperService::STATUS_DRAFT;

                    break;
                // 已终止
                case PaperService::PAPER_STOP:

                    $val['exam_status'] = PaperService::STATUS_STOP;

                    break;
                // 已发布
                case PaperService::PAPER_PUBLISH:

                    // 如果是任务类考试或者线下培训类考试或者其他类考试
                    if (in_array($val['exam_type'], [Constant::EXAM_TYPE_TASK, Constant::EXAM_TYPE_TRAIN, Constant::EXAM_TYPE_OTHER])) {
                        // 默认已开始
                        $val['exam_status'] = PaperService::STATUS_ING;
                        break;
                    }

                    // 【未开始】
                    if ($val['begin_time'] > MILLI_TIME) {

                        $val['exam_status'] = PaperService::STATUS_NOT_START;
                    }

                    // 【已开始】
                    if (
                        $val['begin_time'] < MILLI_TIME &&
                        ($val['end_time'] >= MILLI_TIME || $val['end_time'] == 0)
                    ) {

                        $val['exam_status'] = PaperService::STATUS_ING;
                    }

                    // 【已结束】
                    if ($val['end_time'] > 0 && $val['end_time'] < MILLI_TIME && $val['exam_type'] == Constant::EXAM_TYPE_NORMAL) {

                        $val['exam_status'] = PaperService::STATUS_END;
                    }

                    break;
                default:
            }

            unset($val['last_time']);

            // 获取分类
            if ($val['ec_id']) {

                $category = $this->cate_service->get($val['ec_id']);

                $val['ec_name'] = $category['ec_name'];
                unset($val['ec_id']);
            }

            $val['ep_id'] = (int)$val['ep_id'];
            $val['ep_type'] = (int)$val['ep_type'];
            $val['paper_type'] = (int)$val['paper_type'];
            $val['exam_type'] = intval($val['exam_type']);
            $val['exam_status'] = (int)$val['exam_status'];
            $val['cate_status'] = (int)$val['cate_status'];

            // 【任务对接】
            if (!empty($used_ids) && is_array($used_ids)) {
                // 组装使用情况:1:未使用,2:已使用
                $val['is_used'] = in_array($val['ep_id'], $used_ids) ? self::PAPER_USED : self::PAPER_NO_USE;
            } else {
                $val['is_used'] = self::PAPER_NO_USE;
            }

        }

        return true;
    }

}