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

class ShopOrder_Model extends MY_Model {

	const SUCCESS = 1;
    public $table = 'shoporder';
    private $list_where = array();


    public function __construct()
    {
        parent::__construct();
    }

    public function insert($data){
        $data['create_time'] = time();
        $this->db->insert($this->table, $data);
    }


    public function getRecordBySid($sid, $start, $limit, $date){

        if(!empty($date)){
            $time_start = strtotime($date);
            $time_end = $time_start + (3600 * 24);
            $this->db->where("create_time > '{$time_start}' AND create_time < '{$time_end}'");
        }
        $this->db->order_by('create_time', 'desc');
        $this->db->limit($limit, $start);
    	$this->db->where('sid', $sid);
    	$result = $this->db->get($this->table)->result_array();

    	if(empty($result)){
    		return array();
    	}
        $this->load->model('user_model');
    	foreach($result as $k => $row){
            $result[$k]['create_time'] = date('Y-m-d H:i:s', $row['create_time']);
            unset($result[$k]['id'], $result[$k]['sid'], $result[$k]['uid']);
		}

		return $result;
    }

    public function getRecordByUid($uid, $start = 0, $num = 10){
        $shop_table = $this->db->dbprefix('shop');
        $shoporder_table = $this->db->dbprefix('shoporder');
        $this->db->select("{$shop_table}.name,{$shoporder_table}.username, {$shoporder_table}.level,{$shoporder_table}.price,{$shoporder_table}.discount_price,{$shoporder_table}.number,{$shoporder_table}.create_time");
        $this->db->where('uid', $uid);

        $this->db->join($shop_table, "{$shoporder_table}.sid = {$shop_table}.sid", 'left');
        $this->db->order_by('create_time', 'desc');
        $this->db->limit($num, $start);
        $result = $this->db->get($this->table)->result_array();

        if(empty($result)){
            return array();
        }

        $this->load->model('user_model');
        foreach($result as $k => $row){
            $result[$k]['create_time'] = date('Y-m-d H:i:s', $row['create_time']);
            
        }

        return $result;
    }


    public function isExistsByCreatetime($sid, $uid){
        $this->db->where('sid', $sid);
        $this->db->where('uid', $uid);
        $this->db->where('create_time >', strtotime('today'));
        $this->db->where('create_time <', strtotime('tomorrow') - 1);

        $query = $this->db->get($this->table);

        if($query->num_rows() > 0){
            return true;
        }

        return false;
    }

    public function setList($key, $val){
        if(empty($val)){
            return false;
        }
        $shoporder_table = $this->db->dbprefix('shoporder');
        $this->list_where[$shoporder_table.'.'.$key] = $val;
    }

    private function setListWhere(){
        foreach($this->list_where as $key => $value){
            $this->db->where($key, $value);
        }
    }


    public function getList($start, $pagesize){
        $this->setListWhere();
        $shop_table = $this->db->dbprefix('shop');
        $shoporder_table = $this->db->dbprefix('shoporder');
        $this->db->select("{$shoporder_table}.sid,{$shop_table}.name,{$shoporder_table}.username, {$shoporder_table}.level,{$shoporder_table}.price,{$shoporder_table}.discount_price,{$shoporder_table}.number,{$shoporder_table}.create_time");
        $this->db->join($shop_table, "{$shoporder_table}.sid = {$shop_table}.sid", 'left');
        $this->db->order_by('create_time', 'desc');
        $this->db->limit($pagesize, $start);
        $result = $this->db->get($this->table)->result_array();

        return $result;
    }

    public function countList(){
        $this->setListWhere();
        return $this->db->count_all_results($this->table);
    }
}
/* End of file card_model.php */
/* Location: /application/models/card_model.php */