AnswerModel.class.php 7.64 KB
<?php
/**
 * 试卷-答卷表
 * @author: houyingcai
 * @email:    594609175@qq.com
 * @date :  2017-05-19 18:04:10
 * @version $Id$
 */

namespace Common\Model;

class AnswerModel extends AbstractModel
{
    // 批阅状态:待批阅
    const ANSWER_STATUS_NO = 1;
    // 批阅状态:批阅中
    const ANSWER_STATUS_ING = 2;
    // 批阅状态:已批阅
    const ANSWER_STATUS_OK = 3;

    /**
     * 构造方法
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 查询一条数据
     *
     * @autor 蔡建华
     * @param array $conds 查询条件
     * @param string $fields 返还字段
     *
     * @return array|bool
     */
    public function fetchOne($conds = [], $fields = '*')
    {
        $params = [];
        // 条件
        $wheres = [];
        if (!$this->_parse_where($wheres, $params, $conds)) {
            return false;
        }
        // 企业标记
        $wheres[] = "`{$this->prefield}domain`=?";
        $params[] = QY_DOMAIN;
        // 状态条件
        $wheres[] = "`{$this->prefield}status`<?";
        $params[] = $this->get_st_delete();
        $sql = "select {$fields} from  __TABLE__  WHERE  " . implode(' AND ', $wheres);
        return $this->_m->result($sql, $params);
    }

    /**
     * 获取考试参与人员列表
     *
     * @author  houyingcai
     * @param array $conds 查询条件参数列表
     * @param array $page_option 分页参数
     * @param array $order_option 排序参数
     * @param string $fields 返回字段
     *
     * @return array|bool
     */
    public function get_mock_answer_list($conds, $page_option = null, $order_option = [], $fields = '*')
    {

        $params = [];
        // 条件
        $wheres = [];
        if (!$this->_parse_where($wheres, $params, $conds)) {
            return false;
        }
        // 企业标记
        $wheres[] = '`domain`=?';
        $params[] = QY_DOMAIN;
        // 状态条件
        $wheres[] = '`status`<?';
        $params[] = $this->get_st_delete();

        // 排序
        $orderby = '';
        if (!$this->_order_by($orderby, $order_option)) {

            return false;
        }
        // 分页参数
        $limit = '';
        if (!$this->_limit($limit, $page_option)) {

            return false;
        }   

        $sql = "SELECT {$fields},COUNT(uid) AS join_count,MAX(my_score) AS my_max_score FROM (SELECT {$fields} FROM __TABLE__ WHERE " . implode(' AND ',
                $wheres) . " ORDER BY my_score DESC,my_time ASC ) AS a GROUP BY a.uid {$orderby} {$limit}";
        return $this->_m->fetch_array($sql, $params);
    }

    /**
     * 统计考试参与人员总数
     *
     * @author  houyingcai
     * @param array $conds 查询条件参数列表
     *
     * @return array|bool
     */
    public function count_mock_answer($conds)
    {
        $params = [];
        // 条件
        $wheres = [];
        if (!$this->_parse_where($wheres, $params, $conds)) {
            return false;
        }
        // 企业标记
        $wheres[] = '`domain`=?';
        $params[] = QY_DOMAIN;
        // 状态条件
        $wheres[] = '`status`<?';
        $params[] = $this->get_st_delete();

        $sql = 'select count(*) from (select * from __TABLE__ WHERE ' . implode(' AND ',
                $wheres) . ' GROUP BY uid) __TABLE__';

        return $this->_m->result($sql, $params);

    }

    /**
     * 根据条件读取数据
     *
     * @author  houyingcai
     * @param array $conds 查询条件数组
     * @param array $order_option 排序数组
     * @param String $fields 查询字段
     *
     * @return array|bool
     */
    public function get_by_conds($conds, $order_option = [], $fields = '*')
    {

        $params = [];
        // 条件
        $wheres = [];
        if (!$this->_parse_where($wheres, $params, $conds)) {

            return false;
        }

        // 排序
        $orderby = '';
        if (!$this->_order_by($orderby, $order_option)) {

            return false;
        }

        // 企业标记
        $wheres[] = "`{$this->prefield}domain`=?";
        $params[] = QY_DOMAIN;
        // 状态条件
        $wheres[] = "`{$this->prefield}status`<?";
        $params[] = $this->get_st_delete();

        // 执行 SQL
        return $this->_m->fetch_row("SELECT {$fields} FROM __TABLE__ WHERE " . implode(' AND ',
                $wheres) . "{$orderby} LIMIT 1",
            $params);
    }


    /**
     * 执行自定义查询SQL语句(安装应用回调时用)
     *
     * @author 侯英才
     * @param string $sql 执行的SQL语句
     *
     * @return mixed
     */
    public function query($sql)
    {
        $res = $this->_m->query($sql);
        return $res;
    }

    /**
     * 执行自定义非查询SQL语句(安装应用回调时用)
     *
     * @author 侯英才
     * @param string $sql 执行的SQL语句
     *
     * @return mixed
     */
    public function execute($sql)
    {
        $res = $this->_m->execute($sql);
        return $res;
    }

    /**
     * 通过试卷IDS查询对应试卷已参与人员统计列表
     *
     * @author 何岳龙
     * @param array $ep_ids 试卷IDS
     *
     * @return array
     */
    public function list_by_total($ep_ids = [])
    {
        // sql 语句
        $sql = "SELECT count(distinct (uid)) AS total,ep_id FROM __TABLE__";

        // 试卷IDS
        $wheres[] = '`ep_id` IN(?)';
        $params[] = $ep_ids;

        // 我的用时
        $wheres[] = '`my_time` > ?';
        $params[] = 0;

        // 企业标记
        $wheres[] = '`domain`=?';
        $params[] = QY_DOMAIN;

        // 状态条件
        $wheres[] = '`status`<?';
        $params[] = $this->get_st_delete();

        return $this->_m->fetch_array($sql . ' WHERE ' . implode(' AND ', $wheres) . '   group BY ep_id', $params);
    }


    /**
     * 获取批阅列表
     *
     * @param array $conds 查询条件
     * @param array $page_option 分页
     * @param array $order_option 排序
     *
     * @return array|bool
     */
    public function list_marking_paper($conds, $page_option = [], $order_option = [])
    {
        $params = [];
        // 条件
        $wheres = [];
        if (!$this->_parse_where($wheres, $params, $conds)) {
            return false;
        }
        // 企业标记
        $wheres[] = "`{$this->prefield}domain`=?";
        $params[] = QY_DOMAIN;
        // 状态条件
        $wheres[] = "`{$this->prefield}status`<?";
        $params[] = $this->get_st_delete();

        // 排序
        $orderby = '';
        if (!$this->_order_by($orderby, $order_option)) {
            return false;
        }
        // 分页参数
        $limit = '';
        if (!$this->_limit($limit, $page_option)) {
            return false;
        }

        $sql = "SELECT ep_id,count(ea_id) as total FROM __TABLE__  WHERE " . implode(' AND ', $wheres) . " GROUP BY ep_id {$limit} ";

        return $this->_m->fetch_array($sql, $params);
    }

    /**
     * 统计批阅列表总数
     *
     * @param array $conds 查询条件
     *
     * @return array|bool
     */
    public function count_marking_paper($conds)
    {
        $params = [];
        // 条件
        $wheres = [];
        if (!$this->_parse_where($wheres, $params, $conds)) {
            return false;
        }
        // 企业标记
        $wheres[] = "`{$this->prefield}domain`=?";
        $params[] = QY_DOMAIN;
        // 状态条件
        $wheres[] = "`{$this->prefield}status`<?";
        $params[] = $this->get_st_delete();


        $sql = "SELECT COUNT(*) FROM (SELECT * FROM __TABLE__  WHERE " . implode(' AND ', $wheres) . " GROUP BY ep_id ) __TABLE__";

        return $this->_m->result($sql, $params);
    }
}