BaseinfoModel.class.php 3.95 KB
<?php
/**
 * BaseinfoModel.class.php
 * $author$
 */

namespace Common\Model;

use Common\Service\BaseinfoService;

class BaseinfoModel extends AbstractModel
{

    // 全部人可见
    const IS_ALL = 1;
    // 部分人可见
    const NOT_ALL = 0;

    // 调研发布状态:发布
    const PUBLISH_STATUS = 3;

    /**
     * 构造方法
     */
    public function __construct()
    {

        parent::__construct();
    }

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

        $res = $this->_m->query($sql);

        return $res;
    }

    /**
     * 获取有权限的调研总数
     *
     * @param int $qu_status 调研状态 1进行中,2已结束
     * @param array $ids 有权限的调研ID集合
     *
     * @return mixed
     */
    public function count_by_where($qu_status, $ids)
    {

        //格式化查询条件
        list($where, $params) = $this->get_list_where($qu_status, $ids);

        $sql = 'SELECT COUNT(*) FROM __TABLE__' . $where;

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

    }

    /**
     * 获取有权限的调研列表
     *
     * @param int $qu_status 调研状态 1进行中,2已结束
     * @param array $ids 有权限的调研ID集合
     * @param array $page_option 分页数组
     * @param array $order_option 排序添加
     *
     * @return mixed
     */
    public function list_by_where($qu_status, $ids, $page_option, $order_option)
    {

        // 格式化查询条件
        list($where, $params) = $this->get_list_where($qu_status, $ids);

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

            return false;
        }

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

            return false;
        }

        $sql = 'SELECT * FROM __TABLE__' . $where . $order_by . $limit;

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

    /**
     * 调研sql查询条件
     *
     * @param $qu_status
     * @param $ids
     *
     * @return array
     */
    private function get_list_where($qu_status, $ids)
    {

        // sql查询条件
        $where = ' WHERE `qu_type`=? AND `status`<? AND `domain`=? AND `release_status`=? ';
        $params = [
            BaseinfoService::ROUTINE_TYPE, // 常规类型
            self::ST_DELETE,
            QY_DOMAIN,
            self::PUBLISH_STATUS // 调研发布状态:发布
        ];

        // 进行中
        if (BaseinfoService::FRONTEND_QU_STATUS_ING == $qu_status) {

            $where .= ' AND `deadline` >=?';
        } elseif (BaseinfoService::FRONTEND_QU_STATUS_END == $qu_status) { // 已结束

            $where .= ' AND `deadline` <?';
        }

        $params[] = MILLI_TIME;

        // 如果传入的调研id集合为空
        if (empty($ids)) {

            $where .= ' AND `is_all`=?';
        } else {

            $id_arr = implode(',', $ids);
            $where .= ' AND (`is_all`=? OR `qu_id` IN (' . $id_arr . '))';
        }

        $params[] = self::IS_ALL;

        return [$where, $params];
    }

    /**
     * 执行sql语句
     *
     * @param $sql
     *
     * @return false|int
     */
    public function execute($sql)
    {

        $res = $this->_m->execute($sql);

        return $res;
    }

    /**
     * 查询分类id及对应的调研总数
     *
     * @return mixed
     */
    public function classify_baseinfo_count()
    {

        // 查询条件
        $where['status'] = ['lt', self::ST_DELETE];
        $where['domain'] = QY_DOMAIN;

        return $this->_m->field('qc_id,COUNT(qc_id) AS count')->where($where)->group('qc_id')->select();
    }
}