CircleModel.class.php 5.45 KB
<?php
/**
 * 同事圈信息表
 * User: 代军
 * Date: 2017-04-24
 */
namespace Common\Model;

use Common\Common\Constant;

class CircleModel extends AbstractModel
{

    // 有附件
    const ATTACH = 1;

    // 无附件
    const NOT_ATTACH = 0;

    // 标识
    const CIRCLE_PID = 0;

    //帖子标识
    const CIRCLE_MATE = 0;

    //话题标识
    const CIRCLE_TOPIC =1;

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

    /**
     * 获取帖子列表的评论数据
     * @param array $ids 帖子ID集合
     * @param int $audit_state 评论数据的状态
     * @param null $page_option 分页数据
     * @param array $order_option 排序数据
     * @return array|bool
     */
    public function get_comment_num($ids=[], $audit_state, $page_option = null, $order_option = [])
    {
        $where = ' domain=? AND  status<? AND pid in(?) AND audit_state=? ';
        $params = array(QY_DOMAIN, self::ST_DELETE, $ids, $audit_state);

        $sql = 'SELECT pid, COUNT( * ) as total FROM __TABLE__  WHERE ' . $where . " GROUP BY pid ";

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

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

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

    /**
     * 【微信端】 获取评论列表根据点赞倒序
     * @param array $conditions 查询条件
     * @param array $page_option 分页参数
     * @param array $order_by 排序字段
     * @param string $file 默认搜索字段
     * @return mixed
     */
    public function list_by_comment($conditions = [], $page_option = [], $order_option = [], $file = '*')
    {

        $sql = "SELECT {$file},(SELECT COUNT(*) from oa_workmate_like WHERE cid=`id` AND  domain=? AND  status<? ) as like_total FROM __TABLE__  WHERE ";

        // 初始化like_total条件
        $params[] = QY_DOMAIN;
        $params[] = self::ST_DELETE;

        // 如果状态存在
        if (is_numeric($conditions['audit_state'])) {

            $where[] = 'audit_state=?';
            $params[] = $conditions['audit_state'];
        }

        $where[] = 'pid=?';
        $params[] = $conditions['pid'];
        $where[] = 'domain=?';
        $where[] = 'status<?';
        $params[] = QY_DOMAIN;
        $params[] = self::ST_DELETE;

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

        // 默认排序
        $order = array(
            'like_total' => 'DESC',
            'audit_time' => 'DESC',
            'created' => 'DESC'
        );

        // 如果有其他排序
        if (!empty($order_option)) {

            $order = array_merge($order, $order_option);
        }

        // 排序
        $order_by = '';
        if (!$this->_order_by($order_by, $order)) {
            return false;
        }

        return $this->_m->fetch_array($sql . implode(' AND ', $where) . $order_by . $limit, $params);

    }

    /**
     * 【微信端】 获取评论列表根据点赞倒序
     * @param array $conditions 查询条件
     * @return mixed
     */
    public function topic_count_by_conds($conditions = [])
    {
        list($where,$params)=$this->get_topic_where($conditions);

        $sql = "SELECT COUNT(*) as total FROM __TABLE__  WHERE ";

        return $this->_m->fetch_row($sql. implode(' AND ', $where) , $params);
    }

    /**
     * 【微信端】 获取评论列表根据点赞倒序
     * @param array $conditions 查询条件
     * @param array $page_option 分页参数
     * @param array $order_by 排序字段
     * @param string $file 默认搜索字段
     * @return mixed
     */
    public function topic_list_by_conds($conditions = [], $page_option = [], $order_option = [], $file = '*')
    {
        // 分页参数
        $limit = '';
        if (!$this->_limit($limit, $page_option)) {
            return false;
        }

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


        list($where,$params)=$this->get_topic_where($conditions);

        $sql = "SELECT {$file} FROM __TABLE__  WHERE ";

        return $this->_m->fetch_array($sql . implode(' AND ', $where) . $order_by . $limit, $params);

    }

    /**
     * 组装话题搜索条件
     * @param $conditions
     * @return array
     */
    protected function get_topic_where($conditions){

        // 如果状态存在
        if (isset($conditions['audit_state'])) {
            $where[] = 'audit_state=?';
            $params[] = $conditions['audit_state'];
        }

        // 上级ID
        if(isset($conditions['pid'])){
            $where[] = 'pid=?';
            $params[] = $conditions['pid'];
        }

        // 类型
        if(!empty($conditions['type'])) {
            $where[] = 'type=?';
            $params[] = $conditions['type'];
        }

        // 如果存在数据
        if(!empty($conditions['id'])){
            $where[] = '(is_all=1 OR id in (?))';
            $params[] = $conditions['id'];
        }else{
            $where[] = 'is_all=?';
            $params[] = '1';
        }

        $where[] = 'domain=?';
        $where[] = 'status<?';
        $params[] = QY_DOMAIN;
        $params[] = self::ST_DELETE;

        return [$where,$params];
    }
}