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

class Post_model extends MY_Model {

    public $table = 'post';
    protected $key = 'id';
    protected $insert_filed = array(
        'uid','username','content','platform', 'sourceid',
        'status','responsecount','likecount','isdelete','createtime'
    );
    public function __construct()
    {
        parent::__construct();
    }

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

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

    }

    public function get_one($id)
    {
        $this->db->where('id',$id);
        return $this->db->get($this->table)->row_array();
    }

    public function get_by_pids($post_ids, $field = '') {
        if (!empty($field)) {
            $this->db->select($field);
        }
        $this->db->where_in('id', $post_ids);
        $this->db->where('isdelete', 0);
        $this->db->order_by('id', 'DESC');
        return $this->db->get($this->table)->result_array();
    }

    public function get_post_list($where,$fields, $limit, $offset){
        if(count($where)>0)
        {
            $where = implode(' and ', $where);
            $this->db->where($where);
        }
        //$this->db->where($where);
        $this->db->select($fields);
        $this->db->order_by('readcount', 'DESC');
       return $this->db->get($this->table, $limit, $offset)->result_array();

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

     public function get_by_pids_tz($post_ids, $field = '',$pagesize,$offset) {
        if (!empty($field)) {
            $this->db->select($field);
        }
        $where=array();
        $where['isdelete']=0;
        $this->db->where_in('id', $post_ids);
        $this->db->where($where);
        $this->db->order_by('id', 'DESC');
        return $this->db->get($this->table,$pagesize,$offset)->result_array();
    }

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

    }

    public function get_one_by_uid($uid){
        $where=array();
        $where['uid']=$uid;
        $this->db->where($where);
        $this->db->select('id');
        $this->db->where('isdelete', 0);
        return $this->db->get($this->table)->row_array();

    }

    public function countAdd($id){
        $this->db->where(array('id'=>$id));
        $this->db->set('readcount','readcount + 1',FALSE);
        return $this->db->update($this->table);
    }


}