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

class Exchange_model extends MY_Model{

    const NOPAY = 4;
    const SUCCESS = 1;
 
 

    protected $table = 'score_exchange';

    protected $insert_filed = array('gid','uid','exchange_time','status','extra','cate');
    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 getOne($eid){
        $this->db->where('id', $eid);

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


    public function createOrder($uid, $gid, $score, $cate, $type){
        $data = array(
        'uid' => $uid,//用户的id
        'gid' =>$gid,
        'exchange_time' =>time(),
        'status'=> self::NOPAY,
        'extra'=>!empty($_POST['extra'])?$_POST['extra']:'',
        'cate' => $cate,
        'type' => $type
        );
        return parent::insert($data);
    }

    public function insert($data)
    {      
        return parent::insert($data);
    }
    /**
     * [exchNum 商品被兑换的数量]
     * @param  [type] $gid [description]
     * @return [type]      [description]
     */
    public function exchNum($gid)
    {
        $this->db->where('gid',$gid);
        return $this->db->get($this->table)->num_rows();
    }

    public function count($gid)
    {
        $this->db->where('gid',$gid);
        return $this->db->get($this->table)->num_rows();
    }  


    public function countExchange($term){
        foreach ($term as $k => $v) {
            $this->db->where($k,$v);
        }
        return $this->db->get($this->table)->num_rows();
    }

    public function getExchangeList($term,$start,$limit)
    {
        foreach ($term as $k => $v) {
            $this->db->where($k,$v);
        }
        $this->db->order_by('status', 'ASC');
        $this->db->order_by('exchange_time', 'desc');

        $this->db->limit($limit, $start);
        $result = $this->db->get($this->table)->result_array();
   
        return $result;  
    }

    public function getSuccess($term)
    {
        foreach ($term as $k => $v) {
            $this->db->where($k,$v);
        }
        $this->db->order_by('exchange_time', 'desc');
        $result = $this->db->get($this->table)->row_array();
        return $result;
    }

}