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

class Export_service {

    protected $CI;

    public function __construct()
    {
        $this->CI = &get_instance();
        $this->CI->load->model('site_user_model');
        $this->CI->load->model('code_model');
        $this->CI->config->load('otherconf',TRUE);
    }

    private function  export_csv($filename,$data)
    {
        header("Content-type:text/csv");   
        header("Content-Disposition:attachment;filename=".$filename);   
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');   
        header('Expires:0');   
        header('Pragma:public');   
        return $data; 
    }
    public function export()
    {
        
        $format_data = '用户名,开卡时间,姓名,手机号,性别,年龄,消费次数,高级权益,开通时间,到期时间,开通方式'."\n";
        $format_data =  iconv('utf-8','gb2312//IGNORE',$format_data);

        $fields = "username,createtime,realname,tel,sex,age,buys,status,start_time,end_time,type";
        $format_data = $format_data.$this->CI->site_user_model->listexuser($fields,100000,0);

        $filename = date('Ymd').'user.csv'; //设置文件名   
        return $this->export_csv($filename,$format_data); //导出
    }

    public function exportcode()
    {

        $format_data = '兑换码,时长,生成时间,有效期,使用状况,激活用户,备注'."\n";
        $format_data =  iconv('utf-8','gb2312//IGNORE',$format_data);

        $fields = "code,duration,create_time,expires,status,username,note";
        $format_data = $format_data.$this->CI->code_model->listexcode($fields,100000,0);

        $filename = date('Ymd').'ExchangeCode.csv'; //设置文件名   
        return $this->export_csv($filename,$format_data); //导出 
        unset($format_data);
    }

}