RandomSnapshotModel.class.php 3.16 KB
<?php
/**
 * 随机题库表
 * @author: 蔡建华
 * @email:     594609175@qq.com
 * @date :  2017-07-14 18:06:08
 * @version $Id$
 */

namespace Common\Model;


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

    /**
     * 获取随机快照列表
     *
     * @author  caijanhua
     * @param array $conds 查询条件参数列表
     * @param array $page_option 分页参数
     * @param array $order_option 排序参数
     * @param string $fields 返回字段
     *
     * @return array|bool
     */
    public function conds_random_snapshot_list($conds, $page_option = null, $order_option = [], $fields = '*')
    {

        $params = [];
        // 条件
        $wheres = [];
        if (!$this->_parse_where($wheres, $params, $conds)) {
            return false;
        }
        $snapModel = new StatisticsModel();
        $table = $snapModel->get_tname();
        // 企业标记
        $wheres[] = 'a.`domain`=?';
        $params[] = QY_DOMAIN;
        // 状态条件
        $wheres[] = 'a.`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, CASE WHEN b.answer_total>0 THEN b.right_num/b.answer_total ELSE 'false' END as probability,CASE WHEN  a.et_type=" . self::TOPIC_TYPE_VOICE . " or a.et_type=" . self::TOPIC_TYPE_QUESTION . " THEN 1 ELSE 0 END as ea_sort,
        CASE when a.et_type=" . self::TOPIC_TYPE_SINGLE . " THEN 0
            when a.et_type=" . self::TOPIC_TYPE_MULTIPLE . " THEN 1
            when a.et_type=" . self::TOPIC_TYPE_JUDGMENT . " THEN 2
            when a.et_type=" . self::TOPIC_TYPE_QUESTION . " THEN 3
            when a.et_type=" . self::TOPIC_TYPE_VOICE . " THEN 4
        END as order_defualt
        FROM __TABLE__ a LEFT JOIN $table b ON a.er_id=b.esr_id AND a.ep_id=b.ep_id WHERE " . implode(' AND ',
                $wheres) . " group by esr_id {$orderby} {$limit}";

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

    /**
     * 获取随机快照总数
     *
     * @author  caijainhua
     * @param array $conds 查询条件参数列表
     *
     * @return int
     */
    public function conds_random_snapshot_count($conds)
    {
        $params = [];
        // 条件
        $wheres = [];
        if (!$this->_parse_where($wheres, $params, $conds)) {
            return false;
        }
        $snapModel = new StatisticsModel();
        $table = $snapModel->get_tname();
        // 企业标记
        $wheres[] = 'a.`domain`=?';
        $params[] = QY_DOMAIN;
        // 状态条件
        $wheres[] = 'a.`status`<?';
        $params[] = $this->get_st_delete();

        $sql = "SELECT count(er_id) as total FROM __TABLE__ a LEFT JOIN $table b ON a.er_id=b.esr_id AND a.ep_id=b.ep_id WHERE " . implode(' AND ',
                $wheres) . ' group by esr_id';

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

}