ActivityModel.class.php 4.03 KB
<?php

namespace Common\Model;


use Common\Common\Constant;

class ActivityModel extends AbstractModel
{

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

    /**
     * 查询总数
     * @param array $params 条件sql,不包含where关键字
     * @return int|mixed
     */
    public function count_by_where($params = [])
    {

        $where = $this->get_where($params);

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

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

    }


    public function get_where($params = [])
    {
        // 组装查询语句
        $where = ' status<' . self::ST_DELETE . " AND domain= '" . QY_DOMAIN . "'";

        if (!empty($params['title'])) {
            $params['title'] = str_replace("%", '\%', $params['title']);
            // 如果标题不为空
            $where .= " AND title like '%" . trim($params['title']) . "%' ";
        }

        // 活动时间范围
        if (!empty($params['start_time']) && $params['start_time'] != 'NaN' && $params['end_time'] != 'NaN') {

            $start_time = rstrtotime(rgmdate(strval($params['start_time']), 'Y-m-d') . ' 00:00:00');

            $end_time = rstrtotime(rgmdate(strval($params['end_time']), 'Y-m-d') . ' 23:59:59');

            $where .= ' AND ( end_time >' . $start_time . ' AND start_time <' . $end_time . ' )';
        }

        if (!empty($params['activity_status'])) {

            switch ($params['activity_status']) {
                // 草稿
                case Constant::STATUS_DRAFT:
                    $where .= ' AND activity_status = ' . Constant::ACTIVITY_DRAFT;
                    break;
                // 未开始
                case Constant::STATUS_NOT_START:
                    $where .= ' AND activity_status = ' . Constant::ACTIVITY_PUBLISH . ' AND start_time > ' . MILLI_TIME;
                    break;
                // 进行中
                case Constant::STATUS_ING:
                    $where .= ' AND activity_status = ' . Constant::ACTIVITY_PUBLISH . ' AND start_time < ' . MILLI_TIME . ' AND end_time >= ' . MILLI_TIME;
                    break;
                // 已结束
                case Constant::STATUS_END:
                    $where .= ' AND activity_status = ' . Constant::ACTIVITY_PUBLISH . ' AND  end_time < ' . MILLI_TIME;
                    break;
                // 已终止
                case Constant::STATUS_STOP:
                    $where .= ' AND activity_status = ' . Constant::ACTIVITY_STOP;

                    break;

                default:
            }
        }

        return $where;
    }


    /**
     * 查询列表
     * @param array $params 条件sql,不包含where关键字
     * @param null $page_option 分页参数
     * @param array $order_option 排序参数
     * @param string $fields 查询的字段
     * @return array|bool
     */
    public function list_by_where($params = [], $page_option = null, $order_option = [], $fields = '*')
    {
        $where = $this->get_where($params);
        // 排序
        $orderby = '';
        if (!$this->_order_by($orderby, $order_option)) {
            return false;
        }
        // 分页参数
        $limit = '';
        if (!$this->_limit($limit, $page_option)) {
            return false;
        }

        $sql = "SELECT {$fields} FROM __TABLE__ WHERE " . $where . " {$orderby}{$limit}";

        // 读取记录
        return $this->_m->fetch_array($sql, []);

    }

    /**
     * 执行自定义查询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;
    }

}