PlatformordersController.php 7.9 KB
<?php
namespace app\admin\controller;

use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use function GuzzleHttp\Psr7\str;
use think\Db;
class PlatformordersController extends AdminBaseController{

    /**
     * 平台订单列表
     */
    public function plat_list(){

        $param = $this->request->param();
        $where = [
            "indent_type" => 1
        ];
        if(!empty($param['start_time']) && !empty($param['end_time'])){
            $start_time = strtotime($param['start_time']);
            $end_time = strtotime($param['end_time']);
            $where['create_time'] = [['>=',$start_time],['<=',$end_time]];
        }
        if(!empty($param['keyword'])){
            $where['order_number|name|phone'] = ['like','%'.trim($param['keyword']).'%'];
        }
        if(!empty($param['state'])){
            $where['state'] = $param['state'];
        }
        $data = Db::name('indent') -> where($where)->order('create_time','DESC') -> paginate(12);
        $data->appends($param);
        $data1 = $data -> toArray();
        if(count($data1['data'])!=0){
            foreach ($data1['data'] as $key => $val){
                $data1['data'][$key]['dizhi'] = Db::name('address') -> where('id',$val['indent_address']) -> find();
            }
        }
        $this -> assign('data1',$data1['data']);
        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('keyword', isset($param['keyword']) ? trim($param['keyword']) : '');
        $this->assign('state', isset($param['state']) ? $param['state'] : '');
        $this -> assign('data',$data);
        return $this -> fetch();

    }

    /**
     *平台订单查看
     */
    public function plat_view(){

        $id = $this -> request -> param();
        $indent_id = $id['id'];
        $data_goods = Db::name('indent_goods') -> where('indent_id',$indent_id) -> select();
        $data_in = Db::name('indent') -> where('id',$indent_id) -> find();
        //地址查询
        $address = Db::name('address') -> where("id",$data_in['indent_address']) -> find();
        if($address){
            $data_in['name'] = $address['name'];
            $data_in['phone'] = $address['phone'];
            $this -> assign('address',$address);
        }else{
            $this -> assign('address',null);
        }
        $this -> assign('data_goods',$data_goods);
        $this -> assign('data_in',$data_in);

        return $this -> fetch();

    }

    /**
     * 订单删除
     */
    public function plat_del(){

        $id = $_POST['id'];
        Db::name('indent') -> delete($id);
        Db::name('indent_goods') -> where('indent_id',$id) -> delete();
        return true;

    }



    public function out()
    {
        // 进行查询条件的处理
        $param = $this->request->param();
        $where = [
            "indent_type" => 1
        ];
        if(!empty($param['start_time']) && !empty($param['end_time'])){
            $start_time = strtotime($param['start_time']);
            $end_time = strtotime($param['end_time']);
            $where['create_time'] = [['>=',$start_time],['<=',$end_time]];
        }
        if(!empty($param['keyword'])){
            $where['order_number|name|phone'] = ['like','%'.$param['keyword'].'%'];
        }
        if(!empty($param['state'])){
            $where['state'] = $param['state'];
        }
        $data = Db::name('indent') -> where($where)->order('create_time','DESC') -> paginate(12);
        $data->appends($param);
        $data1 = $data -> toArray();
        if(count($data1['data'])!=0){
            foreach ($data1['data'] as $key => $val){
                $data1['data'][$key]['dizhi'] = Db::name('address') -> where('id',$val['indent_address']) -> find();
            }
        }
        $this->expList($data1['data']);
    }


    // 数据导出
    public function expList($array){

        //导出Excel
        $objPHPExcel = new \PHPExcel();

        //Add some data
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '序号')
            ->setCellValue('B1', '订单号')
            ->setCellValue('C1', '姓名')
            ->setCellValue('D1', '手机号')
            ->setCellValue('E1', '收货地址')
            ->setCellValue('F1', '数量')
            ->setCellValue('G1', '价钱')
            ->setCellValue('H1', '订单状态')
            ->setCellValue('I1', '创建时间');

        // sheet个数
        $sheet_index = 0;
        $sheet_count = 1;
        if(count($array) > 65535) {
            $sheet_count = ceil(count($array)/65535);
            for ($j = 1; $j < $sheet_count; $j++) {
                $objPHPExcel->createSheet();
                $objPHPExcel->setActiveSheetIndex($sheet_index+$j)
                    ->setCellValue('A1', '序号')
                    ->setCellValue('B1', '订单号')
                    ->setCellValue('C1', '姓名')
                    ->setCellValue('D1', '手机号')
                    ->setCellValue('E1', '收货地址')
                    ->setCellValue('F1', '数量')
                    ->setCellValue('G1', '价钱')
                    ->setCellValue('H1', '订单状态')
                    ->setCellValue('I1', '创建时间');
            }
        }

        // 设置单元格宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(50);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(25);

        $i = 2;
        // 计数器
        $cnt = 0;
        // 每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
        $limit = 35000;
        foreach ($array as $k=>$v) {
            $cnt ++;
            if ($limit == $cnt) { //刷新一下输出buffer,防止由于数据过多造成问题
                ob_flush();
                flush();
                $cnt = 0;
            }
            $state = '待付款';
            if($v['state'] == 1) {
                $state = '已付款';
            }
            if($v['state'] == 2) {
                $state = '未完成';
            }
            if($v['state'] == 3) {
                $state = '已完成';
            }
            $objPHPExcel->setActiveSheetIndex($sheet_index)
                ->setCellValue('A' . $i, $v['id'])
                ->setCellValue('B' . $i, $v['order_number'])
                ->setCellValue('C' . $i, $v['name'])
                ->setCellValue('D' . $i, $v['phone'])
                ->setCellValue('E' . $i, $v['dizhi']['region'].' '.$v['dizhi']['detailed'])
                ->setCellValue('F' . $i, $v['book_num'])
                ->setCellValue('G' . $i, $v['money'])
                ->setCellValue('H' . $i, $state)
                ->setCellValue('I' . $i, date('Y-m-d H:i:s',$v['create_time']));
            $i++;
            if($k == ($sheet_index+1)*65535) {
                $i = 2;
                $sheet_index++;
            }
        }

        $filename = '平台订单数据'. date('Y-m-d').'.xls';
        ob_end_clean();
        header('Content-Type: text/csv');
//        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }
}