<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Code_model extends MY_Model{
    const NORMAL = 1;
    const USED = 2;
    const EXPIRE = 3;//过期
    public $table = 'code';
    public function __construct()
    {
        parent::__construct();
        $this->config->load('otherconf',TRUE);
        $this->load->library('Export_service');
        $this->load->library('pagination');
    }
    /**

     * [count 统计兑换码中所有行]

     * @return [type] [description]

     */
    public function count($where)
    {
        if(count($where)>0)
        {
            $where = implode(' and ',$where);
            $this->db->where($where);            
        }
        $query = $this->db->get($this->table);
        return $query->num_rows();
    }
    public function listcode($where,$fields,$limit,$offset)
    {
        if(count($where)>0)
        {
            $where = implode(' and ',$where);
            $this->db->where($where);            
        }       

        $this->db->select($fields);
        $this->db->order_by('create_time' , 'desc');
        $query = $this->db->get($this->table,$limit,$offset);
        return $query->result_array();
    }

    public function listexcode($fields,$limit,$offset)
    {    
        $this->db->select($fields);
        $this->db->order_by('create_time' , 'desc');
        $query = $this->db->get($this->table,$limit,$offset);
        $k=0;
        $data = array();
        $format_data = '';
        $status = $this->config->item('codestatus','otherconf');
        while ($row = $query->unbuffered_row())
        {
            $data[$k]['code'] = $row->code;
            $data[$k]['duration'] = $row->duration;
            $data[$k]['create_time'] = date('Y-m-d',$row->create_time);
            $data[$k]['expires'] = $row->expires;
            $data[$k]['status'] = iconv('utf-8','gb2312//IGNORE',$status[$row->status]);
            $data[$k]['username'] = iconv('utf-8','gb2312//IGNORE',$row->username);
            $data[$k]['note'] = iconv('utf-8','gb2312//IGNORE',$row->note);
            $format_data = $format_data.implode(",",$data[$k])."\n";
        }
        return $format_data;
    }

    public function get_list(){
        $this->db->limit(10, 0);
        return $this->db->get($this->table)->result_array();
    }


    public function isExists($code){
        $this->db->where('code', $code);
        $num = $this->db->get($this->table)->num_rows();
        if($num > 0 ){
            return true;
        }else{
            return false;
        }
    }


    public function generate(){

        $str = "qwertyuioplkjhgfdsazxcvbnm0123456789";
        $code =  substr(str_shuffle($str), 0, 8);
        if($this->isExists($code)){
            $this->generate(); 
        }
        return $code;

    }

    public function insert($data){

        $data['create_time'] = time();

        $data['status'] = self::NORMAL;

        $this->db->insert($this->table, $data);

    }

    public function getCode($code){

        $this->db->where('code', $code);

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

    }
    public function useCode($code, $username, $ordernumber){

        $this->db->where('code', $code);

        $this->db->update($this->table, array('username' => $username, 'status' => self::USED, 'ordernumber' => $ordernumber));

    }
    public function expireCode($code){
        $this->db->where('code',$code);
        $this->db->update($this->table,array('status'=>self::EXPIRE));
    }



}

