<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Topic_model extends MY_Model {

    public $table = 'topic';
    protected $key = 'id';
    protected $insert_filed = array(
        'uid','username','title','typeid','listimg','detailimg','sourceid',
        'buzzcount','concerncount','involcount','extracount','piccount','isrec','istop','isdelete','createtime'
    );
    public function __construct()
    {
        parent::__construct();
    }

    public function getall() {
        return $this->db->get($this->table)->result_array();
    }

    public function total($where)
    {
        if(count($where)>0)
        {
            $where = implode(' and ', $where);
            $this->db->where($where);
        }
        return $this->db->count_all_results($this->table);
    }

    public function get_topic_list($where, $fields, $limit, $offset,$order='id')
    {
        if(count($where)>0)
        {
            $where = implode(' and ',$where);
            $this->db->where($where);
        }
        $this->db->select($fields);
        $this->db->order_by($order, 'DESC');
        if($limit == '-1')
        {
            return $this->db->get($this->table)->result_array();
            exit;
        }
        return $this->db->get($this->table, $limit, $offset)->result_array();
    }
    
    public function get_topic($where,$fields, $limit, $offset)
    {   $this->db->where($where);
        $this->db->select($fields);
        $this->db->order_by('involcount', 'DESC');
        $this->db->order_by('id', 'DESC');
        return $this->db->get($this->table, $limit, $offset)->result_array();
    }


    public function insert($data)
    {
        return parent::insert($data);
    }

    public function update($data, $id)
    {
        $data = $this->check_insert($data);
        $this->db->where('id', $id);
        $this->db->update($this->table, $data);
        return $id;
    }

    public function bantch_delete($ids)
    {
        $tids = explode(',', $ids);
        foreach($tids as $id) {
            $this->db->where('id', $id);
            $this->db->update($this->table, array('isdelete' => 1));
        }
    }

     public function get_one_by_title($title, $field = '')
    {
        $this->db->where('title',$title);
        if (!empty($field)) {
            $this->db->select($field);
        }
        return $this->db->get($this->table)->row_array();

    }
    
    public function get_one($id, $field = '')
    {
        $this->db->where('id',$id);
        if (!empty($field)) {
            $this->db->select($field);
        }
        return $this->db->get($this->table)->row_array();
    }

    public function get_by_tids($tids, $field) {
        $this->db->select($field);
        $this->db->where_in('id', $tids);
        $this->db->where('isdelete', 0);
        return $this->db->get($this->table)->result_array();
    }

    public function check_by_title($title) {
        $this->db->where('title',$title);
        $this->db->where('isdelete', 0);
        return $this->db->get($this->table)->row_array();
    }

    public function get_one_fileds($id,$fileds)
    {
        $this->db->where('id',$id);
        $this->db->select($fileds);
        $this->db->where('isdelete', 0);
        return $this->db->get($this->table)->row_array();
    }
    public function get_count($where)
    {
        $this->db->where($where);
        $this->db->where('isdelete', 0);
        return $this->db->count_all_results($this->table);
    }



}