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

class Goods_model extends MY_Model{

    protected $table = 'score_goods';
    protected $insert_filed = array('name','cate','type','chance','score','num','get_type','address','piclist','pic','message','recommend','status','update_time','create_time','exchange_num');

    public function insert($data)
    {
        $data['update_time'] = time();
        $data['create_time'] = time();
        $data['recommend'] = 0;
        $data['status'] = 1;      
        return parent::insert($data);
    }

    public function update($data, $id)
    {
        $data['update_time'] = time();
        $data = $this->check_insert($data);
        $this->db->where('id', $id);
        $this->db->update($this->table, $data);
        return $id;
    }
    public function count()
    {
        return $this->db->count_all($this->table);
    }
    public function getGoodsList($limit,$offset)
    {
        $this->db->order_by('create_time', 'desc');
        return $this->db->get($this->table,$limit,$offset)->result_array();   
    }
    /**
     * [getGoodsInfo 商品信息]
     * @return [type] [description]
     */
    public function getGoodsInfo($id){
        $this->db->where('id',$id);
        return $this->db->get($this->table)->row_array();
    }


    public function countGoods($term){
        foreach ($term as $k => $v) {
            $this->db->where($k,$v);
        }

        return $this->db->get($this->table)->num_rows();
    }

    public function getGoods($term,$start,$limit)
    {

        foreach ($term as $k => $v) {
            $this->db->where($k,$v);
        }
        $this->db->limit($limit, $start);
        //如果是推荐操作，则按照修改时间降序排列
        isset($term['recommend'])?$this->db->order_by('update_time','desc'):$this->db->order_by('id','desc');

        $result = $this->db->get($this->table)->result_array();

        return $result;  
    }
}