ListController.class.php 4.24 KB
<?php
/**
 * 【考试中心-手机端】批阅列表接口
 * ListController.class.php
 * @author: 蔡建华
 * @date: 2017-06-19
 */

namespace Api\Controller\Marking;

use Common\Service\AnswerService;
use Common\Service\PaperService;
use Common\Service\RightService;

class ListController extends AbstractController
{
    /** @var  PaperService  实例化试卷对象 */
    protected $paper_service;
    /** @var  AnswerService  实例化答卷表对象 */
    protected $answer_service;
    /** @var  RightService  实例化权限表对象 */
    protected $right_service;

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

        $this->paper_service = new PaperService();
        $this->answer_service = new AnswerService();
        $this->right_service = new RightService();

        return true;
    }

    public function Index_post()
    {
        // 接收post参数
        $params = I('post.');

        $mk_type = rintval($params['mk_type']);

        if ($mk_type != PaperService::READ_WAITING) {
            $mk_type = PaperService::READ_OVER;
        }

        if (!in_array($mk_type, [PaperService::READ_WAITING, PaperService::READ_OVER])) {
            // 无效的批阅类型
            E('_ERR_MARK_TYPE_INTEGRAL');
        }

        $page = !empty($params['page']) ? intval($params['page']) : self::DEFAULT_PAGE;
        $limit = !empty($params['limit']) ? intval($params['limit']) : self::DEFAULT_LIMIT;
        // 查询条件
        $conds = [];

        // 排序
        $order_option = [];

        // 分页
        list($start, $limit) = page_limit($page, $limit);
        $page_option = [$start, $limit];

        // 待阅卷
        if (PaperService::READ_WAITING == $mk_type) {

            // 获取当前登陆用户有权限批阅的试卷 ep_id 集合
            $conds = [
                'er_type' => RightService::RIGHT_MARKING,
                'uid' => $this->uid,
            ];
            $right = $this->right_service->list_by_conds($conds, [], [], 'epc_id');

            $right_ep_ids = array_column($right, 'epc_id');

            if (empty($right_ep_ids)) {
                // 没有可批阅的试卷
                return true;
            }

            $conds = [
                'answer_status' => PaperService::READ_WAITING,
                'ep_id' => $right_ep_ids,
            ];
        }

        // 已批阅
        if (PaperService::READ_OVER == $mk_type) {
            $conds = [
                'answer_status' => PaperService::READ_OVER,
                'marking_uid' => $this->uid,
            ];
        }

        $mark_list = $this->answer_service->list_marking_paper($conds, [], $order_option);

        // 批阅列表为空时,直接返回
        if (empty($mark_list)) {

            $this->_result = [
                'page' => $page,
                'limit' => $limit,
                'total' => 0,
                'list' => [],
            ];

            return true;
        }

        $mark_list = array_combine_by_key($mark_list, 'ep_id');
        // 获取试卷名称、考试时间、参加人数
        $ep_ids = array_column($mark_list, 'ep_id');
        $conds = ['ep_id' => $ep_ids, 'cate_status' => PaperService::EC_OPEN_STATES];

        $total = $this->paper_service->count_by_conds($conds);

        $list = [];
        if ($total) {

            $fields = 'ep_id,ep_name,begin_time,end_time,join_count,exam_status';
            $paper_list = $this->paper_service->list_by_conds($conds, $page_option, ['last_time' => 'DESC'], $fields);

            // 组装数据
            foreach ($paper_list as $v) {

                $res = $mark_list[$v['ep_id']];

                $res['ep_name'] = $v['ep_name'];
                $res['join_count'] = $v['join_count'];

                // 转换考试状态
                $res['paper_status'] = $this->paper_service->paper_status($v['exam_status'], $v['begin_time'], $v['end_time']);
                $res['end_time'] = $v['end_time'];

                $list[] = $res;
            }
        }

        // 返回结果
        $this->_result = [
            'page' => $page,
            'limit' => $limit,
            'total' => $total,
            'list' => $list,
        ];

        return true;
    }
}