<?php
namespace wstmart\admin\model;
use think\Db;
use think\Loader;
use think\Validate;
class Member extends Base{
	public function memberByPage(){
		$where='u.userStatus=1 AND u.dataFlag=1 AND u.userType=0';
		$start=input('startDate');
		$end=input('endDate');
		$loginName=input('loginName');
		$userPhone=input('userPhone');
		//dump($nonlogTime);
        if($loginName!='')$where.= ' AND u.loginName LIKE "%'.$loginName.'%"';
        if($userPhone!='')$where.= ' AND u.userPhone LIKE "%'.$userPhone.'%"';
		if($start!='' && $end!=''){
    		$start = date('Y-m-d 00:00:00',strtotime(input('startDate')));
    		$end = date('Y-m-d 23:59:59',strtotime(input('endDate')));
    		$where.=' AND con.loginTime BETWEEN "'.$start.'" AND "'.$end.'"';
		}else if($start!=''){
    		$start = date('Y-m-d 00:00:00',strtotime(input('startDate')));
            $where.= ' AND con.loginTime>="'.$start.'"';
		}else if($end!=''){
    		$end = date('Y-m-d 00:00:00',strtotime(input('endDate')));
            $where.= ' AND con.loginTime<="'.$end.'"';
		}
		$result=Db::table('__USERS__')->alias('u')
		->join('(select l.*,count(l.userId) logNum from __LOG_USER_LOGINS__ l group by l.userId ) con','con.userId=u.userId','left')
		->join('__ORDERS__ o','u.userId=o.userId','left')
		->where($where)
		->field('u.loginName,trueName,u.userPhone,lastTime,u.createTime,con.logNum,count(o.userId) orderNum')
		->order('loginTime desc')
		->group('u.userId,trueName')
		->paginate(input('limit/d'))->toArray();
		//dump(db::getlastsql());
		//dump($result);
		return $result;
	}
	public function toExport(){
		$name="活跃用户数据列表";
		$where='u.userStatus=1 AND u.dataFlag=1 AND u.userType=0';
		$start=input('startDate');
		$end=input('endDate');
		$loginName=input('loginName');
		$userPhone=input('userPhone');
		//dump($nonlogTime);
        if($loginName!='')$where.= ' AND u.loginName LIKE "%'.$loginName.'%"';
        if($userPhone!='')$where.= ' AND u.userPhone LIKE "%'.$userPhone.'%"';
		if($start!='' && $end!=''){
    		$start = date('Y-m-d 00:00:00',strtotime(input('startDate')));
    		$end = date('Y-m-d 23:59:59',strtotime(input('endDate')));
		}else if($start!=''){
    		$start = date('Y-m-d 00:00:00',strtotime(input('startDate')));
		}else if($end!=''){
    		$end = date('Y-m-d 00:00:00',strtotime(input('endDate')));
		}else{
    		$end = date('Y-m-d 23:59:59',strtotime("now"));
		}
		$where.=' AND con.loginTime BETWEEN "'.$start.'" AND "'.$end.'"';

		$page=Db::table('__USERS__')->alias('u')
		->join('(select l.*,count(l.userId) logNum from __LOG_USER_LOGINS__ l group by l.userId ) con','con.userId=u.userId','left')
		->join('__ORDERS__ o','u.userId=o.userId','left')
		->where($where)
		->field('u.loginName,trueName,u.userPhone,lastTime,u.createTime,con.logNum,count(o.userId) orderNum')
		->order('loginTime desc')
		->group('u.userId')
		->select();
		Loader::import('phpexcel.PHPExcel.IOFactory');
        $objPHPExcel = new \PHPExcel();
        // 设置excel文档的属性
        $objPHPExcel->getProperties()->setCreator("heyuanhui")//创建人
        ->setLastModifiedBy("heyuanhui")//最后修改人
        ->setTitle($name)//标题
        ->setSubject($name)//题目
        ->setDescription($name)//描述
        ->setKeywords("订单")//关键字
        ->setCategory("Test result file");//种类
    
        // 开始操作excel表
        $objPHPExcel->setActiveSheetIndex(0);
        // 设置工作薄名称
        $objPHPExcel->getActiveSheet()->setTitle(iconv('gbk', 'utf-8', 'Sheet'));
        // 设置默认字体和大小
        $objPHPExcel->getDefaultStyle()->getFont()->setName(iconv('gbk', 'utf-8', ''));
        $objPHPExcel->getDefaultStyle()->getFont()->setSize(11);
        $styleArray = array(
                'font' => array(
                        'bold' => true,
                        'color'=>array(
                                'argb' => 'ffffffff',
                        )
                ),
                'borders' => array (
                        'outline' => array (
                                'style' => \PHPExcel_Style_Border::BORDER_THIN,  //设置border样式
                                'color' => array ('argb' => 'FF000000'),     //设置border颜色
                        )
                )
        );
        //设置宽
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFill()->getStartColor()->setARGB('333399');
        
        $objPHPExcel->getActiveSheet()
                                ->setCellValue('A1', '登录账号')
                                ->setCellValue('B1', '真实姓名')
                                ->setCellValue('C1', '联系方式')
                                ->setCellValue('D1', '最后登录时间')
                                ->setCellValue('E1', '登陆次数')
                                ->setCellValue('F1', '订单数量')
                                ->setCellValue('G1', '创建时间');
        $objPHPExcel->getActiveSheet()->getStyle('A1:G1')->applyFromArray($styleArray);
    
        for ($row = 0; $row < count($page); $row++){
            $i = $row+2;
            $objPHPExcel->getActiveSheet()
                                ->setCellValue('A'.$i, $page[$row]['loginName'])
                                ->setCellValue('B'.$i, $page[$row]['trueName'])
                                ->setCellValue('C'.$i, $page[$row]['userPhone'])
                                ->setCellValue('D'.$i, $page[$row]['lastTime'])
                                ->setCellValue('E'.$i, $page[$row]['logNum'])
                                ->setCellValue('F'.$i, $page[$row]['orderNum'])
                                ->setCellValue('G'.$i, $page[$row]['createTime']);

        }
        //输出EXCEL格式
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        // 从浏览器直接输出$filename
        header('Content-Type:application/csv;charset=UTF-8');
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-excel;");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition: attachment;filename="'.$name.'.xls"');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
	}
}